本程序适用于:
icoding的数据结构并没有一个测试代码,其都是直接编写一个函数的形式,因此很难知道自己的实际输出是什么。针对部分题目,我编写了一系列测试代码以供大家进行数据输出的测试。
int compute_reverse_polish_notation(char *str){ //TODO }
请将您的函数代码复制到上述函数中,然后修改main函数的相关内容,完成测试样例的输入
题目原文:
请使用已定义好的栈完成后缀表达式计算:
(1)如果是操作数,直接入栈
(2)如果是操作符op,连续出栈两次,得到操作数x 和 y,计算 x op y,并将结果入栈。后缀表达式示例如下:
9 3 1 - 3 * + 10 2 / +
13 445 + 51 / 6 -
操作数、操作符之间由空格隔开,操作符有 +,-,*, /, %共 5 种符号,所有操作数都为整型。栈的定义如下:
#define Stack_Size 50 typedef struct{ ElemType elem[Stack_Size]; int top; }Stack; bool push(Stack* S, ElemType x); bool pop(Stack* S, ElemType *x); void init_stack(Stack *S);
其中,栈初始化的实现为:
void init_stack(Stack *S){ S->top = -1; }
需要完成的函数定义为:
int compute_reverse_polish_notation(char *str);
函数接收一个字符指针,该指针指向一个字符串形式的后缀表达式,函数返回该表达式的计算结果。
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#include <string.h>
//#include "list.h" // 请不要删除,否则检查不通过
#define ElemType int
//定义栈
typedef struct stack{
int *data;
int top;
}Stack;
bool push(Stack* S, ElemType x){
S->data[S->top++] = x;
return true;
}
bool pop(Stack* S, ElemType *x){
if(S->top == -1){
return false;
}
*x = S->data[--S->top];
return true;
}
void init_stack(Stack *S){
S->data = (int*)malloc(sizeof(int)*100);
S->top = -1;
}
//--------以上为辅助函数-------------
int compute_reverse_polish_notation(char *str)
{
//TODO
}
int main(){
//---------以 下 内 容 可 修 改----------
char str[] = "1 2 3 4 5 + * - %";
//测试用例1:1 2 3 + 4 * +(结果为21)
//测试用例2:9 3 1 - 3 * + 10 2 / +(结果为20)
//测试用例3:13 445 + 51 / 6 -(结果为2)
//测试用例4:1 2 3 4 5 6 7 8 9 0 + * - / %(结果为1)
//测试用例5:2 4 + 3 / 0 -(结果为2)
//数字/运算符之间的空格只能有一个!!!!!
//首尾不能有空格!!!需以数字开头,运算符结尾
//最多能有100个数据!!!!!
//---------以 上 内 容 可 修 改----------
//检测str的合法
printf("icoding\n后缀表达式计算\n----开始测试----\n>>str合法性检测...\n");
int len=strlen(str);
if(len>100){
printf("错误:字符串长度超过100!!!\n程序自动终止,请修改\n");
return 0;
}
else if (str == NULL)
{
printf("str未输入内容\n程序自动终止,请修改\n");
return -1;
}
else{
int num_cou=0;
int opt_cou=0;
int bla_cou=0;
bool has_zero=false;
bool has_chufa=false;
for(int i=0;i<strlen(str);i++){
if(opt_cou==num_cou&&i!=0){
printf("str中存在一个子串(字串下标0~%d)中 运算符个数==数字个数 这不符合规则\n",i);
printf("如 \"1 4 + * ……\"不正确,运行前三个后,*无法找到5*[?]中的[?]\n");
printf("如 \"+ 1 4 * ……\"也不正确,运行前一个后,+无法找到相加的数字\n");
printf("如\"2 / 3 ……\"也不正确,运行前2个后,/无法找到除数\n程序自动终止,请修改\n");
return -1;
}
if(str[i]>='0'&&str[i]<='9'){
if((i!=0&&str[i]=='0'&&str[i-1]==' ')||(i==0&&str[i]==0)) has_zero=true;
if(i!=0&&str[i-1]!=' '){
continue;//上一个是数字,那么这两个数字合起来是一个多位整数
}
else{
num_cou++;
continue;
}
}
else if(str[i]!=' '&&str[i]!='+'&&str[i]!='-'&&str[i]!='*'&&str[i]!='/'&&str[i]!='%'&&str[i]!='\0'){
printf("str中含有非法字符 %c(下标%d)\n程序自动终止,请修改\n",str[i],i);
return -1;
}
else if(str[i]==' '){
if(i!=0&&str[i-1]==' '){
printf("在下标%d存在两个连续的空格,输入不合法\n程序自动终止,请修改\n",i);
return -1;
}
bla_cou++;
continue;
}
else{
if(i!=0&&str[i-1]!=' '){
printf("运算符前一个数据必须为空格,在'%c'(下标%d)处输入不合法\n程序自动终止,请修改\n",str[i],i);
return -1;
}
else if(i!=strlen(str)-1&&str[i+1]!=' '){
printf("运算符后一个数据必须为空格,在'%c'(下标%d)处输入不合法\n程序自动终止,请修改\n",str[i],i);
return -1;
}
if(str[i]=='/'){
has_chufa=true;
}
opt_cou++;
continue;
}
}
if(num_cou==0||opt_cou==0||bla_cou==0){
printf("str的数字个数%d 运算符个数%d 空格数%d\n",num_cou,opt_cou,bla_cou);
printf("str中没有数字/运算符/空格,请检查!\n程序自动终止,请修改\n");
return -1;
}
char check1=str[strlen(str)-1];
if(check1!='+'&&check1!='-'&&check1!='*'&&check1!='/'&&check1!='%'){
if(check1==' ')printf("str最后一个字符一定是运算符,而输入的是空格 请检查!\n程序自动终止,请修改\n");
else printf("str最后一个字符一定是运算符,而输入的是%c 请检查!\n程序自动终止,请修改\n",check1);
return -1;
}
else if(num_cou!=opt_cou+1){
printf("str的数字个数%d 运算符个数%d\n",num_cou,opt_cou);
printf("str中数字个数与运算符个数不匹配,正确应该为 运算符个数=数字个数-1 ;请检查!\n程序自动终止,请修改\n");
return -1;
}
else if(bla_cou+1!=opt_cou+num_cou){
printf("str的数字个数%d 运算符个数%d 空格数%d\n",num_cou,opt_cou,bla_cou);
printf("str中空格个数不匹配,正常情况应该是 空格个数+1=运算符个数+数字个数 请检查!\n程序自动终止,请修改\n");
return -1;
}
else{
printf("<<str合法,继续后续测试\n");
if(has_chufa&&has_zero){
printf("*警告* str中含有除法运算符且含有0,请确保0不被做除数"
"\n(若后续出现Floating point exception,则说明您犯了这个错误)\n");
}
}
//printf("%d %d",num_cou,opt_cou);
}
printf(">>待求表达式:%s\n", str);
printf("结果为\t%d\n", compute_reverse_polish_notation(str));
printf("----测试结束----\n");
}
如果不对内置测试样例进行更改,那么正确的输出结果将如下
使用方法https://blog.csdn.net/jjq15008419406/article/details/125011238?spm=1001.2014.3001.5501