1 /*********************************************************************
2 程序名: 7.13 编程练习
3 1. 编写一个程序, 不断要求用户输入两个数, 直到其中的一个为
4 0。 对于每两个数, 程序将使用一个函数来计算它们的调和平均数, 并
5 将结果返回给main( ), 而后者将报告结果。 调和平均数指的是倒数平均
6 值的倒数, 计算公式如下:
7 调和平均数=2.0 * x * y / (x + y)
8 版权:
9 作者: 大野狼
10 日期: 2022/4/19-星期二 10:44:53
11 说明:
12 *********************************************************************/
13 #include <iostream>
14 double harm(int a,int b);
15 int main(void) {
16 using namespace std;
17 char ch;
18 int a,b;
19 double harmonic_mean; //调和平均数
20 cout<<"请输入两个数,以空格分隔:";
21 while((cin>>a>>b) && 0!=a && 0!=b)
22 {
23 harmonic_mean = harm(a,b);
24 cout<<"调和平均数 = "<<harmonic_mean<<endl;
25 cout<<"请输入两个数,以空格分隔:";
26 }
27
28 return 0;
29 }
30 double harm(int a,int b)
31 {
32 double result = 0;
33 result = 2*(a*b)/(a+b);
34 return result;
35 }
1 /*********************************************************************
2 程序名: 2. 编写一个程序, 要求用户输入最多10个高尔夫成绩, 并将其存
3 储在一个数组中。 程序允许用户提早结束输入(负数结束), 并在一行上显示所有成
4 绩, 然后报告平均成绩。 请使用3个数组处理函数来分别进行输入、 显
5 示和计算平均成绩。
6 版权:
7 作者: 大野狼
8 日期: 2022/4/19-星期二 11:09:20
9 说明:
10 *********************************************************************/
11 #include <iostream>
12 using namespace std;
13 void in_data(double arr[],unsigned int * count); // 输入
14 void show_data(const double arr[],const unsigned int count); //输出
15 void calc_data(const double arr[],const unsigned int count); //计算
16 const int SIZE =10;
17
18 int main(void) {
19 double score[SIZE];
20 unsigned int count=0; //计数
21 cout<<"请输入10个高尔夫成绩,以空格分隔。不足10个以 'q' 结束录入 :\n";
22 in_data(score,&count); //输入
23 show_data(score,count); //显示
24 calc_data(score,count); //计算平均并输出
25 return 0;
26 }
27 void in_data(double arr[],unsigned int * count) //输入
28 {
29 while((*count)<10 && cin>>arr[*count])
30 {
31 (*count)++;
32 }
33 // for(int i=0;i<SIZE;++i)
34 // {
35 // cin>>arr[i];
36 // if(arr[i]<0)
37 // break; //输入负数提早结束录入
38 // ++count;
39 // }
40 // return count;
41 }
42 void show_data(const double arr[],const unsigned int count) //输出
43 {
44 for(int i=0;i<count;++i)
45 cout<<arr[i]<<" ";
46 cout<<endl;
47 }
48
49 void calc_data(const double arr[],const unsigned int count) //计算平均成绩
50 {
51 double total=0;
52 for(int i=0;i<count;++i)
53 total += arr[i];
54
55 cout<<"平均成绩为: "<<total/count<<endl;
56 }
1 /*********************************************************************
2 程序名: 3. 下面是一个结构声明:
3 a. 编写一个函数, 按值传递box结构, 并显示每个成员的值。
4 b. 编写一个函数, 传递box结构的地址, 并将volume成员设置为其他三维长度的乘积。
5 c. 编写一个使用这两个函数的简单程序
6 版权:
7 作者: 大野狼
8 日期: 2022/4/19-星期二 14:26:04
9 说明:
10 *********************************************************************/
11 #include <iostream>
12 using namespace std;
13 struct box
14 {
15 char maker[40];
16 float height;
17 float width;
18 float length;
19 float volume;
20 };
21
22 void show_struct(box a); //显示成员值
23 void address_struct(box * a); //传结构体地址
24
25 int main(void) {
26 box a={"纸盒子",10,20,30};
27 show_struct(a); //问题 a
28 address_struct(&a); //问题 b
29 show_struct(a); //问题 a
30 return 0;
31 }
32 void address_struct(box * a) //传结构体地址
33 {
34 a->volume = a->height * a->width * a->length;
35 }
36 void show_struct(box a) //显示成员值
37 {
38 cout<<a.maker<<endl;
39 cout<<a.height<<endl;
40 cout<<a.width<<endl;
41 cout<<a.length<<endl;
42 cout<<a.volume<<endl;
43 }
1 /*********************************************************************
2 程序名: 4. 许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩
3 法的变体。 在这些玩法中, 玩家从一组被称为域号码(field number) 的
4 号码中选择几个。 例如, 可以从域号码1~47中选择5个号码; 还可以从
5 第二个区间(如1~27) 选择一个号码(称为特选号码) 。 要赢得头
6 奖, 必须正确猜中所有的号码。 中头奖的几率是选中所有域号码的几率
7 与选中特选号码几率的乘积。 例如, 在这个例子中, 中头奖的几率是从
8 47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的
9 几率的乘积。 请修改程序清单7.4, 以计算中得这种彩票头奖的几率。
10 版权:
11 作者: 大野狼
12 日期: 2022/4/19-星期二 14:40:53
13 说明:
14 *********************************************************************/
15 #include <iostream>
16 // Note: some implementations require double instead of long double
17 long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2);
18 int main()
19 {
20 using namespace std;
21 double total1, choices1;
22 double total2, choices2;
23 cout << "在游戏卡上输入选择的总数,然后允许拾取的数量:\n 如47选5+27选1 则输入: 47 5 27 1\n";
24 while ((cin >> total1 >> choices1>> total2 >> choices2) && choices1 <= total1
25 && choices2 <= total2)
26 {
27 cout << "您的获奖几率为: ";
28 cout << probability(total1, choices1, total2, choices2); // compute the odds
29 cout << " 分之一.\n";
30 cout << "下一组两个数字为: (q to quit): ";
31 }
32 cout << "bye\n";
33 // cin.get();
34 // cin.get();
35 return 0;
36 }
37
38 // the following function calculates the probability of picking picks
39 // numbers correctly from numbers choices
40 long double probability(unsigned numbers1, unsigned picks1, unsigned numbers2, unsigned picks2)
41 {
42 long double result1 = 1.0; // here come some local variables
43 long double result2 = 1.0; // here come some local variables
44 long double n;
45 unsigned p;
46
47 for (n = numbers1, p = picks1; p > 0; n--, p--)
48 result1 = result1 * n / p ;
49 for (n = numbers2, p = picks2; p > 0; n--, p--)
50 result2 = result2 * n / p ;
51 return result1 * result2;
52 }
1 /*********************************************************************
2 程序名: 5. 定义一个递归函数, 接受一个整数参数, 并返回该参数的阶
3 乘。 前面讲过, 3的阶乘写作3!, 等于3*2!, 依此类推; 而0!被定义为
4 1。 通用的计算公式是, 如果n大于零, 则n!=n*(n?1) !。 在程序中对
5 该函数进行测试, 程序使用循环让用户输入不同的值, 程序将报告这些
6 值的阶乘。
7 版权:
8 作者: 大野狼
9 日期: 2022/4/19-星期二 14:59:18
10 说明:
11 *********************************************************************/
12 #include <iostream>
13 double jiecheng(int a); //计算阶乘并返回
14 int main(void) {
15 using namespace std;
16 int a;
17 double aa=0;
18 cout<<"输入一个正整数,程序会计算这个数的阶乘:";
19 while(cin>>a && a>=0)
20 {
21 aa = jiecheng(a);
22 cout<<a<<" 的阶乘 = "<<aa<<endl;
23 cout<<"退出输入'Q',或继续输入一个数:";
24 }
25 return 0;
26 }
27
28 double jiecheng(int a)
29 {
30 double result;
31 if(a<=1)
32 result = 1;
33 else
34 {
35 result = jiecheng(a-1) * a;
36 }
37 return result;
38 }
1 /*********************************************************************
2 程序名: 6. 编写一个程序, 它使用下列函数:
3 Fill_array( )将一个double数组的名称和长度作为参数。 它提示用户
4 输入double值, 并将这些值存储到数组中。 当数组被填满或用户输入了
5 非数字时, 输入将停止, 并返回实际输入了多少个数字。
6 Show_array( )将一个double数组的名称和长度作为参数, 并显示该
7 数组的内容。
8 Reverse-array( )将一个double数组的名称和长度作为参数, 并将存
9 储在数组中的值的顺序反转。
10 程序将使用这些函数来填充数组, 然后显示数组; 反转数组, 然后
11 显示数组; 反转数组中除第一个和最后一个元素之外的所有元素, 然后
12 显示数组。
13 版权:
14 作者: 大野狼
15 日期: 2022/4/19-星期二 17:23:34
16 说明:
17 *********************************************************************/
18 #include <iostream>
19 using namespace std;
20 const int MAX=10;
21 int Fill_array(double arr[],int size);
22 int Show_array(double arr[],int size);
23 int Reverse_array(double arr[],int size);
24
25 int main(void) {
26 double arr[MAX];
27 int count;
28 cout<<"输入数字:";
29 count = Fill_array(arr,MAX); //输入
30 cout<<"共计输入了:"<<count<<" 个数字!\n";
31 Show_array(arr,count); //显示
32 Reverse_array(arr,count); //反转
33 Show_array(arr,count);
34 return 0;
35 }
36
37 int Fill_array(double arr[],int size)
38 {
39 int i=0;
40 while( i<size && cin>>arr[i])
41 {
42 ++i;
43 }
44 return i;
45 }
46
47 int Show_array(double arr[],int size)
48 {
49 for(int i=0;i<size;++i)
50 cout<<arr[i]<<" ";
51 cout<<endl;
52 }
53
54 int Reverse_array(double arr[],int size)
55 {
56 double temp;
57 for(int i=0;i<size/2;++i)
58 {
59 temp = arr[i];
60 arr[i] = arr[size-i-1];
61 arr[size-i-1] = temp;
62 }
63 }
1 /*********************************************************************
2 程序名: 7. 修改程序清单7.7中的3个数组处理函数, 使之使用两个指针参
3 数来表示区间。 fill_array( )函数不返回实际读取了多少个数字,
4 而是返回一个指针, 该指针指向最后被填充的位置; 其他的函数可以将该
5 指针作为第二个参数, 以标识数据结尾。
6 版权:
7 作者: 大野狼
8 日期: 2022/4/19-星期二 17:59:26
9 说明:
10 *********************************************************************/
11 #include <iostream>
12 const int Max = 5;
13
14 // function prototypes
15 //int fill_array(double ar[], int limit);
16 //int fill_array(double *begin, double *end);
17 //void show_array(const double ar[], int n); // don't change data
18 //void revalue(double r, double ar[], int n);
19 double * fill_array(double *begin, double *end); //返回 最后元素的指针
20 void show_array(double *begin, double *end);
21 void revalue(double *begin, double *end,double r);
22
23 int main()
24 {
25 using namespace std;
26 double properties[Max];
27 double *pend;
28 // int size = fill_array(properties, properties + Max);
29 // int size = fill_array(properties, &properties[Max]+1);
30 // show_array(properties, size);
31 pend = fill_array(properties, properties + Max);
32 show_array(properties, pend);
33 if ((pend-properties) > 0)
34 {
35 cout << "输入一个系数 : ";
36 double factor;
37 while (!(cin >> factor)) // bad input
38 {
39 cin.clear();
40 while (cin.get() != '\n')
41 continue;
42 cout << "无效数据输入,请重新运行程序: ";
43 }
44 // revalue(factor, properties, size);
45 // show_array(properties, size);
46 // show_array(properties, properties + size);
47 revalue(properties, pend,factor);
48 show_array(properties, pend);
49 }
50 cout << "完成.\n";
51 // cin.get();
52 // cin.get();
53 return 0;
54 }
55
56 //int fill_array(double ar[], int limit)
57 //int fill_array(double *begin, double *end)
58 double * fill_array(double *begin, double *end)
59 {
60 using namespace std;
61 double temp;
62 int count=0;
63 double * ptr = nullptr;
64 for(ptr = begin; ptr < end; ptr++)
65 {
66 cout << "输入值 #" << (count + 1) << ": ";
67 cin >> temp;
68 if (!cin) // bad input
69 {
70 cin.clear();
71 while (cin.get() != '\n')
72 continue;
73 cout << "无效数据输入,请重新运行程序.\n";
74 break;
75 }
76 else if (temp < 0) // signal to terminate
77 break;
78 *ptr = temp;
79 ++count;
80 }
81 return ptr;
82 }
83
84 // the following function can use, but not alter,
85 // the array whose address is ar
86 //void show_array(const double ar[], int n)
87 void show_array(double *begin, double *end)
88 {
89 using namespace std;
90 int i=0;
91 for(double * ptr = begin; ptr < end; ptr++,i++)
92 // for (int i = 0; i < n; i++)
93 {
94 cout << "Property #" << (i + 1) << ": $";
95 cout << *ptr << endl;
96 }
97 }
98
99 // multiplies each element of ar[] by r
100 //void revalue(double r, double ar[], int n)
101 void revalue(double *begin, double *end,double r)
102 {
103 // for (int i = 0; i < n; i++)
104 for(double * ptr = begin; ptr < end; ptr++)
105 (*ptr) *= r;
106 // ar[i] *= r;
107 }
1 /*********************************************************************
2 程序名: 8. 在不使用array类的情况下完成程序清单7.15所做的工作。 编写
3 两个这样的版本:
4 a. 使用const char *数组存储表示季度名称的字符串, 并使用double
5 数组存储开支。
6 b. 使用const char *数组存储表示季度名称的字符串, 并使用一个
7 结构, 该结构只有一个成员——一个用于存储开支的double数组。 这种
8 设计与使用array类的基本设计类似。
9 版权:
10 作者: 大野狼
11 日期: 2022/4/19-星期二 18:31:25
12 说明:
13 *********************************************************************/
14 //arrobj.cpp -- functions with array objects
15 #include <iostream>
16 #include <array>
17 #include <string>
18 const int Seasons = 4;
19 //const std::array<std::string, Seasons> Snames = {"Spring", "Summer", "Fall", "Winter"};
20 const char * Snames[Seasons] = {"Spring", "Summer", "Fall", "Winter"};
21 struct cost{
22 double expenses[Seasons];
23 };
24
25 //void fill(std::array<double, Seasons> * pa);
26 //void show(std::array<double, Seasons> da);
27 //void fill(double arr[]);
28 //void show(double arr[]);
29 void fill(struct cost * ptr); //结构体法 b题
30 void show(struct cost * ptr);
31 int main()
32 {
33 // std::array<double, 4> expenses; //array 方法
34 // double expenses[Seasons]; //数组方法 a题
35 // fill(expenses);
36 // show(expenses);
37
38 cost * pexpenses = new cost; //结构体法 b题
39 fill(pexpenses); //结构体法 b题
40 show(pexpenses);
41 // std::cin.get();
42 // std::cin.get();
43 delete pexpenses;
44 return 0;
45 }
46
47 //void fill(std::array<double, Seasons> * pa)
48 //void fill(double arr[])
49 void fill(struct cost * ptr) //结构体法 b题
50 {
51 for (int i = 0; i < Seasons; i++)
52 {
53 std::cout << "Enter " << Snames[i] << " expenses: ";
54 std::cin >> ptr->expenses[i];
55 }
56 }
57
58 //void show(std::array<double, Seasons> da)
59 //void show(double arr[])
60 void show(struct cost * ptr) //结构体法 b题
61 {
62 double total = 0.0;
63 std::cout << "\nEXPENSES\n";
64 for (int i = 0; i < Seasons; i++)
65 {
66 // std::cout << Snames[i] << ": $" << arr[i] << '\n';
67 // total += arr[i];
68 std::cout << Snames[i] << ": $" << ptr->expenses[i] << '\n';
69 total += ptr->expenses[i];
70 }
71 std::cout << "Total: $" << total << '\n';
72 }
1 /*********************************************************************
2 程序名: 9. 这个练习让您编写处理数组和结构的函数。 下面是程序的框
3 架, 请提供其中描述的函数, 以完成该程序。
4 版权:
5 作者: 大野狼
6 日期: 2022/4/20-星期三 10:50:21
7 说明:
8 *********************************************************************/
9 #include <iostream>
10 using namespace std;
11 const int SLEN = 30;
12 struct student
13 {
14 char fullname[SLEN];
15 char hobby[SLEN];
16 int ooplevel;
17 };
18 // 两个参数,一个数组起始元素的指针,一个容量 ,并返回实际学生的数量
19 int getinfo(student pa[], int n)
20 {
21 int i=0;
22 for(;i<n;++i)
23 {
24 cout << "输入学生的信息 #" << i+1 << endl;
25 cout << "输入全名 (空白行退出!): ";
26 cin.getline(pa[i].fullname,SLEN);
27 // if(pa[i].fullname == '\n') break;
28 cout << "输入爱好 : ";
29 cin.getline(pa[i].hobby,SLEN);
30 cout << "输入年龄 : ";
31 cin>>pa[i].ooplevel;
32 cin.get();
33 // while (cin.get() != '\n')
34 // continue;
35 }
36 return i;
37 }
38
39 // 一个参数 显示学生目录
40 void display1(student st)
41 {
42 cout<<"display1(ptr_stu[i]);\n";
43 cout<<st.fullname<<" "<<st.hobby<<" "<<st.ooplevel<<endl;
44 }
45 void display2(const student *st)
46 {
47 cout<<"display2(&ptr_stu[i]);\n";
48 cout<<st->fullname<<" "<<st->hobby<<" "<<st->ooplevel<<endl;
49 }
50 void display3(const student pa[], int n)
51 {
52 cout<<"display3(ptr_stu, entered);\n";
53 for(int i=0;i<n;++i)
54 cout<<pa[i].fullname<<" "<<pa[i].hobby<<" "<<pa[i].ooplevel<<endl;
55 }
56 void p7_9(void)
57 {
58 cout << "输入学生的数量: ";
59 int class_size;
60 cin >> class_size;
61 while (cin.get() != '\n')
62 continue;
63
64 student *ptr_stu = new student[class_size];
65 int entered = getinfo(ptr_stu, class_size);
66 for (int i = 0; i < entered; i++)
67 {
68 display1(ptr_stu[i]);
69 display2(&ptr_stu[i]);
70 }
71 display3(ptr_stu, entered);
72 //
73 delete[] ptr_stu;
74 cout << "Done.\n";
75 return;
76 }
77 int main(void) {
78 p7_9();
79 return 0;
80 }
1 /*********************************************************************
2 程序名: 10. 设计一个名为calculate( )的函数, 它接受两个double值和一个指
3 向函数的指针, 而被指向的函数接受两个double参数, 并返回一个
4 double值。 calculate( )函数的类型也是double, 并返回被指向的函数使用
5 calculate( )的两个double参数计算得到的值。 例如, 假设add( )函数的定
6 义如下:
7 版权:
8 作者: 大野狼
9 日期: 2022/4/20-星期三 15:18:43
10 说明:
11 *********************************************************************/
12 #include <iostream>
13 double add(double x, double y);
14 double mul(double x, double y);
15 double calculate(double x,double y,double (*pad)(double x, double y));
16
17 int main(void) {
18 using namespace std;
19 double a,b;
20 cout<<"请输入两个数字,以空格分隔:";
21 while(cin>>a>>b)
22 {
23 cout<<"a,b,add = "<<calculate(a,b,add)<<endl;
24 cout<<"a,b,mul = "<<calculate(a,b,mul)<<endl;
25 cout<<"继续输入两个数:";
26 }
27 return 0;
28 }
29
30 double calculate(double x,double y,double (*pad)(double x, double y))
31 {
32 return pad(x,y);
33 }
34 double add(double x, double y)
35 {
36 return x + y;
37 }
38 double mul(double x, double y)
39 {
40 return x * y;
41 }