在C语言中实现四则运算的方法有多种,包括使用基本运算符、函数和一些复杂的数据结构。 输入处理、运算符优先级、错误处理、扩展功能是实现四则运算的关键步骤。本文将详细探讨这些步骤并提供具体的代码示例。
一、输入处理
在C语言中,处理用户输入是实现四则运算的第一步。用户输入可以通过控制台输入获取,例如使用scanf
函数。
#include <stdio.h>
int main() {
char operator;
double num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
// Further processing will be done here
return 0;
}
二、运算符优先级
处理运算符优先级是实现四则运算的关键。简单的四则运算通常不需要复杂的优先级管理,但复杂表达式可能需要我们考虑运算符的优先级。
直接实现四则运算:
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator); // Note the space before %c to consume any leftover whitespace
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0)
result = num1 / num2;
else {
printf("Error! Division by zero.n");
return 1;
}
break;
default:
printf("Error! Operator is not correct.n");
return 1;
}
printf("%.2lf %c %.2lf = %.2lfn", num1, operator, num2, result);
return 0;
}
三、错误处理
在编写代码时,错误处理是不可忽视的部分。可能的错误包括:输入非法字符、除以零等。我们需要在代码中做好相应的防护措施。
#include <stdio.h>
#include <stdlib.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
if (scanf(" %c", &operator) != 1) {
printf("Error! Invalid operator input.n");
return 1;
}
printf("Enter two operands: ");
if (scanf("%lf %lf", &num1, &num2) != 2) {
printf("Error! Invalid number input.n");
return 1;
}
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0)
result = num1 / num2;
else {
printf("Error! Division by zero.n");
return 1;
}
break;
default:
printf("Error! Operator is not correct.n");
return 1;
}
printf("%.2lf %c %.2lf = %.2lfn", num1, operator, num2, result);
return 0;
}
四、扩展功能
为了使四则运算功能更强大,可以扩展功能,例如支持多级表达式、括号、负数等。实现这些功能需要使用更复杂的数据结构和算法。
扩展支持多级表达式的示例:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_EXPR_SIZE 100
typedef struct {
char operator;
double value;
int isOperator; // 1 if it is an operator, 0 if it is a value
} Token;
double compute(char operator, double a, double b) {
switch (operator) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
default: printf("Error! Invalid operator.n"); exit(1);
}
}
double evaluateExpression(Token *tokens, int count) {
double stack[MAX_EXPR_SIZE];
int top = -1;
for (int i = 0; i < count; i++) {
if (tokens[i].isOperator) {
if (top < 1) {
printf("Error! Invalid expression.n");
exit(1);
}
double b = stack[top--];
double a = stack[top--];
stack[++top] = compute(tokens[i].operator, a, b);
} else {
stack[++top] = tokens[i].value;
}
}
if (top != 0) {
printf("Error! Invalid expression.n");
exit(1);
}
return stack[top];
}
int main() {
char expr[MAX_EXPR_SIZE];
Token tokens[MAX_EXPR_SIZE];
int count = 0;
printf("Enter an expression: ");
fgets(expr, MAX_EXPR_SIZE, stdin);
for (int i = 0; expr[i] != '