zoukankan      html  css  js  c++  java
  • C++实训(2.1)

    源程序:

    #include <iostream>
    #include <string>
    #define N 4
    using namespace std;

    class student
    {
    private:
    int num;
    string name;
    string sex;
    int age;
    public:
    student() {};
    student(int n, string na, string se, int ag);
    friend void straightinsertsort(student stud[], int n);
    void show();
    };

    student::student(int n, string na, string se, int ag)
    {
    num = n;
    name = na;
    sex = se;
    age = ag;
    }
    //按姓名排序-直接插入排序
    void straightinsertsort(student stud[], int n)
    {
    int i, j;
    student temp;
    printf(" 按学生姓名排序 ");
    for (i = 1; i < n; i++)
    {
    temp = stud[i];
    j = i - 1;
    while ((j >= 0) && temp.name < stud[j].name)
    {
    stud[j + 1] = stud[j];
    j--;
    }
    stud[j + 1] = temp;
    }
    }

    //显示结果
    void student::show()
    {
    std::cout << num << " " << name << " " << sex << " " << age << endl;
    }

    //主函数
    int main()
    {
    student stu[N] = {student(1001,"zhangs","m",20),
    student(1002,"lisi","f",21),
    student(1003,"wangwu","m",20),
    student(1004,"zhaoliu","f",19) };

    int len = sizeof(stu) / sizeof(stu[0]);
    straightinsertsort(stu, len);
    cout << "学号" << " " << "姓名" << " " << "性别" << " " << "年龄" << endl;
    for (int i = 0; i < len; i++)
    {
    stu[i].show();
    cout << endl;
    }
    return 1;
    }

    运行结果:

  • 相关阅读:
    CDH 重装 kafka 报错,挺常见的错误
    SpringBoot + easyexcel + vue 下载 excel 问题
    第二节 全球金融市场
    第一节 金融市场概述
    第1章-起 步
    第3关-input()函数
    第2关-条件判断与条件嵌套
    风变编程-Python基础语法
    第0关-千寻的名字
    Git知识点记录3-Git基础
  • 原文地址:https://www.cnblogs.com/duanqibo/p/13127118.html
Copyright © 2011-2022 走看看