charpter09_homework.cpp
#include <iostream>
#include <string>
#include <cstring>
#include "golf.h"
#include "sales.h"
using namespace std;
using namespace SALES;
//函数原型
void p9_1(void);
void p9_2(void);
void p9_3(void);
void p9_4(void);
//主函数
int main()
{
//p9_1();
//p9_2();
//p9_3();
p9_4();
return 0;
}
//-----------作业一--------------
void p9_1(void)
{
golf arr[3];
int i;
int num=0;
for(i=0;i<3;i++)
num=setgolf(arr[i]);
handicap(arr[0],20);
for(i=0;i<num;i++)
showgolf(arr[i]);
return;
}
//------------作业二---------------
const int ArSize=10;
void strcount(const string str)
{
static int total=0;
int count=str.length();
total+=count;
cout<<str<<endl;
cout<<"count= "<<count<<endl;
cout<<"total= "<<total<<endl;
}
void p9_2(void)
{
string str;
cout<<"Enter a line: ";
getline(cin,str);
while(str!="")
{
strcount(str);
cout<<"Enter the next line: ";
getline(cin,str);
}
}
//-----------作业三--------------
struct chaff
{
char dross[20];
int slag;
};
void fill_chaff(chaff& chaff)
{
cout<<"Enter the dross: ";
char temp[20];
cin.get(temp,20);
strcpy(chaff.dross,temp);
cout<<"Enter the slag: ";
cin>>chaff.slag;
while(cin.get()!='\n')
cin.get();
}
void show_chaff(const chaff& chaff)
{
cout<<chaff.dross<<endl;
cout<<chaff.slag<<endl;
}
void p9_3(void)
{
const int BUF=512;
char buffer[BUF];//静态存储区,不需要delete也不能
chaff* ps=new (buffer)chaff[2];//定位New
int i=0;
for(i=0;i<2;i++)
fill_chaff(ps[i]);
for(i=0;i<2;i++)
show_chaff(ps[i]);
return;
}
//---------------作业四----------------
void p9_4(void)
{
Sales s1,s2;
setSales(s1);//交互版本
showSales(s1);
double arr[4]={1.0,2.0,3.0,4.0};
setSales(s2,arr,QUARTERS);
showSales(s2);
}
sales.cpp
#include<iostream>
#include"sales.h"
using namespace std;
using namespace SALES;
namespace SALES
{
void setSales(Sales& s,const double arr[],int n)
{
int num=4;
if(n<4)
num=n;
double max=arr[0];
double min=arr[0];
double sum=0;
for(int i=0;i<num;i++)
{
s.sales[i]=arr[i];
if(max<arr[i])
max=arr[i];
if(min>arr[i])
min=arr[i];
sum+=arr[i];
}
s.average=sum/n;
s.max=max;
s.min=min;
}
void setSales(Sales& s)
{
cout<<"Enter the sales: ";
double temp[QUARTERS];
for(int i=0;i<QUARTERS;i++)
{
cout<<"# "<<i+1<<" : ";
cin>>temp[i];
}
s.max=temp[0];
s.min=temp[1];
double sum=0;
for(int i=0;i<QUARTERS;i++)
{
s.sales[i]=temp[i];
sum+=temp[i];
if(s.max<temp[i])
s.max=temp[i];
if(s.min>temp[i])
s.min=temp[i];
}
s.average=sum/QUARTERS;
}
void showSales(const Sales& s)
{
cout<<"sales: ";
for(int i=0;i<QUARTERS;i++)
cout<<s.sales[i]<<" ";
cout<<endl;
cout<<"average: "<<s.average<<endl;
cout<<"max: "<<s.max<<endl;
cout<<"min: "<<s.min<<endl;
}
}
sales.h
#ifndef SALES_H_
#define SALES_H_
namespace SALES
{
const int QUARTERS=4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales& s,const double ar[],int n);
void setSales(Sales& s);
void showSales(const Sales& s);
}
#endif
golf.cpp
#include<iostream>
#include<cstring>
#include"golf.h"
using namespace std;
void setgolf(golf& g,const char* name,int hc)
{
//g.fullname[Len]=*name;
strcpy(g.fullname,name);//字符串复制
g.handicap=hc;
}
int setgolf(golf& g)
{
static int count=0;
cout<<"Enter the name: ";
char name[Len];
cin.get(name,Len);
if(strcmp(name,"")==0)
return count;
cout<<"Enter the handicap: ";
int handi;
cin>>handi;
while(cin.get()!='\n')//读取掉回车符
cin.get();
setgolf(g,name,handi);
count++;
return count;
}
void handicap(golf& g,int hc)
{
g.handicap=hc;
}
void showgolf(const golf& g)
{
cout<<g.fullname<<endl;
cout<<g.handicap<<endl;
}
golf.h
#ifndef GOLF_H_
#define GOLF_H_
const int Len=40;
struct golf
{
char fullname[Len];
int handicap;
};
void setgolf(golf& g,const char* name,int hc);
int setgolf(golf& g);
void handicap(golf& g,int hc);
void showgolf(const golf& g);
#endif
如有错误,欢迎指正!?
|