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 2
Oldest  Newest  Rating
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 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 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 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 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

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 Ed Gon · December 10, 2014
@franz never notice it was on the arguments. i though it was in storing AX, i tried printing it out so that i would know it really do store. anyways thanks for the help again!
0 Franz Schmidt · December 10, 2014
Yes there was a little mistake in the if statement of the syntax error :ermm:

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

long long AX = 0;
long long OP1 = 0;
long long OP2 = 0;
enum CMDS { ADD, PRT, EXT, HELP, ERR } OPCODE;
bool EXIT = false;
enum ERRORS { CMD, NUM, SYNTAX} 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;
}
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){
/*fails from the if above:
1. ADD1,2
2. ADD ,2
3. ADD 1,
4. ADD 1 2
5. ADD 1,2, or ADD 1,,2
6. ADD  1,2
7. ADD 1,  2
*/
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")){
OP1 = atoi(x.c_str());
OP2 = atoi(y.c_str()); //calculates the result
}
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:
//convert x and y to binary and save binary values in b_x and b_y
bool b_x[31];
bool b_y[31];
long long x_num;
long long y_num;

x_num = OP1;
y_num = OP2;
for (int n = 30; n >= 0; n--){
if ((x_num - pow(2, n)) >= 0){
b_x[n] = 1;
x_num -= pow(2, n);
}
else 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 if ((y_num + pow(2, n)) <= 0){
b_y[n] = 1;
y_num += pow(2, n);
}
else{
b_y[n] = 0;
}
}

/*cout << endl << "Direct conversion: ";
cout << endl << "b_x: ";
for (int n = 30; n >= 0; n--){
cout << b_x[n];
}
cout << endl << "b_y: ";
for (int n = 30; n >= 0; n--){
cout << b_y[n];
}*/

//add/substract numbers in binary form
bool b_sum[31];
bool carry;

x_num = OP1;
y_num = OP2;
carry = 0;
if (x_num >= 0 && y_num >= 0 || x_num <= 0 && y_num <= 0){
//addition
for (int n = 0; n < 31; 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];
}
}
}
}
else{
//substraction
bool b_big[31];
bool b_small[31];
bool borrow[31] = { 0 };//variable for substraction
if (x_num < 0){
x_num *= (-1);
}
if (y_num < 0){
y_num *= (-1);
}
if (x_num > y_num){
memcpy(b_big, b_x, sizeof(b_x));
memcpy(b_small, b_y, sizeof(b_y));
}
else{
memcpy(b_big, b_y, sizeof(b_y));
memcpy(b_small, b_x, sizeof(b_x));
}

for (int n = 0; n < 31; n++){
if (b_small[n]){
if (!b_big[n] && !borrow[n]){
int u = 1;
while (1){
if (b_big[n + u]){
b_big[n + u] = 0;
break;
}
else{
borrow[n + u] = 1;
}
u++;
}
b_sum[n] = 1;
}
else{
if (b_big[n] && borrow[n]){
b_sum[n] = 1;
}
else{
b_sum[n] = 0;
}
}
}
else if (b_big[n]){
if (borrow[n]){
b_sum[n] = 0;
}
else{
b_sum[n] = 1;
}
}
else{
if (borrow[n]){
b_sum[n] = 1;
}
else{
b_sum[n] = 0;
}
}
}
}
/*cout << endl << "sum: ";
for (int n = 30; n >= 0; n--){
cout << b_sum[n];
}
cout << endl;*/

//convert binary sum back to a normal dezimal number
long long sum;
x_num = OP1;
y_num = OP2;
sum = 0;
for (int n = 30; n >= 0; n--){
if (b_sum[n] == 1){
sum += pow(2, n);
}
}
if ((x_num <= 0 && y_num <= 0) || (x_num < 0 && (x_num * (-1)) > y_num) || (y_num < 0 && (y_num * (-1)) > x_num)){
sum *= (-1);
}
//cout << "Sum from binary addition: " << sum << endl;
AX = sum;
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;
}
cout << endl;
break;
}
}
int main(){
string cmd;
init();
do{
cmd = getinput();
decode(cmd);
execute();
} while (!EXIT);
}
-1 Ed Gon · December 10, 2014
@franz i try manipulating the code that you provided. i met some issues with it.

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

using namespace std;

long long AX = 0;
long long OP1 = 0;
long long OP2 = 0;
enum CMDS {ADD, PRT, EXT, ERR} OP_CODE;//enum is a variable type which can take different fits
bool EXIT = false;

void init(){
//OP_CODE = ERR;
cout << "Type in this format ADD (x,y): "  << endl;
}
string getinput(){
string input;
cout << ">" << endl << ">";
getline(cin, input);//catches the data
return input;
}
void decode(string cmd){
if (cmd.find("ADD") == 0){
if (cmd.find(" ") == strlen("ADD") && cmd.find(",") != strlen("ADD ") && cmd.find(",") != string::npos && cmd.find(",", cmd.find(",") + 1) == string::npos && cmd.find(" ", cmd.find(" ") + 1) != (cmd.find(" ") + 1) && cmd.find(" ", cmd.find(" ") + 1) != cmd.find(" ", cmd.find(" ") + 1)){
OP_CODE = 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
//OP1 = atoi(x.c_str());
}
else{
y = cmd.substr(cmd.find(",") + 1);//store the value of y
                //OP2 = atoi(y.c_str());
}
if (strspn(x.c_str(), "0123456789") && strspn(y.c_str(), "0123456789")){
OP1 = atoi(x.c_str());
OP2 = atoi(y.c_str()); //calculates the result
}
else{
OP_CODE = ERR;
}
}
else if (cmd.find("PRT") == 0 && cmd.length() == strlen("PRT")){//Triggers the PRT AX
OP_CODE = PRT;
        }
}
else if (cmd.find("EXT") == 0 && cmd.length() == strlen("EXT")){//Triggers the EXIT
OP_CODE = EXT;
        }
else{
OP_CODE = ERR;
}
}
void execute(){
switch (OP_CODE){
        case ADD:
            AX = OP1 + OP2;
            break;
case PRT:
cout << ">AX = " << AX << endl;//Prints the result
break;
case EXT:
EXIT = true;
break;
case ERR:
cout << ">Error: Unknown command!" << endl;
break;
}
}
int main(){
string cmd;
init();
do{
cmd = getinput();
decode(cmd);
execute();
} while (!EXIT);
}


http://i.imgur.com/VD4LeS4.jpg
0 Franz Schmidt · December 7, 2014
Just practice :D

C++

126,496 followers
About

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

Links
Moderators
Bucky Roberts Administrator