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 13, 2014
@franz

You will modify the first program since it operates only on decimal numbers. This exercise will have the conversation of decimal numbers to it binary form. The binary additions will simulate how a computer operates at the CPU level. Assume that our machine will work only on 16-bits data.

You should have the following addiotional functions:
string DecToBin(int num) - converts the decimal number to its equivalent binary string format
int BinToDec(string num) - converts the binary string to its equivalent decimal number.
string TwosComplimenent(string num) - get the two's compliment of a binary string.
string AddBin(string num1, string num2) - add two binary strings.
Modify the following in your second exercise:

In your decode function, make the transformation of decimal to its equivalent binary string.  This includes the converstion of 2s compliment for negative numbers.
AX, OP1 and OP2 global variables should already be string.
In the execute function, make use of the AddBin function in your ADD operation function.
The PRT operation function will however display the decimal equivalent of AX.  Therefore, use the function BinToDec in this section of the code.

UPDATE INSTRUCTION XDD
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 Franz Schmidt · December 13, 2014
So I think this is it, but slowly it gets really confusing.:D
Btw. Nice method with this 2's complement, didn't know it also works like this.

The only thing that confuses me at the instruction is the 16-bit data...
What do you mean with "only on 16-bit data" ?
Does this mean the result can have maximum 16 bit or the singl inputs (OP1, OP2)
Because if I add two 16 bit values I need a 17th bit to save the result.
And if I have a 16 bit result I only can add two 15 bit values.

#include <iostream>
#include <string>
#define size 17
using 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") && strspn(y.c_str(), "-0123456789")){
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);
}
}
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:
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);
}
0 Ed Gon · December 13, 2014
@franz, yea where thinking right about 16 bit, my prof limit it so that i wont be a big ordeal in deal with "machine level" we are slow building a own virtual machine. thanks again franz!
0 Ed Gon · December 13, 2014
@franz i'm having this error, i guess its about the compiler or library. I'm using codeblocks to compile the program.
This is the library im using. 


#include <iostream>
#include <string>
#define size 17
#include <math.h>
#include <string.h>
#include <cstdlib>
#include <ostream>
#include <conio.h>



http://i.imgur.com/VJgD7MY.jpg
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 13, 2014
I can't follow any of your errors :ermm:

The three errors which are the same just says that I compair a pointer with an integer, but I compair a char with an integer/char
Try to save the value of num.c_str() in an extra char array.
This should be the explanation, but the code should work. May codeblocks is the cause.
http://stackoverflow.com/questions/2263681/c-compile-error-iso-c-forbids-comparison-between-pointer-and-integer
If you didn't change anything the code should work. I use Visual Studio Express

And the last error is really supicious, because to_string is declared in <string>
http://www.cplusplus.com/reference/string/to_string/
0 Franz Schmidt · December 13, 2014
So I insalled codeblocks as well and I found out that this error with the to_string is caused by codeblocks itself.
http://stackoverflow.com/questions/19893922/c11-to-string-to-working-with-codeblocks-std-c11-flag-already-selected

So I modified this part to work without the to_string.
#include <iostream>
#include <string>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define size 17
using 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") && strspn(y.c_str(), "-0123456789")){
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);
}
}
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:
            char *str;
            str=(char *) malloc(size*sizeof(char));
   itoa(BinToDec(AddBin(OP1, OP2)), str, 10);
   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);
}
0 Ed Gon · December 14, 2014
I change '1' to "1" in this snippet and it work pretty well though.


if (num.c_str() == "1"/*'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"/*'1'*/ && OP2.c_str() == "1" /*'1'*/ || neg){
sum *= (-1);

C++

124,665 followers
About

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

Links
Moderators
Bucky Roberts Administrator