c++ primerPlus第六版程式設計練習第七章
阿新 • • 發佈:2018-11-17
7.1
#include<stdafx.h> #include<iostream> using namespace std; long double horm_aver(double x, double y); /*當輸入為字元時將不能繼續輸入,可以使用cin.clear()函式恢復輸入*/ int main() { double x,y; cout << "enter tow number<end to enter 0>:"; cin >> x >> y; while (x!=0&&y!=0) { long double h_a = horm_aver(x, y); cout << "the harmonic average of the numbers is: " << h_a; cout << "\nenter next two number:"; cin >> x >> y; } return 0; } long double horm_aver(double x, double y) { long double h_a; h_a = 2.0*x*y / (x + y); return h_a; }
7.2
#include<stdafx.h> #include<iostream> const int Max = 10; int input(double ar[]); double aver(double ar[], int count); void display(double ar[], int count, double aver); using namespace std; int main() { double score[Max]; int count; double average; count = input(score); average = aver(score, count); display(score, count, average); return 0; } int input(double ar[]) { int count=0; cout << "you can enter up to " << Max << "numbers<-2 to quite>"; for (int i = 0; i < Max; i++) { cout << "\n#" << i + 1 << ": "; cin >> ar[i]; if (ar[i] == -2) //以-2表示輸入結束 break; ++count; } return count; } double aver(double ar[],int count) { double sum = 0; double aver; for (int i = 0; i < count; i++) { sum = sum + ar[i]; } aver = sum / count; return aver; } void display(double ar[], int count,double aver) { cout << "your scores is :\n"; for (int i = 0; i < count; i++) { cout << ar[i] << " "; } cout << "\nyour average score is: " << aver << endl; }
7.3
#include<stdafx.h> #include<iostream> void display_value(struct box box_value); void display_adr(box *p_box); struct box { char maker[40]; float height; float width; float length; float volume; }; using namespace std; int main() { box box1 = { "BOXMAAKER",12.5,2.34,23.4,0.0 }; display_value(box1); box* p = &box1; display_adr(p); return 0; } void display_value(struct box box_value) { cout << "the member of struct box is:(transmit by value)\n"; cout << box_value.maker << "," << box_value.height << "," << box_value.width << "," << box_value.length << "," << box_value.volume << endl; } void display_adr(box *p_box) { cout << "the number of struct box is:(transmit by address)\n"; cout << p_box->maker << "," << p_box->height << "," << p_box->width << "," << p_box->length << "," << p_box->volume << endl; p_box->volume = p_box->height*p_box->length*p_box->width; cout << "volume after change: " << p_box->volume; }
7.4
#include<stdafx.h>
#include<iostream>
long double probability(unsigned numbers, unsigned picks);
using namespace std;
int main()
{
int number_f, number_s, picks_f;
cout << "enter the total field number,allowed picks and total special number\n";
while (cin >> number_f >> picks_f>>number_s)
{
long double result,result_f, result_s;
result_f = probability(number_f, picks_f);
result_s = probability(number_s, 1);
result = result_f * result_s;
cout << result << " of winning.\n";
cout << "Next three numbers(q to quit): ";
}
cout << "Bye\n";
return 0;
}
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
int n, p;
for (n = numbers,p=picks; p > 0; n--, p--)
{
result = result * p / n;
}
return result;
}
7.5
#include<stdafx.h>
#include<iostream>
long double factorial(int n);
using namespace std;
int main()
{
int n;
long double reslut;
cout << "enter the number: ";
while (cin >> n)
{
reslut = factorial(n);
cout << n <<"!="<< reslut<<endl;
cout << "enter nex number(q to quit):";
}
return 0;
}
long double factorial(int n)
{
if (n == 1 || n == 0)
return 1;
long double result = n;
result=result*factorial(--n);
return result;
}
7.6
#include<stdafx.h>
#include<iostream>
const int Max = 10;
void show_arry(double ar[], int n);
int Fill_arry(double ar[], int Max);
void Reverse_arry(double ar[], int n);
using namespace std;
int main()
{
double arry[Max];
int n;
n = Fill_arry(arry,Max);
show_arry(arry, n);
Reverse_arry(arry, n);
show_arry(arry,n);
return 0;
}
void show_arry(double ar[],int n)
{
cout << "now,the number of arry is:\n";
for (int i = 0; i < n; i++)
cout << ar[i] << endl;
}
int Fill_arry(double ar[], int Max)
{
int count = 0;
cout << "enter numbers(non-number to quit).";
for (int i = 0; cin >> ar[i] && i < Max; i++)
{
++count;
}
return count;
}
void Reverse_arry(double ar[], int n)//只需交換n/2次,最中間的數無需交換
{
double temp;
for (int i = 0, j = n - 1; i!= n / 2; i++, j--)
{
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
7.7
#include<stdafx.h>
#include<iostream>
const int Max = 5;
void show_arry(const double* ar_begin, const double* ar_end);
double* Fill_arry(double *ar_begin, int limit);
void revalue(double* ar_begin, double* ar_end, double r);
using namespace std;
int main()
{
double properties[Max];
double* ar_end = Fill_arry(properties, Max);
show_arry(properties, ar_end);
double factor;
cout << "enter revaluation factor: ";
cin >> factor;
revalue(properties, ar_end, factor);
show_arry(properties, ar_end);
cout << "Done.\n";
return 0;
}
void show_arry(const double* ar_begin,const double* ar_end)
{
const double* p = ar_begin;
cout << "now,the number of arry is:\n";
while (p!=ar_end)
{
cout <<"$"<< *p << endl;
++p;
}
}
double* Fill_arry(double *ar_begin, int limit)
{
double temp;
int i;
cout << "enter numbers(negative number to quit).\n";
for ( i = 0; ar_begin + i < ar_begin+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)
*(ar_begin + i) = temp;
else
break;
}
return ar_begin + i ;
}
void revalue(double* ar_begin, double* ar_end,double r)
{
double* p = ar_begin;
while (p!=ar_end)
{
*p *=r;
++p;
}
}
7.8
a、
#include<stdafx.h>
#include<iostream>
const int Seasons = 4;
const char*Sname[4][10] = { {"Spring"},{"Summer"},{"Fall"},{"Winter"} };
void fill(double ar[]);
void show(double ar[]);
using namespace std;
int main()
{
double exp[Seasons];
fill(exp);
show(exp);
return 0;
}
void fill(double ar[])
{
for(int i=0;i<Seasons;i++)
{
cout << "enter " << *Sname[i] << " expenses: ";
cin >> ar[i];
}
}
void show(double ar[])
{
for (int i = 0; i < Seasons; i++)
{
cout << *Sname[i] << ": $" << ar[i]<<endl;
}
}
b、
#include<stdafx.h>
#include<iostream>
const int Seasons = 4;
const char*Sname[4][10] = { {"Spring"},{"Summer"},{"Fall"},{"Winter"} };
void fill(struct exp *p);
void show(struct exp *p);
struct exp
{
double coast;
};
using namespace std;
int main()
{
struct exp expenses[Seasons] ;
fill(expenses);
show(expenses);
return 0;
}
void fill(struct exp *p)
{
for(int i=0;i<Seasons;i++)
{
cout << "enter " << *Sname[i] << " expenses: ";
cin >> (p+i)->coast;
}
}
void show(struct exp *p)
{
for (int i = 0; i < Seasons; i++)
{
cout << *Sname[i] << ": $" << (p+i)->coast<<endl;
}
}
7.9
#include<stdafx.h>
#include<iostream>
const int SLEN = 30;
struct student{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n);
void display1(struct student stu);
void display2(struct student *p);
void display3(struct student *p_f, int n);
using namespace std;
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 count = 0;
for (int i = 0; i < n; i++)
{
cout << "enter the information of student #" << i + 1 << endl;
cout << "enter full name: ";
cin.getline(pa[i].fullname, SLEN);
cout << "enter hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "enter ooplevel: ";
cin >> pa[i].ooplevel;
while (cin.get() != '\n')//用於當輸入學生資訊ooplevel最後多輸入的非數字字元
continue;
++count;
}
return count;
}
void display1(struct student stu)
{
cout << "full name: " << stu.fullname
<< ",hobby: " << stu.hobby
<< ",ooplevel: " << stu.ooplevel << endl;
}
void display2(struct student *p)
{
cout << "full name: " << p->fullname
<< ",hobby: " << p->hobby
<< ",ooplevel: " << p->ooplevel << endl;
}
void display3(struct student *p_f,int n)
{
for (int i = 0; i < n; i++)
{
cout << "full name: " << (p_f+i)->fullname
<< ",hobby: " << (p_f+i)->hobby
<< ",ooplevel: " << (p_f+i)->ooplevel << endl;
}
}
7.10
#include<stdafx.h>
#include<iostream>
double calculate(double x, double y, double(*p)(double x, double y));
double add(double x, double y);
double multip(double x, double y);
using namespace std;
int main()
{
double x, y;
double(*p)(double, double);
cout << "enter two number to calculate: ";
while (cin>>x>>y)
{
p = add;
cout << x << "+" << y << "=" << calculate(x, y, p)<<endl;
p = multip;
cout << x << "*" << y << "=" << calculate(x, y, p)<<endl;
cout << "enter next two number(q to quit): ";
}
return 0;
}
double calculate(double x, double y, double(*p)(double x, double y))
{
return(p(x, y));
}
double add(double x, double y)
{
return x + y;
}
double multip(double x, double y)
{
return x * y;
}