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 207 208
|
#include <iostream>
#include <cstdlib>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
void eatspaces(char* str);
double expr(char* str);
double exponent(char* str, int& index);
double term(char* str, int& index);
double number(char* str, int& index);
char* extract(char* str, int& index);
const int MAX = 80;
int main(void)
{
char buffer[MAX] = { 0 };
cout << endl
<< "Welcome to your friendly calculator."
<< endl
<<"Enter an expression, or an empty line to quit."
<< endl;
for(;;)
{
cin.getline(buffer, sizeof buffer);
eatspaces(buffer);
if(!buffer[0])
return 0;
cout << "\t= " << expr(buffer)
<< endl << endl;
}
}
void eatspaces(char* str)
{
int i = 0;
int j = 0;
while((*(str + i) = *(str + j++)) != '\0')
if (*(str + i) != ' ')
i++;
return;
}
double expr(char* str)
{
double value = 0.0;
int index = 0;
value = term(str, index);
for(;;)
{
switch(*(str + index++))
{
case '\0':
return value;
case '+':
value += term(str, index);
break;
case '-':
value -= term(str, index);
break;
default:
cout << endl
<< "Arrrgh!*#!! There's an error"
<< endl;
cout << str << endl;
for (; index > 1; index--)
cout << " ";
cout << "^" << endl;
exit(1);
}
}
}
double term(char* str, int& index)
{
double value = 0.0;
value = number(str, index);
if(*(str + index) == '^')
{
number(str, index);
}
while((*(str + index) == '*') || (*(str + index) == '/'))
{
if(*(str + index) == '*')
value *= number(str, ++index);
if (*(str + index) == '/')
value /= number(str, ++index);
}
return value;
}
double exponent(char* str, int& index, double exponentiate)
{
double value = 0.0;
int expo = 0;
int copyindex = index;
int subtractor = 1;
while(*(str + copyindex) != '^')
{
copyindex--;
}
copyindex++;
while(isdigit((*(str + copyindex) - subtractor)))
{
expo *= 10;
expo += (*(str + copyindex) - subtractor) - '0';
subtractor++;
}
cout << expo << endl;
return value;
}
double number(char* str, int& index)
{
double value = 0.0;
if (*(str + index) == '(')
{
char* psubstr = 0;
psubstr = extract(str, ++index);
value = expr(psubstr);
delete[] psubstr;
return value;
}
if(*(str + index) == '^')
{
double exponentiate = number(str, ++index);
double value = exponent(str, index, exponentiate);
return value;
}
while(isdigit(*(str + index)))
value = 10 * value + (*(str + index++) - '0');
if(*(str + index) != '.')
return value;
double factor = 1.0;
while(isdigit(*(str + (++index))))
{
factor *= .1;
value = value + (*(str + index) - '0');
}
return value;
}
char* extract(char* str, int& index)
{
char buffer[MAX];
char* pstr = 0;
int numL = 0;
int bufindex = index;
do
{
buffer[index - bufindex] = *(str + index);
switch(buffer[index - bufindex])
{
case ')':
if (numL == 0)
{
buffer[index - bufindex] = '\0';
index++;
pstr = new char[index - bufindex];
if(!pstr)
{
cout << "Memory allocation failed,"
<< " program terminated.";
exit(1);
}
strcpy_s(pstr, index-bufindex, buffer);
return pstr;
}
else
{
numL--;
break;
}
case '(':
numL++;
break;
}
} while(*(str + index++) != '\0');
cout << "Ran off the end of the expression, must be bad input."
<< endl;
exit(1);
return pstr;
}
|