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

+7 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 <iostream>
#include <string.h>
#include <cstdlib>

using namespace std;

int main(){
string cmd;

long long ax = 0;
bool error = false;

cout << "Type in command (type \"HELP\" to see all commands): " <<endl;
while(1){
error = false;
cout << ">";
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;
}
}
}

Post a Reply

Replies

- page 4
Oldest  Newest  Rating
0 Franz Schmidt · December 6, 2014
Oh damn forgot about the negative numbers :(
I will have a look at it but it will take me some time because substraction is way more difficult then just adding :ermm:

If you want to try it one your own here is a tutorial how to substract binary nums: https://www.youtube.com/watch?v=S9LJknZTyos
0 Ed Gon · December 6, 2014
@franz im stuck up in this format, while merging it in the main prog.

//Edwin B. Gonzales Jr.
//Csc 112

#include <iostream>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>

using namespace std;

int main(){
string cmd;
long long ax = 0;
bool error = false;
string x;
string y;

cout << "Type in command (type \"HELP\" to see all commands): " << endl;

while(1){
error = false;
cout << ">";
getline(cin, cmd);
if (cmd.find("ADD") == 0){
if (cmd.find(" ") == strlen("ADD")){
string x = cmd.substr(strlen("ADD "), cmd.length()-(cmd.length() - cmd.find(",") + strlen("ADD ")));
string y = cmd.substr(cmd.find(",") + 1);
                    ax = atoi(x.c_str()) + atoi(y.c_str());
                    //convert x and y to binary and save binary values in b_x and b_y
                        bool 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());
                        //converting decimal to binary
                        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 form
    bool 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 number
unsigned 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;


}
else{
error = true;
}
}
else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){
cout << ">AX = " << ax << endl;
}
else if (cmd.find("HELP") == 0 && cmd.length() == strlen("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")){
break;
}
else{
error = true;
}

if (error){
cout << ">Error: Unknown command!" << endl;
}
}

cout << endl << "Sum from normal addition: " << ax;
    cout << endl << "Bitset conversion: ";//binary value of ax
    bitset < 32 > s(ax);
    cout << endl << "sum: " << s << endl;
    bitset < 32 > num1(atoi(x.c_str()));
    cout << endl << "x: " << num1;//binary value of x
    bitset < 32 > num2(atoi(y.c_str()));
    cout << endl << "y: " << num2;//binary value of y


}

http://i.imgur.com/8O0DvQb.jpg
This is the issue I'm handling right now. 
0 Ed Gon · December 6, 2014
@franz how did you insert it in the main program?
0 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 ax
bitset < 32 > num1(atoi(x.c_str()));
cout << endl << "x: " << num1;//binary value of x
bitset < 32 > num2(atoi(y.c_str()));
cout << endl << "y: " << num2;//binary value of y
bitset < 32 > s(ax);
cout << endl << "sum: " << s << endl;

//convert x and y to binary and save binary values in b_x and b_y
bool 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 form
bool 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 number
unsigned 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;
0 Ed Gon · December 5, 2014
@franz it should be on our own without those premade libs.
0 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 <CHAR_BIT> s(ax);
cout << endl << "Sum normal addition: " << endl << s;//binary value of ax
bitset <CHAR_BIT> num1(atoi(x.c_str()));
cout << endl << "x: " << num1;//binary value of x
bitset <CHAR_BIT> num2(atoi(y.c_str()));
cout << endl << "y: " << num2 << endl;//binary value of y

bitset <CHAR_BIT> 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 sum
cout << 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?
0 Ed Gon · December 5, 2014
@Franz, I made two seperate codes for the miniADD and Flip but im stuck on how to incorporate it in the main code,



miniADD (got some little issues)

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char a[32];
    char b[32];
    char carry = '0';
    char result[32];


    cin >> a >> b;

    for(int i = 0; i < 32; i++)
    {

        if(a == '1' && b == '1') //&& carry == '0')
        {
            result = '0';
            carry = '1';
        }
        else if(a == '0' && b == '1' && carry == '1')
        {
           result = '0';
           carry = '1';
        }
        else if(a == '1' && b == '1' && carry == '1')
        {
            result = '1';
            carry = '1';
        }
        else if(a == '1' && b == '0' && carry == '1')
        {
            result = '0';
            carry = '1';
        }
        else if(a == '1' && b == '0' && carry == '0')
        {
            result = '1';
            carry = '0';
        }
        else if(a == '0' && b == '0' && carry == '1')
        {
            result = '1';
            carry = '0';
        }
        else if(a == '0' && b == '1' && carry == '0')
        {
            result = '1';
            carry = '0';
        }
        else if(a == '0' && b == '0' && carry == '0')
        {
            result = '0';
            carry = '0';
        }


    }

    result[32] = carry;
    for(int j = 32; j >= 0; j--)
    {
        cout << result[j];

    }

    cout << endl;
    return 0;
}

FLIP (not fully working)

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>


using namespace std;

int main()
{

    int j;
    int x[16];
    int i;
    string a;
    int z = 0;
    stringstream convert;


    cin >> i;
    convert << i;
    a = convert.str();


    for(j = 0; j < 15; j++)
    {

        x[j] = a.substr(z, z + 1);
        if(x[j] == 0)
        {

            x[j] = 1;

        }
        else if(x[j] == 1)
        {
            x[j] = 0;

        }
        else
        {
            cout << " ";
        }
        z++;
    }

    cout << x[j];
    return 0;
}

0 Franz Schmidt · December 4, 2014
I give you a little starting point:

bitset <CHAR_BIT> b(x);
cout << endl << b << endl << b[3];


The b is a binary variable and is an array.
There is only the problem with the size of the b variable (CHAR_BIT only is 8)
0 Ed Gon · December 4, 2014
@wheately aw so sorry appreciated the effort!
0 Ed Gon · December 4, 2014
@wheately

you just change the interface, its just my code. -.-

C++

129,871 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator