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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章