### I want to make a Multiple Operator calculator

So here's my problem. I want to make a multiple calculator. I mean making something like 3+5=8. Now I want my calculator to be able to manipulate with 8 directly, meaning adding something to it or subtracting or multiplying or dividing it or using it in my power-base calculations etc. And you can see here, I am nowhere near there. Please help!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169`` ``````#include #include #include using namespace std; float product() { int counter=0; int no_of_terms=0; float x=0.0,y=0.0; Retry: cout<<"Enter number of terms: "; cin>>no_of_terms; if (no_of_terms==1 || no_of_terms==0) { cout<<"Do not use 0 or 1!"; goto Retry; } cout<<"Enter a number: "; cin>>x; for (counter=1;counter>y; x*=y; } cout<<"Result is: "<>no_of_terms; if (no_of_terms==1 || no_of_terms==0) { cout<<"Do not use 0 or 1!"; goto Retry; } cout<<"Enter a number: "; cin>>x; for (counter=1;counter>y; x+=y; } cout<<"Result is: "<>no_of_terms; if (no_of_terms==1 || no_of_terms==0) { cout<<"Do not use 0 or 1!"; goto Retry; } cout<<"Enter a number: "; cin>>x; for (counter=1;counter>y; x-=y; } cout<<"Result is: "<>no_of_terms; if (no_of_terms==1 || no_of_terms==0) { cout<<"Do not use 0 or 1!"; goto Retry; } cout<<"Enter a number: "; cin>>x; for (counter=1;counter>y; x/=y; } cout<<"Result is: "<>main_choice; switch (main_choice) //starting main switch, choose between Numeric, Trignometric & Exponential calculations { case'*': product(); break; case'+': addition(); break; case'-': subtraction(); break; case'/': division(); break; case '^': float x,y; cout<<"Enter base: "; cin>>x; cout<<"Enter power: "; cin>>y; pow (x,y); cout<<"Result is: "<>x; if (x<0 && x<0) { cout<<"Error! Enter a non-negative number!"; goto ln; } else { cout<<"Artificial log of "<>x; if (x<0 && x<0) { cout<<"Error! Enter a non-negative number!"; goto ln2; } else { cout<<"Natural log of "<
Last edited on
What do you mean by 8, base-8 math? And wherever possible break your functions down into simpler functions so you can just call a function over and over instead of having to type out the same code. For instance:

if (no_of_terms==1 || no_of_terms==0)
{
cout<<"Do not use 0 or 1!";
goto Retry;
}
cout<<"Enter a number: "; cin>>x;

this code could be it's own function, and you could just return it by reference or with the return statement.

I've found that breaking down code into smaller fragments makes it easier to keep straight and to write.

Also my first calculator program just said "what is the first number?" "what is the second number?" "what do you want to do with it (* / + -)?" Then if (symbol == 'x' || symbol == '*') cout << a*b;" and so on. Much simpler, but not as nice a program. Now I'm trying one that gets a whole problem at once and breaks it down, e.g. 15x9. Much more complicated than it might look. An idea perhaps for a future modification of your program.
Last edited on
He means 3+5=8 then he wants to save the 8 so he can add to it or do whatever with 8 and just set your results to a variable and use that
I can give you some code to look at, if u want. I am learning from programming principle practice(Strostrup), and I made a calculator yesterday. Its more like a project, but it's not complete. I have to add a few more functionality, so it can also use variables.

My calculator uses a grammar:

Expression:
term;
term+expression;
term - expression;

term:
secondary;
term*secondary;
term/secondary;

secondary:
primary;
secondary%primary;

primary:
numbers;
"(expression)";

number:
floating-point literals;

Note:If you are making a calculator, a more of a complex calculator,
which can provide you the correct results, if you do something like this:

2 + 3*6; This should be solved like this 2+(3*6)=20
and not (2+3)*6= 30

For solving it the right way and for multiple calculations like you wanna do,

9+2-1*3, You need to define some kind of grammar, so ur compiler knows what you wanna do first (* or +). YOu need clearly defined rules.

Here is my code, ask me if u don't understand.It's not complete yet, needs commenting and a lot of clearing.

//Calculator

#include "std_lib_facilities.h"

