數據結構 實驗報告08

一、實驗目的和要求

在課後作業03-07中,任選至少一個(1-3個),涉及到抽象數據類型時,採取複用STL的方式改造代碼完成原需求,並給出分析報告。

爲檢測程序的兼容性

二、實驗環境

編譯器:Vscode DevC++

系統:Windows10

CPU:[email protected]

三、實驗內容

在課後作業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"); 

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章