如何用计算机做四则混合运算,C++实现四则混合运算计算器

本文实例为大家分享了C++实现四则混合运算的计算器,供大家参考,具体内容如下

计算器是带括号的四则运算实际上利用的是栈后进先出的原则

425da3bfee9db2984d667e9db017d0a3.png

42f545a21cafcb743d95985f0c8e189c.png

转换思想:

6dc123c1045e8b1305bfec89f733b978.png

#define _CRT_SECURE_NO_WARNINGS 1

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

#define maxsize 1024

typedef struct

{

float data[maxsize];

int top;

}Stack1;

void Initstack1(Stack1 *S)

{

S = (Stack1*)malloc(sizeof(Stack1));

S->top = -1;

}

int Push1(Stack1 *S, float ch)

{

if (S->top == maxsize - 1)

return 0;

else

{

S->top++;

S->data[S->top] = ch;

return 1;

}

}//入栈Push

int Pop1(Stack1 *S, float ch)

{

if (S->top == -1)

printf("栈上溢出!

");

else

ch = S->data[S->top];

S->top--;

return 1;

}//出栈

typedef struct

{

char data[maxsize];

int top;

}Stack2;

void Initstack2(Stack2 *S)

{

S = (Stack2*)malloc(sizeof(Stack2));

S->top = -1;

}

int Push2(Stack2 *S, char ch)

{

if (S->top == maxsize - 1)

return 0;

else

{

S->top++;

S->data[S->top] = ch;

return 1;

}

}//入栈Push

int Pop2(Stack2 *S, char ch)

{

if (S->top == -1)

printf("栈上溢出!

");

else

ch = S->data[S->top];

S->top--;

return 1;

}//出栈

int Comop(char ch) //判断是否是运算符

{

switch (ch)

{

case"+":

case"-":

case"*":

case"/":

case"(":

case")":

case"#":

return 1;

default:

return 0;

}

}//判断ch是否为运算符

char Prior[7][7] =

{ // 运算符优先级表

// "+" "-" "*" "/" "(" ")" "#"

/*"+"*/{ ">", ">", "", ">" },

/*"-"*/{ ">", ">", "", ">" },

/*"*"*/{ ">", ">", ">", ">", "", ">" },

/*"/"*/{ ">", ">", ">", ">", "", ">" },

/*"("*/{ "

/*")"*/{ ">", ">", ">", ">", " ", ">", ">" },

/*"#"*/{ " ", "=" },

};

int Opid(char op1)

{

switch (op1)

{

case"+":return 0;

case"-":return 1;

case"*":return 2;

case"/":return 3;

case"(":return 4;

case")":return 5;

case"#":return 6;

default:return -123456;

}

}

char Precede(char op1, char op2) //优先级比较

{

int a, b;

a = Opid(op1); b = Opid(op2);

return(Prior[a][b]);

}

float Operation(float a, char op, float b)

{

switch (op)

{

case "+":

return b + a;

case "-":

return b - a;

case "*":

return b * a;

case "/":

return b / a;

default:

return -123456;

}

}

void CreateExpression(char *exp)

{

if (exp == NULL)

{

exit(1);

}

scanf("%s", exp);

}

void TransmitExpression(char *exp, char postexp[]) //中缀表达式转换后缀表达式

{

Stack2 FZ;

Initstack2(&FZ);

int i = 0;

char x;

FZ.top = -1;

Push2(&FZ, "#");

FZ.data[FZ.top] = "#";

while (*exp != " ")

{

if (!Comop(*exp))

{

while (*exp >= "0"&&*exp <= "9")//读取一个数字串

{

postexp[i++] = *exp;

exp++;

}

postexp[i++] = "#";

}

else

switch (Precede(FZ.data[FZ.top], *exp))

{

case"

Push2(&FZ, *exp);

exp++;

break;

case"=":

x = FZ.data[FZ.top];

Pop2(&FZ, x);

exp++;

break;

case">":

postexp[i++] = FZ.data[FZ.top];

x = FZ.data[FZ.top];

Pop2(&FZ, x);

break;

}

}

while (FZ.data[FZ.top] != "#")

{

postexp[i++] = FZ.data[FZ.top];

x = FZ.data[FZ.top];

Pop2(&FZ, x);

}

postexp[i] = " ";

}

float EvaluateExpression(char *postexp) //后缀表达式的计算

{

Stack1 SZ;

Initstack1(&SZ);

float a, b, d;

SZ.top = -1;

while (*postexp != " ")

{

switch (*postexp)

{

case"+":

case"-":

case"*":

case"/":

a = SZ.data[SZ.top];

Pop1(&SZ, a);

b = SZ.data[SZ.top];

Pop1(&SZ, b);

Push1(&SZ, Operation(a, *postexp, b));

break;

default:

d = 0;

while (*postexp >= "0"&&*postexp <= "9")

{

d = 10 * d + *postexp - "0";

postexp++;

}

Push1(&SZ, d);

SZ.data[SZ.top] = d;

break;

}

postexp++;

}

return(SZ.data[SZ.top]);

}

int Error(char *exp) //错误表达式判断

{

int i = 0;

while (exp[i] != " ")

{

if (

((exp[i] == "+" || exp[i] == "-" || exp[i] == "*" || exp[i] == "/") && (exp[i + 1] == ")"))

|| ((exp[i] == "+" || exp[i] == "-" || exp[i] == "*" || exp[i] == "/") && (exp[i - 1] == "("))

|| (exp[i] == ")"&&exp[i + 1] == "(")

|| (exp[i] == "("&&exp[i + 1] == ")")

|| ((exp[i] == ")") && exp[i + 1] >= "0"&&exp[i + 1] <= "9")

|| (exp[i] >= "0"&&exp[i] <= "9"&&exp[i + 1] == "(")

|| (exp[0] == "+" || exp[0] == "-" || exp[0] == "*" || exp[0] == "/" || exp[0] == ")")

|| ((exp[i] == "+" || exp[i] == "-" || exp[i] == "*" || exp[i] == "/") && (exp[i + 1] == "+" || exp[i + 1] == "-" || exp[i + 1] == "*" || exp[i + 1] == "/"))

|| (exp[i] == "/"&&exp[i + 1] == "0")

)

return 1;

else

if (exp[i] == " ")

return 0;

i++;

}

return 0;

}

void menu()

{

printf("--------------------------------------------

");

printf("| 计算器 |

");

printf("| ---------------- |

");

printf("| | | |

");

printf("| ---------------- |

");

printf("| 1 2 3 + |

");

printf("| 4 5 6 - |

");

printf("| 7 8 9 * |

");

printf("| 0 ( ) / |

");

printf("--------------------------------------------

");

printf(" 请输入你要进行的操作:

");

printf(" a表达式求值 b清空 c退出

");

}

void clear()

{

system("cls");

menu();

}

void Quit()

{

system("cls");

exit(1);

}

void main()

{

char c;

char exp[maxsize];

char postexp[maxsize] = { 0 };

menu();

while (1)

{

scanf("%c", &c);

switch (c)

{

case "a":

sr :

printf("请输入要计算的表达式:

");

CreateExpression(exp);

if (!Error(exp))

{

TransmitExpression(exp, postexp);

printf("后缀表达式为:%s

", postexp);

printf("表达式结果为:%s=", exp);

printf("%g

", EvaluateExpression(postexp));

break;

}

else

if (Error(exp))

{

printf("您输入的表达式有误!

");

goto sr;//goto语句在循环体里进行跳转

}

case"b":

clear();

break;

case"c":

Quit();

break;

}

}

system("pause");

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持云海天教程。

原文链接:https://blog.csdn.net/persistence_s/article/details/70983787

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值