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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include<stdlib.h>
#include<cmath>
#include <iostream>
using namespace std;
// my prototypes
char menu ();
double getOperand ();
char getOperator ();
bool numOperandsNeeded (char);
double plus (double, char, double);
double sub (double, char, double);
double mult (double, char, double);
double div (double, char, double);
double rem (double, char, double);
double exp (double, char, double);
double log (double, char);
int factorial (int n);
double log (double, char);
void display2 (double, char, double);
void display1 (double, char);
int main ()
{
double op1 =0;
char optor = 0;
double op2 =0;
double ans =0;
menu ();
op1 = getOperand ();
optor = getOperator ();
if((numOperandsNeeded (optor)) == 1)
{ op2 = getOperand ();};
/*switch (optor)
{
case '+':
ans = plus(op1, op2);
case '-':
min ();
case '*':
mult ();
case '/':
div ();
case '%':
rem ();
case '^':
exp ();
case 'L':
log ();
case '^':
exp ();
case '!':
factorial ();
}*/
system ("pause");
return EXIT_SUCCESS;
}
//functions
char menu()
{
cout <<"Welcome to simple calculator!" << endl;
}
double getOperand()
{
double op = 0;
cout <<"Please enter a number"<< endl;
cin >> op ;
if (op >= 10000 || op <= -10000)
{
do
{
cout <<"Your Number is out of Range" ;
cout << " Please Enter Another" << endl;
cin >> op;
}
while (op >= 10000 || op <= -10000);
}
return op;
}
char getOperator ()
{
char Optor = 0;
cout << "Select an operator: " << endl;
cout << " +, -, *, /, %, ^, !, L " << endl;
cout << "Or 'q' to quit" << endl;
cin >> Optor;
while ( Optor == 'q')
{
return 0;
}
if ( Optor == '+' || Optor == '-' || Optor == '*' || Optor == '/' || Optor == '%' || Optor == '^' || Optor == '!' || Optor == 'L')
{
return Optor;
}
else
{
do
{
cout << "The operator is not valid, please select another. " << endl;
cin >> Optor; break;
} while (Optor != '+' || Optor != '-' || Optor != '*' || Optor != '/' || Optor != '%' || Optor != '^' || Optor != '!' || Optor != 'L');
}
return Optor;
}
bool numOperandsNeeded (char Optor)
{
switch (Optor)
{
case '+':;
case '-':;
case '*':;
case '/':;
case '%':;
case '^': Optor = 1; break;
case '!':;
case 'L': Optor = 0; break;
}
return Optor;
}
double plus (double Op, char Optor, double Op2)
{
double ans = 0;
ans = Op + Op2;
return ans;
}
double sub (double Op, char Optor, double Op2)
{
double ans = 0;
ans = Op - Op2;
return ans;
}
double mult (double Op, char Optor, double Op2)
{
double ans = 0;
ans = Op * Op2;
return ans;
}
double div (double Op, char Optor, double Op2)
{
double ans = 0;
ans = Op / Op2;
return ans;
}
int rem (int Op, char Optor, int Op2)
{
int ans = 0;
ans = Op % Op2;
return ans;
}
double exp (double Op, char Optor, double Op2)
{
double ans = 0;
ans = pow( Op, Op2);
return ans;
}
int factorial(int Op)
{ int i=0,fact=1;
if (Op<=1)
{
return(1);
}
else
{
for(i=1;i<=Op;i++)
{
fact=fact*i;
}
return(fact);
}
}
double log (double Op, char Optor)
{
double ans = 0;
ans =(log (Op) / log (10));
return ans;
}
| |