#include "stdafx.h"
#include
#include
#include
#include
const int SIZE = 101;
using namespace std;
double tiaohe(double, double);
double tiaohe(double x, double y)
{
double z;
z = 2.0*x*y / (x + y);
return z;
}
void Xiti1()
{
double d1, d2;
while (cin>>d1>>d2&&(d1!=0||d2!=0))
{
cout << "调和平均数为:"<<tiaohe(d1, d2) << endl;
}
return;
}
int input(double chengji[], int time);
double avge(double chengji[],int time);
void display(double chengji[], int time, double avge);
const int SIZE1 = 10;
int input(double chengji[],int time)
{
int i;
for ( i = 0; i < time;i++)
{
if(!(cin >> chengji[i])) break;
}
return i;
}
double avge(double chengji[], int time)
{
int i;
double sum=0;
for ( i = 0; i < time; i++)
{
sum += chengji[i];
}
return sum / time;
}
void display(double chengji[], int time, double avge)
{
int i;
for ( i = 0; i < time; i++)
{
cout << chengji[i]<<" ";
}
cout <<"平均值:" <<avge << endl;
}
void Xiti2()
{
double *grade = new double[SIZE1];
double average;
int time=SIZE1;
time=input(grade,time);
average = avge(grade,time);
display(grade,time,average);
return;
}
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void display(box bb);
void display(box bb)
{
cout << "高度:"<<bb.height<<endl<<"长度:" << bb.length<<endl<<"宽度:" << bb.width <<"体积:"<< bb.volume<<endl;
}
void tiji(box*point);
void tiji(box*pp)
{
cout << "输入长度:";
cin >> pp->length;
cout << "输入宽度:";
cin >> pp->width;
cout << "输入高度:";
cin >> pp->height;
pp->volume = pp->height*pp->length*pp->width;
}
void Xiti3()
{
box changfang;
tiji(&changfang);
display(changfang);
return;
}
double jilv(unsigned number, unsigned pick);
double jilv(unsigned number, unsigned pick)
{
double result=1;
unsigned m, p;
for (m = number,p=pick; p >0;m--,p--)
{
result = result*m / p;
}
return result;
}
void Xiti4()
{
cout << "输入第一组号码总数和选取的数:";
unsigned total1, piack1, total2, piack2;
double jj,kk;
while ((cin >> total1 >> piack1 >> total2>> piack2)&&piack1 <= total1)
{
jj = jilv(total1, piack1);
kk = jilv(total2, piack2);
cout<<"获奖几率:"<<jj*kk<<"中选1";
}
return;
}
double jiecheng(int);
double jiecheng(int num)
{
if (num < 2) return 1;
else return num*jiecheng(num - 1);
}
void Xiti5()
{
int money;
while (cin >> money)
{
cout <<money<< "的阶乘为:" << jiecheng(money) << endl;
}
return;
}
int Fill_array(double arr[],int size);
int Fill_array(double arr[], int size)
{
int i=0;
cout << "输入" << size << "个double型数据\n";
for ( i = 0; i < size; i++)
{
if (cin >> arr[i]&& (i != size - 1))
{
cout << "还需要输入" << size - 1 - i << "个double数据\n";
}
}
return i;
}
void Show_array(double arr[], int size);
void Show_array(double arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i]<<" ";
}
cout << endl;
}
void Reverse_array(double arr[], int size);
void Reverse_array(double arr[], int size)
{
double temp;
for (int i = 1; i < size/2; i++)
{
temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
}
void Xiti6()
{
cout << "输入数组的长度:\n";
int num;
if (cin >> num)
{
double * pp = new double[num];
cin.get();
cout << "一共" << Fill_array(pp, num)<<"个数据。";
Show_array(pp, num);
cout << "翻转一下子啊:\n";
Reverse_array(pp, num);
Show_array(pp, num);
}
else cout << "输入不规范!\n";
return;
}
double* fill_array1(double ar[], int limit);
void show_array(const double ar[], double*lastpoint);
void revalue(double r, double ar[], double*lastpoint);
const int Max = 5;
double* fill_array1(double ar[], int limit)
{
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;
}
void show_array(const double ar[], double*lastpoint)
{
using namespace std;
for (int i = 0; i < lastpoint-ar; i++)
{
cout << "Property #" << (i + 1) << ": $";
cout << ar[i] << endl;
}
}
void revalue(double r, double ar[], double*lastpoint)
{
for (int i = 0; i < lastpoint - ar; i++)
ar[i] *= r;
}
void Xiti7()
{
double properties[Max];
double*size= fill_array1(properties, Max);
show_array(properties, size);
if (size > 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(factor, properties, size);
show_array(properties, size);
}
cout << "Done.\n";
return;
}
const int Seasons = 4;
const char*Snames[Seasons]= { "Spring", "Summer", "Fall", "Winter" };
struct mys
{
double mypp[Seasons];
};
void fill(mys *shuju);
void show(mys *shuju);
void fill(mys *shuju)
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> shuju->mypp[i];
}
}
void show(mys *shuju)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << shuju->mypp[i] << '\n';
total += shuju->mypp[i];
}
cout << "Total: $" << total << '\n';
}
void Xiti8()
{
mys expenses;
fill(&expenses);
show(&expenses);
return;
}
const int SLEN = 30;
struct student {
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n);
int getinfo(student pa[], int n)
{
int i;
for ( i = 0; i < n; i++)
{
cout << "输入第" << i + 1 << "个学生的信息\n";
cout << "姓名:";
cin.getline(pa[i].fullname, SLEN);
cout << "爱好:";
cin.getline(pa[i].hobby, SLEN);
cout << "ooplevel:";
cin >> pa[i].ooplevel;
cin.get();
}
return i;
}
void diaplay1(student st);
void diaplay1(student st)
{
cout << "display1 学生姓名:" << st.fullname << endl;
cout << "display1 爱好:" << st.hobby << endl;
cout << "display1 ooplevel:" << st.ooplevel << endl;
}
void display2(const student * ps);
void display2(const student * ps)
{
cout << "display2 学生姓名:" << ps->fullname << endl;
cout << "display2 爱好:" << ps->hobby << endl;
cout << "display2 ooplevel:" << ps->ooplevel << endl;
}
void display3(const student pa[], int n);
void display3(const student pa[], int n)
{
for (const student* stu_point = pa; stu_point != pa + n; stu_point++)
{
cout << "display3 学生姓名:" << stu_point->fullname << endl;
cout << "display3 爱好:" << stu_point->hobby << endl;
cout << "display3 ooplevel:" << stu_point->ooplevel << endl;
}
}
void Xiti9()
{
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++)
{
diaplay1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete[] ptr_stu;
cout << "Done\n";
return;
}
double add(double x, double y);
double mul(double x, double y);
double sub(double x, double y);
double div(double x, double y);
double calculate(double x, double y,double (*ppp)(double,double));
double add(double x, double y)
{
return x + y;
}
double mul(double x, double y)
{
return x*y;
}
double sub(double x, double y)
{
return x - y;
}
double div(double x, double y)
{
return x / y;
}
double calculate(double x, double y, double(*ppp)(double, double))
{
return ppp(x,y);
}
void Xiti10()
{
cout << "请输入2个数字用空格隔开:\n";
double x, y;
char choose;
while (cin >> x >> y)
{
cin.get();
cout << "请输入运算符:+-*/\n";
if (cin.get(choose))
{
switch (choose)
{
case '+': cout << x << choose << y << "=" << calculate(x, y, add) << endl;
break;
case '-': cout << x << choose << y << "=" << calculate(x, y, sub) << endl;;
break;
case '*': cout << x << choose << y << "=" << calculate(x, y, mul) << endl;;
break;
case '/': cout << x << choose << y << "=" << calculate(x, y, div) << endl;;
break;
default:
cout << "输入错误!\n";
break;
}
}
cout << "请输入2个数字用空格隔开:\n";
}
return;
}
void Xiti10_2()
{
cout << "请输入2个数字用空格隔开:\n";
double x, y;
double(*pf[4])(double, double) = {add,sub,mul,div};
while (cin >> x >> y)
{
cin.get();
for (int i = 0; i < 4; i++)
{
cout << calculate(x, y, pf[i])<<endl;
}
cout << "请输入2个数字用空格隔开:\n";
}
return;
}
int main()
{
int XitiNo = 1;
cout << "第四章编程练习答案:\n";
cout << "请输入习题编号或按(0)退出:\n";
cin >> XitiNo;
while (XitiNo != 0)
{
if (cin)
{
if (XitiNo >= 0 && XitiNo <= 10)
{
switch (XitiNo)
{
case 0:
{
cout << "即将退出程序啦!\n";
}break;
case 1:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti1();
}break;
case 2:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti2();
}break;
case 3:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti3();
}break;
case 4:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti4();
}break;
case 5:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti5();
}break;
case 6:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti6();
}break;
case 7:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti7();
}break;
case 8:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti8();
}break;
case 9:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti9();
}break;
case 10:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti10_2();
}break;
default:
{
cout << "即将退出程序啦\n";
XitiNo = 0;
}
break;
}
cout << "继续查看请输入习题编号或按(0)退出:\n";
cin >> XitiNo;
}
else
{
cout << "没有这样的编号\n继续查看请输入习题编号或按(0)退出:\n";
cin >> XitiNo;
}
}
else
{
XitiNo = 0;
}
}
system("pause");
return 0;
}