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!
#include <iostream>
#include <math.h>
#include <stdlib.h>
usingnamespace 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;
}
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.
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.
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()
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':