日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++PrimerPlus学习——第七章编程练习

發(fā)布時間:2025/3/11 c/c++ 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++PrimerPlus学习——第七章编程练习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

感覺變困難了很多,必須要注意細(xì)節(jié),不如就會出各種bug
7-1

#include <iostream> double average(double a, double b);int main() {using namespace std;double a, b;a = b = 0;cout << "Enter ta number:" << endl;cin >> a;cout << "Enter another number: " << endl;while (cin >> b && b != 0){double c = average(a, b);cout << "調(diào)和平均為" << c << endl;cout << "Enter another number: " << endl;a = b;}return 0; }double average(double x, double y) {double c;c = 2.0 * x * y / (x + y);return c; }

7-2
發(fā)現(xiàn)while (std::cin >> si[length] && length <10)在輸入10個值的時候會無法退出,改為添加了一個break終止輸入

#include <iostream> struct box {char maker[40];float height;float width;float length;float volume; }; int scoreinput(double si[]); void scoreoutput(double so[], int length); void averagescore(double as[], int length);int main() {double score[10];int a = 0;a = scoreinput(score);scoreoutput(score, a);averagescore(score, a);return 0; }int scoreinput(double si[]) {int length = 0;std::cout << "Please enter the record: (non-number to stop)\n";while (std::cin >> si[length]){length++;if (length >= 10)break;}return length; } void scoreoutput(double so[], int length) {std::cout << "The scores are ";for (int i = 0; i < length; i++)std::cout << so[i] << " ";std::cout << std::endl; } void averagescore(double as[], int length) {double sum = 0;for (int i = 0; i < length; i++)sum += as[i];std::cout << "平均成績?yōu)?#xff1a;" << sum / length << std::endl; }

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 << "輸入普通號碼池數(shù)量:\n";cin >> n1;cout << "輸入普通號碼選擇數(shù)量:\n";cin >> p1;cout << "輸入特殊號碼池數(shù)量:\n";cin >> n2;cout << "輸入特殊號碼選擇數(shù)量:\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 << "輸入一個正整數(shù):\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 << "逆轉(zhuǎn)后的";Show_array(array, n);Reverse_array(array, n);Reverse_array(array+1, n - 2);std::cout << "逆轉(zhuǎn)除第一項和最后一項得到的";Show_array(array, n);return 0; } int Fill_aray(double array[], int length) {int i = 0;std::cout << "請輸入數(shù)字:(輸入非數(shù)字終止)\n";while (std::cin >> array[i]){i++;if (i >= length){std::cout << "數(shù)組已被填滿\n";break;}}return i; } void Show_array(double array[], int length) {std::cout << "數(shù)組為:";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
要理解指針的意義

#include <iostream> const int Max = 5; double* fill_array(double* begin, double* end); void show_array(double* begin, double* end); void revalue(double r, double* begin, double* end);int main() {using namespace std;double properties[Max];double* end = fill_array(properties, properties + Max);show_array(properties, end);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, end);show_array(properties, end);cout << "Done.\n";cin.get();cin.get();return 0; }double* fill_array(double* begin, double* end) {using namespace std;double* i;int j = 0;for (i = begin; i != end; i++){cout << "Enter value #" << (j + 1) << ":";cin >> *i;if (!cin){cin.clear();while (cin.get() != '\n')continue;cout << "Bad input; input process terminated.\n";break;}else if (*i < 0) break;j++;}return i; } void show_array(double* begin, double* end) {using namespace std;;int j = 0;for (double* i = begin; i != end; i++){cout << "Property#" << (j + 1) << ": $";cout << *i << endl;j++;} } void revalue(double r, double* begin, double* end) {for (double* i = begin; i != end; i++)(*i) *= r; }

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 << "個計算結(jié)果為:" << 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; }

總結(jié)

以上是生活随笔為你收集整理的C++PrimerPlus学习——第七章编程练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。