用C语言设计一个程序,能够进行带括号和负数的四则运算。
时间: 2024-02-11 08:05:59 浏览: 72
用C语言编的四则运算系统
好的,这个任务可以分为两个部分:表达式解析和四则运算计算。首先,我们需要编写一个函数来解析输入的表达式,并将其转换为计算机可识别的形式,例如后缀表达式。下面是一个简单的表达式解析函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define STACK_SIZE 100
typedef struct {
int data[STACK_SIZE];
int top;
} stack;
void init_stack(stack *s) {
s->top = -1;
}
int is_empty(stack *s) {
return s->top == -1;
}
int is_full(stack *s) {
return s->top == STACK_SIZE - 1;
}
void push(stack *s, int value) {
if (is_full(s)) {
fprintf(stderr, "stack overflow\n");
exit(1);
}
s->data[++s->top] = value;
}
int pop(stack *s) {
if (is_empty(s)) {
fprintf(stderr, "stack underflow\n");
exit(1);
}
return s->data[s->top--];
}
int peek(stack *s) {
if (is_empty(s)) {
fprintf(stderr, "stack underflow\n");
exit(1);
}
return s->data[s->top];
}
int is_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
int precedence(char c) {
switch (c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
void infix_to_postfix(char *infix, char *postfix) {
stack s;
init_stack(&s);
int i = 0, j = 0;
while (infix[i] != '\0') {
if (isdigit(infix[i])) {
postfix[j++] = infix[i++];
while (isdigit(infix[i])) {
postfix[j++] = infix[i++];
}
postfix[j++] = ' ';
} else if (infix[i] == '(') {
push(&s, infix[i++]);
} else if (infix[i] == ')') {
while (peek(&s) != '(') {
postfix[j++] = pop(&s);
postfix[j++] = ' ';
}
pop(&s);
i++;
} else if (is_operator(infix[i])) {
while (!is_empty(&s) && is_operator(peek(&s)) && precedence(infix[i]) <= precedence(peek(&s))) {
postfix[j++] = pop(&s);
postfix[j++] = ' ';
}
push(&s, infix[i++]);
} else {
i++;
}
}
while (!is_empty(&s)) {
postfix[j++] = pop(&s);
postfix[j++] = ' ';
}
postfix[j] = '\0';
}
```
然后我们需要编写一个函数来计算后缀表达式。下面是一个简单的计算函数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define STACK_SIZE 100
typedef struct {
int data[STACK_SIZE];
int top;
} stack;
void init_stack(stack *s) {
s->top = -1;
}
int is_empty(stack *s) {
return s->top == -1;
}
int is_full(stack *s) {
return s->top == STACK_SIZE - 1;
}
void push(stack *s, int value) {
if (is_full(s)) {
fprintf(stderr, "stack overflow\n");
exit(1);
}
s->data[++s->top] = value;
}
int pop(stack *s) {
if (is_empty(s)) {
fprintf(stderr, "stack underflow\n");
exit(1);
}
return s->data[s->top--];
}
int calculate(char *postfix) {
stack s;
init_stack(&s);
int i = 0;
while (postfix[i] != '\0') {
if (isdigit(postfix[i])) {
int num = 0;
while (isdigit(postfix[i])) {
num = num * 10 + (postfix[i] - '0');
i++;
}
push(&s, num);
} else if (postfix[i] == '+') {
int b = pop(&s);
int a = pop(&s);
push(&s, a + b);
i++;
} else if (postfix[i] == '-') {
int b = pop(&s);
int a = pop(&s);
push(&s, a - b);
i++;
} else if (postfix[i] == '*') {
int b = pop(&s);
int a = pop(&s);
push(&s, a * b);
i++;
} else if (postfix[i] == '/') {
int b = pop(&s);
int a = pop(&s);
push(&s, a / b);
i++;
} else {
i++;
}
}
return pop(&s);
}
```
使用上述两个函数,我们可以完成带括号和负数的四则运算。下面是一个简单的测试程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX_EXPR_LEN 100
int main() {
char infix[MAX_EXPR_LEN];
char postfix[MAX_EXPR_LEN];
printf("请输入一个带括号和负数的四则运算表达式:");
fgets(infix, MAX_EXPR_LEN, stdin);
infix[strlen(infix) - 1] = '\0';
infix_to_postfix(infix, postfix);
printf("后缀表达式为:%s\n", postfix);
int result = calculate(postfix);
printf("计算结果为:%d\n", result);
return 0;
}
```
希望这个程序对你有帮助!
阅读全文