Need help with code

0 Hosein ES · May 24, 2015
Hi,

my teacher asked me to create a calculator that convert bases 2,8,10,16 and also has 4 operations + - / * at the same time,
i found to separate sources one for base conversion and another for calculating + * - / , but i don't know how should i combine them together, 
is there anybody help me with this,
any help will be appreciated,

Best Regards,

Post a Reply

Replies

Oldest  Newest  Rating
0 Nav Kamboj · June 3, 2015
Well it is pretty simple:
I have 2 methods of doing this so use which ever u like...

Method 1) Calculating all 4 values in same program;
#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a = 2, b = 8, c = 10, d = 16, e = 4, f;     //a, b, c, d, e are for the given values and "f" is for the output result.

f = a + b * c - d / e;                  /*While using this remember that this is gonna use the BODMAS rule...
so if u want to add or multiply first just close that () for example
I want it to add first and afterwards use the bodmas rule
so i will do it like this...  "(a + b) * c - d / e"  */
cout << "The answer is: " << f << endl;
return 0;
}

Method 2) You can ask the digit from the user and solve it.
#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a, b, c, d, e, f;
f = a + b * c - d / e;
cout << "Enter the first value for adding it with second" << endl;
cin >> a;
cout << "Enter the second value that you want to multiply with third" << endl;
cin >> b;
cout << "Enter the third value that you want to subtract from forth" << endl;
cin >> c;
cout << "Enter the forth value that you want to divide with fifth" << endl;
cin >> d;
cout << "Enter the fifth value" << endl;
cin >> e;

cout << "The solution of the above calculation is: " << f << endl;
return 0;
}
0 Alper Akgoz · June 3, 2015
Not sure how your program exactly works but I think you can add keywords to your program if the calculator is a console app,
some thing like "FromBase10 123 ToBase2" and if you need to combine conversion and operations together then I'd suggest using parentheses. It'd make parsing the input much easier.

So if you have
(FromBase16 FF ToBase10) + (FromBase2 01010 ToBase10)

loop through the string
  whenever you encounter "(" create a new string to hold the expression in parentheses
  store operator +,-,/,* (I'd suggest using a switch statement after expressions are converted)
  store the second expression
  split expressions by " " so you have the terms in the expression
  use a switch statement to decide which conversion function to call, for example FromBase16("FF", "ToBase10");
  convert and store the terms, if you have an operator then always convert to base 10

It seems like a lot of work but this is how I'd solve the problem. I hope it helps.
0 Anton Wieslander · May 31, 2015
I am still a basic noob, there is probably a much better way to do it then what I will say here.

Make 2 methods/procedures 1 - conversion 2 - calculate
in conversion break the problem down again you wanna banana recognise what base number it is and to what number you will convert it
in calculate all you want to do is take in 2 numbers and then ask for what operator (ADD,SUB,MUL,DIV). 

Think of a problem as a block made of lego, break it down in to smaller pieces and then program those pieces 1 by 1. Also watch the C++ series tutorial will help a lot.

PEACE
  • 1

C++

107,025 followers
About

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

Links
Moderators
Bucky Roberts Administrator