(2011.07.08)编程练习_07.08_处理数组和结构的函数.cpp 输出结果有问题。

// 编程练习_07.08_处理数组和结构的函数.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 encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n);


// display1()takes a student structure as an argument
// and displays its contents
void display1(student st);


// display2()takes the address of student structure as an 
// argument and displays the structure's contents
void display2(const student *ps);


// display3()takes the address of the first element of an array
// of student structures and the number of array elements as


// arrguments and displays the contents of the structures 
void display3(const student pa[], int n);


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;
}


*****************************************************************/


#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 encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n);


// display1()takes a student structure as an argument
// and displays its contents
void display1(student st);


// display2()takes the address of student structure as an 
// argument and displays the structure's contents
void display2(const student *ps);


// display3()takes the address of the first element of an array
// of student structures and the number of array elements as


// arrguments and displays the contents of the structures 
void display3(const student pa[], int n);


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;
}




// 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 encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n)
{
for (int i = 0; i < n && pa[i-1].fullname == ""; ++i)
{
cout << "Please enter this student's fullname: ";
cin >> pa[i].fullname;
cout << "Please enter this student's hobby: ";
cin >> pa[i].hobby;
cout << "Please enter this student's ooplevel: ";
cin >> pa[i].ooplevel;
}
return i;
}


// display1()takes a student structure as an argument
// and displays its contents
void display1(student st)
{
cout << st.fullname << '\t' << st.hobby << '\t' 
<< st.ooplevel;
}


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




// display3()takes the address of the first element of an array
// of student structures and the number of array elements as


// arrguments and displays the contents of the structures 
void display3(const student pa[], int n)
{
for (int i = 0; i < n && pa[i-1].fullname == ""; ++i)
{
cout << pa[i].fullname << '\t' ;
cout << pa[i].hobby << '\t' ;
cout << pa[i].ooplevel << endl;
}

}


//  哈哈,终于改正成功了。


// 编程练习_07.08_处理数组和结构的函数.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 encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n);

// display1()takes a student structure as an argument
// and displays its contents
void display1(student st);

// display2()takes the address of student structure as an 
// argument and displays the structure's contents
void display2(const student *ps);

// display3()takes the address of the first element of an array
// of student structures and the number of array elements as

// arrguments and displays the contents of the structures 
void display3(const student pa[], int n);

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;
}

*****************************************************************/

#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 encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n);

// display1()takes a student structure as an argument
// and displays its contents
void display1(student st);

// display2()takes the address of student structure as an 
// argument and displays the structure's contents
void display2(const student *ps);

// display3()takes the address of the first element of an array
// of student structures and the number of array elements as

// arrguments and displays the contents of the structures 
void display3(const student pa[], int n);

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";
		cin.get();
		cin.get();
		cin.get();
		return 0;
}


// 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 encoutering a blank line for the student
// name. The function returns the actual namber of array elements
// filled.
int getinfo(student pa[], int n)
{
    int i;
	for (i = 0; i < n ; ++i)
	{
		cout << "Please enter this student's fullname: ";
        
		int j;
		cin.get(pa[i].fullname[0]);
 		if (pa[i].fullname[0] == '\n')
 		{
 			break;
         }

		for (j = 1; j < SLEN && (pa[i].fullname[j-1] != '\n'); ++j)
		{
			 cin.get(pa[i].fullname[j]);
			 pa[i].fullname[j+1] = '\0';
		}
		
		cout << "Please enter this student's hobby: ";
		cin >> pa[i].hobby;
		cout << "Please enter this student's ooplevel: ";
		cin >> pa[i].ooplevel;
		cin.get();

	}
	return i;
}

// display1()takes a student structure as an argument
// and displays its contents
void display1(student st)
{
	cout << st.fullname << '\t' << st.hobby << '\t' 
		<< 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 << ps -> fullname << '\t' 
		<< ps -> hobby << '\t' 
		<< ps -> ooplevel << endl;
}


// display3()takes the address of the first element of an array
// of student structures and the number of array elements as

// arrguments and displays the contents of the structures 
void display3(const student pa[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		if (pa[i].fullname == "")
		{
			break;
		}
		cout << pa[i].fullname << '\t' ;
		cout << pa[i].hobby << '\t' ;
		cout << pa[i].ooplevel << endl;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值