简单的加减乘除
只能纯粹的一次加或减或乘或除
先确定要进行什么运算,然后再输入两个数,最后得出结果。
这个太简单就不多说了,应该人人都能看懂。
代码
#include <stdio.h>
int main(){
char operator;
double firstNumber,secondNumber;
printf("输入操作符(+,-,*,/)\n");
scanf("%c",&operator);
if(operator == '+' || operator == '-' || operator == '*' || operator == '/'){
printf("输入两个数字:\n");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf\n",firstNumber,secondNumber,firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf\n",firstNumber,secondNumber,firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf\n",firstNumber,secondNumber,firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf\n",firstNumber,secondNumber,firstNumber/secondNumber);
break;
default:
printf("Error! operator is not correct\n");
break;
}
}
else{
printf("Error! operator is not correct\n");
}
}
进阶的计算器
有小括号的引入,可以进行多项式运算。
例如:12*(3+4)、42/7+4 等等。
分析
定义两个(先进后出)栈:数据(字)栈和符号栈
- 遇到运算数直接输出
- 遇到左括号直接将其压栈底
- 遇到右括号表明已经扫描完毕,于是将栈顶的运算符依次出栈,直至遇到左括号,然后把左括号弹出(不输出)注:右括号不入栈
- 遇到运算符则与栈顶符号运算级对比,若运算级大于栈顶则入栈,否则都要先出栈,再把当前的运算符号压栈底
- 若表达式计算完成,则将栈中剩余运算符全部出栈
代码
#include <stdio.h>
#include <stdlib.h>
/*数据栈*/
struct shuju
{
/* data */
int data[100];
int top;
};
/*符号栈*/
struct fuhao
{
char symbol[100];
int top;
};
void InitOperateNum(struct shuju *StackNum)
{
StackNum->top = -1;
}
void InitOperateSymbol(struct fuhao *StackSymbol)
{
StackSymbol->top = -1;
}
/*存入数据栈*/
void Inshuju(struct shuju *StackNum,int num)
{
StackNum->top ++;
StackNum->data[StackNum->top] = num;
}
/*存入符号栈*/
void Infuhao(struct fuhao *StackSymbol,char ch)
{
StackSymbol->top ++;
StackSymbol->symbol[StackSymbol->top] = ch;
}
/*读取数据栈*/
int Randshuju(struct shuju *StackNum)
{
return StackNum->data[StackNum->top];
}
/*读取符号栈*/
char Randfuhao(struct fuhao *StackSymbol)
{
return StackSymbol->symbol[StackSymbol->top];
}
/*从数据栈读取数据*/
int Putshuju(struct shuju *StackNum)
{
int x;
x = StackNum->data[StackNum->top];
StackNum->top --;
return x;
}
/*从符号栈取出符号*/
char Putfuhao(struct fuhao *StackSymbol)
{
char c;
c = StackSymbol->symbol[StackSymbol->top];
StackSymbol->top --;
return c;
}
/*符号优先级判断*/
int judge(char ch){
if(ch == '('){
return 1;
}
if(ch == '+' || ch == '-'){
return 2;
}
else if(ch == '*' || ch == '/'){
return 3;
}
else if(ch == ')'){
return 4;
}
return 0;
}
/*四则运算*/
int Math(int v1, int v2, char c)
{
int sum;
switch(c){
case '+':{
sum = v1 + v2;
break;
}
case '-':{
sum = v1 - v2;
break;
}
case '*':{
sum = v1 * v2;
break;
}
case '/':{
sum = v1 / v2;
break;
}
}
return sum;
}
int main()
{
struct shuju data;
struct fuhao symbol;
InitOperateNum(&data);
InitOperateSymbol(&symbol);
int i, t, sum, v1, v2;
char c;
i = t = sum = 0;
char v[100] = {0};
char *str = (char *)malloc(sizeof(char)*200);
while((c = getchar()) != '\n'){
str[i] = c;
i ++;
}
str[i] = '\0';
for(i = 0; str[i] != '\0'; i ++){
if(i == 0 && str[i] == '-'){
v[t++] = str[i];
}
else if(str[i] == '(' && str[i+1] == '-'){
i ++;
v[t++] = str[i++];
while(str[i] >= '0' && str[i] <= '9'){
v[t] = str[i];
t ++;
i ++;
}
Inshuju(&data, atoi(v));
while(t > 0){
v[t] = 0;
t --;
}
if(str[i] != ')'){
i --;
Infuhao(&symbol, '(');
}
}
else if(str[i] >= '0' && str[i] <= '9'){
while(str[i] >= '0' && str[i] <= '9'){
v[t] = str[i];
t ++;
i ++;
}
Inshuju(&data, atoi(v));
while(t > 0){
v[t] = 0;
t --;
}
i --;
}
else{
if(symbol.top == -1){//如果符号栈没有元素,直接把符号放入符号栈
Infuhao(&symbol, str[i]);
}
else if(judge(str[i]) == 1){//如果此符号是'(',直接放入符号栈
Infuhao(&symbol, str[i]);
}
else if(judge(str[i]) == 2){//如果此符号是'+'或'-',判断与栈栈顶符号是优先级
if(judge(Randfuhao(&symbol)) == 1){//如果栈顶符号是'(',放入符号栈
Infuhao(&symbol, str[i]);
}
else if(judge(Randfuhao(&symbol)) == 2){//如果栈顶符号是'-'或'+',则出栈运算
while(symbol.top >= 0 && data.top >=1){//循环出栈
v2 = Putshuju(&data);
v1 = Putshuju(&data);
sum = Math(v1, v2,Putfuhao(&symbol));
Inshuju(&data, sum);//将运算结果压入数据栈
}
Infuhao(&symbol, str[i]);//新符号进栈
}
else if(judge(Randfuhao(&symbol)) == 3){//如果栈顶符号是'*'或'/',则进符号栈
while(symbol.top >= 0 && data.top >=1){//循环出栈
v2 = Putshuju(&data);
v1 = Putshuju(&data);
sum = Math(v1, v2, Putfuhao(&symbol));
Inshuju(&data, sum);//将运算结果压入数据栈
}
Infuhao(&symbol, str[i]);//新符号入栈
}
/*栈顶符号不可能为')',故不做判断*/
}
else if(judge(str[i]) == 3){//如果此符号是'*'或'/',判断与栈栈顶符号是优先级
if(judge(Randfuhao(&symbol)) == 1){//如果栈顶符号是'(',放入符号栈
Infuhao(&symbol, str[i]);
}
else if(judge(Randfuhao(&symbol)) == 2){//如果栈顶符号是'+'或'-',
Infuhao(&symbol, str[i]);//新符号进栈
}
else if(judge(Randfuhao(&symbol)) == 3){
while(symbol.top >= 0 && data.top >= 1){//循环出栈
v2 = Putshuju(&data);
v1 = Putshuju(&data);
sum = Math(v1, v2, Putfuhao(&symbol));
Inshuju(&data, sum);//将运算结果压入数据栈
}
Infuhao(&symbol,str[i]);//新符号进栈
}
}
else if(judge(str[i]) == 4){//如果遇到')',则出栈运算直到遇到'('
do{//循环出栈直到遇到'('
v2 = Putshuju(&data);
v1 = Putshuju(&data);
sum = Math(v1, v2, Putfuhao(&symbol));
Inshuju(&data, sum);//将数据结果压入数据栈
}while (judge(Randfuhao(&symbol)) != 1);
Putfuhao(&symbol);//括号内运算结束后使'('出栈
}
}
}
//free(str);//释放内存空间
while (symbol.top != -1){
v2 = Putshuju(&data);
v1 = Putshuju(&data);
sum = Math(v1, v2, Putfuhao(&symbol));
Inshuju(&data, sum);
}
printf("%s = %d\n",str, data.data[0]);
free(str);//释放内存空间
return 0;
}