Skip to content

Commit

Permalink
update Exp calc
Browse files Browse the repository at this point in the history
  • Loading branch information
ChestnutHeng committed Nov 25, 2015
1 parent 2763805 commit aef2322
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
make: exec

main.o: main.cpp main.h
g++ -c -o main.o main.cpp
g++ -c -o main.o main.cpp -g
linar.o: linar.cpp
g++ -c -o linar.o linar.cpp
g++ -c -o linar.o linar.cpp -g
fours.o: fours.cpp
g++ -c -o fours.o fours.cpp
g++ -c -o fours.o fours.cpp -g
numsys.o: numsys.cpp
g++ -c -o numsys.o numsys.cpp
g++ -c -o numsys.o numsys.cpp -g
evaluate.o: evaluate.cpp
g++ -c -o evaluate.o evaluate.cpp
g++ -c -o evaluate.o evaluate.cpp -g

MathCalc.exe: main.o linar.o fours.o numsys.o evaluate.o
g++ -o MathCalc.exe main.o linar.o fours.o numsys.o evaluate.o
g++ -o MathCalc.exe main.o linar.o fours.o numsys.o evaluate.o -g

exec: MathCalc.exe
./MathCalc.exe
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@

> * Ver 3.0 添加了表达式计算功能
* ver 3.1 在进制转换系统中添加了小数和负数的进制转换



* ver 3.2 在表达式计算中支持了小数和负数


71 changes: 54 additions & 17 deletions evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include "Stack.h"

#define N_OPTR 9
#define VERIFY(_STACK_) {\
if (_STACK_.empty()) {\
return false;\
}\
}

using namespace std;

Expand All @@ -21,13 +26,18 @@ const char pri[N_OPTR][N_OPTR] = {
}; // +,-,*,/,^,!,(,),\0

bool isdigit(char s){
if((s <='9' && s >= '0') ||s == '.')
if((s <='9' && s >= '0'))
return true;
else return false;
}

int power(int a,int b){
int sum = 1;
float power(int a,int b){
float sum = 1;
if(b < 0){
while((b++) < 0)
sum /= a;
return sum;
}
while((b--) > 0)
sum *= a;
return sum;
Expand All @@ -40,6 +50,14 @@ char * readNumber(char *s,Stack <float> &num){
new_num = new_num * 10;
new_num += (*s - 48);
}
if(*s == '.') {
int i = -1;
while(isdigit(*(++s))){
cout << "ff";
new_num += (*s - 48) * power(10,i);
i--;
}
}
num.push(new_num);
return s;
}
Expand Down Expand Up @@ -136,41 +154,55 @@ char orderBetween(char s1,char s2){
return pri[raw][col];
}

float evaluate(char *S){
bool evaluate(char *S, float *result){
Stack <float> opnd;
Stack <char> optr;
char *origin = S;
optr.push('\0');
while(!optr.empty()){
if(isdigit(*S)){
S = readNumber(S,opnd);
}else{
if (*S == '-') {
if (S == origin) {
opnd.push(0);
} else if (S[-1] == '(') {
opnd.push(0);
}
}
switch(orderBetween(optr.top(),*S)){
case '>':
{
char op = optr.pop();
if('!' == op){
float pOpnd = opnd.pop();
opnd.push(calcu(op,pOpnd));
}else{
float pOpnd2 = opnd.pop();
float pOpnd1 = opnd.pop();
opnd.push(calcu(pOpnd1,op,pOpnd2));
{
VERIFY(optr);
char op = optr.pop();
if('!' == op){
float pOpnd = opnd.pop();
VERIFY(opnd);
opnd.push(calcu(op,pOpnd));
}else{
VERIFY(opnd);
float pOpnd2 = opnd.pop();
VERIFY(opnd);
float pOpnd1 = opnd.pop();
opnd.push(calcu(pOpnd1,op,pOpnd2));
}
break;
}
}
break;
case '<':
optr.push(*S);
S++;
break;
case '=':
VERIFY(optr);
optr.pop();
S++;
break;
}
}

}
return opnd.pop();
*result = opnd.pop();
return true;
}

void expression_calc(){
Expand All @@ -180,6 +212,11 @@ void expression_calc(){
cout << ">>>Input your expression:";
scanf("%s",exp);
if (exp[0] == 'q') return;
cout << "Answer: " << evaluate(exp) << endl;
float output;
if (evaluate(exp, &output)) {
cout << "Answer: " << output << endl;
} else {
cout << "Syntax error." << endl;
}
}
}

0 comments on commit aef2322

Please sign in to comment.