//9.1
//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);
//return 1 if name is entered ,return 0 if name is empty string
int setgolf(golf& g);
void handicap(golf& g,int hc);
void showgolf(const golf& g);
#endif
//golf.cpp
#include "golf.h"
#include<string>
#include<iostream>
using namespace std;
void setgolf(golf& g,const char* name,int hc)
{
strcpy(g.fullname,name);
g.handicap = hc;
}
int setgolf(golf& g)
{
cout<<"please enter the golf's fullname:";
string str;
getline(cin,str);
if(str == "")
return 0;
strcpy(g.fullname,str.c_str());
cout<<"please enter the golf's handicap:";
if(!(cin>>g.handicap))
{
cout<<"enter a bad handicap data,END!"<<endl;
return 0;
}
cin.get();
return 1;
}
void handicap(golf& g,int hc)
{
g.handicap= hc;
}
void showgolf(const golf& g)
{
cout<<"golf's name is "<<g.fullname<<",the handicap is "<<g.handicap<<endl;
}
//main.cpp
#include<iostream>
#include "golf.h"
using namespace std;
int main()
{
const int LEN = 5;
static int available_len = 0;
struct golf arr_str[LEN]={{"",0}};
for(int i = 0;i<5;i++)
{
if(0==setgolf(arr_str[i]))
break;
available_len++;
}
for(int j = 0;j<available_len;j++)
showgolf(arr_str[j]);
return 0;
}
//9.2
#include <iostream>
#include <string>
const int ArSize = 10;
void strcount(std::string str);
int main()
{
//print();
using namespace std;
//char input[ArSize];
string str;
//char next;
cout<<"Enter a line:";
//cin.get(input,ArSize);
//while(cin)
while(getline(cin,str))
{
if("" == str)
{
cout<<"the string is empty!";
break;
}
else
strcount(str);
}
cout<<"Bye!";
return 0;
}
void strcount(std::string str)
{
using namespace std;
static int total = 0;
int count = 0;
cout<<"\""<<str<<"\" contains ";
count =str.length();
total = count;
cout<<count<<" characters\n";
cout<<total<<" characters total\n";
}
//9.3
//chaff.h
#ifndef __CHAFF__H__
#define __CHAFF__H__
struct chaff
{
char dross[20];
int slag;
};
void setChaff(chaff &g);
void showChaff(chaff &g);
#endif
//chaff.cpp
#include <iostream>
#include "chaff.h"
using std::cin;
using std::cout;
using std::endl;
void setChaff(chaff &g)
{
cout<<"Please enter the dross:";
char temp[20];
cin.get(temp,20);
strcpy(g.dross,temp);
cin.get();
cout<<"Please enter the slag:";
cin>>g.slag;
cin.get();
}
void showChaff(chaff &g)
{
cout<<"the dross is:"<<g.dross<<" , and the slag is "<<g.slag<<endl;
}
//main.cpp
#include <iostream>
#include "chaff.h"
using std::cout;
using std::cin;
using std::endl;
const int ArSize = 200;
const int N =2;
char buffer[ArSize];
int main()
{
chaff *pd1,*pd2;
pd1 = new chaff[N];
pd2 = new (buffer) chaff[N];
for(int i = 0;i<N;i++)
{
setChaff(pd1[i]);
setChaff(pd2[i]);
}
for(int i = 0;i<N;i++)
{
showChaff(pd1[i]);
showChaff(pd2[i]);
}
delete [] pd1;
}
//9.4
//sale.h
#ifndef __SALE__H__
#define __SALE__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
//sale.cpp
#include <iostream>
#include "sale.h"
using namespace std;
namespace SALES
{
static double calcAverage(double arr[], int arrsize)
{
double sum = 0;
for(int i = 0;i<arrsize;i++)
sum+=arr[i];
return (sum/arrsize);
}
static double calcMax(double arr[], int arrsize)
{
int idMax = 0;
for(int i =1;i<arrsize;i++)
{
if(arr[i]>arr[idMax])
idMax = i;
}
return arr[idMax];
}
static double calcMin(double arr[], int arrsize)
{
int idMin = 0;
for(int i =1;i<arrsize;i++)
{
if(arr[i]<arr[idMin])
idMin = i;
}
return arr[idMin];
}
void setSales(Sales & s, const double ar[],int n)
{
int times = n<QUARTERS?n:QUARTERS;
for(int i = 0;i<times;i++)
s.sales[i]=ar[i];
for(int i =times;i<QUARTERS;i++)
s.sales[i] = 0;
s.average = calcAverage(s.sales, times);
s.max = calcMax(s.sales, times);
s.min = calcMin(s.sales, times);
}
void setSales(Sales & s)
{
cout << "Please enter " << QUARTERS << " sale record:";
int times = QUARTERS;
double arr[QUARTERS] = {0};
for (int i = 0;i < QUARTERS; i++) {
cin >> arr[i];
if (!cin) {
times = i; //表示输入异常
break;
}
}
setSales(s, arr, QUARTERS); //先得到初始数据在进行操作
s.average = calcAverage(s.sales, times);
s.max = calcMax(s.sales, times);
s.min = calcMin(s.sales, times);
}
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;
}
}
//main.cpp
#include <iostream>
#include "sale.h"
using namespace std;
int main ()
{
using namespace SALES;
Sales salesCar;
const double salesList[] = {2.24,234,53,23.6,6.4};
setSales(salesCar, salesList, (sizeof(salesList)/sizeof(salesList[0])));
showSales(salesCar);
Sales salesBus;
setSales(salesBus);
showSales(salesBus);
}