#include<iostream>#include<new>#include"namesp.h"#include"golf.h"#include"string.h"usingnamespace std;//hw1***voidhw_1(){
golf an1;setgolf(an1,"Ann Birdfree",24);showgolf(an1);
golf andy[5];int count=0;while((count<5)&&(setgolf(andy[count]))){
count++;}for(int i=0;i<count;i++){showgolf(andy[i]);}int change;for(int i=0;i<count;i++){
cout <<"#"<<(i +1)<<":\n";
cout <<"Please enter the change:";
cin >> change;handicap(andy[i], change);showgolf(andy[i]);}}//hw1***//hw2***voidstrcount(const string &str){staticint total =0;//静态局部变量int count =0;
cout <<"\""<< str <<"\" contains ";while(str[count])//{
count++;}
total+=count;
cout << count <<" characters\n";
cout << total <<" characters total\n";}voidhw_2(){
string input;
cout<<"Enter a line :\n";getline(cin,input);while(input!=""){strcount(input);
cout <<"Enter next line (empty line to quit):\n";getline(cin, input);}}//hw2***//hw3***constint BUF =512;char buffer[BUF];struct chaff
{char dross[20];int slag;};voidget(chaff &str,char* ch){strcpy(str.dross, ch);
cout <<"Please enter the slag:";
cin >> str.slag;
cin.get();}voidshow(const chaff &str){staticint count =0;
cout <<"#"<<++count <<":\n";
cout <<"dross of the chaff is: "<< str.dross << endl;
cout <<"slag of the chaff is: "<< str.slag << endl;}voidhw_3(){
chaff *p1 =new(buffer) chaff[2];//buffer 爲 靜 態內存,不需要deletefor(int i=0;i<2;i++){staticint count=0;
cout <<"#"<<++count <<":\n";
cout <<"Please enter the dross:";char ch[20];
cin.getline(ch,20);get(p1[i],ch);}for(int i =0; i <2; i++)show(p1[i]);}//hw3***//hw4***constint ArSize =4;voidhw_4(){using SALES::Sales;//using SALES::setSales;using SALES::showSales;
Sales s1,s2;double a[ArSize];
cout<<"Please enter numbers: "<<endl;for(int i=0;i<ArSize;i++){
cin>>a[i];}setSales(s1,a,ArSize);showSales(s1);setSales(s2);showSales(s2);}//hw4***intmain(){//hw_1();//hw_2();//hw_3();hw_4();return0;}
golf.h
//// Created by pl on 2021/4/27.//#ifndef CH9_GOLF_H#define CH9_GOLF_H#endif//CH9_GOLF_H#include<iostream>#include<string>#include<cstring>usingnamespace std;//golf.h--for pe9-1.cppconstint Len =40;struct golf {char fullname[Len];int handicap;};voidsetgolf(golf &g,constchar*name,int hc);intsetgolf(golf & g);voidhandicap(golf &g,int hc);voidshowgolf(const golf &g);
golf.cpp
//// Created by pl on 2021/4/27.//#include"golf.h"voidsetgolf(golf &g,constchar*name,int hc){strcpy(g.fullname,name);
g.handicap=hc;}intsetgolf(golf & g){staticint i=0;//
cout<<"#"<<++i<<":\n";
cout<<"Please enter the name: ";
cin.getline(g.fullname,Len);//int temp=1;if(strcmp(g.fullname," ")==0){
temp=0;return0;}else{
cout<<"Please enter the grade: ";
cin>>g.handicap;
cin.get();//这样是为了简单的暂停screenreturn temp;}}voidhandicap(golf &g,int hc){
g.handicap=hc;}voidshowgolf(const golf &g){
cout<<"The Name of golf is "<<g.fullname<<endl;
cout<<"The Grade of golf if "<<g.handicap<<endl;}
namesp.h
//// Created by pl on 2021/4/27.//#ifndef CH9_NAMESP_H#define CH9_NAMESP_H#endif//CH9_NAMESP_H#include<iostream>usingnamespace std;namespace SALES
{constint QUARTERS =4;struct Sales
{double sales[QUARTERS];double average;double max;double min;};voidsetSales(Sales & s,constdouble ar[],int n);voidsetSales(Sales &s);voidshowSales(const Sales & s);}
namesp.cpp
//// Created by pl on 2021/4/27.//#include"namesp.h"namespace SALES
{voidsetSales(Sales &s,constdouble ar[],int n){double sum=0.0;for(int i=0;i<n;i++){
s.sales[i]=ar[i];
sum += s.sales[i];}
s.average=sum/n;
s.max=s.min=s.sales[0];for(int i=0;i<n;i++){if(s.max<s.sales[i]){
s.max=s.sales[i];}if(s.min>s.sales[i]){
s.min=s.sales[i];}}}voidsetSales(Sales &s){double sum =0.0;for(int i =0; i < QUARTERS; i++){
cout <<"Please enter sales:";
cin >> s.sales[i];
sum += s.sales[i];}
cin.get();
s.average = sum/QUARTERS;
s.max = s.min = s.sales[0];for(int i =0; i < QUARTERS; i++){if(s.max < s.sales[i])
s.max = s.sales[i];if(s.min > s.sales[i])
s.min = s.sales[i];}}voidshowSales(const Sales & s){for(int i=0;i<QUARTERS;i++){
cout<<s.sales[i]<<" "<<endl;
cout<<"The average is: "<<s.average<<endl;
cout <<"The max is: "<< s.max << endl;
cout <<"The min is: "<< s.min << endl;}}}