以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆。仅供参考,DEV C++已通过编译运行

练习1

#include<iostream>
using namespace std;
double avg(double, double);int main()
{double num1, num2;cout << "Enter 2 numbers:";while (cin >> num1 >> num2){if (num1 == 0 || num2 == 0)break;elsecout << avg(num1, num2) << " is the average." << endl;cout << "Enter 2 numbers: ";}return 0;
}double avg(double number1, double number2)
{double ravg;ravg = 2.0 * number1 * number2 / (number1 + number2);return ravg;
}

练习2

#include<iostream>
const int maxsize = 10;
using namespace std;void show(int ar[], int size);
int input(int ar[],int size);
double avg(int ar[], int size);int main()
{int golf[maxsize];int acsize;acsize = input(golf,maxsize);show(golf, acsize);cout << "golf average score is : " << avg(golf, acsize) << endl;return 0;
}int input(int ar[],int size)//返回实际数组数以供后续函数调用该实际数
{int cnt = 0;cout << "Enter golf score#" << cnt+1 << " : ";while ((cin >> ar[cnt]) && cnt < size){++cnt;cout << "Enter golf score#" << cnt+1 << " : ";}return cnt;
}void show(int ar[], int size)
{int cnt;cout << "Golf score is: ";for (cnt = 0; cnt < size; cnt++)cout << ar[cnt] << "  ";cout << endl;
}double avg(int ar[], int size)
{int cnt, sum, average;for (cnt = 0,sum = 0; cnt < size; cnt++)sum += ar[cnt];average = sum / size;return average;
}

练习3

#include<iostream>
using namespace std;struct box
{char maker[40];float height;float width;float length;float volume;
};void show(box abox);
void showvolume(box* pbox);int main()
{box xbox;cout << "Enter name: ";cin.getline(xbox.maker, 40);cout << "Enter height: ";cin >> xbox.height;cout << "Enter width: ";cin >> xbox.width;cout << "Enter length: ";cin >> xbox.length;showvolume(&xbox);show(xbox);return 0;
}void show(box abox)
{cout << "Name: " << abox.maker << endl;cout << "Height: " << abox.height << endl;cout << "Width: " << abox.width << endl;cout << "Length: " << abox.length << endl;cout << "Volume: " << abox.volume << endl;
}void showvolume(box* pbox)
{pbox->volume = pbox->height * pbox->length * pbox->width;
}

练习4

#include<iostream>long double probability(unsigned numbers, unsigned picks, unsigned special);int main()
{using namespace std;unsigned  total, choice, schoice;cout << "Enter the total and special number of choices and the pick allowed:" << endl;while ((cin >> total >> choice >> schoice) && choice <= total){cout << "You have one chance in ";cout << probability(total, choice, schoice);cout << " of winning." << endl;cout << "Next one: ";}cout << "Bye!" << endl;return 0;
}long double probability(unsigned numbers, unsigned picks, unsigned special)
{long double result = 1.0;long double n;unsigned p;for (n = numbers, p = picks; p > 0; n--, p--)result *= (n / p);result *= special;return result;
}

练习5

#include<iostream>
using namespace std;long Factorial(int n);int main()
{int num;cout << "Enter a number(alpha to quit):";while ((cin >> num) && num>=0){cout << num << "!= " << Factorial(num) << endl;cout << "NEXT: ";}return 0;
}long Factorial(int n)
{long fac;if (n > 0)fac = n * Factorial(n - 1);if (n == 0)//将递归最底层的fac(0)=1,否则fac会返回0;fac = 1;return fac;//vs2019显示此处C6001警告,目前暂不清楚原因,但不影响。
}

练习6

#include<iostream>
using namespace std;
const int maxsize = 10;
int Fill_array(double*, int);
void Show_array(double*, int);
void Reverse_array(double*, int);int main()
{double Array[maxsize];int size;size = Fill_array(Array, maxsize);Show_array(Array, size);Reverse_array(Array, size);Show_array(Array, size);return 0;
}int Fill_array(double* array, int n)
{int count = 0;cout << "Enter array elements#" << count + 1 << " : ";while ((cin >> array[count]) && count < n){++count;cout << "Enter array elements#" << count + 1 << " : ";}return count;
}void Show_array(double* array, int n)
{int count = 0;for (; count < n; ++count)cout << "array[" << count + 1 << "] = " << array[count] << endl; \cout << endl;
}void Reverse_array(double* array, int n)
{double temp;int begin, end;for (begin = 1, end = n - 2; begin < (n / 2); ++begin, --end){temp = array[begin];array[begin] = array[end];array[end] = temp;}
}

练习7

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

练习8-1

#include<iostream>
using namespace std;
const int season = 4;
const char *pseason[season] = { "Spring","Summer","Fall","Winter" };
void fill(double*, int);
void show(double*, int);int main()
{double Array[season];fill(Array, season);show(Array, season);return 0;
}void fill(double* array, int n)
{for (int i = 0; i < season; ++i){cout << "Enter " << pseason[i] << " espenses: ";cin >> array[i];}
}void show(double* array, int n)
{double total = 0;cout << "\nEXPENSES\n";for (int i = 0; i < season; ++i){cout << pseason[i] << ":$ " << array[i] << endl;total += array[i];}cout << "Total Expenses :$ " << total << endl;
}

练习8-2

