C++primer plus 第九章习题

#ifndef GOLF_H_
#define GOLF_H_
const int len = 40;
struct golf
{
<span style="white-space:pre">	</span>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


 
#include<iostream>
#include"golf.h"
//第九章
int main()
{
	using namespace std;
	golf team[3];
	int i;
	for (i = 0; i < 3; i++)
	{
		if (setgolf(team[i]) == 0)
			break;
	}
	for (int j = 0; j < i; j++)
	{
		showgolf(team[j]);
	}
	golf ann;
	setgolf(ann, "ann bridfree", 5);
	showgolf(ann);

	handicap(ann, 3);
	showgolf(ann);
	system("pause");
	return 0;
}


 
#include<iostream>
#include<stdlib.h>
#include<string>

using std::string;
using std::cin;
using std::cout;
using std::endl;
void strcount(string str);
int main()
{
	string input;
	cout << "Enter line:";
	getline(cin, input);
	while (input != "")
	{
		strcount(input);
		cout << "Enter next line:";
		getline(cin, input);
	}
	cout << "Bye!"<<endl;
}

void strcount(string str)
{
	static int total = 0;
	int count = 0;
	cout << "\"" << str << "\"contains ";
	count=str.size();
	total += count;
	cout << count << " characters, ";
	cout << total << " characters total\n";

}
#include<iostream>
#include<stdlib.h>
#include<cstring>
struct chaff
{
	char dross[20];
	int slag;
};
const int BUF = 60;
char buf[BUF];
int main()
{
	using namespace std;
	chaff *pc = new(buf)chaff[2];
	strcpy_s(pc[0].dross, "fpoo");
	pc[0].slag = 1;
	strcpy_s(pc[1].dross, "mmpoo");
	pc[1].slag = 2;
	for (int i = 0; i < 2; i++)
	{
		cout << pc[i].dross << ":" << pc[i].slag << endl;
	}
	system("pause");
	return 0;
}


 
</pre><pre name="code" class="cpp">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);
}


 
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include"golf.h"


int main()
{
	using namespace SALES;
	Sales s1;
	double salesList[] = { 11.2, 11.3, 11.4, 11.5, 11.6 };
	setSales(s1, salesList, sizeof(salesList) / sizeof(double));
	showSales(s1);
	Sales s2;
	setSales(s2);
	showSales(s2);
	system("pause");
	return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值