数据结构 实验报告08

一、实验目的和要求

在课后作业03-07中,任选至少一个(1-3个),涉及到抽象数据类型时,采取复用STL的方式改造代码完成原需求,并给出分析报告。

为检测程序的兼容性

二、实验环境

编译器:Vscode DevC++

系统:Windows10

CPU:i5-8265U@1.60GHz

三、实验内容

在课后作业03-07中,任选至少一个(1-3个),涉及到抽象数据类型时,采取复用STL的方式改造代码完成原需求,并给出分析报告。

四、实验过程

4.1 任务定义和问题分析

先尝试修改看是否有什么错误

 

需要修改的地方:

  1. 头文件
  2. 函数名称

 

需要注意的地方:

自定义的函数功能和STL库中的函数可能会有所不同

需要手动修改代码来贴合STL

4.2 数据结构的选择和概要设计

由于过往实验中只用到了STL中的stack,queue两种可以实现的结构,故挑选两个实验03、04作为代表修改。

目前挑选实验报告03(实现计算机的功能)、实验报告04(输出一定行数的杨辉三角)。

4.3 详细设计

1.实验03中用到了stack

使用了stack中的GetTop(top)、OutStack(pop)、InsertStack(push)等函数----->需要换名

在声明类对象时需要作出修改

原因是 自定义的类有栈大小的设置,STL的没有

 

2.实验04中用到了queue

使用了queue中的GetTop(front)、PopTop(pop)、InsertQueue(push)----->这些函数需要换名

在声明类对象时需要作出修改

原因是 自定义的类有队列大小的设置,STL的没有

r=q.PopTop();

需要修改为

r = q.front();

q.pop();

原因:STL中queue的pop函数不返回弹出值 而自定义的返回

 

五、测试及结果分析

5.1 实验数据

实验03:

测试表达式

输出结果

正确结果

1+2

3

3

1+2*3-6/2+2/1*0

4

4

12+5*(2+3)*6/2-4

83

83

3*0+(9-6*4/2)*(2+1)

-9

-9

(1+2)+(2)+3*(4)

17

17

-1

-1

-1

(-1)

-1

-1

 

 

 

 

 

 

 

实验04:

测试数:5

 

测试数:9

 

测试数:10

 

5.2 结果及分析

 

 

需要修改的地方不多

说明自定义类和STL的功能封装后无多大区别

 

 

六、实验收获

模块化代码更利于修改

(怪不得大项目函数一大堆)

七、参考文献

八、附录(源代码)

实验03(修改为STL后的代码)

// #include"my_stack_first.h"

#include <iostream>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <map>

#include <stack>

using namespace std;

map<char, int> ys;

stack<char> stk_ch;

stack<double> stk_di;

int solve[5][5];//第一维表示待入栈运算符 第二表示栈顶运算符

//值为1 表示栈顶运算符出栈进行操作

//值为0 表示待入栈运算符入栈

//值为2 表示待入栈运算符出栈  栈顶运算符出栈  ()以及##

void init()

{

    ys['+'] = 1;

    ys['-'] = 1;

    ys['*'] = 2;

    ys['/'] = 2;

    ys['('] = 3;

    ys[')'] = 4;

    ys['#'] = 0;

    //以上是先给各个运算符编号

    solve[1][0] = 0;

    solve[1][1] = 1;

    solve[1][2] = 1;

    solve[1][3] = 0;

    // solve[1][4] = 1;

    //由于‘)’根本不会入栈 所以不存在4作为第二维出现

    //以下注释掉的组合均是不会出现的情况

    solve[2][0] = 0;

    solve[2][1] = 0;

    solve[2][2] = 1;

    solve[2][3] = 0;

    // solve[2][4] = ;



    solve[3][0] = 0;

    solve[3][1] = 0;

    solve[3][2] = 0;

    solve[3][3] = 0;

    // solve[3][4] = 1;



    // solve[4][0] = 1;

    //不会存在这种情况

    solve[4][1] = 1;

    solve[4][2] = 1;

    solve[4][3] = 2;

    // solve[4][4] = 1;



    solve[0][0] = 2;

    solve[0][1] = 1;

    solve[0][2] = 1;

    // solve[0][3] = 0;

    // solve[0][4] = 2;

}

