感觉变困难了很多,必须要注意细节,不如就会出各种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 << "调和平均为" << 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
发现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 << "平均成绩为:" << 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 << "输入普通号码池数量:\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
要理解指针的意义

#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 << "个计算结果为:" << 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学习——第七章编程练习相关推荐

  1. C++PrimerPlus学习——第四章编程练习

    **疫情期间学习C++ 4-1 需要使用cin.get()设置读取位数,避免空格导致无法读取多个词 #include <iostream> struct info_people //def ...

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

    17-1 不知道有没有理解错题意,参考list17.14 #include <iostream>int main() {using std::cout;using std::cin;usi ...

  3. C++PrimerPlus学习——第十三章编程练习

    13-1 注意char*前面加const,不然就会报错 Classis.h #ifndef CLASSIC_H_ #define CLASS_H_ #include <string> cl ...

  4. C++PrimerPlus学习——第十一章编程练习

    11-1 应该是修改list11.15,当当官方店买的,难道是盗版书吗... 打开file之后,操作跟cout类似 vect.h #ifndef VECT_h_ #define VECT_h_ #in ...

  5. C++PrimerPlus学习——第六章编程练习

    6-1 有个问题,如果输入的字符既不是数字也不是字母是不是应该原样输出呢? #include <iostream> #include <cctype>int main() {u ...

  6. c语言第七章作业,C语言学习第七章

    今天开始学习指针,指针在C语言中具有很重要的地位,按照老师所说,学C学不好指针跟没学一样,可见指针在C语言中的重要地位.废话不多说,首先我们先要知道什么是指针. 指针:指针是一个变量,它存储另一个对象 ...

  7. unixlinux大学教程学习 第七章

    unix&linux大学教程学习 第七章 学习了第七章. 主要讲解键盘信号 每一个键盘对应一个信号.主要的有: ^代表 Ctrl <Backspance> erase ,删除一个字 ...

  8. HTML学习第七章---XHTML

    HTML学习第七章-XHTML 教材:Head First HTML与CSS 关于XHTML XHTML(eXtensible HTML)是HTML的新标准,以可扩展的标记语言XML为基础(HTML也 ...

  9. C++ Primer Plus 第七章编程题练习

    C++ Primer Plus 第七章编程题练习 第一题 题目描述 编写一个程序,不断要求用户输入两个数,直到其中的一个为0.对于每两个数,程序将使用一个 函数来计算它们的调和平均数,并将结果返回给m ...

最新文章

  1. linux查看文件大小
  2. 使用kubeadm安装kubernetes高可用集群
  3. linux下运行python_在Linux命令行终端中使用python的简单方法(推荐)
  4. python爬虫问题:error: command 'gcc' failed with exit status 1
  5. 【文末福利】聊天机器人的几种主要架构实现
  6. 科创人·优艾智合创始人张朝辉:死磕细分行业Know-How,中国制造将引领全球移动机器人市场
  7. 三年java不会线程_Java后端开发三年多线程你都懂,问你异步编程你说你没听过???...
  8. 【数学】线性增长,指数增长,对数增长,幂增长
  9. java 给pdf文档加水印
  10. 违反开源项目GPL协议,法院判罚了!国内首例
  11. 自然语言处理中的Attention Model:是什么及为什么
  12. 致命打击:携号转网马上要实施,移动吐血!
  13. 科技公司产品专利申请介绍
  14. 还在用excel做应收应付管理吗?你的痛苦我都知道
  15. 关于设定校园二手租赁系统的计划、功能及建议
  16. Android利用WiFiManager扫描AP列表的实例
  17. php开源商城 yii,Yii2开源电商商城Fecshop
  18. 搭建在线帮助中心,轻松帮助客户解决问题
  19. python作品简单手工做法_教你用塑料瓶做一款简易的小汽车
  20. 异常Exception和异常日志笔记

热门文章

  1. codeMirror配置
  2. nginx启动与停止
  3. 面向.Net程序员的dump分析
  4. pjtool用到的数据库----oracle范畴
  5. 日均互动50万次 微信的营销的成功之道
  6. python爬虫常见报错_对Python爬虫常见工具总结,欢迎补充
  7. 计算机无法播放,如果无法播放计算机mp4文件怎么办?
  8. java 根据类名示例化类_Java MathContext类| 带示例的getRoundingMode()方法
  9. sumo的简单应用_sumo快速运行简单仿真实例详细教程
  10. java 获取service_Java service层获取HttpServletRequest工具类的方法