/* 函数功能:求运算符的优先级 */ int priority(char op) { switch(op) { case '#':return 1; case '(':return 0; case '+': case '-':return 1; case '*': case '/':return 2; default: return 1; } } 判断该函数是否有问题
时间: 2024-02-14 09:21:32 浏览: 24
该函数看起来没有明显的问题,它使用了一个switch语句来根据输入的操作符返回它的优先级。其中,'#'表示栈底元素,'('表示左括号,'+','-','*','/'分别表示加、减、乘、除操作符。默认情况下,函数返回1,这可能是因为default分支在函数中没有实际用途。但是,函数的正确性还需要根据它在程序中的使用情况来进一步确认。
相关问题
试设计一个算术四则运算表达式求值的简单计算器。 基本要求: 1、 操作数均为非负整数常数,操作符仅为+、-、*、/、、; 2、 编写 main 函数进行测试
好的,让我来回答您的问题。
以下是一个简单的算术四则运算表达式求值计算器的设计及测试代码:
```
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool is_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// 计算运算符的优先级
int priority(char op) {
switch(op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
case ')':
return 0;
default:
return -1; // 表示错误
}
}
// 计算两个数的结果
int caculate(int a, int b, char op) {
int result = 0;
switch(op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
return result;
}
// 转换表达式,将中缀表达式转换成后缀表达式
string trans(string exp) {
stack<char> s;
string result;
for (int i = 0; i < exp.size(); i++) {
char c = exp[i];
if (isdigit(c)) { // 如果是数字
result += c; // 直接输出
} else if (c == '(') { // 如果是左括号
s.push(c); // 将其入栈
} else if (c == ')') { // 如果是右括号
while (!s.empty() && s.top() != '(') {
result += s.top();
s.pop();
}
s.pop(); // 将左括号从栈中弹出
} else { // 如果是运算符
while (!s.empty() && priority(c) <= priority(s.top())) {
result += s.top(); // 输出优先级较高的运算符
s.pop(); // 将其从栈中弹出
}
s.push(c); // 最后将当前运算符入栈
}
}
while (!s.empty()) { // 最后将栈中的元素全部弹出
result += s.top();
s.pop();
}
return result;
}
// 计算后缀表达式的结果
int evaluate(string exp) {
stack<int> s;
for (int i = 0; i < exp.size(); i++) {
char c = exp[i];
if (isdigit(c)) { // 如果是数字
s.push(c - '0'); // 将其入栈
} else if (is_operator(c)) { // 如果是运算符
int b = s.top(); // 弹出栈顶元素作为第二个运算数
s.pop();
int a = s.top(); // 弹出栈顶元素作为第一个运算数
s.pop();
int result = caculate(a, b, c); // 计算两个数的结果
s.push(result); // 将结果入栈
}
}
return s.top(); // 最终栈中只剩下一个元素,即为最终的结果
}
int main() {
string expression = "1 + 2 * (4 - 3) / 2";
string postfix_expression = trans(expression); // 将中缀表达式转换成后缀表达式
int result = evaluate(postfix_expression); // 计算后缀表达式的结果
cout << expression << " = " << result;
return 0;
}
```
这是一个比较基础的计算器,只能处理最简单的四则运算表达式,并且不支持负数和小数。但是它足以满足本题的要求。
希望对您有所帮助,如果您有其他问题,欢迎随时提出。
用c语言写算术表达式求值。演示程序和源程序已给出。用户从键盘输入一个算术表达式,数字都是十进制整型(必须支持负数),运算符支持+-*/还有()。 编写源程序中的 double deal_exp (char *exp) 函数,对这个算术表达式求值,返回结果。要求用两个栈,一个储存数字,一个储存运算符
以下是实现算术表达式求值的C语言程序,其中使用了两个栈,一个存储数字,一个存储运算符。
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_STACK_SIZE 100
typedef struct {
int top;
int stack[MAX_STACK_SIZE];
} NumStack;
typedef struct {
int top;
char stack[MAX_STACK_SIZE];
} OpStack;
void num_push(NumStack *s, int num) {
if (s->top == MAX_STACK_SIZE - 1) {
printf("Error: number stack overflow.\n");
exit(1);
}
s->stack[++(s->top)] = num;
}
int num_pop(NumStack *s) {
if (s->top == -1) {
printf("Error: number stack underflow.\n");
exit(1);
}
return s->stack[(s->top)--];
}
void op_push(OpStack *s, char op) {
if (s->top == MAX_STACK_SIZE - 1) {
printf("Error: operator stack overflow.\n");
exit(1);
}
s->stack[++(s->top)] = op;
}
char op_pop(OpStack *s) {
if (s->top == -1) {
printf("Error: operator stack underflow.\n");
exit(1);
}
return s->stack[(s->top)--];
}
int priority(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
return 0;
default:
printf("Error: invalid operator '%c'.\n", op);
exit(1);
}
}
double deal_exp(char *exp) {
NumStack num_stack;
OpStack op_stack;
num_stack.top = -1;
op_stack.top = -1;
char *p = exp;
while (*p) {
if (isspace(*p)) {
p++;
} else if (isdigit(*p) || *p == '-') {
int num = 0;
int sign = 1;
if (*p == '-') {
sign = -1;
p++;
}
while (isdigit(*p)) {
num = num * 10 + (*p - '0');
p++;
}
num_push(&num_stack, sign * num);
} else if (*p == '+' || *p == '-' || *p == '*' || *p == '/') {
while (op_stack.top >= 0 && priority(op_stack.stack[op_stack.top]) >= priority(*p)) {
char op = op_pop(&op_stack);
int num2 = num_pop(&num_stack);
int num1 = num_pop(&num_stack);
switch (op) {
case '+':
num_push(&num_stack, num1 + num2);
break;
case '-':
num_push(&num_stack, num1 - num2);
break;
case '*':
num_push(&num_stack, num1 * num2);
break;
case '/':
num_push(&num_stack, num1 / num2);
break;
}
}
op_push(&op_stack, *p);
p++;
} else if (*p == '(') {
op_push(&op_stack, *p);
p++;
} else if (*p == ')') {
while (op_stack.top >= 0 && op_stack.stack[op_stack.top] != '(') {
char op = op_pop(&op_stack);
int num2 = num_pop(&num_stack);
int num1 = num_pop(&num_stack);
switch (op) {
case '+':
num_push(&num_stack, num1 + num2);
break;
case '-':
num_push(&num_stack, num1 - num2);
break;
case '*':
num_push(&num_stack, num1 * num2);
break;
case '/':
num_push(&num_stack, num1 / num2);
break;
}
}
if (op_stack.top == -1) {
printf("Error: unmatched ')'.\n");
exit(1);
}
op_pop(&op_stack);
p++;
} else {
printf("Error: invalid character '%c'.\n", *p);
exit(1);
}
}
while (op_stack.top >= 0) {
char op = op_pop(&op_stack);
int num2 = num_pop(&num_stack);
int num1 = num_pop(&num_stack);
switch (op) {
case '+':
num_push(&num_stack, num1 + num2);
break;
case '-':
num_push(&num_stack, num1 - num2);
break;
case '*':
num_push(&num_stack, num1 * num2);
break;
case '/':
num_push(&num_stack, num1 / num2);
break;
}
}
if (num_stack.top != 0) {
printf("Error: invalid expression.\n");
exit(1);
}
return (double) num_stack.stack[0];
}
int main() {
char exp[MAX_STACK_SIZE];
printf("请输入算术表达式:\n");
fgets(exp, MAX_STACK_SIZE, stdin);
printf("结果是:%.2f\n", deal_exp(exp));
return 0;
}
```
在该程序中,`deal_exp` 函数接受一个算术表达式字符串,使用两个栈对其进行求值,并返回结果。其中 `NumStack` 和 `OpStack` 分别表示数字栈和运算符栈。`num_push` 和 `num_pop` 函数分别表示向数字栈中压入数字和弹出数字。`op_push` 和 `op_pop` 函数分别表示向运算符栈中压入运算符和弹出运算符。`priority` 函数接受一个运算符,返回其优先级。在 `deal_exp` 函数中,通过遍历整个算术表达式字符串,依次处理每个字符,如果遇到数字,则将其转换成整型并压入数字栈中;如果遇到运算符,则将其与运算符栈顶的运算符比较优先级,如果栈顶运算符的优先级不低于当前运算符,则将栈顶运算符弹出,并从数字栈中弹出两个数字进行计算,将计算结果压入数字栈中,然后将当前运算符压入运算符栈中;如果遇到左括号,则将其压入运算符栈中;如果遇到右括号,则将运算符栈顶的运算符弹出,并从数字栈中弹出两个数字进行计算,将计算结果压入数字栈中,直到遇到左括号为止;如果遇到其他字符,则提示输入有误。最后,如果数字栈中只剩下一个数字,则返回该数字,否则提示表达式有误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)