#include<iostream>
using namespace std;
struct expense
{double money;
};
const int season = 4;
const char* pseason[season] = { "Spring","Summer","Fall","Winter" };void fill(double*);
void show(double*);int main()
{expense Money[season];fill(&Money[0].money);show(&Money[0].money);return 0;
}
void fill(double* array)
{for (int i = 0; i < season; ++i){cout << "Enter " << pseason[i] << " espenses: ";cin >> array[i];}
}
void show(double* array)
{double total = 0;cout << "\nEXPENSES\n";for (int i = 0; i < season; ++i){cout << pseason[i] << ":$ " << array[i] << endl;total += array[i];}cout << "Total Expenses :$ " << total << endl;
}

练习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)
{int count = 0;cout << "Enter name: ";while ((cin.getline(pa[count].fullname, SLEN)) && count < n){cout << "Enter hobby: ";cin.getline(pa[count].hobby, SLEN);cout << "Enter ooplevel: ";cin >> pa[count].ooplevel;cin.get();count++;if (count < n)cout << "Enter name: ";elsebreak;}cout << "Input complete!" << endl;return count;
}
void display1(student st)
{cout << "Name: " << st.fullname << endl;cout << "Hobby: " << st.hobby << endl;cout << "Ooplevel: " << st.ooplevel << endl;
}
void display2(const student* ps)
{cout << "Name: " << ps->fullname << endl;cout << "Hobby: " << ps->hobby << endl;cout << "Ooplevel: " << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{int count = 0;for (; count < n; ++count){cout << "Name: " << pa[count].fullname << endl;cout << "Hobby: " << pa[count].hobby << endl;cout << "Ooplevel: " << pa[count].ooplevel << endl;}
}

练习10

#include<iostream>
using namespace std;
double calculate(double num1, double num2, double(*pfun)(double num1, double num2));
double add(double num1, double num2);
double subtract(double num1, double num2);int main()
{double num1, num2;double (*pfun[2])(double num1, double num2);pfun[0] = add;pfun[1] = subtract;cout << "Enter 2 numbers : " << endl;while (cin >> num1 >> num2){for (int cnt = 0; cnt < 2; ++cnt)cout << "Result is: " << (*pfun[cnt])(num1, num2) << endl;}cout << "Complete!" << endl;return 0;
}double calculate(double num1, double num2, double(*pfun)(double num1, double num2))
{return (*pfun)(num1, num2);
}double add(double num1, double num2)
{return num1 * num1 + num2 * num2;
}double subtract(double num1, double num2)
{return num1 * num1 - num2 * num2;
}

2020 我的C++学习之路 C++PrimerPlus第七章课后习题相关推荐

  1. 2020 我的C++学习之路 C++PrimerPlus第十一章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 .第5题第6题改写过于麻烦,机械性重复就不写了,写的犯恶心. 练习1 //p1.h #in ...

  2. 2020 我的C++学习之路 C++PrimerPlus第六章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 #include<iostream> #include<cc ...

  3. 2020 我的C++学习之路 C++PrimerPlus第五章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 #include<iostream>int main() {int ...

  4. 2020 我的C++学习之路 C++PrimerPlus第四章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行. 练习1 #include <iostream> int main() {us ...

  5. 2020 我的C++学习之路 C++PrimerPlus第十章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 //p1.h #include<string>class Acco ...

  6. 2020 我的C++学习之路 C++PrimerPlus第九章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 //golf.h const int Len = 40; struct gol ...

  7. 2020 我的C++学习之路 C++PrimerPlus第八章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1:题目我是看了好久才反应过来,不得不说翻译的不太行 #include<io ...

  8. 鸟哥的Linux私房菜基础学习篇(第二版)第二章课后习题与答案

    习题: 1.计算机总是出现问题,有一个错误信息为"fatal:SASL per- connection security setup",请找出可能的原因. 答:先跑到 http:/ ...

  9. MYSQL学习笔记(自用)第七章

    MYSQL学习笔记(自用)第七章 第一节.创建视图| Creating Views USE sql_invoicing;CREATE VIEW sales_by_client AS SELECT c. ...

最新文章

  1. 常用Linux命令总结
  2. memcached企业面试题
  3. Android 之窗口小部件高级篇--App Widget 之 RemoteViews - 跨到对岸去
  4. 打印dataframe的前十行_小学生之十行Python解高思五星题(一)
  5. C和C++中include 搜索路径的一般形式以及gcc搜索头文件的路径
  6. sklearn模型评估
  7. c++引用专题之常引用
  8. gwt格式_活性GWT
  9. 硬核!这所大学包下高铁,接滞留湖北的学生返校!
  10. 推荐一些不错的公众号【三】
  11. 一个***与电脑白痴的爱情故事
  12. 微博html5版转换成网页版,微博网页版html5入口
  13. c语言编程 设计密码锁,单片机控制安全密码锁的设计(附程序,pcb,原理图)
  14. spss数据分析软件
  15. 【阿里云IoT YF3300】6.物联网设备报警配置
  16. java 导出多个excel_Java导出Excel压缩包
  17. 从“洗脸巾”到“湿厕纸”,生活用纸的品类扩张之路
  18. 高数__已知2个平面方程, 求这2个平面的夹角
  19. Java实现局域网流量监控
  20. 产品经理:一个商业 AIoT 智能硬件产品的完整拆解

热门文章

  1. 栈——用链表实现栈操作
  2. C语言中compile time assert的实现
  3. [register]-ARMV8系统中通用寄存器和系统寄存器的介绍和总结
  4. go项目中使用数据库的配置文件
  5. babymips(下) 寒假逆向生涯(14/100)
  6. 【网络安全】如何利用工具发现内部或外部网络攻击面
  7. Windows驱动开发学习笔记(三)—— 内核空间内核模块
  8. 开源一个自写的病毒技术工具集
  9. MySQL默认值(DEFAULT)
  10. 1.16 快速排序法(Quicksort)