Help me for the insertion of binary function in my add function

 Ed Gon · December 3, 2014 Here is my code, I need the to insert the process where the inputed number converts to binary digit and those numbers will be add in binary form then converts back to decimal as a result. PLEASE HELP~=======#include #include #include using namespace std;int main(){ string cmd; long long ax = 0; bool error = false; cout << "Type in command (type \"HELP\" to see all commands): " <"; getline(cin, cmd);//catches the data if (cmd.find("ADD") == 0){ if (cmd.find(" ") == strlen("ADD")){ string x = cmd.substr(strlen("ADD "), cmd.length()-(cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x string y = cmd.substr(cmd.find(",") + 1);//store the value of y ax = atoi(x.c_str()) + atoi(y.c_str()); //calculates the result } else{ error = true; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX cout << ">AX = " << ax << endl;//Prints the result } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP cout << ">" << endl; cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; cout << ">" << endl; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT break; } else{ error = true; } if (error){ cout << ">Error: Unknown command!" << endl; } }}

Replies

- page 2

 Ed Gon · December 14, 2014 ughhhh. adding the negative is screwing again. pffft. Franz Schmidt · December 5, 2014 Sorry, but I don't understand your code and it won't compile at all But I made a little programm how to add binary values:``bitset s(ax);cout << endl << "Sum normal addition: " << endl << s;//binary value of axbitset num1(atoi(x.c_str()));cout << endl << "x: " << num1;//binary value of xbitset num2(atoi(y.c_str()));cout << endl << "y: " << num2 << endl;//binary value of ybitset sum;bool carry = 0;for (int n = 0; n < 32; n++){ if (carry){ if (num1[n] && num2[n]){ carry = 1; sum[n] = 1; } else{ carry = 0; if (num1[n] || num2[n]){ carry = 1; sum[n] = 0; } else{ sum[n] = 1; } } } else{ if (num1[n] && num2[n]){ carry = 1; sum[n] = 0; } else{ sum[n] = num1[n] | num2[n]; } }}cout << endl << endl << sum << endl << endl;//binary sumcout << endl << sum.to_ullong() << endl << endl;//convert bit to unsigned long long``I estimate that your program is just for excercise purposes. Do you also wan't to convert the number into binary and back directly on your own or with a lib? Ed Gon · December 14, 2014 And I found out once it encounters an error like at the top, all the following legal command falls down like ADD 3,4 it will yield like this Ed Gon · December 5, 2014 @franz it should be on our own without those premade libs. Franz Schmidt · December 14, 2014  right i forgot to clear the AX value fixed itbtw. I added some conditions so you can only enter e.g. -234 not 3-23Hope everything works fine now.``#include #include #include #include #include #define size 17using namespace std;void init(void);string getinput(void);string DecToBin(int);string TwoComplement(string);string AddBin(string, string);int BinToDec(string);void decode(string);void execute(void);string AX;string OP1;string OP2;enum CMDS { ADD, PRT, EXT, HELP, ERR } OPCODE;bool EXIT = false;enum ERRORS { CMD, NUM, SYNTAX, TOBIG } ERR_MSG;void init(){ OPCODE = ERR; cout << "Type in command (type \"HELP\" to see all commands): " << endl;}string getinput(){ string input; cout << ">" << endl << ">"; getline(cin, input);//catches the data return input;}string DecToBin(int num){// converts the decimal number to its equivalent binary string format string b_num; for (int n = 0; n < size; n++){ b_num.replace(n, 1, "0"); } for (int n = (size - 1); n >= 0; n--){ if ((num - pow(2, n)) >= 0){ b_num.replace(n, 1, "1"); num -= pow(2, n); } else if ((num + pow(2, n)) <= 0){ b_num.replace(n, 1, "1"); num += pow(2, n); } else{ b_num.replace(n, 1, "0"); } } return b_num;}string TwosComplement(string num){// get the 2s complement of a binary string. for (int n = 0; n < strlen(num.c_str()); n++){ if (num.c_str()[n] == '0'){ num.replace(n, 1, "1"); } else{ num.replace(n, 1, "0"); } } return AddBin(num, "1");}string AddBin(string num1, string num2){// add two binary strings. string b_sum; bool carry = 0; for (int n = 0; n < size; n++){ if (carry){ if (num1.c_str()[n] == '1' && num2.c_str()[n] == '1'){ carry = 1; b_sum.replace(n, 1, "1"); } else{ carry = 0; if (num1.c_str()[n] == '1' || num2.c_str()[n] == '1'){ carry = 1; b_sum.replace(n, 1, "0"); } else{ b_sum.replace(n, 1, "1"); } } } else{ if (num1.c_str()[n] == '1' && num2.c_str()[n] == '1'){ carry = 1; b_sum.replace(n, 1, "0"); } else{ if (num1.c_str()[n] == '1' || num2.c_str()[n] == '1'){ b_sum.replace(n, 1, "1"); } else{ b_sum.replace(n, 1, "0"); } } } } return b_sum;}int BinToDec(string num){// converts the binary string to its equivalent decimal number int sum = 0; bool neg = 0; if (num.c_str() == '1'){ num = TwosComplement(num); neg = 1; } for (int n = 0; n < size; n++){ if (num.c_str()[n] == '1'){ sum += pow(2, n); } } if (OP1.c_str() == '1' && OP2.c_str() == '1' || neg){ sum *= (-1); } return sum;}void decode(string cmd){ if (cmd.find("ADD") == 0){ if (/*1*/cmd.find(" ") == strlen("ADD") &&            /*2*/cmd.find(",") != strlen("ADD ") &&            /*3*/cmd.find(",") != string::npos &&            /*4*/cmd.find(",") != (strlen(cmd.c_str()) - 1) &&            /*5*/cmd.find(",", cmd.find(",") + 1) == string::npos &&            /*6*/cmd.find(" ", cmd.find(" ") + 1) != (cmd.find(" ") + 1) &&            /*7*/(cmd.find(" ", cmd.find(" ") + 1) == string::npos || cmd.find(" ", cmd.find(" ", cmd.find(" ") + 1) + 1) == string::npos)){ OPCODE = ADD; string x; string y; x = cmd.substr(strlen("ADD "), cmd.length() - (cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x if (cmd.find(" ", cmd.find(","), 1) == cmd.find(",") + 1){ y = cmd.substr(cmd.find(" ", cmd.find(",")) + 1);//store the value of y } else{ y = cmd.substr(cmd.find(",") + 1);//store the value of y } if (strspn(x.c_str(), "-0123456789") && (x.find("-") == 0 || x.find("-") == string::npos) && strspn(y.c_str(), "-0123456789") && (x.find("-") == 0 || x.find("-") == string::npos)){ int num1 = atoi(x.c_str()); int num2 = atoi(y.c_str()); if (num1 < pow(2, size) && num1 > -pow(2, size) && num2 < pow(2, size) && num2 > -pow(2, size)){ OP1 = DecToBin(num1); OP2 = DecToBin(num2); if (num1 < 0){ OP1 = TwosComplement(OP1); } if (num2 < 0){ OP2 = TwosComplement(OP2); } //cout << endl << OP1 << endl << OP2 << endl; } else{ OPCODE = ERR; ERR_MSG = TOBIG; } } else{ OPCODE = ERR; ERR_MSG = NUM; } } else{ OPCODE = ERR; ERR_MSG = SYNTAX; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX OPCODE = PRT; } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP OPCODE = HELP; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT OPCODE = EXT; } else{ OPCODE = ERR; ERR_MSG = CMD; }}void execute(){ switch (OPCODE){ case ADD:    //cout << endl << AddBin(OP1, OP2) << endl << endl;            char *str;            str=(char *) malloc(size*sizeof(char));    itoa(BinToDec(AddBin(OP1, OP2)), str, 10);    AX.clear();    AX.replace(0, strlen(str), str);    delete [] str; //AX = to_string(BinToDec(AddBin(OP1, OP2))); break; case PRT: cout << ">AX = " << AX << endl;//Prints the result break; case EXT: EXIT = true; case HELP: cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; break; case ERR: cout << ">Error: "; switch (ERR_MSG){ case CMD: cout << "Unknown command!"; break; case NUM: cout << "Enter only numbers!"; break; case SYNTAX: cout << "Take care of the input syntax!"; break; case TOBIG: cout << "To big number: " << -pow(2, size - 1) - 1 << " > x < " << pow(2, size - 1); break; } cout << endl; break; }}int main(){ string cmd; init(); do{ cmd = getinput(); decode(cmd); execute(); } while (!EXIT);}`` Franz Schmidt · December 5, 2014 I think there is a little problem with the biggest number of unsigned long, but this isn't really a serious problem (won't reach the limit anyway)``cout << endl << "Sum from normal addition: " << ax;cout << endl << "Bitset conversion: ";//binary value of axbitset < 32 > num1(atoi(x.c_str()));cout << endl << "x: " << num1;//binary value of xbitset < 32 > num2(atoi(y.c_str()));cout << endl << "y: " << num2;//binary value of ybitset < 32 > s(ax);cout << endl << "sum: " << s << endl;//convert x and y to binary and save binary values in b_x and b_ybool b_x[32];bool b_y[32];unsigned long long x_num = atoi(x.c_str());unsigned long long y_num = atoi(y.c_str());for (int n = 31; n >= 0; n--){ if ((x_num - pow(2, n)) >= 0){ b_x[n] = 1; x_num -= pow(2, n); } else{ b_x[n] = 0; } if ((y_num - pow(2, n)) >= 0){ b_y[n] = 1; y_num -= pow(2, n); } else{ b_y[n] = 0; }}cout << endl << "Direct converstion: ";cout << endl << "b_x: ";for (int n = 31; n >= 0; n--){ cout << b_x[n];}cout << endl << "b_y: ";for (int n = 31; n >= 0; n--){ cout << b_y[n];}//add numbers in binary formbool b_sum[32];bool carry = 0;for (int n = 0; n < 32; n++){ if (carry){ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 1; } else{ carry = 0; if (b_x[n] || b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = 1; } } } else{ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = b_x[n] | b_y[n]; } }}cout << endl << "sum: ";for (int n = 31; n >= 0; n--){ cout << b_sum[n];}cout << endl;//convert binary sum back to a normal dezimal numberunsigned long long sum = 0;for (int n = 31; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); }}cout << "Sum from binary addition: " << sum  << endl;`` Ed Gon · December 14, 2014 @franz btw did you saw the post before that I change the '1' to "1" but it works pretty well but when I'm about to add "ADD -3,-3" it results different answer, im still using codeblocks btw. still downloading the visual studios. Franz Schmidt · December 14, 2014 XD I remember it also took me long to install it Yes I saw it and this is the reason why negativ addition won't workThe conditions won't work with "1"Sorry man But bucky didn't fixed the bb-code in the code tags at the moment.The problem is that bb-codes use square brackets [] which are also used for arrays as you probably know.And those don't get shown when written.So here is the code without code tags :#include #include #include #include #include #define size 17using namespace std;void init(void);string getinput(void);string DecToBin(int);string TwoComplement(string);string AddBin(string, string);int BinToDec(string);void decode(string);void execute(void);string AX;string OP1;string OP2;enum CMDS { ADD, PRT, EXT, HELP, ERR } OPCODE;bool EXIT = false;enum ERRORS { CMD, NUM, SYNTAX, TOBIG } ERR_MSG;void init(){ OPCODE = ERR; cout << "Type in command (type \"HELP\" to see all commands): " << endl;}string getinput(){ string input; cout << ">" << endl << ">"; getline(cin, input);//catches the data return input;}string DecToBin(int num){// converts the decimal number to its equivalent binary string format string b_num; for (int n = 0; n < size; n++){ b_num.replace(n, 1, "0"); } for (int n = (size - 1); n >= 0; n--){ if ((num - pow(2, n)) >= 0){ b_num.replace(n, 1, "1"); num -= pow(2, n); } else if ((num + pow(2, n)) <= 0){ b_num.replace(n, 1, "1"); num += pow(2, n); } else{ b_num.replace(n, 1, "0"); } } return b_num;}string TwosComplement(string num){// get the 2s complement of a binary string. for (int n = 0; n < strlen(num.c_str()); n++){ if (num.c_str()[n] == '0'){ num.replace(n, 1, "1"); } else{ num.replace(n, 1, "0"); } } return AddBin(num, "1");}string AddBin(string num1, string num2){// add two binary strings. string b_sum; bool carry = 0; for (int n = 0; n < size; n++){ if (carry){ if (num1.c_str()[n] == '1' && num2.c_str()[n] == '1'){ carry = 1; b_sum.replace(n, 1, "1"); } else{ carry = 0; if (num1.c_str()[n] == '1' || num2.c_str()[n] == '1'){ carry = 1; b_sum.replace(n, 1, "0"); } else{ b_sum.replace(n, 1, "1"); } } } else{ if (num1.c_str()[n] == '1' && num2.c_str()[n] == '1'){ carry = 1; b_sum.replace(n, 1, "0"); } else{ if (num1.c_str()[n] == '1' || num2.c_str()[n] == '1'){ b_sum.replace(n, 1, "1"); } else{ b_sum.replace(n, 1, "0"); } } } } return b_sum;}int BinToDec(string num){// converts the binary string to its equivalent decimal number int sum = 0; bool neg = 0; if (num.c_str() == '1'){ num = TwosComplement(num); neg = 1; } for (int n = 0; n < size; n++){ if (num.c_str()[n] == '1'){ sum += pow(2, n); } } if (OP1.c_str() == '1' && OP2.c_str() == '1' || neg){ sum *= (-1); } return sum;}void decode(string cmd){ if (cmd.find("ADD") == 0){ if (/*1*/cmd.find(" ") == strlen("ADD") &&            /*2*/cmd.find(",") != strlen("ADD ") &&            /*3*/cmd.find(",") != string::npos &&            /*4*/cmd.find(",") != (strlen(cmd.c_str()) - 1) &&            /*5*/cmd.find(",", cmd.find(",") + 1) == string::npos &&            /*6*/cmd.find(" ", cmd.find(" ") + 1) != (cmd.find(" ") + 1) &&            /*7*/(cmd.find(" ", cmd.find(" ") + 1) == string::npos || cmd.find(" ", cmd.find(" ", cmd.find(" ") + 1) + 1) == string::npos)){ OPCODE = ADD; string x; string y; x = cmd.substr(strlen("ADD "), cmd.length() - (cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x if (cmd.find(" ", cmd.find(","), 1) == cmd.find(",") + 1){ y = cmd.substr(cmd.find(" ", cmd.find(",")) + 1);//store the value of y } else{ y = cmd.substr(cmd.find(",") + 1);//store the value of y } if (strspn(x.c_str(), "-0123456789") && (x.find("-") == 0 || x.find("-") == string::npos) && strspn(y.c_str(), "-0123456789") && (x.find("-") == 0 || x.find("-") == string::npos)){ int num1 = atoi(x.c_str()); int num2 = atoi(y.c_str()); if (num1 < pow(2, size) && num1 > -pow(2, size) && num2 < pow(2, size) && num2 > -pow(2, size)){ OP1 = DecToBin(num1); OP2 = DecToBin(num2); if (num1 < 0){ OP1 = TwosComplement(OP1); } if (num2 < 0){ OP2 = TwosComplement(OP2); } //cout << endl << OP1 << endl << OP2 << endl; } else{ OPCODE = ERR; ERR_MSG = TOBIG; } } else{ OPCODE = ERR; ERR_MSG = NUM; } } else{ OPCODE = ERR; ERR_MSG = SYNTAX; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX OPCODE = PRT; } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP OPCODE = HELP; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT OPCODE = EXT; } else{ OPCODE = ERR; ERR_MSG = CMD; }}void execute(){ switch (OPCODE){ case ADD:    //cout << endl << AddBin(OP1, OP2) << endl << endl;            char *str;            str=(char *) malloc(size*sizeof(char));    itoa(BinToDec(AddBin(OP1, OP2)), str, 10);    AX.clear();    AX.replace(0, strlen(str), str);    delete [] str; //AX = to_string(BinToDec(AddBin(OP1, OP2))); break; case PRT: cout << ">AX = " << AX << endl;//Prints the result break; case EXT: EXIT = true; case HELP: cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; break; case ERR: cout << ">Error: "; switch (ERR_MSG){ case CMD: cout << "Unknown command!"; break; case NUM: cout << "Enter only numbers!"; break; case SYNTAX: cout << "Take care of the input syntax!"; break; case TOBIG: cout << "To big number: " << -pow(2, size - 1) - 1 << " > x < " << pow(2, size - 1); break; } cout << endl; break; }}int main(){ string cmd; init(); do{ cmd = getinput(); decode(cmd); execute(); } while (!EXIT);} Franz Schmidt · December 14, 2014 Damned didn't work :/Do you see the little thext this is from the (n) in square brackets []. Franz Schmidt · December 14, 2014 Here I uploaded the main file:http://www.file-upload.net/download-9997446/main.cpp.html

C++

129,803 followers