C++ Primer Plus(第六版)第七章课后习题

C++ Primer Plus(第六版)第七章课后习题

7.13.1

#include
double even(double x,double y);
int main()
{
using namespace std;
double x,y;
double z;
cout << "Please enter two numbers: ";
cin >> x >> y;
while(x!=0&&y!=0)
{
z=even(x,y);
cout << "The average value of x,y is " << z << endl;
cout << "Please enter two numbers: ";
cin >> x >> y;
}
cout << “Done! You entered at least one 0!”<<endl;
return 0;
}
double even(double x,double y)
{
double z=2.0xy/(x+y);
return z;
}

7.13.2

#include
const int Max=10;
using namespace std;
int input(double grade[]);
void show_grade(const double grade[],int times);
void average(double grade[],int times);
int main()
{
double *grade;
int times=input(grade);
show_grade(grade,times);
average(grade,times);
return 0;
}
int input(int grade[])
{
cout << "Enter the grades of golf game: "<<endl;
int j=0;
for (int i=0;i<Max;i++)
{
cout << “Please enter #” << i+1 << " grade of golf game: “;
if(cin>>grade[i])
{
j++;
continue;
}
else break;
}
return j;
}
void show_grade(const double grade[],int times)
{
for(int i=0;i<times;i++)
{
cout << " Grade of #” << i+1 << " in golf game: " << grade[i]<< endl;
}
}
void average(double grade[],int times)
{
double sum=0,mean_grade;
for(int i=0;i<times;i++)
{
sum+=grade[i];
}
mean_grade=sum/times;
cout << "The average value of golf grades is " << mean_grade <<endl;
}

7.13.3.a

#include
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(box a);
int main()
{
box a;
cout << "Please enter the maker of box: ";
cin.getline(a.maker,40);
cout << "Please enter heigh, width, length: ";
cin >> a.height >> a.width >> a.length;
a.volume=a.heighta.widtha.length;
show(a);
return 0;
}
void show(box a)
{
cout << "The maker of box is "<< a.maker <<endl;
cout << "Height, width and length of box are " <<
a.height << “,” << a.width << “,” << a.length << endl;
cout << "Volume of box is " << a.volume << endl;
}

7.13.3.a

#include
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(box a);
int main()
{
box a;
cout << "Please enter the maker of box: ";
cin.getline(a.maker,40);
cout << "Please enter heigh, width, length: ";
cin >> a.height >> a.width >> a.length;
a.volume=a.height
a.width*a.length;
box *b=&a;
show(b);
return 0;
}
void show(box *a)
{
cout << "The maker of box is "<< a->maker <<endl;
cout << "Height, width and length of box are " <<
a->height << “,” << (*a) .width << “,” << a->length << endl;
cout << "Volume of box is " << a->volume << endl;
}

7.13.4

#include
double possibility(double number,double pick);
int main()
{
using namespace std;
double total,choice;
double chance1,chance2,chance;
cout << “Enter the total number of choice on the game card and \n”
<< " the number of picks allowed:\n";
while(cin >> total >> choice && choice < total)
{
chance1=possibility(total, choice);
cout << “Enter the second total number of choice on the game card and \n”
<< " the number of picks allowed:\n";
if(cin >> total >> choice && choice < total)
{
chance2=possibility(total,choice);
chance=chance1*chance2;
cout << “You have one chance in " << chance << " of winning.\n”;
cout << “Enter the total number of choice on the game card and \n”
<< " the number of picks allowed:\n";
continue;
}
else
break;
}
cout << “Bye!\n”;
return 0;
}
double possibility(double number,double pick)
{
double result;
double i=number,j=pick;
for(;j>0;i–,j–)
result=j/i;
return result;
}

7.13.5

#include
double jiech(int number);
double jieche(int number);
int main()
{
using namespace std;
cout << “Please enter the number you want to !”;
int number;
double result;
while(cin >> number)
{
result=jiech(number);
cout << "The result of "<< number << "! is " << result << endl;
cout << “Please enter the number you want to !”;
}
return 0;
}
double jiech(int number)
{
double result;
if(number1||number0)
result=1;
else
result=numberjiech(number-1);
return result;
}
double jieche(int number)
{
double result=1;
if (number0||number1)
result=1;
if (number> 2)
for(int j=2;j<=number;j++)
{
result=result
j;
}
return result;
}

