栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

icoding测试代码---后缀表达式计算

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

icoding测试代码---后缀表达式计算

本程序适用于:

 

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 
#include 
#include
#include 
//#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("icodingn后缀表达式计算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='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]!=''){
                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 空格数%dn",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 运算符个数%dn",num_cou,opt_cou);
            printf("str中数字个数与运算符个数不匹配,正确应该为 运算符个数=数字个数-1 ;请检查!n程序自动终止,请修改n");
            return -1;
        }
        else if(bla_cou+1!=opt_cou+num_cou){
            printf("str的数字个数%d 运算符个数%d 空格数%dn",num_cou,opt_cou,bla_cou);
            printf("str中空格个数不匹配,正常情况应该是 空格个数+1=运算符个数+数字个数 请检查!n程序自动终止,请修改n");
            return -1;
        }
        
        else{
            printf("<>待求表达式:%sn", str);
    printf("结果为t%dn", compute_reverse_polish_notation(str));
    printf("----测试结束----n");
}

如果不对内置测试样例进行更改,那么正确的输出结果将如下

使用方法https://blog.csdn.net/jjq15008419406/article/details/125011238?spm=1001.2014.3001.5501 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号