通过指针对结构体成员对象的访问输出C/C++

来源:互联网 发布:linux没有telnet文件 编辑:程序博客网 时间:2024/06/07 03:22


#include <iostream>
using namespace std;
//定义一个结构体用于保存函数的信息参数
typedef struct
{   int id;   //学生的学号
    float math;          //数学
    float physics;  //物理
    float computer;  //计算机
    float chemistry;  //化学
    float english;  //英语
    float political;  //政治
    float sport;   //休育
    float history;  //历史
    float geography;     //地理
} STUDENT_PARMS;
//声明函数print_student_report的原型
void print_student_report(STUDENT_PARMS *);
//主程序
int main(void)
{   STUDENT_PARMS student_parm;         //实例化学生成绩结构体对象
    student_parm.math = 99;  //数学
    student_parm.physics = 80;  //物理
    student_parm.computer = 100; //计算机
    student_parm.chemistry = 76; //化学
    student_parm.english = 86;  //英语
    student_parm.political = 60; //政治
    student_parm.sport = 90;  //休育
    student_parm.history = 75;  //历史
    student_parm.geography = 90; //地理
    print_student_report(&student_parm); 
//通过指向对象的指针参数来打印学生的成绩
    return 1;
}
//定义一个能够通过指针打印出学生全部成绩的函数
void print_student_report(STUDENT_PARMS *p)
{   //输出学生的学号
    cout << "student id is: ";
    cout << (p->id) << "\n"; //注意通过指针来访问结构体成员的方式
    //输出数学分数
    cout << "math score is: " ;
    cout << (p->math) << "\n";
    //输出物理分数
    cout << "physics score is: ";
    cout << (p->physics) << "\n";
    //输出计算机分数
    cout << "computer score is: ";
    cout << (p->computer) << "\n";
    //输出化学分数
    cout << "chemistry score is: ";
    cout << (p->chemistry) << "\n";
    //输出英语分数
    cout << "english score is: ";
    cout << (p->english) << "\n";
    //输出政治分数
    cout << "political score is: ";
    cout << (p->political) << "\n";
    //输出体育成绩
    cout << "sport score is: ";
    cout << (p->sport) << "\n";
    //输出历史分数
    cout << "history score is: ";
    cout << (p->history) << "\n";
    //输出地理成绩
    cout << "geography score is: ";
    cout << (p->geography) << "\n";
}
0 0