I'm having horrible trouble trying to get this converter to work. If more code is needed, please feel free to let me know. The function I'm having trouble with getting the numbers to convert right is OctalIntoDecimal. The number it is trying to convert is a 9 in decimal...which should be 11 in octal. Same thing with 28, should be 30.
I got it to output a 2 instead of 11, which shows me it's doing something but it's not quite there yet. Any help you all could give me would be great. This assignment is due this week! Thanks...
1. genregister is a string array of 8
2. memory is also a string array holding values
3. Go to the commented Second Part and to the if statement about relative deferred add to see where I am implementing the problematic function. (Line 240)
/**************/
int RelativeDeferred(string numberreg) {
//This function helps find the relative deferred number and converts
//the string into a decimal number.
if ("000000" == numberreg)
return 0;
if ("000001" == numberreg)
return 1;
if ("000002" == numberreg)
return 2;
if ("000003" == numberreg)
return 3;
if ("000004" == numberreg)
return 4;
if ("000005" == numberreg)
return 5;
if ("000006" == numberreg)
return 6;
if ("000007" == numberreg)
return 7;
return 0;
}
/**************/
string ConvertAnswer(string number) {
//This function will convert the decimal back into binary for my arrays.
if ("0" == number)
return "000";
if ("1" == number)
return "001";
if ("2" == number)
return "010";
if ("3" == number)
return "011";
if ("4" == number)
return "100";
if ("5" == number)
return "101";
if ("6" == number)
return "110";
if ("7" == number)
return "111";
if ("8" == number)
return "1000";
if ("9" == number)
return "1001";
if ("10" == number)
return "1010";
if ("11" == number)
return "1011";
if ("12" == number)
return "1100";
if ("13" == number)
return "1101";
if ("14" == number)
return "1110";
return 0;
}
/**************/
int Absolute(string numberreg) {
//This function uses the register numbers to help add.
if ("000" == numberreg)
return 0;
if ("001" == numberreg)
return 1;
if ("010" == numberreg)
return 2;
if ("011" == numberreg)
return 3;
if ("100" == numberreg)
return 4;
if ("101" == numberreg)
return 5;
if ("110" == numberreg)
return 6;
if ("111" == numberreg)
return 7;
return 0;
}
/**************/
int ConvertDecimal(string math) {
//This function converts binary into decimal.
if ("000" == math)
return 0;
if ("001" == math)
return 1;
if ("010" == math)
return 2;
if ("011" == math)
return 3;
if ("100" == math)
return 4;
if ("101" == math)
return 5;
if ("110" == math)
return 6;
if ("111" == math)
return 7;
return 0;
}
/**************/
int OctalIntoDecimal(int answer) {
//This function converts octal into decimal.
double i;
int number;
int r;
int m, x, p;
double s = 0;
//number = answer;
for (i = 0; number !=0; i++) {
//r = number % 10;
//s = s + r * (double)pow(10, i);
//number = number / 10;
m = pow(10, i);
number = answer;
x = number % m;
p = x * pow(10, i-1);
r += p;
number /= 10;
}
return number;
}
/**************/
void Add() {
//This function sets the Opcode modes.
string opcode;
string opcode2;
string add;
string add2;
int math = 100;
int math2 = 100;
int answer = 100;
std:: string s;
std:: stringstream output;
string test;
int numberreg;
int numberreg2;
int relative;
int relative2;
int deferred;
int deferred2;
int relativedef;
int relativedef2;
string relativedefstr;
string relativedefstr2;
//Set the variables to the correct spot for each operation.
opcode = IR.substr(4, 3);
add = IR.substr(7, 3);
opcode2 = IR.substr(10, 3);
add2 = IR.substr(13, 3);
//Print out the operations.
cout << "This is the first Opcode mode: " << opcode << endl;
cout << "This is the first number to add: " << add << endl;
cout << "This is the second Opcode mode: " << opcode2 << endl;
cout << "This is the second number to add: " << add2 << endl;
//First Part
if (opcode == "000") {
cout << "Immediate Add" << endl;
math = ConvertDecimal(add);
}
if (opcode == "001") {
cout << "Absolute Add" << endl;
numberreg = Absolute(add);
cout << numberreg << endl;
math = genregister[numberreg];
}
if (opcode == "010") {
cout << "Relative Add" << endl;
numberreg = Absolute(add);
cout << numberreg << endl;
relative = genregister[numberreg] + numberreg;
math = genregister[relative];
}
if (opcode == "011") {
cout << "Relative Deferred Add" << endl;
numberreg = Absolute(add);
cout << numberreg << endl;
relativedef = genregister[numberreg];
cout << "Relative Deferred One: " << relativedef << endl;
cout << memory[relativedef-2] << endl;
math = RelativeDeferred(memory[relativedef-2]);
}
//Second Half of the Equation/ReadIn
if (opcode2 == "000") {
//Brings in second number, adds it to the first,
//then out sources the answer to convert it back to octal.
cout << "Immediate Add" << endl;
math2 = ConvertDecimal(add2);
answer = math + math2;
output << answer;
test = output.str();
memory[genregister[7]] = ConvertAnswer(test);
genregister[7]++;
}
if (opcode2 == "001") {
cout << "Absolute Add" << endl;
numberreg2 = Absolute(add2);
math2 = genregister[numberreg2];
answer = math + math2;
genregister[numberreg2] = math + math2;
}
if (opcode2 == "010") {
cout << "Relative Add" << endl;
numberreg2 = Absolute(add2);
cout << numberreg2 << endl;
relative2 = genregister[numberreg2] + numberreg2;
math2 = genregister[relative2];
genregister[numberreg2] = math + math2;
answer = math + math2;
}
if (opcode2 == "011") {
cout << "Relative Deferred Add" << endl;
numberreg2 = Absolute(add2);
cout << numberreg2 << endl;
relativedef2 = genregister[numberreg2];
cout << "Relative Deferred Two: " << relativedef2 << endl;
cout << memory[relativedef2-2] << endl;
math2 = RelativeDeferred(memory[relativedef2-2]);
answer = math + math2;
genregister[numberreg2] = OctalIntoDecimal(answer);
}
cout << endl;
cout << "First number to be added: " << math << endl;
cout << "Second number to be added: " << math2 << endl;
cout << "The answer to the equation: " << answer << endl;
cout << endl;
}