c++ Primer Plus(习题7.1)
//用户输入啷个数,返回调和平均数
//其中一个为0结束程序
#include<iostream>
using namespace std;
double t_average(double a, double b);
int main()
{
cout << "Please two interger: ";
double x, y;
while (cin >> x && cin >> y) //如果输入非数字,结束程序
{
double average;
if (x == 0 || y == 0)
break;
else
average=t_average(x, y);
cout << average << " is the harmonic mean,accodrind input.\n";
cin.get();
cout << "Please two interger: ";
}
cout << "Bye!\n";
return 0;
}
double t_average(double a, double b)
{
return (2.0 * a* b / (a + b));
}
c++ Primer Plus(习题7.2)
//用函数处理用户输入的10个成绩数据,可提前结束
//三个函数解决输入,输出,还有报告平均值
#include<iostream>
const int num = 10;
using namespace std;
int getscore(double a[]);
void show(double a[], int n);
void average(const double a[], int n);
int main()
{
cout << "Enter 10 golf scores(q to end input):\n\a";
double score[num];
int count;
count=getscore(score);
show(score,count);
average(score,count);
cout << "Bye!\n";
return 0;
}
int getscore(double a[])
{
int count = 0; //用来记录用户输入的成绩个数
for (int i = 0; i < num; i++,count++)
if (!(cin >> a[i]))
break;
return count;
}
void show(double a[], int n)
{
cout << "Scores: ";
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << "\nShow down!\n";
}
void average(const double a[], int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
cout << "The average is " << sum / n << endl;
}
c++ Primer Plus(习题7.3)
//两个函数,一个按值传递结构,一个按指针
//并且设置结构成员的值
#include<iostream>
using namespace std;
struct box {
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(const box a); //先声明结构,再声明函数
void set_v(box *a);
int main() //总是把main写错,无语了
{
box test = { "China",40,10,20,0 };
cout << "Resource:\n";
show(test);
set_v(&test);
cout << "After set volume: \n";
show(test);
cout << "Bye!\n";
return 0;
}
void show(const box a)
{
cout << "Maker: " << a.maker << endl
<< "Height: " << a.height << endl
<< "Wigth: " << a.width << endl
<< "Length: " << a.length << endl
<< "Volume: " << a.volume << endl;
}
void set_v(box *a)
{
a->volume = a->height * a->length * a->width;
}
c++ Primer Plus(习题7.4)
//和书上的计算中奖率的差不多,多了一个参数而已
//这中奖率确实低
//不过是正数,好奇怪,应该是0.000000000这样的
#include<iostream>
long double probability(unsigned numbers, unsigned picks);
int main()
{
using namespace std;
double total, choices;
cout << "Enter the total number of choices on the game card and\n"
"the number of picks allowed:\n";
cout << "Choose 1-47:\n";
while((cin>>total>>choices)&&choices<=total)
{
long double pro=0.0;
cout << "And choose 1-27: ";
pro = probability(total, choices);
cin >> total >> choices;
pro += probability(total, choices);
cout << "You have one chance in ";
cout << pro;
cout << "\n Next,between 1-47:\n";
}
cout << "Bye!\n";
return 0;
}
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p > 0; n--, p--)
result = result*n / p;
return result;
}
c++ Primer Plus(习题7.5)
//使用递归的典型例子,求阶乘
//这阶乘增加的非常快
#include<iostream>
long long factorial(long int n);
int main()
{
using namespace std;
long long result,num;
cout << "Enter a interger(q exit): ";
while (cin >> num)
{
result = factorial(num);
cout << num << "!= " << result << endl;
cout << "Next···(q to exit)\n";
}
return 0;
}
long long factorial(long int n)
{
long long result;
if (n == 0)
return 1;
else if (n > 0)
result = factorial(n-1)*n;
return result;
}
c++ Primer Plus(习题7.6)
//三个函数,一个填充double数组,一个显示数组,一个反转数组
#include<iostream>
const int S = 10;
using namespace std;
int Fill_array(double a[], int n);
void show_array(const double a[], int n);
void reverase_array(double a[], int n);
int main()
{
double num[S];
int counts;
cout << "Enter 10 double value to fill array:\n";
counts=Fill_array(num, S);
cout << "Resource array:\n";
show_array(num, counts);
reverase_array(num, counts);
cout << "After reverace:\n";
show_array(num, counts);
cout << "Bye!\n";
return 0;
}
int Fill_array(double a[], int n)
{
int cout = 0;
for (int i = 0; i < n; i++, cout++)
if (!(cin >> a[i]))
break;
return cout;
}
void show_array(const double a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
void reverase_array(double a[], int n)
{
double temp;
for (int i = 1; i < n / 2; i++)
{
temp = a[i]; //就这里要稍微思考下
a[i] = a[n-i-1];
a[n-i-1] = temp;
}
}
c++ Primer Plus(习题7.7)
//改写程序清单7.7的程序,用指针表示区间
//fill_array返回一个指向最后填充位置的指针
#include<iostream>
const int Max = 5;
double *fill_array(double ar[], int limit);
void show_array(const double *begin, const double *end);
void revalue_array(double r,double *begin, double *end);
int main()
{
using namespace std;
double properties[Max];
double *end = fill_array(properties, Max);
show_array(properties, end);
if (end != properties) //两个指针的比较,额,没办法了
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin>>factor)) //处理错误输出
{
cin.clear();
while (cin.get()!='\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue_array(factor, properties, end);
show_array(properties, end);
}
cout << "Done!\n";
cin.get();
cin.get();
return 0;
}
double *fill_array(double ar[], int limit)
{
using namespace std;
double temp;
double *p = ar;
for (int i = 0; i < limit; i++,p++) //指针的自增
{
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 p;
}
void show_array(const double *begin, const double *end)
{
const double *temp;
using namespace std;
int i = 1; //用于序号
for (temp = begin; temp!= end; temp++,i++)
{
cout << "Property #" << i << ": $";
cout << *temp << endl;
}
}
void revalue_array(double r, double *begin, double *end)
{
for (; begin != end; begin++)
*begin = r*(*begin);
}
c++ Primer Plus(习题7.8)
//又是改写程序,程序清单7.15,
//a.要求不用array类
//b.用一个结构来存储值
#include<iostream>
struct Expense
{
double money;
};
const int Seasons = 4;
const char *Sname[Seasons] = { "Spring","Sunmer","Fall","Winter" };
void fill(double ar[]);
void fill_2(Expense *p);
void show(const double ar[]);
void show_2(const Expense *p);
int main()
{
using std::cout;
double money[Seasons];
Expense moey[Seasons];
fill(money);
show(money);
cout << "******The method using stryuct******\n";
fill_2(&moey[0]);
show_2(&moey[0]);
cout << "Bye!\n";
return 0;
}
void fill(double ar[])
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Sname[i] << " exprnses: ";
cin >> ar[i];
}
}
void fill_2(Expense *p)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Sname[i] << " exprnses: ";
cin >> p[i].money;
}
}
void show(const double ar[])
{
using namespace std;
double total = 0.0;
cout << "EXPENCES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << ": $" << ar[i] << endl;
total += ar[i];
}
cout << "Total Expence: $" << total << endl;
}
void show_2(const Expense *p)
{
using namespace std;
double total = 0.0;
cout << "EXPENCES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Sname[i] << ": $" << p[i].money << endl;
total += p[i].money;
}
cout << "Total Expence: $" << total << endl;
}
c++ Primer Plus(习题7.9)
//填空题,牛逼的人写框架,水平不到的人来实现啊
#include<iostream>
using namespace std;
const int SLEN = 30;
struct Student {
char fullname[SLEN];
char hobby[SLEN];
int coplevel;
};
int getinfo(Student pa[], int n);
void display1(Student st);
void display2(const Student*ps);
void display3(const Student pa[], int n); //相当于*pa
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 (; count < n; count++)
{
cout << "Enter fullname: "; //混合输入字符和数字,要处理换行符
cin.getline(pa[count].fullname,SLEN);
cout << "Hobby: ";
cin.getline(pa[count].hobby, SLEN);
cout << "Coplevel: ";
cin >> pa[count].coplevel;
cin.get(); //换行符
}
return count;
}
void display1(Student st)
{
cout << "Fullname: " << st.fullname << endl
<< "Hobby: " << st.hobby << endl
<< "Coplevel: " << st.coplevel << endl;
cout << "**********disply1 finish!***************\n";
}
void display2(const Student*ps)
{
cout << "Fullname: " << ps->fullname << endl
<< "Hobby: " << ps->hobby << endl
<< "Coplevel: " << ps->coplevel << endl;
cout << "**********disply2 finish!***************\n";
}
void display3(const Student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "Fullname: " << pa->fullname << endl
<< "Hobby: " << pa->hobby << endl
<< "Coplevel: " << pa->coplevel << endl;
}
cout << "**********disply3 finish!***************\n ";
}
c++ Primer Plus(习题7.10)
//函数来处理两个数,函数调用函数,有两种方法
//1.用函数指针
//2.用函数名调用
#include<iostream>
double calculate(double a, double b, const double(*p)(double x, double y)); //用指向函数的指针
const double add(double a, double b); //+
const double plu(double a, double b); //*
const double sub(double a, double b); //-
const double div(double a, double b); // /
int main()
{
using namespace std;
const double (*p[4])(double, double) = {add,sub,plu,div};
const char *str[4] = { "x + y","x - y", "x * y", "x / y" };
cout << "Enter two double value(q to exit): ";
double x, y;
while (cin >> x >> y)
{
for (int i = 0; i < 4; i++)
{
cout << str[i] << " is " << calculate(x, y, p[i]) << endl;;
}
cout << "Next or q to exit: ";
}
cout << "Bye!\n";
return 0;
}
double calculate(double a, double b, const double(*p)(double x, double y))
{
return p(a, b); //给函数传递参数
}
const double add(double a, double b)
{
return a + b;
}
const double plu(double a, double b)
{
return a * b;
}
const double sub(double a, double b)
{
return a - b;
}
const double div(double a, double b)
{
return a / b;
}