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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
int num1, num2, tempNum, result;
int remain;
void doAddition (int, int);
void doSubtraction (int, int);
void doDivision (int, int);
void doMultiplication (int, int);
void doExponent (int, int);
void doFactorial (int);
int main()
{
ifstream inFile;
char ch;
inFile.open("math.txt");
if(inFile.fail())
{
cout << "The math.txt file failed to open";
exit(-1);
}
inFile >> ch;
while(inFile)
{
switch(ch)
{
case '+':doAddition (num1, num2);
break;
case '-':doSubtraction (num1, num2);
break;
case '*':doMultiplication (num1, num2);
break;
case '^':doExponent (num1, num2);
break;
case '/':doDivision (num1, num2);
break;
case '!':doFactorial (num1);
break;
default:inFile.ignore (100, '\n' );
break;
}
}
inFile.close();
}
void doAddition ( int num1, int num2 )
{
//This function will calculate and display a sum. It takes one argument: a reference to an input file stream, and returns nothing.
result=num1+num2;
cout << "Addition\t" << num1 << "\t" << num2 << "\tsum:" << result;
}
void doSubtraction (int num1, int num2 )
{
//This function will calculate and display the difference of two numbers. It takes one argument: a reference to an input file stream, and returns nothing.
result=num1-num2;
cout<< "Subtraction\t" << num1 << "\t" << num2 << "\tdifference:" << result;
}
void doMultiplication (int num1, int num2 )
{
//This function will calculate and display a product. It takes one argument: a reference to an input file stream, and returns nothing.
result=num1*num2;
cout << "Multiplication\t" << num1<< "\t" << num2 << "\tproduct:" << result;
}
void doExponent ( int num1, int num2)
{
//This function will calculate and display the product of raising a number to a power. It takes one argument: a reference to an input file stream, and returns nothing.
result=powl(num1,num2);
cout << "Exponent\t" << num1 << "\t" << num2 << "\tproduct:" << result;
}
void doDivision ( int num1, int num2)
{
//This function will calculate and display the quotient and remainder of a division operation between two numbers. It takes one argument: a reference to an input file stream, and returns nothing.
result= num1/ num2;
remainder=num1%num2;
cout << "Division\t" << num1 <<"\t" << num2 << "\tQuo:" << result;
cout << "Remain:" << remain;
}
void doFactorial (int num1)
{
//This function will calculate and display the factorial of a number. It takes one argument: a reference to an input file stream, and returns nothing.
result = 1;
for(tempNum=num1,tempNum >= 1, tempNum-- )
result = result * tempNum;
cout << "Factorial\t" << num1 <<"\t\t\t\tproduct:" << result << endl;
}
|