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
|
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
//todo: consider xor, log2, ln, log10
int indexx(bool geti = false, bool reverse = false)
{
static int i = 2;
if(geti && reverse)
{
if(i-1 < 0)
return 9;
else return i-1;
}
if(geti)
return i;
if(reverse){i--; if(i<0) i = 9; return i;}
i++; i%=10;
return i;
}
int main(int argc, char **argv)
{
string i;
double s[10]= {0};
double r;
bool done = false;
bool inverse = false;
do
{
cin >> i;
switch (i[0])
{
case 'h': case 'H':
case '?':
cout << "i inverse next\ns sin\nc cos\nt tan\np pi\ne e\n^ power\nd delete entry\nx 1/x\n| swap top 2\nv view all\n\n";
break;
case 's': case 'S':
if(inverse)
r = asin(s[indexx(1)]);
else
r = sin(s[indexx(1)]);
s[indexx()] = r;
printf("%1.15f\n",r);
inverse = false;
break;
case 'c': case 'C':
if(inverse)
r = acos(s[indexx(1)]);
else
r = cos(s[indexx(1)]);
s[indexx()] = r;
printf("%1.15f\n",r);
inverse = false;
break;
case 't': case 'T':
if(inverse)
r = atan(s[indexx(1)]);
else
r = tan(s[indexx(1)]);
s[indexx()] = r;
printf("%1.15f\n",r);
inverse = false;
break;
case 'i': case 'I':
inverse = !inverse;
break;
case 'p': case 'P':
r = 3.1415926535897932384626433832795;
s[indexx()] = r;
printf("%1.15f\n",r);
break;
case 'e': case 'E':
r = 2.7182818284590452353602874713527;
s[indexx()] = r;
printf("%1.15f\n",r);
break;
case '*':
r = s[indexx(1,1)] * s[indexx(1)];
s[indexx(0,1)] = r;
printf("%1.15f\n",r);
break;
case '/':
r = s[indexx(1,1)] / s[indexx(1)];
s[indexx(0,1)] = r;
printf("%1.15f\n",r);
break;
case '-': //detects whether its an operation or a negative number.
if(i.length() == 1)
{
r = s[indexx(1,1)] - s[indexx(1)];
s[indexx(0,1)] = r;
printf("%1.15f\n",r);
}
else
{
try
{
s[indexx()] = stod(i);
}
catch (const std::invalid_argument& ia)
{
done = true;
}
}
break;
case '+':
r = s[indexx(1,1)] + s[indexx(1)];
s[indexx(0,1)] = r;
printf("%1.15f\n",r);
break;
case '^':
if(inverse)
{
inverse = false;
r = pow(s[indexx(1,1)],1.0/s[indexx(1)]);
}
else
{
r = pow(s[indexx(1,1)],s[indexx(1)]);
}
s[indexx(0,1)] = r;
printf("%1.15f\n",r);
break;
case 'd': case 'D': //delete topmost stack value.
s[indexx(1)] = 0.0;
r = s[indexx(0,1)];
printf("%1.15f\n",r);
break;
case 'x': case 'X': // 1/x button
r = 1.0/s[indexx(1)];
s[indexx(1)] = r;
printf("%1.15f\n",r);
break;
case '|': // swap top 2 stack entries.
r = s[indexx(1)];
s[indexx(1)] = s[indexx(1,1)];
s[indexx(1)-1] = r;
printf("%1.15f\n", s[indexx(1,1)]);
printf("%1.15f\n", s[indexx(1)]);
break;
case 'v':
for(int dx = 0; dx < 10; dx++)
cout << s[dx]<<endl;
break;
default: //if its a number, proceed, else consider input to mean 'quit'
try
{
s[indexx()] = stod(i);
}
catch (const std::invalid_argument& ia)
{
done = true;
}
}
}
while(!done);
return 0;
}
|