使用结构体变量表示每个学生的信息:姓名、学号和三门课的成绩。从键盘上输入 10 个学生的数据,然后输出每个学生的姓名和三门课的平均成绩

  • Post author:
  • Post category:其他


//LA_CH02_67
#include<iostream>
using namespace std;
typedef struct  {//定义结构
	char name[20];
	char number[6];
	int score[3];
	int average;
}student;
void input(student *s) {
	for (int i = 0; i < 10; i++) {
		cin >> (s[i].name);//input name
		cin >> (s[i].number);//input number
		for (int j = 0; j < 3; j++) {
			cin >> ((s + i)->score[j]);
			//The address where the data is located may change
		}
	}
}

void make_average(student* s) {
	float sum=0;
	for (int i = 0; i < 10; i++) {
		sum = 0;
		for (int j = 0; j < 3; j++) {
			sum = sum + ((s + i)->score[j]);
		}
		(s + i)->average = sum / 3;
		cout << s[i].name << "'s grade point average in three courses:" << (s + i)->average << endl;
	}
}

int main() {
	student s[10];
	cout << "please input the name,the number and the scores of 10 student" << endl;
	input(s);
	make_average(s);
	return 0;
}

程序流程图



版权声明:本文为m0_64132509原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。