c++ primer plus 习题答案(2)

p221.8

 #include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
const int SLEN = ;
struct student{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n);
void display1(student st);
void display2(const student *ps);
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 = ; i < entered; i++){
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete[]ptr_stu;
cout << "done\n";
system("pause");
return ;
} int getinfo(student pa[], int n){
int i = , count = ;
while (i < n && *pa[i].fullname != '\0'){
cin.getline(pa[i].fullname, SLEN);
cin.getline(pa[i].hobby, SLEN);
cin >> pa[i].ooplevel;
cin.get();
count++;
i++;
}
return count;
} void display1(student st){
cout << "st.fullname" << st.fullname <<
endl << "st.hobby" << st.hobby <<
endl << "st.ooplevel" << st.ooplevel <<
endl;
} void display2(const student *ps){
cout << "ps->fullname" << ps->fullname <<
endl << "ps->hobby" << ps->hobby <<
endl << "ps->ooplevel" << ps->ooplevel <<
endl;
} void display3(const student pa[], int n){
for (int i = ; i < n; i++){
cout << "pa[i].fullname" << pa[i].fullname
<< endl << "pa[i].hobby" << pa[i].hobby
<< endl << "pa[i].ooplevel" << pa[i].ooplevel
<< endl;
}
}

p259.2

 #include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
struct candybar {
char brand[];
float weight;
int calory;
};
void assign(candybar &, char br[]="millennium munch", float w=2.85, int c=);
void show_menu(candybar&); int main(){
candybar tasty;
assign(tasty);
show_menu(tasty);
system("pause");
return ;
} void assign(candybar &delicious, char br[], float w , int c){
strcpy(delicious.brand, br);
delicious.weight = w;
delicious.calory = c;
} void show_menu(candybar&delicious){
cout << delicious.brand << endl;
cout << delicious.weight << endl;
cout << delicious.calory << endl;
}

p259.4

 #include<iostream>
#include<cstdlib>
#include<cctype>
using namespace std;
struct stringy{
char* str;
int ct;
};
void set(stringy &, const char*);
void show(const stringy &, int times = );
void show(const char*, int times = ); int main(){
stringy beany;
char testing[] = "reality isn't what it used to be.";
set(beany, testing);
show(beany);
show(beany, );
delete beany.str;
testing[] = 'D';
testing[] = 'u';
show(testing);
show(testing, );
show("Done!");
system("pause");
return ;
} void set(stringy &beany, const char* testing){
int num = strlen(testing);
beany.str = new char[num + ];
strcpy(beany.str, testing);
beany.ct = num;
} void show(const stringy &beany, int times){
for (int i = ; i < times; i++)
cout << beany.str << endl
<< beany.ct << endl;
} void show(const char* testing, int times){
for (int i = ; i < times; i++)
cout << testing << endl;
}

p260.6

 #include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
template<class T>
T maxn(T ar[], int num);
template<> char *maxn(char *pt[], int num); int main(){
int num1[];
double num2[];
cout << "enter the member of two array\n";
for (int i = ; i < ; i++)
cin >> num1[i];
for (int i = ; i < ; i++)
cin >> num2[i];
int imax = maxn(num1, );
double dmax = maxn(num2, );
cout << "imax is" << imax << endl
<< "dmax is" << dmax << endl;
char *str[];
str[] = "his eyes came round";
str[] = "it all seems very unchanged";
str[] = "which contain of old the pipes";
str[] = "yes, billy, i know";
str[] = "will you be please to dine";
char *address = maxn(str, );
cout << (int*)address << " " <<
address << endl;
system("pause");
return ;
} template<class T>
T maxn(T ar[], int num){
T max=ar[];
for (int i = ; i < num; i++)
if (max < ar[i])
max = ar[i];
return max;
} template<> char *maxn(char *pt[], int num){
int length[], i;
for (i = ; i < ; i++)
length[i] = strlen(pt[i]);
int max = length[];
for (i = ; i < ; i++)
if (max < length[i])
max = length[i];
for (i = ; i < ; i++)
if (max == length[i])
break;
return pt[i];
}
上一篇:滑道可运行轨道 | ::-webkit-slider-runnable-track (Extensions) - CSS 中文开发手册 - Break易站


下一篇:java gc的考察