你可以将代码中的float
类型修改为double
类型,这样结果就能输出小数了。具体修改如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#define MAX_SIZE 100
typedef struct {
double data[MAX_SIZE];
int top;
} Stack;
void initialize(Stack* stack)
{
stack->top = -1;
}
bool isEmpty(Stack* stack) {
return (stack->top == -1);
}
bool isFull(Stack* stack) {
return (stack->top == MAX_SIZE - 1);
}
void push(Stack* stack, double value) {
if (!isFull(stack)) {
stack->data[++(stack->top)] = value;
}
}
double pop(Stack* stack) {
if (!isEmpty(stack)) {
return stack->data[(stack->top)--];
}
return 0.0;
}
double peek(Stack* stack) {
if (!isEmpty(stack)) {
return stack->data[stack->top];
}
return 0.0;
}
int precedence(char operator)
{
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
bool isOperator(char character) {
switch (character) {
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}
bool isOperand(char character) {
return isdigit(character);
}
double evaluateExpression(char expression[]) {
Stack operandStack;
initialize(&operandStack);
for (int i = 0; expression[i] != '\0'; i++) {
if (expression[i] == ' ' || expression[i] == '\t') {
continue;
}
else if (isOperand(expression[i])) {
char num[MAX_SIZE];
int j = 0;
while (isdigit(expression[i]) || expression[i] == '.') {
num[j++] = expression[i++];
}
num[j] = '\0';
double operand = atof(num);
push(&operandStack, operand);
i--;
}
else if (isOperator(expression[i])) {
double operand2 = pop(&operandStack);
double operand1 = pop(&operandStack);
switch (expression[i]) {
case '+':
push(&operandStack, operand1 + operand2);
break;
case '-':
push(&operandStack, operand1 - operand2);
break;
case '*':
push(&operandStack, operand1 * operand2);
break;
case '/':
if (operand2 != 0) {
push(&operandStack, operand1 / operand2);
}
else {
printf(" 除数不能为零!\n");
exit(0); // 程序异常退出
}
break;
}
}
}
return pop(&operandStack);
}
void infixToPostfix(char infix[], char postfix[]) {
Stack operatorStack;
initialize(&operatorStack);
int i, j;
i = j = 0;
while (infix[i] != '\0') {
if (infix[i] == ' ' || infix[i] == '\t') {
i++;
continue;
}
if (isOperand(infix[i])) {
postfix[j++] = infix[i++];
while (isdigit(infix[i]) || infix[i] == '.') {
postfix[j++] = infix[i++];
}
postfix[j++] = ' ';
i--;
}
else if (infix[i] == '(') {
push(&operatorStack, infix[i]);
}
else if (infix[i] == ')') {
while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {
postfix[j++] = pop(&operatorStack);
postfix[j++] = ' ';
}
if (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {
printf("Error: Invalid expression\n");
exit(0); // 程序异常退出
}
else {
pop(&operatorStack); // 弹出左括号
}
}
else if (isOperator(infix[i])) {
while (!isEmpty(&operatorStack) && precedence(peek(&operatorStack)) >= precedence(infix[i])) {
postfix[j++] = pop(&operatorStack);
postfix[j++] = ' ';
}
push(&operatorStack, infix[i]);
}
i++;
}
while (!isEmpty(&operatorStack)) {
postfix[j++] = pop(&operatorStack);
postfix[j++] = ' ';
}
postfix[j] = '\0';
}
int main() {
Stack* stack;
char infix[MAX_SIZE];
printf("请输入中缀(后缀)表达式:");
fgets(infix, MAX_SIZE, stdin);
char postfix[MAX_SIZE];
infixToPostfix(infix, postfix);
printf("后缀表达式为:%s\n", postfix);
double result = evaluateExpression(postfix);
printf("计算结果为:%f\n", result);
return 0;
}
这样修改后,代码将会使用double
类型进行计算,并输出小数结果。
内容由零声教学AI助手提供,问题来源于学员提问