C++PrimerPlus学习——第七章编程练习
生活随笔
收集整理的這篇文章主要介紹了
C++PrimerPlus学习——第七章编程练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
感覺變困難了很多,必須要注意細節,不如就會出各種bug
7-1
7-2
發現while (std::cin >> si[length] && length <10)在輸入10個值的時候會無法退出,改為添加了一個break終止輸入
7-3
#include <iostream> struct box {char maker[40];float height;float width;float length;float volume; }; void displaybox(box boxs); void setvolume(box* boxs);int main() {box *box1 = new box;std::cout << "Please enter box maker and box info(height, width and length): \n";std::cin.get(box1->maker,40);std::cin >> (*box1).height >> (*box1).width >> (*box1).length;setvolume(box1);displaybox(*box1);delete box1;return 0; } void displaybox(box boxs) {std::cout << "The box maker is " << boxs.maker << ".\n";std::cout << "The height, width, and length of the box is " << boxs.height << ", " << boxs.width << ", and " << boxs.length << ".\n";std::cout << "The volume of the box is " << boxs.volume << ".\n"; } void setvolume(box* boxs) {boxs->volume = boxs->height * boxs->width * boxs->length; }7-4
#include <iostream> long double probability(unsigned int numbers, unsigned int pick);int main() {using std::cout;using std::cin;unsigned int n1, n2, p1, p2;cout << "輸入普通號碼池數量:\n";cin >> n1;cout << "輸入普通號碼選擇數量:\n";cin >> p1;cout << "輸入特殊號碼池數量:\n";cin >> n2;cout << "輸入特殊號碼選擇數量:\n";cin >> p2;long double probability_1 = probability(n1, p1);long double probability_2 = probability(n2, p1);cout << "中頭獎概率為:" << 1/(probability_1 * probability_2);return 0; } long double probability(unsigned int numbers, unsigned int pick) {long double result = 1.0;long double n;unsigned p;for (n = numbers, p = pick; p > 0; n--, p--)result = result * n / p;return result; }7-5
#include <iostream> long double factorial(unsigned int n);int main() {unsigned int n;std::cout << "輸入一個正整數:\n";std::cin >> n;long double result;result = factorial(n);std::cout << n << "! = " << result << "\n";return 0; } long double factorial(unsigned int n) {if (n == 0 || n == 1)return 1;elsereturn n * factorial(n - 1); }7-6
#include <iostream> int Fill_aray(double array[], int length); void Show_array(double array[], int length); void Reverse_array(double array[], int length);int main() {int n = 20;double array[20];n = Fill_aray(array, n);Show_array(array, n);Reverse_array(array, n);std::cout << "逆轉后的";Show_array(array, n);Reverse_array(array, n);Reverse_array(array+1, n - 2);std::cout << "逆轉除第一項和最后一項得到的";Show_array(array, n);return 0; } int Fill_aray(double array[], int length) {int i = 0;std::cout << "請輸入數字:(輸入非數字終止)\n";while (std::cin >> array[i]){i++;if (i >= length){std::cout << "數組已被填滿\n";break;}}return i; } void Show_array(double array[], int length) {std::cout << "數組為:";for (int i = 0; i < length; i++){std::cout << array[i] << " ";}std::cout << std::endl; } void Reverse_array(double array[], int length) {double temp;for (int i = 0; i < length / 2; i++){temp = array[i];array[i] = array[length - i - 1];array[length - i - 1] = temp;} }7-7
要理解指針的意義
7-8
//(a) #include <iostream> #include <array> #include <string>const int Seasons = 4; const char* Snames[Seasons] = { "Spring", "Summer", "Fail", "Winter" };void fill(double ex[]); void show(double ex[]);int main() {double expenses[4];fill(expenses);show(expenses);return 0; }void fill(double ex[]) {using namespace std;for (int i = 0; i < Seasons; i++){cout << "Enter " << Snames[i] << " expenses: ";cin >> ex[i];} }void show(double ex[]) {using namespace std;double total = 0.0;cout << "\nEXPENSES\n";for (int i = 0; i < Seasons; i++){cout << Snames[i] << ": $" << ex[i] << endl;total += ex[i];}cout << "Total Expenses: $" << total << endl; } //(b) #include <iostream> #include <array> #include <string>const int Seasons = 4; const char* Snames[Seasons] = { "Spring", "Summer", "Fail", "Winter" };struct expense {double exp[Seasons]; };void fill(expense* ex); void show(expense* ex);int main() {expense* totalExpenses = new expense;fill(totalExpenses);show(totalExpenses);delete totalExpenses;return 0; }void fill(expense* ex) {using namespace std;for (int i = 0; i < Seasons; i++){cout << "Enter " << Snames[i] << " expenses: ";cin >> ex->exp[i];} }void show(expense* ex) {using namespace std;double total = 0.0;cout << "\nEXPENSES\n";for (int i = 0; i < Seasons; i++){cout << Snames[i] << ": $" << ex->exp[i] << endl;total += ex->exp[i];}cout << "Total Expenses: $" << total << endl; }7-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); void display1(student st); void display2(const student* ps); 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;cout << "Done\n";return 0; } int getinfo(student pa[], int n) {using namespace std;int i;for (i = 0; i < n; i++){cout << "Student # " << i + 1 << "\n";cout << "fullname: ";cin.getline(pa[i].fullname, SLEN);cout << "hobby: ";cin.getline(pa[i].hobby, SLEN);cout << "ooplevel: ";cin >> pa[i].ooplevel;cin.get();cout << endl;}return i; } void display1(student st) {using namespace std;cout << "display1";cout << "\nStudent # " << ":";cout << " fullname: " << st.fullname;cout << " hobby: " << st.hobby;cout << " ooplevel: " << st.ooplevel;cout << endl; } void display2(const student* ps) {using namespace std;cout << "display2";cout << "\nStudent #" << ":";cout << " fullname: " << ps->fullname;cout << " hobby: " << ps->hobby;cout << " ooplevel: " << ps->ooplevel;cout << endl; } void display3(const student pa[], int n) {cout << "display3\n";for (int i = 0; i < n; i++){cout << "Student #" << i + 1 <<":";cout << "\nfullname: " << pa[i].fullname;cout << "\nhobby: " << pa[i].hobby;cout << "\nooplevel: " << pa[i].ooplevel;cout << endl;} }7-10
#include <iostream> const int n = 3; double calculate(double x, double y, double (*pf)(double, double)); double add(double x, double y); double substract(double x, double y); double multi(double x, double y);int main() {using namespace std;double x, y;double(*pf[n])(double, double) = { add, substract, multi };cout << "Enter two numbers: \n";while (cin >> x >> y){for (int i = 0; i < n; i++){double result;result = calculate(x, y, pf[i]);cout << "第" << i + 1 << "個計算結果為:" << result << endl;}} }double calculate(double x, double y, double (*pf)(double, double)) { return pf(x, y); }double add(double x, double y) {return x + y; }double substract(double x, double y) {return x - y; }double multi(double x, double y) {return x * y; }總結
以上是生活随笔為你收集整理的C++PrimerPlus学习——第七章编程练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言添加变量到数据库+a+ +b+ 的
- 下一篇: docker rabbitmq_使用Do