用C语言实现四则运算

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

main.cpp
#include "arithmetic.h"
int main()
{
char tmp[100];
printf("please input a arithmetic express:\n");
scanf("%s", tmp);
Arithmetic ari = Arithmetic();
ari.midTopost(tmp);

printf("the result = [%f]\n", ari.calculate());

getchar();
}
arithmetic.h
#include
#include
#include
#include
#include "Stack.h"
class Data
{
public:
double num;
char op;
};
class Arithmetic
{
private:
DoubleStack dStack; //保存四则运算时,后缀表达式中的操作数
CharStack cStack; //保存中缀表达式转换成后缀表达式过程中的操作符
int isp(char op); //栈内优先级
int icp(char op); //栈外优先级
public:
Data post[100]; //保存后缀表达式
int curLen; //后缀表达式数组的实际长度
void midTopost(char *c); //把中缀表达式转换成后缀表达式
double calculate(); //进行四则运算
};
Stack.h
#include
#include
//栈节点数据结构
class DoubleNode
{
friend class DoubleStack;
private:
double value;
DoubleNode *next;
//构造函数
DoubleNode(double value, DoubleNode *p):value(value),next(p){}
};
//栈,用来保存采用后缀表达式进行四则运算时的操作数
class DoubleStack
{
private:
DoubleNode *top;
public:
DoubleStack():top(NULL){}
~DoubleStack();
void push(double value);
double pop();
bool isEmpty(){return top == NULL;}
double getTop();
};
//栈节点数据结构
class CharNode
{
friend class CharStack;
private:
char value;
CharNode *next;
//构造函数
CharNode(char value, CharNode *p):value(value),next(p){}
};
//栈,用来保存中缀表达式转换成后缀表达式时的运算符
class CharStack
{
private:
CharNode *top;
public:
CharStack():top(NULL){}
~CharStack();
void push(char value);
char pop();
bool isEmpty(){return top == NULL;}
char getTop();
void makeEmpty();
};
arithmetic.cpp
#include "arithmetic.h"
void Arithmetic::midTopost(char *c)
{
int i = 0;
int k;
int j;
char tmp;
char a[100];
char num[10];
//下面开始把字符串中的操作符(+-*/)放入后缀表达式
cStack.makeEmpty();
cStack.push('#');
//字符串最后都加一个#
strcat(c, "#");
k = 0;
while (c[k] != '\0')
{
if (c[k] == '+' || c[k] == '-' || c[k] == '*' || c[k] == '/' || c[k] == '(')
{
//如果优先级低,一直退栈,否则入栈
while (isp(cStack.getTop()) > icp(c[k]))
{
post.op = cStack.pop();
}
cStack.push(c[k]);
}
else if (c[k] == ')')
{
//一直退栈到'('
while (isp(cStack.getTop()) >= icp(')'))
{
tmp = cStack.pop();
if (tmp != '(')
{
post.op = tmp;
}
}

}
else if (c[k] == '#')
{
while (isp(cStack.getTop()) > icp('#'))
{
tmp = cStack.pop();
post.op = t

mp;
}
}
else if (c[k] >= '0' && c[k] <= '9')
{
//数字,继续往后获取字符,如果是数字,全部拼装到num中,然后转换成double类型
j = 0;
memset(num, 0, 10);
num[j] = c[k];
k = k + 1;
while (c[k] >= '0' && c[k] <= '9')
{
num[++j] = c[k];
k++;
}
post.num = atof(num);
post.op = '#';
i++;
k--;
}
else
{
//其他非法字符,不处理
}
k++;
}
curLen = i;
}
//栈内优先级,栈内优先级大于栈外优先级
//'('的栈内优先级应该比所有的操作符都小(除了#)
//')'的栈内优先级应该最大
//+-的优先级一致,*/的优先级一致
int Arithmetic::isp(char op)
{
int pri;
switch (op)
{
case '#' : pri = 0;
break;
case '(' : pri = 1;
break;
case '+' : pri = 3;
break;
case '-' : pri = 3;
break;
case '*' : pri = 5;
break;
case '/' : pri = 5;
break;
case ')' : pri = 10;
break;
}
return pri;
}
//栈外优先级
int Arithmetic::icp(char op)
{
int pri;
switch (op)
{
case '#' : pri = 0;
break;
case '(' : pri = 10;
break;
case '+' : pri = 2;
break;
case '-' : pri = 2;
break;
case '*' : pri = 4;
break;
case '/' : pri = 4;
break;
case ')' : pri = 1;
break;
}
return pri;
}
//根据后缀表达式进行四则运算
double Arithmetic::calculate()
{
int i;
double result = 0;
double left, right;
for (i = 0; i < curLen; i++)
{
//如果是数字就入栈
if (post.op == '#')
{
dStack.push(post.num);
}
else //如果是操作符,就取出两个数字进行运算
{
right = dStack.pop();
left = dStack.pop();
switch (post.op)
{
case '+' : dStack.push(left + right);
break;
case '-' : dStack.push(left - right);
break;
case '*' : dStack.push(left * right);
break;
case '/' : dStack.push(left / right);
break;
}
}
}
return dStack.pop();
}
Stack.cpp
#include "Stack.h"
DoubleStack::~DoubleStack()
{
DoubleNode *p;
while (NULL != top)
{
p = top;
top = top->next;
delete p;
}
}
void DoubleStack::push(double value)
{
top = new DoubleNode(value,top);
}
double DoubleStack::pop()
{
if (!isEmpty())
{
DoubleNode *p;
p = top;
top = top->next;
double nodeValue = p->value;
delete p;
return nodeValue;
}
}
double DoubleStack::getTop()
{
return top->value;
}
CharStack::~CharStack()
{
CharNode *p;
while (NULL != top)
{
p = top;
top = top->next;
delete p;
}
}
void CharStack::push(char value)
{
top = new CharNode(value,top);
}
char CharStack::pop()
{
if (!isEmpty())
{
CharNode *p;
p = top;
top = top->next;
char nodeValue = p->value;
delete p;
return nodeValue;
}
}
void CharStack::makeEmpty()
{
CharNode *

p;
while (NULL != top)
{
p = top;
top = top->next;
delete p;
}
}
char CharStack::getTop()
{
return top->value;
}

相关文档
最新文档