#include<iostream>usingnamespace std;#include"Sales.h"#include<set>namespace SALES
{//以交互方式收集4个季度的Sales,将其存储在s的Sales中//并计算和存储平均值、最大值和最小值Sales::Sales(){double maxs =-1, mins =999999, total =0;for(int i =0; i <4;++i){
cout <<"Enter "<< i +1<<" quarter's sales:\n";
std::cin >> sales[i];
maxs = maxs < sales[i]? sales[i]: maxs;
mins = mins > sales[i]? sales[i]: mins;
total += sales[i];}
max = maxs;
min = mins;
average = total /4;}//从数组ar中复制4个或n个中的较小者//并计算和存储所输入项目的平均值、最大值和最小值//Sales的剩余要素(如果有)设置为0Sales::Sales(constdouble ar[],int n){double maxs =-1, mins =999999, total =0;if(n <=4){for(int i =0; i < n; i++){
sales[i]= ar[i];
maxs = maxs > sales[i]? maxs : sales[i];
mins = mins < sales[i]? mins : sales[i];
total += sales[i];}for(int i = n; i <4; i++){
sales[i]=0;
maxs = maxs > sales[i]? maxs : sales[i];
mins = mins < sales[i]? mins : sales[i];}
max = maxs;
min = mins;}else{//对数组 ar 进行排序 冒泡排序double* arr =newdouble[n];for(int i =0; i < n; i++){
arr[i]= ar[i];}for(int i =0; i < n -1; i++){for(int j =0; j < n - i -1; j++){if(arr[j]> arr[j +1]){double temp = arr[j];
arr[j]= arr[j +1];
arr[j +1]= temp;}}}//然后取出前4个for(int i =0; i <4; i++){
sales[i]= arr[i];
total += sales[i];}
max = sales[3];
min = sales[0];}
average = total /4;
cout <<"average = "<< average <<"\t"<<"max = "<< max <<"\t"<<"min = "<< min << endl;}//展示 s 结构体中所有信息voidSales::showSales()const{for(int i =0; i <4; i++){
cout <<"#"<< i +1<<"quarter sales:"<< sales[i]<< endl;}
cout <<"average = "<< average <<"\t"<<"max = "<< max <<"\t"<<"min = "<< min << endl;}}
#include<iostream>#include"ExerciseStack.h"usingnamespace std;ExerciseStack::ExerciseStack(){
top =0;}boolExerciseStack::Isempty()const{return top ==0;}boolExerciseStack::Isfull()const{return top == MAX;}boolExerciseStack::push(const Item& item){if(top < MAX){
items[top++]= item;returntrue;}elsereturnfalse;}boolExerciseStack::pop(Item& item){if(top >0){
item = items[--top];//注意 -- 的位置在前边。在后错误returntrue;}elsereturnfalse;}
exercise05.cpp
// stacker.cpp -- testing the Stack class#include<iostream>#include<cctype>// or ctype.h#include"ExerciseStack.h"intmain(){usingnamespace std;
ExerciseStack est;// create an empty stackchar ch;
Item po;double total =0;
cout <<"Please enter A to add a purchase order,\n"<<"P to process a PO, or Q to quit.\n";while(cin >> ch &&toupper(ch)!='Q'){while(cin.get()!='\n')continue;if(!isalpha(ch)){
cout <<'\a';continue;}switch(ch){case'A':case'a':if(est.Isfull())
cout <<"stack already full\n";else{
cout <<"Enter fullname: ";
cin.get(po.fullname,35);
cout <<"Enter payment: ";
cin >> po.payment;
est.push(po);}break;case'P':case'p':if(est.Isempty())
cout <<"stack already empty\n";else{
est.pop(po);
cout <<"PO "<< po.fullname <<"'s payment "<< po.payment <<" popped\n";
total += po.payment;
cout <<"Total payment: "<< total << endl;}break;}
cout <<"Please enter A to add a purchase order,\n"<<"P to process a PO, or Q to quit.\n";}
cout <<"Bye\n";return0;}
6
Move.h
#pragmaonce#include<iostream>usingnamespace std;classMove{private:double x;double y;public:Move(double a =0,double b =0);voidshowmove()const;
Move add(const Move& m)const;voidreset(double a =0,double b =0);};
exercise06_Move.cpp
#include<iostream>usingnamespace std;#include"Move.h"// set x,y to a,bMove::Move(double a,double b){
x = a;
y = b;}// show current x,y valuesvoidMove::showmove()const{
cout <<"x = "<<this->x <<"\t"<<"y = "<<this->y << endl;}
Move Move::add(const Move& m)const{returnMove(x + m.x, y + m.y);}// reset x,y to a,b//形参列表里写 double b = 0 出错,重定义参数voidMove::reset(double a,double b){
x = a;
y = b;}
C++自学笔记(51 C++ Primer Plus(第六版)中文版编程练习答案)51 C++ Primer Plus(第六版)第十章 编程练习答案1BankAccount.hexercise01_BankAccount.cppexercise01.cpp2Person.hexercise02_Person.cppexercise02.cpp3golf.hexercise03_golf.cppexercise01.cpp4Sales.hexercise04_Sales.cppexercise04.cpp5E