问题遇到的现象和发生背景
提交pta有三个测试点错误
用代码块功能插入代码,请勿粘贴截图
#define _CRT_SECURE_NO_WARNINGS 1
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define Max 100
//运用顺序栈
typedef struct MyStruct
{
double elem[Max];
int top;
}Stack;
void InitStack(Stack* s);
double PushStack(Stack* s, double x);
double PopStack(Stack* s);
int main() {
char c[Max];
char a[Max];
double sum = 0, p, q;
Stack s;
InitStack(&s);
int i = 0, j = 0, flag = 0;
while (1) {
scanf("%s", &c);
if (strcmp(c, "+") && strcmp(c, "-") && strcmp(c, "*") && strcmp(c, "/")) {
sum = strtod(c, NULL);
PushStack(&s, sum);
}
else
{
p = PopStack(&s);
q = PopStack(&s);
if (strcmp(c, "+") == 0) {
sum = p + q;
PushStack(&s, sum);
}
else if (strcmp(c, "-") == 0) {
sum = p - q;
PushStack(&s, sum);
}
else if (strcmp(c, "*") == 0) {
sum = p * q;
PushStack(&s, sum);
}
else if (strcmp(c, "/") == 0) {
sum = p / q;
PushStack(&s, sum);
}
}
if (s.top == 0)
flag++;
if (s.top == 0 && flag == 2)
break;
}
sum = PopStack(&s);
printf("%.1lf\n", sum);
return 0;
}
void InitStack(Stack* s) {
s->top = -1;
}
double PushStack(Stack* s, double x) {
if (s->top == Max - 1)
return 0;
s->elem[++s->top] = x;
return 1;
}
double PopStack(Stack* s) {
double x;
if(s->top==-1)
return 0;
x = s->elem[s->top--];
return x;
}
运行结果及报错内容
结果没有问题,和pta的输入输出一致
我的解答思路和尝试过的方法
做一个栈,是数字就放进去,是符号就取出栈里的两个数字运算,直到栈底为一个数
我想要达到的结果
pta通过测试,并且指出我测试点过不去的原因