【C++ Primer Plus习题】7.9

问题:

这里是引用
在这里插入图片描述

解答:

#include <iostream>
using namespace std;

const int SLEN = 30;
typedef struct _Student
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
}Student;

int  getinfo(Student pa[], int len)
{
	int i = 0;
	for (i = 0; i < len; i++)
	{
		cout << "请输入第" << i + 1 << "个学生的姓名:";
		cin >> pa[i].fullname;
		cout << "请输入第" << i + 1 << "个学生的爱好:";
		cin >> pa[i].hobby;
		cout << "请输入第" << i + 1 << "个学生的水平:";
		cin >> pa[i].ooplevel;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')continue;
			cout << "输入有误,程序终止!" << endl;
			break;
		}
	}
	return i;
}

void display1(Student st)
{
	cout << "学生的姓名为:" << st.fullname << endl;
	cout << "学生的爱好为:" << st.hobby << endl;
	cout << "学生的水平为:" << st.ooplevel << endl;
}

void display2(const Student* ps)
{
	cout << "学生的姓名为:" << ps->fullname << endl;
	cout << "学生的爱好为:" << ps->hobby<< endl;
	cout << "学生的水平为:" << ps->ooplevel << endl;
}

void display3(const Student pa[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout <<"第" <<i+1<< "位学生的姓名为:" << pa[i].fullname << endl;
		cout << "第" << i+1 << "位学生的爱好为:" << pa[i].hobby << endl;
		cout << "第" << i+1<< "位学生的水平为:" << pa[i].ooplevel<< endl;
	}
}

int main()
{
	cout << "请输入班级人数:";
	int class_size;
	cin >> class_size;
	while (cin.get() != '\n')continue;
	Student* ptr_stu = new Student[class_size];
	int entered = getinfo(ptr_stu, class_size);
	for (int i = 0; i < class_size; i++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu, entered);
	delete[]ptr_stu;
	cout << "Done\n";
	
	return 0;
}

运行结果:
在这里插入图片描述

考查点:

  • 结构体指针作为函数参数
  • 结构体数组作为函数参数

2024年8月31日20:39:17

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值