Skip to content

Commit

Permalink
Add expression Calc
Browse files Browse the repository at this point in the history
  • Loading branch information
ChestnutHeng committed Oct 15, 2015
1 parent 46eab24 commit 43adcf4
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ fours.o: fours.cpp
g++ -c -o fours.o fours.cpp
numsys.o: numsys.cpp
g++ -c -o numsys.o numsys.cpp
evaluate.o: evaluate.cpp
g++ -c -o evaluate.o evaluate.cpp

MathCalc.exe: main.o linar.o fours.o numsys.o
g++ -o MathCalc.exe main.o linar.o fours.o numsys.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

exec: MathCalc.exe
./MathCalc.exe
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
- [ ] 矩阵的秩
- [x] 进制转换
- [x] R1 转换为 R2
- [ ] 表达式计算
- [ ] 四则运算
- [ ] 乘方运算
- [x] 表达式计算
- [x] 四则运算
- [x] 乘方运算
- [x] 阶乘运算
- [ ] 对数函数
- [ ] 开方运算

Expand All @@ -34,5 +35,10 @@
> * Ver 2.0 重构了部分方法和文件结构
* ver 2.1 添加了完整的进制转换系统

> * Ver 3.0 添加了表达式计算功能





185 changes: 185 additions & 0 deletions evaluate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#include <iostream>
#include <cstdio>

#include "Stack.h"

#define N_OPTR 9

using namespace std;


const char pri[N_OPTR][N_OPTR] = {
'>','>','<','<','<','<','<','>','>',
'>','>','<','<','<','<','<','>','>',
'>','>','>','>','<','<','<','>','>',
'>','>','>','>','<','<','<','>','>',
'>','>','>','>','>','<','<','>','>',
'>','>','>','>','>','>',' ','>','>',
'<','<','<','<','<','<','<','=',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ',
'<','<','<','<','<','<','<',' ','=',
}; // +,-,*,/,^,!,(,),\0

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

int power(int a,int b){
int sum = 1;
while((b--) > 0)
sum *= a;
return sum;
}

char * readNumber(char *s,Stack <float> &num){
float new_num = 0;
new_num += (*s) - 48;
while(isdigit(*(++s))){
new_num = new_num * 10;
new_num += (*s - 48);
}
num.push(new_num);
return s;
}

float calcu(char op,float p){
int new_p = (int) p;
float sum = 1;
switch(op){
case '!':
while(new_p != 0){
sum *= new_p--;
}
break;
}
return sum;
}

float calcu(float p1,char op,float p2){
switch(op){
case '+':
return p1 + p2;
case '-':
return p1 - p2;
case '*':
return p1 * p2;
case '/':
return p1 / p2;
case '^':
return power(p1,p2);
}
return 0;
}

char orderBetween(char s1,char s2){
int raw,col;
switch(s1){
case '+':
raw = 0;
break;
case '-':
raw = 1;
break;
case '*':
raw = 2;
break;
case '/':
raw = 3;
break;
case '^':
raw = 4;
break;
case '!':
raw = 5;
break;
case '(':
raw = 6;
break;
case ')':
raw = 7;
break;
case '\0':
raw = 8;
break;
}
switch(s2){
case '+':
col = 0;
break;
case '-':
col = 1;
break;
case '*':
col = 2;
break;
case '/':
col = 3;
break;
case '^':
col = 4;
break;
case '!':
col = 5;
break;
case '(':
col = 6;
break;
case ')':
col = 7;
break;
case '\0':
col = 8;
break;
}
return pri[raw][col];
}

float evaluate(char *S){
Stack <float> opnd;
Stack <char> optr;
optr.push('\0');
while(!optr.empty()){
if(isdigit(*S)){
S = readNumber(S,opnd);
}else{
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));
}
}
break;
case '<':
optr.push(*S);
S++;
break;
case '=':
optr.pop();
S++;
break;
}
}

}
return opnd.pop();
}

void expression_calc(){
while(true){
char exp[100];
char *pt = exp;
cout << ">>>Input your expression:";
scanf("%s",exp);
if (exp[0] == 'q') return;
cout << "Answer: " << evaluate(exp) << endl;
}
}
3 changes: 2 additions & 1 deletion linar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void Box::inputbox()

void Box::printbox()
{
cout << ">>>Display your Box:" << endl <<endl;
cout << "Display your Box:" << endl <<endl;
for (int i = 0; i < this->m; ++i)
{
for(int j = 0; j < this->n; ++j)
Expand Down Expand Up @@ -192,6 +192,7 @@ void Linar()
for(int i = 0; i < level - 1; ++i)
box2 = box2 * box1;
box2.printbox();
continue;
}
Box box2 (createbox());
if(op_str == "+")
Expand Down
11 changes: 6 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
#include <string>

#include "main.h"

#include "Stack.h"

using namespace std;

// double fourans(double a, double b,string str);
// int stringconvert(string str);

int main()
{
string str;

while(true)
{
cout << ">>>Input a command:(Linar,Numsys,q):";
cout << ">>>Input a command:(Calc,Linar,Numsys,q):";
cin >> str ;
if(str == "q") {
break;
Expand All @@ -25,9 +24,11 @@ int main()
else if((str == "numsys") || (str == "Numsys")) {
Numsys();
}
else if(str == "Calc" || str == "calc"){
expression_calc();
}
else {cout << "Error input." << endl;}
}
return 0;
}


2 changes: 1 addition & 1 deletion main.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef MAIN_H
#define MAIN_H


void Linar();
void Numsys();
void expression_calc();

#endif
6 changes: 3 additions & 3 deletions numsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ void Numsys()

do
{
cout << "Input number and R1:" ;
cout << ">>>Input number and R1:" ;
cin >> former;
if(former == "q") break;
cin >> r1;
if (r1 == 0) {
cout << "R1 shouldn't be 0." << endl;
cout << "Error:R1 shouldn't be 0." << endl;
continue;
}
cout << "To R2:";
cin >> r2;
if (r2 == 0) {
cout << "R2 shouldn't be 0." << endl;
cout << "Error:R2 shouldn't be 0." << endl;
continue;
}
else if(r2 == 10){
Expand Down

0 comments on commit 43adcf4

Please sign in to comment.