示例#1
0
str divideByTwo(str dec){
    int rem=0;
    int m=dec.size();
    for(int i=0; i<m; i++){
        int a=dec.at(i)-48; //cyfra
        dec.at(i)=(rem+a)/2+48;
        if(a%2){rem=10;}else{rem=0;}
    }
    //printf("DZIEL %s\n", dec.c_str());
    return dec;
}
示例#2
0
str strDecToStrBin(str dec){
    str bin="";
    int m=dec.size();
    int i=0;
    while(dec.size()){
        //printf("DEC %s\n", dec.c_str());
        char l=dec.at(dec.size()-1);
        if(l=='0' || l=='2' || l=='4' || l=='6' || l=='8'){ //parzysta
            bin='0'+bin;
        }else{
            bin='1'+bin;
        }
        dec=divideByTwo(dec);
        if(dec.at(0)=='0'){dec.erase(0,1);}
    }
    return bin;
}
示例#3
0
void binaryNumberToCode(str bin){
    vec<str> v;
    addCodeLine("ZERO");
    //printf("ZMIANA\n");
    while(bin!="0"){
        //printf("BIN %s\n", bin.c_str());
        char l=bin.at(bin.size()-1);
        if(l=='1'){
            v.push_back("INC");
            bin.at(bin.size()-1)='0';
        }else if(l=='0'){
            v.push_back("SHL");
            bin.erase(bin.size()-1, 1);
        }
    }
    for(int i=v.size()-1; i>=0; i--){
        addCodeLine(v.at(i));
	}
}
示例#4
0
文件: util.cpp 项目: vis15/fractions
bool isNumeric(const str& token)
{
    if(token == "")
        return false;

    for(uint i=0; i<token.length(); i++)
    {
        const char num = token.at(i);
        if(i == 0 && Util::toString(num) == kSubtract() && token.size() > 1) //token.size > 1 because "-" is not a number
            continue;
        if(isdigit(num) == false && Util::toString(num) != kDot())
            return false;
    }

    return true;
}