exit的应有

#include<stdio.h>
#include<stdlib.h>//应用exit必须要先声明
int text();
int main()
{
 printf("main  1\n");
 text(); //此条函数的exit(0)让程序终止了
 printf("main  2\n");
 return 0;
}
int text()
{
 printf("text  1\n");
 exit(0);
 printf("text  2\n");
 return 0;
}
以下是使用栈实现表达式求值的C代码: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_EXPR_LEN 100 typedef struct { long long data[MAX_EXPR_LEN]; int top; } Stack; void init(Stack *s) { s->top = -1; } int is_empty(Stack *s) { return s->top == -1; } int is_full(Stack *s) { return s->top == MAX_EXPR_LEN - 1; } void push(Stack *s, long long x) { if (is_full(s)) { printf("Error: Stack overflow\n"); exit(EXIT_FAILURE); } s->data[++(s->top)] = x; } long long pop(Stack *s) { if (is_empty(s)) { printf("Error: Stack underflow\n"); exit(EXIT_FAILURE); } return s->data[(s->top)--]; } long long peek(Stack *s) { if (is_empty(s)) { printf("Error: Stack underflow\n"); exit(EXIT_FAILURE); } return s->data[s->top]; } int precedence(char op) { switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; default: return 0; } } long long evaluate(char *expr) { Stack value_stack, op_stack; init(&value_stack); init(&op_stack); int i = 0; while (expr[i] != '\0') { if (isdigit(expr[i])) { long long num = 0; while (isdigit(expr[i])) { num = num * 10 + (expr[i] - '0'); i++; } push(&value_stack, num); } else if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') { while (!is_empty(&op_stack) && precedence(peek(&op_stack)) >= precedence(expr[i])) { long long rhs = pop(&value_stack); long long lhs = pop(&value_stack); char op = pop(&op_stack); switch (op) { case '+': push(&value_stack, lhs + rhs); break; case '-': push(&value_stack, lhs - rhs); break; case '*': push(&value_stack, lhs * rhs); break; case '/': if (rhs == 0) { printf("Error: Division by zero\n"); exit(EXIT_FAILURE); } push(&value_stack, lhs / rhs); break; } } push(&op_stack, expr[i]); i++; } else if (expr[i] == '(') { push(&op_stack, expr[i]); i++; } else if (expr[i] == ')') { while (peek(&op_stack) != '(') { long long rhs = pop(&value_stack); long long lhs = pop(&value_stack); char op = pop(&op_stack); switch (op) { case '+': push(&value_stack, lhs + rhs); break; case '-': push(&value_stack, lhs - rhs); break; case '*': push(&value_stack, lhs * rhs); break; case '/': if (rhs == 0) { printf("Error: Division by zero\n"); exit(EXIT_FAILURE); } push(&value_stack, lhs / rhs); break; } } if (is_empty(&op_stack) || pop(&op_stack) != '(') { printf("Error: Mismatched parentheses\n"); exit(EXIT_FAILURE); } i++; } else if (isspace(expr[i])) { i++; } else { printf("Error: Invalid character\n"); exit(EXIT_FAILURE); } } while (!is_empty(&op_stack)) { long long rhs = pop(&value_stack); long long lhs = pop(&value_stack); char op = pop(&op_stack); switch (op) { case '+': push(&value_stack, lhs + rhs); break; case '-': push(&value_stack, lhs - rhs); break; case '*': push(&value_stack, lhs * rhs); break; case '/': if (rhs == 0) { printf("Error: Division by zero\n"); exit(EXIT_FAILURE); } push(&value_stack, lhs / rhs); break; } } if (is_empty(&value_stack)) { printf("Error: Empty expression\n"); exit(EXIT_FAILURE); } long long result = pop(&value_stack); if (!is_empty(&value_stack)) { printf("Error: Malformed expression\n"); exit(EXIT_FAILURE); } return result; } int main() { char expr[MAX_EXPR_LEN]; printf("Enter an expression: "); if (fgets(expr, MAX_EXPR_LEN, stdin) == NULL) { printf("Error: Failed to read input\n"); return EXIT_FAILURE; } printf("Result: %lld\n", evaluate(expr)); return EXIT_SUCCESS; } ``` 使用方法: 1. 编译运行上述代码,输入一个中缀表达式,按回车键确认; 2. 如果输入的表达式合法,程序会输出表达式的计算结果; 3. 如果输入的表达式不合法,程序会输出错误提示并退出。 例如,输入`5*(10-3)/7`,程序会输出`Result: 2`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值