//第七章编程练习
//eg.1不断要求用户输入两个数,知道其中一个为零。调用一个函数计算调和平均数,返回平均数,显示结果
using namespace std;
double TiaoHe(double x, double y);
int main()
{
double x;
double y;
cout << "请输入两个数:";
while (cin >> x >> y&&x*y != 0)
cout << x << "和" << y << "的调和平均数是:" << TiaoHe(x, y) << endl;
cout << "Bye" << endl;
system("pause");
return 0;
}
double TiaoHe(double x, double y)
{
return 2.0*x*y / (x + y);
}
//eg.2用户最多输入10个成绩,存储在一个数组里面,允许提前结束输入,并在一行上显示所有成绩
//报告平均成绩,使用三个数组处理函数进行输入、显示、计算平均成绩
//#include<iostream>
//using namespace std;
//const int ArSize = 10;
//int getgolf(int ar[], int n);
//void showgolf(const int ar[], int n);
//double ave(const int ar[], int n);
//int main()
//{
// int golfscores[ArSize];
// int games = getgolf(golfscores, ArSize);
// showgolf(golfscores, games);
// if (games > 0)
// cout << ave(golfscores, games) << " = averages" << endl;
// else
// cout << "No Scores!" << endl;
// system("pause");
// return 0;
//}
//int getgolf(int ar[], int n)
//{
// cout << "Enter up to " << n << " scores<q to quit>:\n";
// int i;
// for (i = 0; i < n; ++i)
// {
// if (!(cin >> ar[i]))
// {
// cin.clear();
// while (cin.get() != '\n')
// continue;
// break;
// }
// }
// return i;
//}
//void showgolf(const int ar[], int n)
//{
// for (int i = 0; i < n; ++i)
// {
// cout << ar[i] << " ";
// }
// cout << endl;
//}
//double ave(const int ar[], int n)
//{
// double tot = 0.0;
// for (int i = 0; i < n; ++i)
// {
// tot += ar[i];
// }
// return tot / n;
//}
//eg.3
//按值传递box结构,并显示每个成员的值
//传递box结构地址,volume=其他三维长度的成绩
//#include<iostream>
//using namespace std;
//struct box
//{
// char maker[40];
// float height;
// float width;
// float length;
// float volume;
//};
//void Show(box b);
//void setbox(box * pb);
//int main()
//{
// box carton = { "Bingo Boxer",2,3,5 };
// setbox(&carton);
// Show(carton);
// system("pause");
// return 0;
//}
//void Show(box b)
//{
// cout << "Box maker: " << b.maker << endl
// << "height: " << b.height << endl
// << "width: " << b.width << endl
// << "length: " << b.length << endl
// << "volumn: " << b.volume << endl;
//}
//void setbox(box * pb)
//{
// pb->volume = pb->height * pb->width * pb->length;
//}
//eg.4彩票中奖问题
//#include <iostream>
//long double probability(unsigned numbers, unsigned picks);
//int main()
//{
// using namespace std;
// double total, choices;
// double mtotal;
// double probability1, probability2;
// cout << "Enter total number of game card choices and\n"
// "number of picks allowed for the field:\n";
// while ((cin >> total >> choices) && choices <= total)
// {
// cout << "Enter total number of game card choices "
// "for the mega number:\n";
// if (!(cin >> mtotal))
// break;
// cout << "The chances of getting all " << choices << " picks is one in"
// << (probability1 = probability(total, choices)) << ".\n";
// cout << "The chances of getting the megaspot is one in "
// << (probability2 = probability(mtotal, 1)) << ".\n";
// cout << "You have one chance in ";
// cout << probability1 * probability2; // compute the probability
// cout << " of winning.\n";
// cout << "Next set of numbers (q to quit): ";
// }
// cout << "bye\n";
// //keep window open
// if (!cin) // input terminated by non-numeric response
// {
// cin.clear(); // reset input
// while (cin.get() != '\n')
// continue; // read rest of line
// }
// else
// cin.get(); // read end of line after last input
// cin.get(); // wait before closing window
//
// return 0;
//}
the following function calculates the probability of picking picks
numbers correctly from numbers choices
//long double probability(unsigned numbers, unsigned picks)
//{
// long double result = 1.0; // here come some local variables
// long double n;
// unsigned p;
// for (n = numbers, p = picks; p > 0; n--, p--)
// result = result * n / p;
// return result;
//}
//#include<iostream>
//using namespace std;
//int JieCheng(int x);
//int main()
//{
// cout << "请输入一个数:";
// int x;
// cin >> x;
// int Zhi = JieCheng(x);
// cout << x << "的阶乘等于" << Zhi << endl;
// system("pause");
// return 0;
//}
//int JieCheng(int x)
//{
// int Zhi;
// if (x == 0)
// Zhi = 1;
// else
// Zhi = x*JieCheng(x - 1);
// return Zhi;
//}
//#include <iostream>
//double rfact(int n);
//int main()
//{
// using namespace std;
// int num;
// cout << "Enter an integer (< 0 to quit): ";
// while (cin >> num && num >= 0)
// {
// cout << num << " factorial = " << rfact(num) << "\n";
// cout << "Enter next value (<0 to quit): ";
// }
// cout << "Bye!\n";
// /* keep window open
// if (!cin) // input terminated by non-numeric response
// {
// cin.clear(); // reset input
// while (cin.get() != '\n')
// continue; // read rest of line
// }
// else
// cin.get(); // read end of line after last input
// cin.get(); // wait before closing window
// */
// return 0;
//}
//double rfact(int n)
//{
// if (n <= 1)
// return 1;
// else
// return n * rfact(n - 1);
//}
//eg.6
//#include<iostream>
//using namespace std;
//const int LIMIT = 10;
//int Fill_array(double ar[], int size);
//void Show_array(const double ar[], int size);
//void Reverse_array(double ar[], int size);
//int main()
//{
// double values[LIMIT];
// int entires = Fill_array(values, LIMIT);
// cout << "数组的值为:\n";
// Show_array(values, entires);
// cout << "数组颠倒:\n";
// Reverse_array(values, entires);
// Show_array(values, entires);
// cout << "除最终值外的所有值: \n";
// Reverse_array(values+1, entires-2);
// Show_array(values, entires);
// if (!cin)
// {
// cin.clear();
// while (cin.get() != '\n')
// continue;
// }
// else
// cin.get();
// cin.get();
// return 0;
//}
//int Fill_array(double ar[], int size)
//{
// int n;
// cout << "Enter up to " << size << " values(q to quit): \n";
// for (n = 0; n < size; ++n)
// {
// cin >> ar[n];
// if (!cin)
// break;
// }
// return n;
//}
//void Show_array(const double ar[], int size)
//{
// int n;
// for (n = 0; n < size; n++)
// {
// cout << ar[n];
// if (n % 8 == 7)
// {
// cout << endl;
// }
// else
// cout << ' ';
// }
// if (n % 8 != 0)
// cout << endl;
//}
//void Reverse_array(double ar[], int size)
//{
// int i, j;
// double temp;
// for (i = 0, j = size - 1; i < j; i++, j--)
// {
// temp = ar[i];
// ar[i] = ar[j];
// ar[j] = temp;
// }
//}
//eg.7
//#include <iostream>
//const int LIMIT = 10;
function prototypes
//double * Fill_array(double * beg, double * end);
//void Show_array(const double * beg, const double * end); // don't change data
//void Reverse_array(double * beg, double * end);
//int main()
//{
// using namespace std;
// double values[LIMIT];
// double * true_end;
// true_end = Fill_array(values, values + LIMIT);
// cout << "Array values:\n";
// Show_array(values, true_end);
// cout << "Array reversed:\n";
// Reverse_array(values, true_end);
// Show_array(values, true_end);
// cout << "All but end values reversed:\n";
// Reverse_array(values + 1, true_end - 1);
// Show_array(values, true_end);
// cout << "Done.\n";
// system("pause");
// return 0;
//}
//double * Fill_array(double * beg, double * end)
//{
// using namespace std;
// double temp;
// double * pt;
// int i;
// for (pt = beg, i = 0; pt != end; pt++, i++)
// {
// cout << "Enter value #" << (i + 1) << ": ";
// cin >> temp;
// if (!cin) // bad input
// {
// cin.clear();
// while (cin.get() != '\n')
// continue;
// cout << "Bad input; input process terminated.\n";
// break;
// }
// else if (temp < 0) // signal to terminate
// break;
// * pt = temp;
// }
// return pt;
//}
the following function can use, but not alter,
the array whose address is ar
//void Show_array(const double * beg, const double * end)
//{
// using namespace std;
// const double * pt;
// int count = 0;
// for (pt = beg; pt != end; pt++, count++)
// {
// cout << *pt << " ";
// if (count % 8 == 7)
// cout << endl;
// }
// if (count % 8 != 0)
// cout << endl;
//}
reverses array contents
//void Reverse_array(double * beg, double * end)
//{
// double * ps;
// double * pe;
// double temp;
// for (ps = beg, pe = end - 1; ps < pe; ps++, pe--)
// {
// temp = *ps;
// *ps = *pe;
// *pe = temp;
// }
//}
//eg.8
//#include<iostream>
//using namespace std;
//const int Seasons = 4;
//const char * Snames[Seasons] = { "Spring","Summer","Fall","Winter" };
//void fill(double ar[], int n);
//void show(const double ar[], int n);
//int main()
//{
// double expenses[Seasons];
// fill(expenses, Seasons);
// show(expenses, Seasons);
// system("pause");
// return 0;
//}
//void fill(double ar[], int n)
//{
// for(int i = 0; i < n; i++)
// {
// cout << "Enter " << Snames[i] << "expenses: \n";
// cin >> ar[i];
// }
//}
//void show(const double ar[], int n)
//{
// double total = 0.0;
// cout << endl << "EXPENSES" << endl;
// for (int i = 0; i < n; i++)
// {
// cout << Snames[i] << ": $" << ar[i] << endl;
// total += ar[i];
// }
// cout << "Total Expenses: $" << total << endl;
//}
//#include<iostream>
//using namespace std;
//const int Seasons = 4;
Snames是一个包含四个指针的数组
//const char * Snames[Seasons] = { "Spring","Summer","Fall","Winter" };
//struct hold_ar
//{
// double values[Seasons];
//};
//void fill(struct hold_ar * pha);
//void show(const struct hold_ar ha);
//int main()
//{
// hold_ar expenses;
// fill(&expenses);
// show(expenses);
// system("pause");
// return 0;
//}
//void fill(struct hold_ar * pha)
//{
// for (int i = 0; i < Seasons; i++)
// {
// cout << "Enter " << Snames[i] << " expenses:";
// cin >> pha->values[i];
// }
//}
//void show(const struct hold_ar ha)
//{
// double total = 0.0;
// cout << endl << "EXPENSES" << endl;
// for (int i = 0; i < Seasons; i++)
// {
// cout << Snames[i] << ": $" << ha.values[i] << endl;
// total += ha.values[i];
// }
// cout << "Total Expenses: $" << total << endl;
//}
//EG.9
//#include <iostream>
//using namespace std;
//const int SLEN = 30;
//struct student {
// char fullname[SLEN];
// char hobby[SLEN];
// int ooplevel;
//};
getinfo() has two arguments: a pointer to the first
element of an array of student structures and an int
representing the number of elements of the array. The
function solicits and stores data about students. It
terminates input upon filling the array or upon
encountering a blank line for the student name. The
function returns the actual number of array elements
filled.
//int getinfo(student pa[], int n);
display1() takes a student structure as an argument
and displays its contents
//void display1(student st);
display2() takes the address of student structure as an
argument and displays the structure's contents
//void display2(const student * ps);
display3() takes (1) the address of the first element of
an array of student structures and (2) the number of
array elements as arguments and displays the contents
of the structures
//void display3(const student pa[], int n);
//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);
// cout << "Done\n";
// cin.get();
// return 0;
//}
//int getinfo(student pa[], int n)
//{
// int i;//返回它
// for (i = 0; i < n; i++)
// {
// cout << "Enter student name: ";
// cin.getline(pa[i].fullname, SLEN);
// if (pa[i].fullname[0] == '\0')
// break;
// cout << "Enter student hobby: ";
// cin.getline(pa[i].hobby, SLEN);
// cout << "Enter student oop level: ";
// cin >> pa[i].ooplevel;
// while (cin.get() != '\n')
// continue;
// }
// return i;
//}
//void display1(student st)
//{
// cout << st.fullname << ": " << st.hobby << ": "
// << st.ooplevel << "\n";
//}
//void display2(const student * ps)
//{
// cout << ps->fullname << ": " << ps->hobby << ": "
// << ps->ooplevel << "\n";
//}
//void display3(const student pa[], int n)
//{
// for (int i = 0; i < n; i++)
// cout << pa[i].fullname << ": " << pa[i].hobby << ": "
// << pa[i].ooplevel << "\n";
//}
//eg.10
//#include <iostream>
//using namespace std;
指向函数的指针
//double calculate(double x, double y, double(*pf)(double, double));
//double add(double x, double y);
//double sub(double x, double y);
//double mean(double x, double y);
//int main()
//{
// //包含三个指针的数组
// double(*pf[3])(double, double) = { add,sub,mean };
// char * op[3] = { "sum","difference","mean" };
// double a, b;
// cout << "Enter pairs of numbers (q to quit): ";
// int i;
// while (cin >> a >> b)
// {
// cout << calculate(a, b, add) << " = sum\n";
// cout << calculate(a, b, mean) << " = mean\n";
// for (i = 0; i < 3; i++)
// {
// cout << calculate(a, b, pf[i]) << " = " << op[i] << endl;
// }
// cout << "Done!" << endl;
// cin.clear();
// while (cin.get() != '\n')
// continue;
// cin.get();
// return 0;
// }
//}
//double calculate(double x, double y, double(*pf)(double, double))
//{
// return(*pf)(x, y);
//}
//double add(double x, double y)
//{
// return x + y;
//}
//double sub(double x, double y)
//{
// return x - y;
//}
//double mean(double x, double y)
//{
// return (x + y) / 2.0;
//}