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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <math.h>
#include <stdlib.h>

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<no_of_terms;counter++)
    {
        cout<<"Enter a number: "; cin>>y;
        x*=y;
    }
    cout<<"Result is: "<<x<<". \n\n";
    return 0;
}

float addition()
{
    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<no_of_terms;counter++)
    {
        cout<<"Enter a number: "; cin>>y;
        x+=y;
    }
    cout<<"Result is: "<<x<<". \n\n";
    return 0;
}

float subtraction()
{
    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<no_of_terms;counter++)
    {
        cout<<"Enter a number: "; cin>>y;
        x-=y;
    }
    cout<<"Result is: "<<x<<". \n\n";
    return 0;
}

float division()
{
    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<no_of_terms;counter++)
    {
        cout<<"Enter a number: "; cin>>y;
        x/=y;
    }
    cout<<"Result is: "<<x<<". \n\n";
    return 0;
}

int main()
{
    char main_choice;
    char num_choice;
    cout<<"\t\t********* TIME-FREEZER'S CALCULATOR *********\n\n";
menu:
    while (1)
    {
    cout<<"Choose number to get your required calculations!\n";
    cout<<"1. Press + for addition\n";
    cout<<"2. Press - for subtraction\n";
    cout<<"3. Press * for multiplication\n";
    cout<<"4. Press / for division\n";
    cout<<"5. Press ^ for exponential calculations\n";
    cout<<"6. Press l for Artificial Log\n";
    cout<<"7. Press L for Natural Log\n";
    cout<<"8. Press e to Exit Program\n";
    cout<<"Your choice: "; cin>>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: "<<pow(x,y)<<". \n\n";
        break;
    case 'l':
        ln:
        cout<<"Enter a number: "; cin>>x;
        if (x<0 && x<0)
        {
            cout<<"Error! Enter a non-negative number!";
            goto ln;
        }
        else
        {
            cout<<"Artificial log of "<<x<<" is "<<log10(x)<<". \n\n";
            break;
        }
    case 'L':
        ln2:
        cout<<"Enter a number: "; cin>>x;
        if (x<0 && x<0)
        {
            cout<<"Error! Enter a non-negative number!";
            goto ln2;
        }
        else
        {
            cout<<"Natural log of "<<x<<" is "<<log(x)<<". \n\n";
            break;
        }
    case 'e':
        cout<<"Thank you for using TIME FREEZER'S CALCULATOR!\n\n";
        exit(0);
        break;
        }
        }

return 0;
}
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 '%':
return Token(ch);

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;
return Token(number,val);
}

default:
error("Bad token");
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.