C++小课堂--第一期---面向对象之前 --- 实现函数返回结构体,实现大数据的传输

来源:互联网 发布:for在c语言中的作用 编辑:程序博客网 时间:2024/05/01 17:57

即将开启C++学习之旅,是时候该说些什么了;

好了话不多说,上代码:

#include<iostream>using namespace std;struct course{    double math;    double computer;    double english;    double physics;};struct student{    char name[20];    course cos;};void display(student &stu);course Calmean(student *pstu, int num);int main (){    student stu [] = {    {"Tom", 90, 88, 76, 85},    {"Jerry", 90, 65, 79, 70},    {"Hellen", 78, 67, 89, 76},    {"Hellen", 78, 67, 89, 76}    };    course c = Calmean(stu, 4); //计算数据的平均值;    cout<<"姓名\t"<<"数学\t"<<"计算机\t"<<"英语\t"<<"物理\t"<<endl;    for(int i=0; i<4; i++)        display(stu[i]);    cout<<"---------------------------------------------------"<<endl;    cout<<"平均分\t"<<c.math<<"\t"<<c.computer<<"\t"<<c.english<<"\t"<<c.physics<<endl;    return 0;}void display(student &stu){    cout<<stu.name<<"\t"<<stu.cos.math<<"\t"<<stu.cos.computer<<"\t"<<stu.cos.english<<"\t"<<stu.cos.physics<<endl;}course Calmean(student *pstu, int num){    course c = {0, 0, 0, 0};    if(num<0){        return c;    }    for(int i=0; i<num; i++){        c.math +=pstu[i].cos.math;        c.computer += pstu[i].cos.computer;        c.english += pstu[i].cos.english;        c.physics += pstu[i].cos.english;    }    c.math /= num;    c.computer /= num;    c.english /= num;    c.physics /= num;    cout<<"因为结构体可以赋值,所以可以直接返回其中的一块内存,而不用考虑到其他的情况 函数已经结束"<<endl;    return c;}实现结构体,传递大型的数据,实现打包传输,类OC中mknetworkkit 中的POST请求,利用字典来传输;
输出结果自己运行吧!
---小红旗,转载请标明出处:http://blog.csdn.net/happylaoxu


0 0
原创粉丝点击