Sorry, I originnaly posted this in the Geek Lounge:
Okay, the first project was to build a calculator that functioned as follows:
----------Using the example functions about, write a calculator program.
Allow for the user to enter a simple math functions to evaluate:
(Note: the '>' designates user input for cin)
>5
>+
>7
>=
result:12
or:
>5
>+
>7
>-
>2
>=
result: 10
Your program will need to evaluate as many numbers and operations until the user enters a '=', at which time the result will be printed
Your program will need a seperate function to evaluate each math operation ( +, -, *, / )
Your program will also need a function to determine which math function to call
The functions you have written for Project 1 have been setup to use read in ints.
You will need to *overload* these functions to also allow for floats----------
I researched all over and could not get int and float to overload. I submitted the following code for that project just using float.
//Project1
#include <cstdlib>
#include <iostream>
using namespace std;
void get_input(float& input1, float input2, float& total1, float total2);
float result,input2,total2,total1,input1;
char sign;
float add(float& total1, float input2){
return total2=total1+input2;
}
float sub(float& total1, float input2){
return total2=total1-input2;
}
float mult(float& total1, float input2){
return total2=total1*input2;
}
float div(float& total1, float input2){
return total2=total1/input2;
}
float total(float& total2){
return total2;
}
int main()
{
float first_num, second_num, result1, result2;
get_input(first_num, second_num, result1, result2);
return (0);
}
void get_input(float& input1, float input2, float& total1, float total2)
{
cout << "Hello, this is a running total calculator. You can add \"+\", subtract \"-\", " << endl;
cout << "multiply \"*\", and divide \"/\". To work this calculator you enter a number " << endl;
cout << "then press [Enter]. You then you enter the operand, \"+,-,*,/\" and [Enter]." <<endl;
cout << "continue this process until you have enter your last number. To get the total, " << endl;
cout << "press \"=\". The functions will work like this: 8+2-4*6/3=12 NOT 8+2-4*6/3=2." <<endl;
cout << "To quit the program, enter \"Q\". Have fun." << endl;
cout << ">";
cin >> total1;
do{
cin >> sign;
if (sign== '='){
result=total(total2);
cout << total2 << endl;
}else
cin >> input2;
if(sign=='+'){
total1=add(total1,input2);
total2=total1;
}else if(sign=='-'){
total1=sub(total1,input2);
total2=total1;
}else if(sign=='*'){
total1=mult(total1,input2);
total2=total1;
}else if(sign=='/'){
total1=div(total1,input2);
total2=total1;
}
}while((sign != 'q') || (sign != 'Q'));
system("pause");
}
The code may not be pretty but it works.
Now using the same program,we are supposed to do the following:
------So, for this project we're going to revisit our Project 1 Calculator.
Now, we're going to use our knowledge of classes and build a Calculator Class with the same functionality of our original calculator, but all of our functions are now going to be embedded in an object class.------
I am still having problems getting the overload to work with int& float. I don't know if it is how i set up the references or what. just setting up touse float, it does not i ger 1.#QNAN. If i set it up for int, it works butnodecimal.
here is the current code:
//FinalProject
#include <cstdlib>
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int& ,int);
float add(float&, float);
int sub(int& ,int);
float sub(float&, float);
int mult(int& ,int);
float mult(float&, float);
int div(int& ,int);
float div(float&, float);
};
//void get_input(int& result,int input1);
void get_input(float& result, float input1);
//int result,input1;
float result, input1;
char sign;
int Calculator::add(int& result,int input1)
{
result+input1;
}
float Calculator::add(float& result, float input1)
{
result+input1;
}
int Calculator::sub(int& result,int input1)
{
result-input1;
}
float Calculator::sub(float& result, float input1)
{
result-input1;
}
int Calculator::mult(int& result,int input1)
{
result*input1;
}
float Calculator::mult(float& result, float input1)
{
result*input1;
}
int Calculator::div(int& result,int input1)
{
result/input1;
}
float Calculator::div(float& result, float input1)
{
result/input1;
}
int main()
{
//int result1, first_num;
float result1, first_num;
get_input(result1, first_num);
//Calculator c;
return (0);
}
//void get_input(int& result,int input1)
void get_input(float& result, float input1)
{
Calculator c;
cout << "Hello, this is a running total calculator. You can add \"+\", subtract \"-\", " << endl;
cout << "multiply \"*\", and divide \"/\". To work this calculator you enter a number " << endl;
cout << "then press [Enter]. You then you enter the operand, \"+,-,*,/\" and [Enter]." <<endl;
cout << "continue this process until you have enter your last number. To get the total, " << endl;
cout << "press \"=\". The functions will work like this: 8+2-4*6/3=12 NOT 8+2-4*6/3=2." <<endl;
cout << "To quit the program, enter \"Q\". Have fun." << endl;
cout << ">";
cin >> result;
do{
cout << ">";
cin >> sign;
if (sign== '='){
result;
cout << result << endl;
}else
cout << ">";
cin >> input1;
if(sign=='+'){
result=c.add(result,input1);
result=result;
}else if(sign=='-'){
result=c.sub(result,input1);
result=result;
}else if(sign=='*'){
result=c.mult(result,input1);
result=result;
}else if(sign=='/'){
result=c.div(result,input1);
result;
}
}while((sign != 'q') || (sign != 'Q'));
system("pause");
}