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
0 Adeline Faye Lompon · December 16, 2014
What is itoa means in the code ?
 itoa(BinToDec(AddBin(OP1, OP2)), str, 10);
0 Ed Gon · December 15, 2014
@franz. btw its buggy sometimes. when you type a wrong command at first it yields different answer, I'm guessing its calculating the command error. anyway thanks again!
0 Franz Schmidt · December 14, 2014
Here I uploaded the main file:
http://www.file-upload.net/download-9997446/main.cpp.html
0 Franz Schmidt · December 14, 2014
Damned didn't work :/
Do you see the little thext this is from the (n) in square brackets [].
0 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 work
The conditions won't work with "1"

Sorry man :angel:
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 :ermm::

#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") && (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);
}
0 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. 
0 Franz Schmidt · December 14, 2014
:D right i forgot to clear the AX value 
fixed it
btw. I added some conditions so you can only enter e.g. -234 not 3-23
Hope everything works fine now.

#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") && (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);
}
0 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 http://i.imgur.com/x5gxeK9.png
0 Ed Gon · December 14, 2014
ughhhh. adding the negative is screwing again. pffft.http://i.imgur.com/WfGDZ2W.png
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,547 followers
About

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

Links
Moderators
Bucky Roberts Administrator