C++ Primer Plus(第6版)---编程作业完成(第九章)

本文档展示了C++编程中关于销售类和高尔夫类的四个练习,包括输入验证、数据结构操作和基本业务逻辑。通过p9_4函数,实现销售数据输入和展示,展示了Sales类的使用和setSales方法的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

如有错误,欢迎指正! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值