第七章
#include <iostream>
using namespace std;
//符号常量
const int Golf_Num = 10;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
//函数原型
double average_0(double,double);
void arr_input(double arr[Golf_Num], int* size);
void arr_display(const double arr[], const int size);
void arr_average(const double arr[], const int size);
void box_inital(void);
void lotto(void);
void compute(void);
void array_plus();
void house(void);
void season_1(void);
void season_2(void);
void excer(void);
void cal_plus(void);
//主函数
int main()
{
//--------作业一---
double input_m, input_n;
double average = 0;
cout << "Enter two numbers(0 to quit) : ";
while (!(cin >> input_m >> input_n))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input! Please input again!\n";
}
while (input_m != 0 && input_n != 0)
{
average = average_0(input_m, input_n);
cout << "input_m: " << input_m << " input_n: " << input_n << endl;
cout << "average: " << average << endl;
cout << "Enter two numbers(0 to quit) : ";
while (!(cin >> input_m >> input_n))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input! Please input again!\n";
}
}
cout << "Done!\n";
//----------作业二---------
double array[Golf_Num];
int size;
arr_input(array, &size);
arr_display(array, size);
arr_average(array, size);
//-------------作业三------------
box_inital();
//----------------作业四------------------
lotto();
//------------作业五-------------------
compute();
//-------------作业六---------------------
array_plus();
//-------------作业七-----------------------
house();
//-----------作业八-------------
season_1();
season_2();
//-------------作业九---------------
excer();
//----------作业十---------------
cal_plus();
return 0;
}
//函数定义
//------------------作业一-------------------
double average_0(double input_m,double input_n)
{
double average = 0;
average = 2.0 * input_m * input_n / (input_m + input_n);
return average;
}
//--------------------作业二-----------------------
//读取
void arr_input(double arr[Golf_Num],int* size)
{
for (int i = 0;i < Golf_Num;i++)
{
cout << "Enter the scores # "<<i+1<<":";
while (!(cin >> arr[i]))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input!Please enter again!\n";
cout << "Enter the scores # " << i + 1 << ":";
}
if (arr[i] < 0)
{
arr[i] = 0;
*size = i;
break;
}
}
return;
}
//显示
void arr_display(const double arr[],const int size)
{
for (int i = 0;i < size;i++)
cout << arr[i] << "\t";
cout << endl;
return;
}
//计算平均成绩
void arr_average(const double arr[], const int size)
{
double sum = 0;
for (int i = 0;i < size;i++)
{
sum += arr[i];
}
cout << "average: " << sum / size << endl;
return ;
}
//------------------------作业三---------------
void box_value(box aa)
{
for (int i = 0;aa.maker[i] != '\0';i++)
cout << aa.maker[i];
cout << endl;
cout << aa.height << endl;
cout << aa.width<< endl;
cout << aa.length << endl;
cout << aa.volume << endl;
return;
}
void box_addr(box* aa)
{
aa->volume = (aa->height) * (aa->length) * (aa->width);
cout << endl << aa->volume << endl;
}
void box_inital(void)
{
box apple = { "apple",2,2,2,0 };
box_value(apple);
box_addr(&apple);
return;
}
//------------------作业四-----------------------------
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
//long double n;
unsigned n;
unsigned p;
//采用交替运算乘除,可以避免中间结果超出最大的浮点数
for (n = numbers, p = picks;p > 0;n--, p--)
result = result * n / p;
return 1.0/result;
}
void lotto(void)
{
//都是正数,因此可用unsigned
long double result = 1.0;
result = probability(47, 5) * probability(27, 1);
cout << "result: " << result << endl;
return ;
}
//-------------------------作业五------------------
//递归!!!
long Factorial(int n)
{
while (n > 0)
{
if (n == 1||n==0)
return 1;
else
return n* Factorial(n - 1);
}
return 0;//会提示不是所有控件都有返回值
}
void compute(void)
{
cout << "Enter the num: ";
int num = 0;
while (!(cin >> num))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Error,please enter again: ";
}
long result = Factorial(num);
cout << "reslut: " << result << endl;
return;
}
//----------------------作业六--------------------------------
const int Size = 10;
int fill_array(double* arr, int* size)
{
cout << "Enter the double: " << endl;
for (int i = 0;i < Size;i++)
{
cout << " # " << i + 1 << " : ";
if (!(cin >> arr[i]))
break;
*size = i+1;
}
return *size;
}
void show_array(double* arr,int size)
{
for (int i = 0;i < size;i++)
cout << arr[i];
return;
}
void reverse_array(double* arr, int size)
{
int i, j;
double temp = 0.0;
for (i = 0, j = size - 1;i < j;i++, j--)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return;
}
void array_plus()
{
double arr[Size];
int num = 0;
num = fill_array(arr, &num);
show_array(arr, num);
reverse_array(arr, num);
cout << "------------------" << endl;
show_array(arr, num);
return;
}
//-------------------------------作业七--------------------------
//填充数组
const int Max = 5;
double* fill_array(double* begin)
{
cout << "Enter the number: " << endl;
int i = 0;
double* ps;
for (ps = begin,i=0;ps != begin+Max;ps++,i++)
{
cout << " # " << i + 1 << " : ";
if (!(cin >> *ps))
break;
}
return ps;
}
//输出数组
void show_array(double* begin,double* end)
{
for (double* ps = begin;ps != end;ps++)
cout << *ps << "\t";
cout << endl;
return;
}
//重新赋值
void revalue(double* begin, double* end,int n)
{
for (double* ps = begin;ps != end;ps++)
*ps = n;
cout << endl;
return;
}
void house(void)
{
double arr[Max];
double* begin = arr;
double* end = fill_array(begin);
show_array(begin, end);
revalue(begin, end, 2);
show_array(begin, end);
return;
}
//-------------------作业八----------------------------------
//a.double数组存储开支
const int Seasons = 4;
const char* Snames[Seasons] = {"Spring","Summer","Fall","Winter"};
void fill_1(double* arr)
{
for (int i = 0;i < Seasons;i++)
{
cout << Snames[i] << " : ";
cin >> arr[i];
}
return;
}
void show_1(double* arr)
{
double total = 0.0;
for (int i = 0;i < Seasons;i++)
{
cout << Snames[i] << " : " << arr[i] << endl;
total += arr[i];
}
cout << "total: " << total << endl;
return;
}
void season_1(void)
{
double array[Seasons];
fill_1(array);
show_1(array);
return;
}
//b.结构,成员是存储开支的double数组
struct expense
{
double arr[Seasons];
};
void fill_2(expense* ps)
{
for (int i = 0;i < Seasons;i++)
{
cout << Snames[i] << " : ";
cin >> ps->arr[i];
}
return;
}
void show_2(expense ps)
{
double total=0.0;
for (int i = 0;i < Seasons;i++)
{
cout << Snames[i] << " : " << ps.arr[i] << endl;
total += ps.arr[i];
}
cout << "total: " << total << endl;
return;
}
void season_2(void)
{
struct expense exp;
fill_2(&exp);
show_2(exp);
return;
}
//---------------------------作业九----------------
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[], int n)//结构数组
{
int i = 0;
for (i = 0;i < n;i++)
{
cout << " # " << i + 1 << " : " << endl;
cout << "Enter the fullname: ";
cin.getline(pa[i].fullname,SLEN);
if (pa[i].fullname == "\n")
break;
cout << "Enter the hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Enter the ooplevel: ";
cin>>pa[i].ooplevel ;
while (cin.get() != '\n')
continue;
}
return i;
}
void display1(student st)
{
cout << "--------------" << endl;
cout << "fullname:\t" << st.fullname << "\thobby:\t" << st.hobby
<< "\toopleavel:\t" << st.ooplevel << endl;
}
void display2(const student* ps)
{
cout << "--------------" << endl;
cout << "fullname:\t" << ps->fullname << "\thobby:\t" << ps->hobby
<< "\toopleavel:\t" << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
for (int i = 0;i < n;i++)
{
cout << "--------------" << endl;
cout << "fullname:\t" << pa[i].fullname << "\thobby:\t" << pa[i].hobby
<< "\toopleavel:\t" << pa[i].ooplevel << endl;
}
}
void excer(void)
{
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;
}
//--------------------作业十---------------------
double add(double x, double y)
{
return x + y;
}
double mul(double x, double y)
{
return x * y;
}
double calculate(double x, double y, double (*ps)(double, double))
{
return ps(x, y);
}
void cal_plus(void)
{
cout << "Enter the two numbers:";
double x, y;
//创建函数指针数组
double (*ps[2]) (double, double) = { add,mul};
double result[2];
while (cin >> x >> y)
{
for (int i = 0;i < 2;i++)
{
result[i]=calculate(x, y, ps[i]);
}
cout << "x+y=" << result[0] << endl;
cout << "x*y= " << result[1] << endl;
cout << "Enter the two numbers:";
}
return;
}
|