7.13.6

#include
const int Arsize=5;
using namespace std;
int Fill_array(double ch[],int i);
void Show_array(double ch[],int i);
void Reverse_array(double ch[],int i);
int main()
{
double ch[Arsize];
int number=Fill_array(ch,Arsize);
Show_array(ch,number);
Reverse_array(ch,number);
cout << "The reverse array is " << endl;
Show_array(ch,number);
return 0;
}
int Fill_array(double ch[],int i)
{
int j=0;
cout << “Please enter the numbers you want to store:”;
while(cin >> ch[j])
{
j++;
if(j==i)
break;
cout << “Please enter the numbers you want to store:”;
}
return j;
}
void Show_array(double ch[],int i)
{
for(int j=0;j<i;j++)
cout << “#” << j+1 << " is " << ch[j] <<endl;
}
void Reverse_array(double ch[],int i)
{
for(int j=0;j<i;j++,i–)
{
double temp=ch[j];
ch[j]=ch[i-1];
ch[i-1]=temp;
}
}

7.7(此处提供7.13.7需要的程序7.7)

#include
const int Max=5;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void revalue(double r, double ar[], int n);
int main()
{
using namespace std;
double properties[Max];
int size=fill_array(properties, Max);
show_array(properties, size);
if(size>0)
{
cout << "Enter revaluation factor: ";
double factor;
while(!(cin >> factor))
{
cin.clear();
while(cin.get()!=’\n’)
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << “Done.\n”;
cin.get();
cin.get();
return 0;
}

int fill_array(double ar[], int limit)
{
using namespace std;
double temp;
int i;
for(i=0;i<limit;i++)
{
cout << “Enter value #” << i+1 << ": ";
cin >> temp;
if(!cin)
{
cin.clear();
while(cin.get()!=‘n’)
continue;
cout << “Bad input; input process terminated.\n”;
break;
}
else if(temp<0)
break;
ar[i]=temp;
}
return i;
}

void show_array(const double ar[], int n)
{
using namespace std;
for(int i=0;i<n;i++)
{
cout << “Property #” << i+1 << “: $”;
cout << ar[i] << endl;
}
}

void revalue(double r, double ar[], int n)
{
for(int i=0;i<n;i++)
{
ar[i]*=r;
}
}

7.13.7

#include
const int Max=5;
double * fill_array(double ar[], int limit);
void show_array(const double ar[], double * n);
void revalue(double r, double ar[], double * n);
int main()
{
using namespace std;
double properties[Max];
double * size=fill_array(properties, Max);
show_array(properties, size);
if(size>0)
{
cout << "Enter revaluation factor: ";
double factor;
while(!(cin >> factor))
{
cin.clear();
while(cin.get()!=’\n’)
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << “Done.\n”;
cin.get();
cin.get();
return 0;
}

double * fill_array(double ar[], int limit)
{
using namespace std;
double temp;
int i;
for(i=0;i<limit;i++)
{
cout << “Enter value #” << i+1 << ": ";
cin >> temp;
if(!cin)
{
cin.clear();
while(cin.get()!=‘n’)
continue;
cout << “Bad input; input process terminated.\n”;
break;
}
else if(temp<0)
break;
ar[i]=temp;
}
return &ar[i-1];
}

void show_array(const double ar[], double * n)
{
using namespace std;
int i=0;
for(;&ar[i]!=n;i++)
{
cout << “Property #” << i+1 << “: $”;
cout << ar[i] << endl;
}
cout << “Property #” << i+1 << “: $”;
cout << ar[i] << endl;
}

void revalue(double r, double ar[], double * n)
{
int i=0;
for(;&ar[i]!=n;i++)
{
ar[i]=r;
}
ar[i]
=r;
}

7.15(此处提供7.13.8需要的程序7.15)

#include
#include
#include
const int Seasons=4;
const std::array<std::string, Seasons> Snames={“Spring”, “Summer”, “Fall”, “Winter”};
void fill(std::array<double,Seasons> *pa);
void show(std::array<double, Seasons> da);
int main()
{
std::array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}

void fill(std::array<double, Seasons> *pa)
{
using namespace std;
for(int i=0;i<Seasons;i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}

void show(std::array<double, Seasons> da)
{
using namespace std;
double total=0.0;
cout << “\nEXPENSES\n”;
for(int i=0;i<Seasons;i++)
{
cout << Snames[i] << “: $” << da[i] << endl;
total+=da[i];
}
cout << “Total Expenses: $” << total << endl;
}

7.13.8.a

#include
#include
#include
const int Seasons=4;
const char* Snames[Seasons]={“Spring”, “Summer”, “Fall”, “Winter”};
void fill(double pa[]);
void show(double da[]);
int main()
{
double expenses[Seasons];
fill(expenses);
show(expenses);
return 0;
}

void fill(double pa[])
{
using namespace std;
for(int i=0;i<Seasons;i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pa[i];
}
}

void show(double da[])
{
using namespace std;
double total=0.0;
cout << “\nEXPENSES\n”;
for(int i=0;i<Seasons;i++)
{
cout << Snames[i] << “: $” << da[i] << endl;
total+=da[i];
}
cout << “Total Expenses: $” << total << endl;
}

7.13.8.b

#include
#include
#include
struct Expense
{
double num;
};
const int Seasons=4;
const char* Snames[Seasons]={“Spring”, “Summer”, “Fall”, “Winter”};
void fill(Expense pa[]);
void show(Expense da[]);
int main()
{
Expense expenses[Seasons];
fill(expenses);
show(expenses);
return 0;
}

void fill(Expense pa[])
{
using namespace std;
for(int i=0;i<Seasons;i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> pa[i].num;
}
}

void show(Expense da[])
{
using namespace std;
double total=0.0;
cout << “\nEXPENSES\n”;
for(int i=0;i<Seasons;i++)
{
cout << Snames[i] << “: $” << da[i].num << endl;
total+=da[i].num;
}
cout << “Total Expenses: $” << total << endl;
}

7.13.9

#include
using namespace std;
const int SLEN=30;
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=0;i<entered;i++)
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete [] ptr_stu;
cout << “Done\n”;
return 0;
}

int getinfo(student pa[], int n)
{
int i=0;
for(;i<n;i++)
{
cout << “#” << i+1 << ": " << endl;
cout << "Enter fullname: ";
cin.getline(pa[i].fullname, SLEN);
if(strlen(pa[i].fullname)==0)
break;
cout << "Enter hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Enter ooplevel: ";
cin >> pa[i].ooplevel;
cin.get();
}
return i;
}

void display1(student st)
{
cout << "Display1: " << endl;
cout << "Fullname: " << st.fullname << endl;
cout << "Hobby: " << st.hobby << endl;
cout << "Opplevel: " << st.ooplevel << endl;
cout << endl;
}
void display2(const student *ps)
{
cout << "Display2: " << endl;
cout << "Fullname: " << ps->fullname << endl;
cout << "Hobby: " << ps->hobby << endl;
cout << "Opplevel: " << ps->ooplevel << endl;
cout << endl;
}
void display3(const student pa[], int n)
{
for(int i=0;i<n;i++)
{
cout << "Display3: " << endl;
cout << "Fullname: " << pa[i].fullname << endl;
cout << "Hobby: " << pa[i].hobby << endl;
cout << "Opplevel: " << pa[i].ooplevel << endl;
cout << endl;
}
}

7.13.10

#include
double calculate(double,double,double (*p)(double,double));
double add(double, double);
int main()
{
using namespace std;
cout << “Enter x, y:”;
double x,y;
while (cin >> x >> y)
{
double q=calculate(x,y,add);
cout << "The sum of x,y is " << q <<endl;
cout << “Enter x, y:”;
}
cout <<“You entered wrong !”;
return 0;
}
double calculate(double x,double y,double (*p)(double,double))
{
double i=p(x,y);
return i;
}
double add(double x,double y)
{
return x+y;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值