c语言:简单算式

文章讲述了如何在C语言中,不使用if和switch语句,通过字符运算符判断来解析并计算简单的加法或减法算式,遇到非加减运算时输出错误提示。
摘要由CSDN通过智能技术生成

简单算式

任务描述

现有1个形如A+B或A-B的算式,要求你编程读入这个算式并输出它的结果。
X同学在2050级新生中C语言学的最好,他表示用if语句可以轻松地解决这道题,可是Y老师不允许他使用if语句和switch语句。X同学犯愁了,不知道该怎么办。
不使用if语句和switch语句,你能解决这个问题吗?

输入格式:
一行中给出一个算式,形如A+B或A-B,A、B为整数。运算符前后可能包含若干空格。

输出格式:
在一行中输出算式结果,如果算式不是加法和减法,输出:error!。

输入示例

1               +     2

输出示例

3

代码

#include <stdio.h>
int main() {
    char op; 
    int a,A, B, result;
    scanf("%d %c %d", &A, &op, &B);
    result = (op == '+') ? (A + B) : (op == '-') ? (A - B) : 0;
    a=(op == '+' || op == '-')?printf("%d",result):printf("error!");
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里为您提供一个用C语言编写的可以计算中缀算式的程序实现。 首先,您需要定义一个栈结构体来存储算式中的操作数和操作符。这个栈可以使用数组来实现。例如: ```c #define STACK_SIZE 100 typedef struct { double data[STACK_SIZE]; int top; } Stack; ``` 接下来,您可以编写一个函数来判断一个字符是否是操作符。例如: ```c int isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } ``` 然后,您需要实现一个函数来将中缀算式转换为后缀算式。这个函数可以使用栈和队列来实现。例如: ```c typedef struct { double data[STACK_SIZE]; int front; int rear; } Queue; void infixToPostfix(char* infix, char* postfix) { Stack opStack; opStack.top = -1; Queue outputQueue; outputQueue.front = 0; outputQueue.rear = -1; int i = 0; while(infix[i] != '\0') { if(isdigit(infix[i])) { // 如果当前字符是数字,则将其加入输出队列 double operand = 0; while(isdigit(infix[i])) { operand = operand * 10 + (infix[i] - '0'); i++; } if(infix[i] == '.') { i++; double fraction = 0.1; while(isdigit(infix[i])) { operand += fraction * (infix[i] - '0'); fraction /= 10; i++; } } outputQueue.rear++; outputQueue.data[outputQueue.rear] = operand; } else if(isOperator(infix[i])) { // 如果当前字符是操作符,则将其加入操作符栈,直到遇到优先级更低的操作符或左括号 while(opStack.top >= 0 && opStack.data[opStack.top] != '(' && ((infix[i] == '+' || infix[i] == '-') && (opStack.data[opStack.top] == '*' || opStack.data[opStack.top] == '/'))) { outputQueue.rear++; outputQueue.data[outputQueue.rear] = opStack.data[opStack.top]; opStack.top--; } opStack.top++; opStack.data[opStack.top] = infix[i]; i++; } else if(infix[i] == '(') { // 如果当前字符是左括号,则将其加入操作符栈 opStack.top++; opStack.data[opStack.top] = infix[i]; i++; } else if(infix[i] == ')') { // 如果当前字符是右括号,则将操作符栈中的操作符加入输出队列,直到遇到左括号 while(opStack.top >= 0 && opStack.data[opStack.top] != '(') { outputQueue.rear++; outputQueue.data[outputQueue.rear] = opStack.data[opStack.top]; opStack.top--; } if(opStack.top >= 0 && opStack.data[opStack.top] == '(') { opStack.top--; } i++; } else { // 否则忽略当前字符 i++; } } // 将操作符栈中的所有操作符加入输出队列 while(opStack.top >= 0) { outputQueue.rear++; outputQueue.data[outputQueue.rear] = opStack.data[opStack.top]; opStack.top--; } // 将后缀算式输出到postfix数组中 int j = 0; while(outputQueue.front <= outputQueue.rear) { double data = outputQueue.data[outputQueue.front]; outputQueue.front++; if(data == '+') { postfix[j] = '+'; j++; } else if(data == '-') { postfix[j] = '-'; j++; } else if(data == '*') { postfix[j] = '*'; j++; } else if(data == '/') { postfix[j] = '/'; j++; } else { sprintf(&postfix[j], "%.2f", data); j += strlen(&postfix[j]); } } postfix[j] = '\0'; } ``` 最后,您可以编写一个函数来计算后缀算式的结果。这个函数可以使用栈来实现。例如: ```c double calculate(char* postfix) { Stack stack; stack.top = -1; int i = 0; while(postfix[i] != '\0') { if(isdigit(postfix[i])) { // 如果当前字符是数字,则将其加入栈中 double operand = 0; while(isdigit(postfix[i])) { operand = operand * 10 + (postfix[i] - '0'); i++; } if(postfix[i] == '.') { i++; double fraction = 0.1; while(isdigit(postfix[i])) { operand += fraction * (postfix[i] - '0'); fraction /= 10; i++; } } stack.top++; stack.data[stack.top] = operand; } else if(isOperator(postfix[i])) { // 如果当前字符是操作符,则从栈中弹出两个操作数,并将计算结果入栈 double operand2 = stack.data[stack.top]; stack.top--; double operand1 = stack.data[stack.top]; stack.top--; switch(postfix[i]) { case '+': stack.top++; stack.data[stack.top] = operand1 + operand2; break; case '-': stack.top++; stack.data[stack.top] = operand1 - operand2; break; case '*': stack.top++; stack.data[stack.top] = operand1 * operand2; break; case '/': stack.top++; stack.data[stack.top] = operand1 / operand2; break; //TODO: 添加更多的操作符 } i++; } else { // 否则忽略当前字符 i++; } } // 返回栈顶元素作为计算结果 return stack.data[stack.top]; } ``` 最后,您可以编写一个主函数来调用infixToPostfix和calculate函数,并将结果输出给用户。例如: ```c int main() { char infix[100]; char postfix[100]; printf("请输入一个中缀算式:"); scanf("%s", infix); infixToPostfix(infix, postfix); double result = calculate(postfix); printf("结果为:%f\n", result); return 0; } ``` 当然,这只是一个简单的实现思路,您可以根据自己的需要进一步完善和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值