C++實驗9

一、sum_diagonal & find max

#include<iostream>
using namespace std;
#include<time.h>
#include<stdlib.h>
int sum_d(int a[][5])
{
    int s=0;
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            if(i==j)
                s=s+a[i][j];
    return s;
}
void find_max(int a[][5],int &m,int &n)
{
    int max=0;
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
        {
            if(a[i][j]>max)
            {
                max=a[i][j];
                m=i;
                n=j;
            }
        }
}
void main()
{
    int a[5][5];
    srand(time(0));
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            cout<<(a[i][j]=rand()%101)<<'\t';
        cout<<'\n';
    }
    int m,n;
    find_max(a,m,n);
    cout<<"這個5×5數組的對角線元素之和爲:"<<sum_d(a)<<'\n';
    cout<<"數組中最大的元素爲:"<<a[m][n]<<" 座標("<<m+1<<","<<n+1<<")"<<endl;

}

二、二維數組冒泡排序

#include<iostream>
using namespace std;
#include<time.h>
#include<stdlib.h>
void bubblesort2D(int a[][5]);
void main()
{
    int a[5][5];
    srand(time(0));
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            cout<<(a[i][j]=rand()%101)<<'\t';
        cout<<'\n';
    }
    bubblesort2D(a);
    cout<<"排序後爲:\n";
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<5;j++)
            cout<<a[i][j]<<'\t';
        cout<<'\n';
    }


}
void bubblesort2D(int a[][5])
{
    bool flag=true;
    for(int i=0;i<24&&flag;i++)
    {
        flag=false;
        for(int j=0;j<24-i;j++)
        {
            int temp;
            if(a[j/5][j%5]>a[(j+1)/5][(j+1)%5])
            //重點。利用一維數組的排序思想,講二維數組下標用一維數組下標表示
            //注意!次數變化的是j,而不是a[i/5][i%5...]
            //也可將其賦值到一個25個元素的一維數組中,再冒泡
            {
                temp=a[j/5][j%5];
                a[j/5][j%5]=a[(j+1)/5][(j+1)%5];
                a[(j+1)/5][(j+1)%5]=temp;
                flag=true;
            }
        }
    }
}

三、數組模擬堆棧過程

#include<iostream>
using namespace std;
#include<time.h>
#include<stdlib.h>
void pushontostack(char [],int &);
//char []爲棧,int爲終止符位置
void pushoutstack(char [],int &);
void main()
{
    char a[10];         //a爲棧堆
    int stacktop=0;    //函數中值傳遞,保存結束符位置。
    int n;      //n用來存儲123選項

    while(true)
    {
        cout<<"請選擇操作:\n1(進棧)\n2(出棧)\n3(結束操作)\n";
        while(true)
        {
            cin>>n;
            if(n==1||n==2||n==3)
                break;
            else
                cout<<"數據輸入錯誤,請重新輸入:\n";
        }
        if(n==3)
            if(stacktop==0)
            {
                cout<<"棧中沒有任何數據\n";
                break;
            }
            else
            {
                cout<<"結束操作,棧中數據爲:"<<a<<endl;
                break;
            }
        if(n==2)
            pushoutstack(a,stacktop);
        if(n==1)
            pushontostack(a,stacktop);
    }
}
void pushoutstack(char a[],int &stacktop)
{
    if(stacktop)
    {
        cout<<"棧中數據爲"<<a<<'\t'<<"出站數據爲:"<<a[stacktop-1]<<'\n';
        a[--stacktop]=0;     //出棧向前移動終止符
    }
    else
    {
        cout<<"棧中無數據,";
        return;
    }
}
void pushontostack(char a[],int &stacktop)
{
    if(stacktop==9)
    {
        cout<<"棧滿,請重新選擇\n";
        return;
    }
    else
    {
        cout<<"請輸入要進棧的字符:\n";
        cin>>a[stacktop];
        a[++stacktop]=0; 
        //進棧後向後移動終止符    
        cout<<"進棧後的棧爲:"<<a;         
    }
}
發佈了45 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章