// 编程练习_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;
}
/******************************************************************
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;
}
}