void work(char wit)

{

    //char top = stk_ch.GetTop();

    char top = stk_ch.top();

    if (solve[ys[wit]][ys[top]] == 0)

    {

        //stk_ch.InsertStack(wit);

        stk_ch.push(wit);

        return;

    }       

    while (solve[ys[wit]][ys[top]] == 1)

    {

        // double num2 = stk_di.GetTop();

        // stk_di.OutStack();

        // double num1 = stk_di.GetTop();

        // stk_di.OutStack();

        double num2 = stk_di.top();

        stk_di.pop();

        double num1 = stk_di.top();

        stk_di.pop();

        switch(top){

            case '+':

                num1 += num2;

                break;

            case '-':

                num1 -= num2;

                break;

            case '*':

                num1 *= num2;

                break;

            case '/':

                num1 /= num2;

                break;

        }

        // stk_di.InsertStack(num1);

        // stk_ch.OutStack();

        // top = stk_ch.GetTop();

        stk_di.push(num1);

        stk_ch.pop();

        top = stk_ch.top();

    }

    if (solve[ys[wit]][ys[top]] == 0)

    {

        // stk_ch.InsertStack(wit);

        stk_ch.push(wit);

        return;

    }

    if (solve[ys[wit]][ys[top]] == 2)

        // stk_ch.OutStack();

        stk_ch.pop();

}

int main()

{

    string sh;

    getline(cin, sh);

    init();

    sh = '#'+sh + '#';

    // stk_ch.InsertStack('#');

    stk_ch.push('#');

    for (int i = 1; i < sh.length();i++)

    {

        if(isdigit(sh[i])){

            int flag = 1;

            if(ys[sh[i-1]]==1&&(sh[i-2]=='('||sh[i-2]=='#'))

            {

                if (sh[i - 1] == '-')

                    flag = -1;

                // cout << i << "\n";

                // stk_ch.OutStack();

                stk_ch.pop();

            }

            int di = 0;

            while (isdigit(sh[i])){

                di *= 10;

                di += sh[i] - '0';

                i++;

            }

            di *= flag;

            // stk_di.InsertStack(di);

            stk_di.push(di);

            // cout << di;

        }

        if(sh[i]==' ')

            continue;

        work(sh[i]);

    }

    // cout << "\n";

    // cout << stk_di.GetTop();

    cout << stk_di.top();

    system("pause");

}

实验04:

#include<iostream>

#include<cstdio>

#include<algorithm>

#include<queue>

using namespace std;



// Queue<int> q(1010);

queue<int> q;

int n;

void init()

{

    // q.InsertQueue(0); 

    // q.InsertQueue(1);

    // q.InsertQueue(0);

    q.push(0);

    q.push(1);

    q.push(0);

}



void work(int x)

{

    int l=0,r=0;

    // q.InsertQueue(0);

    // r=q.PopTop();

    q.push(0);

    r = q.front();

    q.pop();

    for(int i=1;i<=x;i++)

    {

        // l=r;r=q.PopTop();

        l = r;

        r = q.front();

        q.pop();

        if(i==1&&x==n)

        printf("%3d",r);

        else

        printf("%6d",r);

        // q.InsertQueue(r+l);

        q.push(r + l);

    }

    cout<<"\n";

    // l=r;r=q.PopTop();

    // q.InsertQueue(l+r);

    // q.InsertQueue(0);

    l = r;

    r = q.front();

    q.pop();

    q.push(l + r);

    q.push(0);

}

void kprint(int num,int x)

{

    if(num==x)return ;

    int len=(num-x-1)*3;

    for(int i=1;i<=len;i++)

    {

        cout<<" ";

    }

}

int main()

{

    init();

    cin>>n;

    for(int i=1;i<=n;i++)

    {

        kprint(n,i);

        work(i);

    }

    system("pause"); 

}

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值