const char number = '8';
const char print = ';';
const char quit = 'q';
const string prompt = ">";
const string result = "=";

void calculate();

void keep_window_opens()
{
char ch;
cout<<"enter this '~' to exit\n";
while(cin>>ch){
if(ch=='~')
{
break;
}

}

}

class Token{
public:
char kind;
double value;
Token(char ch)
:kind(ch),value(0){}
Token(char ch,double val)
:kind(ch),value(val){}
};

class Token_stream{
public:
Token_stream(); //make a token_stream that reads from cin
Token get(); //get a token
void putback(Token t); //put a token back
void ignore(char c);

private:
bool full; //is there a Token in the buffer
Token buffer; //here is where we keep a token put back using putback()

};

Token_stream::Token_stream()
:full(false),buffer(0){}

void Token_stream:: ignore(char c)
{
// check if buffer is full
if(full && c==buffer.kind)
{
full =false;
return;
}

full =false;

char ch=0;
while(cin>>ch)
if(ch==c)return;
}

void Token_stream::putback(Token t)
{
if(full)
{
error("putback() into a full buffer");
keep_window_opens();
}
buffer=t;
full=true;
}

Token Token_stream:: get()
{
if(full){
full=false;
return buffer;
}

char ch;
cin>>ch;

switch(ch)
{
case print:
case quit:
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '{':
case '}':
case'!':
case '%':

case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':

{ cin.putback(ch);
double val;
cin>>val;
}

default:
keep_window_opens();
return 1;

}

}

Token_stream ts; //provides get() and pushback()
double expression();

double primary()
{
Token t= ts.get();
switch(t.kind)
{

case '{':
{ t=ts.get();
if(t.kind!='(')
{
error("'(' expected");
keep_window_opens();
}
ts.putback(t);
double d = expression();
t=ts.get();
if(t.kind!='}')
{
error("'}' expected");
keep_window_opens();
}
return d;
break;

}

case '(':
{ double d = expression();
t=ts.get();
if(t.kind!=')')
{
error("')' expected");
keep_window_opens();
}
return d;
break;
}

case '+':
return primary();
break;

case '-':
return -primary();
break;

case number:
return t.value;
break;

default:
error("Primary expected");
keep_window_opens();
return 1;
}

}

double secondary()
{
double left =primary();
Token t=ts.get();
double fact=1.0;

switch(t.kind)
{
case '!':

{ if(left=='0')
{
left=1;
return left;
}

for(;left>0;left--)
{
fact = fact * left;
}

return fact;

}

default:
ts.putback(t);
return left;

}

}

double term()
{
double left = secondary();
Token t= ts.get();

while(1){

switch(t.kind)
{
case '*':
left*=secondary();
t=ts.get();
break;

case '/':

{ double d=secondary();
if(d==0)
{
error("divide by zero");
keep_window_opens();
}
left/=d;
t=ts.get();
break;
}

case '%':
{
double d =term();
int i1 = narrow_cast<int>(left);
if(i1!=left)error("Left hand operator not int");

int i2=narrow_cast<int>(d);
if(i2!=d)error("Right hand operator not int");
if(i2==0)error("Divided by zero");

left=i1 % i2;
t=ts.get();
break;
}

default:
ts.putback(t);
return left;

}

}

}

double expression()
{
double left=term();
Token t=ts.get();

while(1){

switch(t.kind)
{

case '+':
left+=term();
t=ts.get();
break;

case '-':
left-=term();
t=ts.get();
break;

default:
ts.putback(t);
return left;
}

}

}

void clean_up_mess()
{
ts.ignore(print);
}

int main()
try{

calculate();
keep_window_opens();
return 0;
}
catch(exception& e){
cerr<<e.what()<<endl;
keep_window_opens();
return 1;
}
catch(...){
cerr<<"Unknown exception"<<endl;
keep_window_opens();
return 2;
}

void calculate()
{
while(cin)
try{

cout<<prompt;
Token t =ts.get();
while(t.kind==print)t=ts.get(); //eat
if(t.kind==quit)
{
keep_window_opens();

}

ts.putback(t);
cout<<result<<expression()<<'\n';
}
catch(exception& e){
cerr<<e.what()<<endl;
clean_up_mess();
}

}

Topic archived. No new replies allowed.