//7.13.10
#include
double add(double x,double y);
double subtraction(double x,double y);
double calculate(double x,double y ,double (*ps)(double a ,double b));
int main()
{
using namespace std;
cout << "Please enter two number: \n";
double x,y;
cin >> x >>y ;
if(cin)
{
double result_add,result_subtraction;
result_add = calculate(x,y,add);
result_subtraction = calculate(x,y,subtraction);
cout << "The sum of "<< x << " and "<< y << "is " << result_add< cout << "The subtraction of "<< x << " and "<< "is "<< result_subtraction << endl; cout << "Enter a number: "; cin >> x; cout << "Enter another number: "; cin >> y; } return 0; } double add(double x,double y) { return x+y; } double subtraction(double x,double y) { return x-y; } double calculate(double x,double y,double (*ps)(double,double)) { double a; a = (*ps)(x,y); return a; } /*//7.13.9 #include 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 encoutering a blank line for the student // name. The function returns the actual number of array elements // filled. int getinfo(student pa[], int n); // dispay1() 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 the address of the first element of an array // of student structures and 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); delete [] ptr_stu ; return 0 ; } int getinfo(student pa[],int n) { int i = 0 ; while ( i < n) { cout << "Enter your name: "; cin.get( pa[i].fullname,SLEN) ; char stop; stop =cin.get(); if (stop!= '\0') break; cout << "Enter your hobby: "; cin.getline(pa[i].hobby ,SLEN); cout << "Enter the ooplevel: "; cin >> pa[i].ooplevel ; cin.get(); i++; } return i ; } void display1(student st) { using namespace std ; cout << st.fullname << endl << st.hobby < << st.ooplevel << endl ; } void display2(const student * ps) { using namespace std ; cout << ps->fullname << endl << ps->hobby << endl << ps->ooplevel << endl ; } void display3(const student pa[],int n) { using namespace std ; int i = 0 ; while (i < n) { cout << pa[i].fullname << endl << pa[i].hobby << endl << pa[i].ooplevel << endl ; i++; } } */ /*//7.13.8(b) #include struct expense { double ex[4]; }; const int Seasons = 4; const char arr[4][10] = { "Spring", "Summer", "Fall", "Winter" }; void fill(expense * ps); void show(const expense * ps); int main() { using namespace std ; expense esp; fill(&esp); show(&esp); return 0 ; } void fill(expense * ps) { using namespace std ; for (int i = 0 ;i < Seasons; i++) { cout << "Enter " << arr[i] <<" expenses: "; cin >> ps->ex[i]; } } void show(const expense * ps) { using namespace std ; double total = 0.0; cout << "\nEXPENSES\n"; for (int i = 0 ;i< Seasons;i++) { cout << arr[i] << ": $" << ps->ex[i] << endl ; total += ps->ex[i]; } cout << "Total Expenses: $" << total << endl ; } */ /*//7.13.8 ? #include const int Seasons = 4 ; const char arr[4][10] = { "Spring", "Summer", "Fall", "Winter" }; void fill(double arr[]); void show(const double arr[]); int main() { using namespace std ; double expenses[4]; fill(expenses); show(expenses); return 0 ; } void fill(double ar[]) { using namespace std ; for (int i = 0 ;i < Seasons; i++) { cout << "Enter " << arr[i] <<" expenses: "; cin >> ar[i]; } } void show(const double ar[]) { using namespace std ; double total = 0.0; cout << "\nEXPENSES\n"; for (int i = 0 ;i< Seasons;i++) { cout << arr[i] << ": $" << ar[i] << endl ; total += ar[i]; } cout << "Total Expenses: $" << total << endl ; } */ /*//7.13.7 #include const int Max = 5; double * fill_array(double arr[], int limit); void show_array(double arr[],double * ps); void revalue(double r , double arr[], double * ps); int main() { using namespace std ; double properties[Max]; double * ps ; ps = fill_array(properties, Max); show_array(properties,ps); if (ps != 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,ps); show_array(properties,ps); } cout << "Done.\n"; cin.get(); cin.get(); return 0; } double * fill_array(double arr[], int limit) { using namespace std ; 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; arr[i] = temp ; } return &arr[i-1]; } void show_array(double arr[],double * ps) { using namespace std ; for (int i = 0; ps != &arr[i];i++) { cout << "Property #"<< (i+1) << ": &"; cout << arr[i] << endl ; } } void revalue(double r ,double arr[], double * ps) { for (int i = 0 ;ps != &arr[i]; i++) arr[i] *= r ; } */ /*//7.13.6 #include const int Max = 100 ; int Fill_array(double arr[],int length); void Show_array(const double arr[],int length); void Reverse_array(double arr[],int length); int main() { using namespace std ; double arr[Max]; int Size; Size = Fill_array(arr,Max); Show_array(arr,Size); cout << endl ; Reverse_array(arr,Size); Show_array(arr,Size); return 0 ; } int Fill_array(double arr[],int Max) { using namespace std ; int i = 0 ; cout << "Please enter number!!!\n"; for (i;i < Max;i++) { cout << "Enter number to the arr[" << i << "]: "; cin >> arr[i]; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "Not a number!!! the process terminated"; break; } } return i ; } void Show_array(const double arr[],int length) { using namespace std ; cout << endl; int i = 0 ; while (i < length) { cout << arr[i] << " "; i++; } } void Reverse_array(double arr[],int length) { int j ; j = length/2 ; int k = 1; while (k <=j) { double arr1 ; arr1 = arr[k]; arr[k]= arr[length-1-k]; arr[length-1-k]=arr1; k++; } } */ /*//7.13.5 #include long double function(unsigned number); int main() { using namespace std ; long double sj; unsigned number ; cout << "Enter a number: "; cin >> number; sj = function(number); cout << number << "! = " << sj << endl ; return 0 ; } long double function(unsigned number) { long double sj ; if (number == 0) sj = 1; else sj = number * function(number -1 ); return sj ; } */ /*//7.13.4 #include long double probability(unsigned numbers, unsigned picks); int main() { using namespace std ; double ch[4]; cout << "ch[4]数簓组哩?中 D,?ch[0]:total1,ch[1]:choices1;ch[2]:total2,ch[3]:choices2\n"; for (int i = 0; i<4;i++) { cout << "the " << i+1 << " numbers: "; cin >> ch[i]; if (!cin) { cin.clear(); while(cin.get() != '\n') continue; cout << "bad input"; break; } else if (ch[i] < 0) break; } long double p1; p1 = probability(ch[0],ch[1]); long double p2; p2 = probability(ch[2],ch[3]); long double p3 ; p3 = p1 * p2 ; cout << "you have one chance in " << p3 << " of win.\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; } */ /*//7.13.3 #include struct box { char maker[40]; float height; float width ; float length ; float volume ; }; void display(box b1); void function(box * ps); int main() { box b1 = { "Jack H", 3.2, 2.3, 6.5 }; function(&b1); display(b1); return 0; } void display(box b1) { using namespace std;