一些c++程序题

// 冒泡排序.cpp : 定义控制台应用程序的入口点。
//比较相邻两个元素,若第一个比第二个大,就交换两个元素

#include "stdafx.h"
#include<iostream>

using namespace std;

int main()
{
    const int N = 7;
    int bubble[N];
    cout << "输入数字:";
    for (int i = 0; i < N; i++)
    {
        cin >> bubble[i];
    }
    cout << endl;
    
    for (int i = 0; i < N; i++)//控制循环次数
    {
        //int max = bubble[i];
        for (int j = 0; j < N - i-1; j++)
        {
             if (bubble[j] > bubble[j+1])
            {
                int temp;
                temp = bubble[j];
                bubble[j] = bubble[j+1];
                bubble[j + 1] = temp;
            }
        }
        //bubble[N - i - 1] = max;
    }
    cout << "从小到大的顺序为:";
    for (int i = 0; i < N; i++)
    {
        cout<< bubble[i]<<" ";
    }
    system("pause");
    return 0;
}

// 选择排序.cpp : 定义控制台应用程序的入口点。
//一个序列当中选出一个最大的(升序为最小的),然后和第一个元素交换。

#include "stdafx.h"
#include<iostream>

using namespace std;

int main()
{
    const int N = 7;
    int bubble[N];
    cout << "输入数字:";
    for (int i = 0; i < N; i++)
    {
        cin >> bubble[i];
    }
    cout << endl;
    int min;
    for (int i = 0; i < N; i++)
    {
        min = bubble[i];
        int temp;
        for (int j = i; j < N; j++)
        {
            if (min > bubble[j])
            {
                temp = min;
                min = bubble[j];
                bubble[j] = temp;
            }
        }    
        bubble[i] = min;
    }
    cout << "从小到大的顺序为:";
    for (int i = 0; i < N; i++)
    {
        cout << bubble[i] << " ";
    }
    system ("pause");
    return 0;
}

// 二维数组.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string stu_name[] = { "张三","李四","王五" };
    string course[] = { "语文","数学","英语" };
    const int row = sizeof(stu_name) / sizeof(stu_name[0]);
    const int col = sizeof(course) / sizeof(course[0]);
    double grade[row][col];
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < row; j++)
        {
            cout << stu_name[i] << "的成绩:" << course[j] << ":";
            cin >> grade[i][j];
        }
        cout << endl;
    }
    cout << "总成绩单为:" << endl;
    cout <<" \t";
    for (int i=0;i<row;i++)
    {
        cout << stu_name[i] << "\t";
    }
    cout << endl;
    for (int j = 0; j < row; j++)
    {
        cout << course[j] << "\t";
        for (int k = 0; k < row; k++)
        {
            cout << grade[0][k] << "\t";
        }
        cout << endl;
    }
    system("pause");
    return 0;
}

//将二维数组作为参数传入函数,利用指针计算学生总分,不及格名单及平均分等

//int无法强制转换成double类型

//解释,尽管我没看懂https://blog.csdn.net/qq_39632912/article/details/79124330

// 成绩统计.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>

using namespace std;

double average(double **array, int m, int n);
void fail(double **array, int m, int n);
void great(double **array, int m, int n);
int main()
{
    double grade[5][6] = { { 1,74,75,78,79,85 },
    { 2,45,41,42,47,85 },
    { 3,75,74,71,12,13 },
    { 4,99,83,98,85,89 },
    { 5,94,95,96,95,91 } };;  //行表示学生,列表示课程,5行6列
    /*
    grade[5][6] = {{ 1,74,75,78,79,85},
    { 2,45,41,42,47,85},
    { 3,75,74,71,12,13},
    { 4,14,12,74,45,89},
    { 5,74,75,76,85,81}};
    */
    /*cout << "请输入学生的学号和各科成绩!" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "请输入学生" << i + 1 << "的学号:";
        cin >> grade[i][0];
        cout << "请输入学生" << i + 1 << "的成绩:";
        for (int j = 1; j < 6; j++)
        {
            cin >> grade[i][j];
        }
        cout << endl;
    }*/
    cout << "所有学生的平均成绩为:"<<average((double**)grade,5,6) << endl;
    fail((double**)grade, 5, 6);
    great((double**)grade, 5, 6);
    system("pause");
    return 0;
}

double average(double **array, int m, int n)
{
    double all=0; //int/double类型转换?
    double aver;
    int num = (m - 1)*(n - 1);
    for (int i = 0; i < m; i++)
    {
        for (int j = 1; j < n; j++)
        {
            all += *((double*)array + i*n + j);
        }
    }
    aver = all / num;
    return aver;
}
void fail(double **array, int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        int num = 0;  //用来记录每个人不及格的次数
        int all = 0;  //记录每个人的总分
        for (int j = 1; j < n; j++)
        {
            if (*((double*)array + i*n + j) < 60)
                num++;
            all += *((double*)array + i*n + j);
        }
        if (num > 2)
        {
            cout << *((double*)array+ i*n) << "的总成绩为:"
                << all << ",其中平均成绩为:" << all / 5 << endl;
        }
    }
}

void great(double **array, int m, int n)
{
    int all=0;
    double ave;
    for (int i = 0; i < m; i++)
    {
        for (int j = 1; j < n; j++)
        {
            all += *((double*)array + i*n + j);
        }
        ave = all / 5;
        if(ave>90)
            cout << *((double*)array + i*n) << "平均分高于90!优秀!"<< endl;
        else
        {
            for (int j = 1; j < n; j++)
            {
                if (*((double*)array + i*n + j) < 85)
                    break;
                else if(j=n-1)
                    cout << *((double*)array + i*n) << "全部课程高于85!优秀!" << endl;
            }
        }
        all = 0;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42093825/article/details/82288456