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;
    }

    运行结果:

  • 相关阅读:
    采用二叉搜索树来统计文本中单词出现的频率
    一个表查找程序
    unix grep命令的大致实现
    字符串操作函数
    关于宏和逗号表达式
    http状态码的分类
    nginx源码学习资源(不断更新)转
    什么是CGI
    字符串操作 删除字符
    Ubuntu安装特定版本安装包
  • 原文地址:https://www.cnblogs.com/duanqibo/p/13127118.html
Copyright © 2011-2022 走看看