老样子
复习题+编程练习
//7.12 //1 定义函数,提供原型,调用函数 //2 void igor(); float tofu(int); double mpg(double, double); long sumation(long[], int n); double doctor(const char[]); void ofcourse(boss); char* plot(map*); //3 void becint(int arr[], int n, int value) { for (int i = 0; i < n; i++) arr[i] = value; } //4 void becint(int* begin, int* end, int value) { int* pt; for (pt = begin; pt != end; pt++) *pt = value; } //5 double returnmax(const double arr[], int size) { if (size < 1) { std::cout << "Invalid size of array:" << size << endl; std::cout << "Returning a value of 0\n"; return 0; } else { double max; max = arr[0]; for (int i = 0; i < size; i++) if(arr[i] > max) max = arr[i]; return max; } } //6 将const限定符用于指针,可以防止原始数据被修改。但是当程序传递基本类型时,将按值传递 以便函数使用副本。这样原始数据将得到保护 //7 字符串可以存储在char数组中 可以用带双引号的字符串表示 可以用指向字符串第一个字符的指针表示 //8 int replace(char* str, char c1, char c2) { int num = 0; while (str != '\0') { if (str == c1) { str = c2; num++; } str++; } return num; } //9 字符串解释为第一个元素的地址,因此第一个表示p的地址,第二个表示c的地址 换句话说,字符串常量和数组名的行为相同 //10 按值传递直接传递结构名glitz,按地址传递要使用地址运算符&glitz 按值传递自动保护原始数据,但是要以时间和内存为代价 按地址传递可以节省时间和内存,但不能保护原始数据除非使用const限定符 按值传递可以使用常规的结构成员表示法,按地址必须使用间接成员运算符 //11 int judge(int (*pf)(const char*)); //12 void fa(applicant a) { std::cout << a.name << endl; for (int i = 0; i < 3; i++) std::cout << a.credit_ratings[i] << endl; } void fb(const applicant* pa) { std::cout << pa->name << endl; for (int i = 0; i < 3; i++) std::cout << pa->credit_ratings[i] << endl; } //13 typedef void (*p_f1)(applicant* a); p_f1 p1 = f1; typedef const char* (*p_f2)(const applicant* a1, const applicant* a2); p_f2 p2 = f2; p_f1 ap[5]; p_f2(*pa)[10]; //pa是指向(包含10个p_f2类型指针的数组)的指针 //end
唉,实在太太太太长了,复制都麻烦
Shaift+Alt选取竖行,因为都要主函数,又不能一个题建一个项目太麻烦了,所以批量注释掉了,不过你们选取竖行就很好复制了,也可以删了注释线再复制
////practice 1 //#include<iostream> //using namespace std; // //double tiaohe(double a, double b); // //int main() //{ // double a, b; // cout << "Enter two numbers(quit by 0): "; // cin >> a >> b; // while (a != 0 && b != 0) // { // double th = tiaohe(a, b); // cout << "Their harmonic mean is: " << th << endl; // cout << "Enter two more numbers: "; // cin >> a >> b; // } // cout << "Done!"; // // return 0; //} // //double tiaohe(double a, double b) //{ // return 2 * a * b / (a + b); //} ////practice 2 //#include<iostream> //using namespace std; // //const int SIZE = 10; // //int input(double*, int size); //void display(double*, int size); //void ave(double*, int size); // //int main() //{ // double arr[SIZE]; // int size = input(arr, SIZE); // display(arr, size); // ave(arr, size); // // return 0; //} // //int input(double* arr, int size) //{ // int i = 0; // for (i; i < size; i++) // { // double score; // cout << "Enter the grade(quit by minus): "; // cin >> score; // if (!cin) //if不是循环,break直接跳出整个for循环 // { // cin.clear(); // while (cin.get() != '\n') // continue; // cout << "Error!\n"; // break; // } // else if (score < 0) // break; // arr[i] = score; // } // return i; //} // //void display(double* arr, int size) //{ // for (int i = 0; i < size; i++) // cout << arr[i] << " "; //} // //void ave(double*arr, int size) //{ // double sum=0; // for (int i = 0; i < size; i++) // sum += arr[i]; // cout << "The average is: " << sum / size; //} ////practice 3 //#include<iostream> // //using namespace std; // //struct box //{ // char maker[40]; // float height; // float width; // float length; // float volume; //}; // //void display(const box b) //{ // cout << b.maker << endl; // cout << b.height << endl; // cout << b.width << endl; // cout << b.length << endl; // cout << b.volume << endl; //} // //void volume(box* pb) //{ // pb->volume = pb->height * pb->width * pb->length; //} // //int main() //{ // box bbb = { "yuetian",10,10,5 }; // display(bbb); // volume(&bbb); // cout << "The volume of yuetian's box is: " << bbb.volume; // // return 0; //} ////practice 4 //#include<iostream> //using namespace std; // //long double probability(unsigned numbers, unsigned picks); // //int main() //{ // unsigned total, choices; // cout << "Enter the total number of choices on the game card and\n" // "the number of picks allowed:\n"; // // int i = 0; // long double arr[2] = {}; // while (i < 2 && (cin >> total >> choices) && choices < total) // //i<2的条件要放在前面,否则先判断输入,必须输入值才会继续判断,才会输出总概率 // { // cout << "You have one change in "; // arr[i] = probability(total, choices); // cout << arr[i]; // cout << " of winning.\n"; // i++; // } // cout << "The final winning probability is: " << arr[0] * arr[1] << endl; // cout << "Bye!"; // return 0; //} // //long double probability(unsigned numbers, unsigned picks) //{ // long double result = 1; // long double n; // unsigned p; // for (n = numbers, p = picks; p > 0; n--, p--) // result = result * n / p; // // return result; //} ////practice 5 //#include<iostream> // //using namespace std; // //unsigned long f(unsigned n) //{ // long int factorial = 0; // if (n == 0 || n == 1) // factorial = 1; // else // factorial = n * f(n - 1); // return factorial; //} // //int main() //{ // unsigned num = 0; // cout << "Enter a number to calculate its factorial(q to quit): "; // while (1) // { // if (!(cin >> num)) // { // cin.clear(); // while (cin.get() != '\n') // continue; // cout << "Error!"; // break; // } // else // cout << "The factorial is: " << f(num) << endl; // } // return 0; //} ////practice 6 //#include<iostream> // //using namespace std; // //int Fill_array(double* arr, int size) //{ // int i = 0; // for (i; i < size; i++) // { // cout << "Enter a double value(char to end early): "; // if (cin >> arr[i]); // else // { // cin.clear(); // while (cin.get() != '\n') // continue; // cout << "Error!"; // break; // } // } // return i; //} // //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) //{ // double substitute = 0; // for (int i = 0; i < size/2; i++) // { // substitute = arr[i]; // arr[i] = arr[size - i - 1]; // arr[size - i - 1] = substitute; // } //} // //int main() //{ // double arr[10]; // int size = Fill_array(arr, 10); // Show_array(arr, size); // // Reverse_array(arr, size); // Show_array(arr, size); // // Reverse_array(arr+1, size-2); // Show_array(arr, size); // // return 0; //} ////practice 7 //#include<iostream> // //const int MAX = 5; // //double* fill_array(double arr[], double arrend[]); //void show_array(const double arr[], const double arrend[]); //void revalue_array(double r, double arr[], double arrend[]); // //using namespace std; // //int main() //{ // double properties[MAX]; // // double* end = fill_array(properties, properties + MAX); // show_array(properties, end); // if ((end - properties) > 0) // { // cout << "Enter revaluation factor: "; // double factor; // while (!(cin >> factor)) // { // cin.clear(); // while (cin.get() != '\n') // continue; // cout << "Bad input;pleas enter a number: "; // } // revalue_array(factor, properties, end); // show_array(properties, end); // } // cout << "Done!\n"; //// cin.get(); //// cin.get(); // return 0; //} // //double* fill_array(double arr[], double arrend[]) //{ // double temp; // int i; // for (i = 0; i < arrend - arr; 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; //} // //void show_array(const double arr[], const double arrend[]) //{ // for (int i = 0; i < arrend - arr; i++) // { // cout << "Property#" << i + 1 << ": $"; // cout << arr[i] << endl; // } //} // //void revalue_array(double r, double arr[], double arrend[]) //{ // for (int i = 0; i < arrend - arr; i++) // arr[i] *= r; //} ////practice 8 //#include<iostream> //using namespace std; // //const int Seasons = 4; //const char* Snames[4] = //{ "Spring","Summer","Fall","Winter" }; // //void fill1(double* expenses, int size); //void show1(double* expenses, int size); // //struct expenses //{ // double ex[Seasons]; //}; // //void fill2(expenses* pex); //void show2(expenses* pex); // ////int main() ////{ //// double expenses[Seasons]; //// fill1(expenses, Seasons); //// show1(expenses, Seasons); //// return 0; ////} // //int main() //{ // expenses ex; // fill2(&ex); // show2(&ex); // return 0; //} // //void fill1(double* expenses, int size) //{ // for (int i = 0; i < size; i++) // { // cout << "Enter " << Snames[i] << " expenses: "; // cin >> expenses[i]; // } //} // //void show1(double* expenses, int size) //{ // double total = 0; // cout << "EXPENSES\n"; // for (int i = 0; i < size; i++) // { // cout << Snames[i] << " $: " << expenses[i] << endl; // total += expenses[i]; // } // cout << "Total Expenses :$" << total << endl; //} // // //void fill2(expenses* pex) //{ // for (int i = 0; i < Seasons; i++) // { // cout << "Enter " << Snames[i] << " expenses: "; // cin >> pex->ex[i]; // } //} // //void show2(expenses* pex) //{ // double total = 0; // cout << "EXPENSES\n"; // for (int i = 0; i < Seasons; i++) // { // cout << Snames[i] << " $: " << pex->ex[i] << endl; // total += pex->ex[i]; // } // cout << "Total Expenses :$" << total << endl; //} ////practice 9 //#include<iostream> //using namespace std; //const int Slen = 30; //struct student //{ // char fullname[Slen]; // char hobby[Slen]; // int ooplevel; //}; // //int getinfo(student pa[], int n) //{ // int i; // cout << "Enter the data about student:\n"; // // for (i = 0; i < n; i++) // { // cout << "The fullname: "; // cin.getline(pa[i].fullname, Slen); // cout << "The hobby: "; // cin.getline(pa[i].hobby, Slen); // cout << "The ooplevel: "; // cin >> pa[i].ooplevel; // cin.get(); // } // return i; //} // //void display1(student st) //{ // cout << "The student's fullname: " << st.fullname << endl; // cout << "The hobby: " << st.hobby << endl; // cout << "The ooplevel: " << st.ooplevel << endl; //} // //void display2(const student* ps) //{ // cout << "The student's fullname: " << ps->fullname << endl; // cout << "The hobby: " <<ps->hobby << endl; // cout << "The ooplevel: " << ps->ooplevel << endl; //} // //void display3(const student pa[], int n) //{ // for (int i = 0; i < n; i++) // { // cout << "The "<<i+1<<" student's fullname: " << pa[i].fullname << endl; // cout << "The hobby: " << pa[i].hobby << endl; // cout << "The ooplevel: " << pa[i].ooplevel << endl; // } //} // //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; // cout << "Done!\n"; // return 0; //} //practice 10 #include<iostream> using namespace std; double add(double a, double b) { return a + b; } double substract(double a, double b) { return a - b; } double multiply(double a, double b) { return a * b; } double calculate(double a, double b, double(*pf)(double, double)) { return pf(a, b); } int main() { while (1) { double a = 0, b = 0; cout << "Enter two number to chalculate(quit by char): "; cin >> a >> b; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "Quit!\n"; break; } cout << "The result 1 is: " << calculate(a, b, add) << endl; cout << "The result 2 is: " << calculate(a, b, substract) << endl; cout << "The result 3 is: " << calculate(a, b, multiply) << endl; } return 0; }