⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complement.cpp

📁 我学习C++ Primer Plus过程中写下的课后作业的编程代码
💻 CPP
字号:
//8.这个练习让您编写处理数组和结构的函数。下面是程序的框架,请提供其中
//描述的函数,以完成该程序。

#include <iostream>
using namespace std;

const int SLEN = 30;

struct student{
	char fullname[SLEN];
	char hobby[SLEN];
	int  ooplevel;
};

// getinfo() has two arguments: a pointer to the first element of 
// an array of student structures and an int representing the
// number of elements of the array. The function solicits and
// stores data about students. It terminates input upon filling
// the array or upon encountering a blank lin for the student
// name. The function returns the actual number of array elements
// filled.
int getinfo (student pa[],int n)
{
	int i=0;
	for(i=0;i<n;i++)
	{
		cout<<"Do you want to input the student["<< i+1 <<"](Y/N)?"<<endl;
		char choose;
		if( cin>>choose && ('Y'==choose||'y'==choose) )
		{
			
			cout<<"name:";
			cin.get();
			cin.get(pa[i].fullname,SLEN).get();
			cout<<"hobby:";
			cin.getline(pa[i].hobby,SLEN);
			cout<<"ooplevel:";
			cin>>pa[i].ooplevel;
		}
		else
			break;
	}
	return i;
}



// display1()takes a student structure as an argument 
// and displays its contents
void display1 (student st)
{
	
	cout<<"name:"<<st.fullname<<endl;
	cout<<"hobby:"<<st.hobby<<endl;
	cout<<"ooplevel:"<<st.ooplevel<<endl;
	
}

// display2()takes the address of student structure as an
// argument and displays the structure's contents
void display2(const student *ps)
{
	
	cout<<"name:"<<ps->fullname<<endl;
	cout<<"hobby:"<<ps->hobby<<endl;
	cout<<"ooplevel:"<<ps->ooplevel<<endl;
	
}

// display3()takes the address of the first element of an array
// of student structures and the number of array elements as
// arguments and displays the contents of the structures 
void display3 (const student pa[],int n)
{
	for(const student *i=pa; i<(pa+n); i++)
	{
		cout<<"name:"<<i->fullname<<endl;
		cout<<"hobby:"<<i->hobby<<endl;
		cout<<"ooplevel:"<<i->ooplevel<<endl;
	}
}

int main()
{
	cout<<"Enter class size: ";
	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<entered; i++)
	{
		display1(ptr_stu[i]);
		display2(&ptr_stu[i]);
	}
	display3(ptr_stu,entered);
	delete []ptr_stu;
	cout<<"Done\n";
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -