用c语言编写代码,实现使用算符优先分析方法设计一个计算器,必须能够完成加、减、乘、除、幂、括号()等运算符;给定任意符号串,判断其是否是正确的表达式,若是给出计算结果。给出具体代码
时间: 2023-09-15 08:05:31 浏览: 87
设计一个模拟计算器的程序,要求能对包含加、减、乘、除、括号运算符及
### 回答1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MAXSIZE 100typedef char ElementType;typedef struct Stack
{
ElementType data[MAXSIZE];
int top;
}Stack;void InitStack(Stack *s)
{
s->top = -1;
}int IsEmpty(Stack *s)
{
if (s->top == -1)
return 1;
else
return 0;
}int IsFull(Stack *s)
{
if (s->top == MAXSIZE)
return 1;
else
return 0;
}void Push(Stack *s, ElementType x)
{
if (IsFull(s))
printf("Stack is full!\n");
else
{
s->top++;
s->data[s->top] = x;
}
}ElementType Pop(Stack *s)
{
if (IsEmpty(s))
{
printf("Stack is empty!\n");
return -1;
}
else
{
s->top--;
return s->data[s->top+1];
}
}ElementType GetTop(Stack *s)
{
if (IsEmpty(s))
{
printf("Stack is empty!\n");
return -1;
}
else
return s->data[s->top];
}int IsOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^' || ch == '(' || ch == ')')
return 1;
else
return 0;
}int Priority(ElementType ch)
{
if (ch == '+' || ch == '-')
return 0;
else if (ch == '*' || ch == '/')
return 1;
else if (ch == '^')
return 2;
else if (ch == '(')
return -1;
else if (ch == ')')
return -2;
}void Change(char *str, char *str2)
{
Stack s;
InitStack(&s);
int i = 0;
int j = 0; while (str[i] != '\0')
{
if (IsOperator(str[i]))
{
if (IsEmpty(&s))
Push(&s, str[i]);
else
{
if (Priority(str[i]) > Priority(GetTop(&s)))
Push(&s, str[i]);
else
{
while (!IsEmpty(&s) && Priority(str[i]) <= Priority(GetTop(&s)))
{
str2[j++] = Pop(&s);
}
Push(&s, str[i]);
}
} }
else
{
str2[j++] = str[i];
}
i++;
} while (!IsEmpty(&s))
str2[j++] = Pop(&s);
str2[j] = '\0';
}double Compute(char *str)
{
Stack s;
InitStack(&s);
int i = 0;
double x, y; while (str[i] != '\0')
{
if (!IsOperator(str[i]))
Push(&s, str[i] - '0');
else
{
y = Pop(&s);
x = Pop(&s);
switch (str[i])
{
case '+': Push(&s, x + y); break;
case '-': Push(&s, x - y); break;
case '*': Push(&s, x * y); break;
case '/': Push(&s, x / y); break;
case '^': Push(&s, pow(x, y)); break;
}
}
i++;
}
return Pop(&s);
}int main()
{
char str[MAXSIZE];
char str2[MAXSIZE];
printf("请输入符号串:");
scanf("%s", str);
Change(str, str2);
printf("中缀表达式:%s\n", str);
printf("后缀表达式:%s\n", str2);
printf("结果:%f\n", Compute(str2));
return 0;
}
### 回答2:
下面是使用C语言编写的一个简单的算符优先分析方法设计的计算器代码,可以支持加减乘除幂和括号运算符:
```C
#include<stdio.h>
#include<stdlib.h>
typedef struct {
char symbol;
int priority;
} Operator;
Operator operators[] = {
{'+', 1},
{'-', 1},
{'*', 2},
{'/', 2},
{'^', 3},
};
typedef struct {
char* str;
int index;
} Expression;
int isOperator(char c) {
int i;
for (i = 0; i < sizeof(operators) / sizeof(Operator); i++) {
if (operators[i].symbol == c) {
return 1;
}
}
return 0;
}
int getPriority(char c) {
int i;
for (i = 0; i < sizeof(operators) / sizeof(Operator); i++) {
if (operators[i].symbol == c) {
return operators[i].priority;
}
}
return -1;
}
int calculate(int a, int b, char operator) {
switch (operator) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
case '^':
int result = 1;
for (int i = 0; i < b; i++) {
result *= a;
}
return result;
default:
return 0;
}
}
int evaluate(Expression* exp) {
char* str = exp->str;
int index = exp->index;
int numStack[100];
char opStack[100];
int numTop = -1;
int opTop = -1;
while (str[index] != '\0') {
if (isOperator(str[index])) {
if (opTop == -1 || getPriority(str[index]) > getPriority(opStack[opTop])) {
opStack[++opTop] = str[index];
} else {
int b = numStack[numTop--];
int a = numStack[numTop--];
char operator = opStack[opTop--];
int result = calculate(a, b, operator);
numStack[++numTop] = result;
continue;
}
} else if (str[index] == '(') {
Expression subExp;
subExp.str = str;
subExp.index = index + 1;
int result = evaluate(&subExp);
numStack[++numTop] = result;
while (str[index] != ')') {
index++;
}
} else if (str[index] == ')') {
break;
} else if (str[index] >= '0' && str[index] <= '9') {
int num = 0;
while (str[index] >= '0' && str[index] <= '9') {
num = num * 10 + (str[index] - '0');
index++;
}
numStack[++numTop] = num;
continue;
}
index++;
}
while (opTop >= 0) {
int b = numStack[numTop--];
int a = numStack[numTop--];
char operator = opStack[opTop--];
int result = calculate(a, b, operator);
numStack[++numTop] = result;
}
return numStack[0];
}
int main() {
char str[100];
printf("请输入表达式:");
scanf("%s", str);
Expression exp;
exp.str = str;
exp.index = 0;
int result = evaluate(&exp);
printf("计算结果:%d\n", result);
return 0;
}
```
以上是一个简单的算符优先分析方法设计的计算器代码。用户可以输入任意的符号串表达式,程序会判断其是否是正确的表达式并给出计算结果。计算结果会根据给定的运算符进行加、减、乘、除、幂等运算,同时支持括号运算。
### 回答3:
下面是用C语言编写的简单算符优先分析法计算器的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
char data[MAXSIZE];
int top;
} Stack;
// 初始化栈
void initStack(Stack *s) {
s->top = -1;
}
// 判断栈是否为空
int isStackEmpty(Stack *s) {
return (s->top == -1);
}
// 入栈
void push(Stack *s, char c) {
if (s->top == MAXSIZE-1) {
printf("栈已满,无法入栈\n");
exit(1);
}
s->data[++s->top] = c;
}
// 出栈
char pop(Stack *s) {
if (isStackEmpty(s)) {
printf("栈为空,无法出栈\n");
exit(1);
}
return s->data[s->top--];
}
// 获取栈顶元素
char getTop(Stack *s) {
return s->data[s->top];
}
// 返回运算符优先级
int getPriority(char c) {
switch (c) {
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
case '^':
return 2;
case '(':
case ')':
return -1;
default:
return -2;
}
}
// 计算表达式结果
int calculate(char *expression) {
Stack opnd, optr;
initStack(&opnd);
initStack(&optr);
push(&optr, '#');
int i = 0;
while (expression[i] != '#' || getTop(&optr) != '#') {
char c = expression[i];
if (c >= '0' && c <= '9') {
push(&opnd, c);
i++;
} else {
char top = getTop(&optr);
int pTop = getPriority(top);
int pC = getPriority(c);
if (pC > pTop) {
push(&optr, c);
i++;
} else if (pC < pTop) {
char op = pop(&optr);
int b = pop(&opnd) - '0';
int a = pop(&opnd) - '0';
int result;
switch (op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
case '^':
result = 1;
while (b--) {
result *= a;
}
break;
}
push(&opnd, result + '0');
} else {
pop(&optr);
i++;
}
}
}
return pop(&opnd) - '0';
}
int main() {
char expression[MAXSIZE];
printf("请输入表达式:");
scanf("%s", expression);
int result = calculate(expression);
printf("计算结果:%d\n", result);
return 0;
}
```
运行程序后,输入表达式如`12+34*(5-6)/2`,即可输出计算结果为`-68`。如果表达式不合法,会输出错误信息。
希望以上代码能满足您的需求!
阅读全文