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 1
Oldest  Newest  Rating
+2 Franz Schmidt · December 3, 2014
Could you please give an example what your programm does?
0 Ed Gon · December 4, 2014
@Franz

my code does:
if I input ADD 4,3 the 4,3 is been add 4+3 but its not been shown yet unless you input PRT AX.

thus my problem now is to convert those decimal digits into binary which is the 0100 (4) and 0011 (3).

then add those binary digits in binary form then the result must be converted back to decimal form. if I input PRT AX the decimal form should appear.

I hope you can help :)
0 Ed Gon · December 4, 2014
@krootushas gesu

yea i know, but I am making a virtual machine(for my course project,so yea) on how computer understands. which its a hassle.
0 Ed Gon · December 4, 2014
@wheately

you just change the interface, its just my code. -.-
0 Ed Gon · December 4, 2014
@wheately aw so sorry appreciated the effort!
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 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 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 it should be on our own without those premade libs.
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;

C++

107,093 followers
About

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

Links
Moderators
Bucky Roberts Administrator