美文网首页
第七章 文件和结构体 7.2 结构体 P140

第七章 文件和结构体 7.2 结构体 P140

作者: 壹顾倾城 | 来源:发表于2019-12-16 15:05 被阅读0次
/********************************
 * 程序来源:董老师一本通
 * 程序名称:140 7.2
 * 章    节:7.2 结构体 
 * 作    者:tiaya@qq.com
 * 运行测试:通过
 *******************************/
//#include <bits/stdc++.h>  //万能头文件,不建议使用
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

//struct
struct student{
    string name;
    double math;
    double chinese;
    double totle;
}stu[100];

//main() star
int main() {
    //code here
    int n;    //count
    cin >> n;
    
    for(int i=0; i<n; i++) {
        cin >> stu[i].name >> stu[i].math >> stu[i].chinese;
        
        stu[i].totle = stu[i].math + stu[i].chinese;
    }
    
    //冒泡排序 
    for(int i=n-1; i>0; i--) {
        for(int j=0; j<i; j++) {
            if(stu[j].totle < stu[j+1].totle) {
                swap(stu[j], stu[j+1]);
            }
        }
    }
    
    //print
    cout << "student list: "<< endl; 
    for(int i=0; i<n; i++) {
        cout << stu[i].name << "\t";
        cout << stu[i].math << "\t";
        cout << stu[i].chinese << "\t";
        cout << stu[i].totle <<endl;
    }
    return 0;
}

测试:
输入数据:

4
sd 23 89
ss 67 56
gg 89 76
gs 67 90

输出数据:

student list:
gg      89      76      165
gs      67      90      157
ss      67      56      123
sd      23      89      112

--------------------------------
Process exited after 29.75 seconds with return value 0
请按任意键继续. . .

相关文章

  • 第七章 文件和结构体 7.2 结构体 P140

    测试:输入数据: 输出数据:

  • 第七章 文件和机构体 143页 上机练习1

    程序来源:董老师一本通 章 节:7.2 结构体 程序名称:第七章 文件和机构体 143页 上机练习1 作...

  • 第七章 文件和结构体 7.2 结构体 P141

    程序来源:董老师一本通章 节:7.2.2 结构体的使用程序名称:第七章 文件和机构体 141页作 ...

  • C中将结构体写入文件及读取

    需要导入的头文件 写入结构体 假设有结构体: 结构体写入文件的方法: 读取文件中的结构体 知识扩展 函数fopen...

  • 结构体的基本操作

    结构体定义和初始化 结构体变量相互赋值 结构体数组 结构体嵌套一级指针 结构体嵌套二级指针 结构体偏移量 联合体 ...

  • 6.结构体相关

    一 C 结构体和结构体指针 eg1: 一 结构体里面定义函数 通过结构体指针访问结构体里面定义的函数。 eg2:

  • C语言-结构体,文件操作

    1、结构体 结构体统一取别名,兼容多平台编译器 2、结构体指针 注意结构体指针采用->访问属性 3、文件操作 读文...

  • 12-Go语言结构体

    结构体 结构体的基本概念 什么是结构体Go语言中的结构体几乎和C语言中的结构体一模一样都需要先定义结构体类型, 再...

  • 结构体和结构体指针

    定义: 下边定义了两种类型: 1.ZJWPoint为结构体类型; 2.ZJWPointClass为指向ZJWPoi...

  • Go 面向对象编程

    struct 实例化方式 结构体语法糖 结构体是值类型 结构体 深拷贝和浅拷贝 即值传递和引用传递 匿名结构体 结...

网友评论

      本文标题:第七章 文件和结构体 7.2 结构体 P140

      本文链接:https://www.haomeiwen.com/subject/nphdnctx.html