C++ primer plus 第九章编程练习

1、

/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/15
From:C++ primer plus 第九章编程练习 第1题
*********************************************************************************************/

/***********************************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);
/*********************golf.cpp****************************************/
#include "stdafx.h"
#include<iostream>
#include<string>
#include"golf.h"
using namespace std;

void setgolf(golf & g, const char * name, int hc)
{
	strcpy_s(g.fullname, name);
	g.handicap = hc;
}

int setgolf(golf & g)
{
	cout << "Please enter the name:";
	cin.getline(g.fullname, Len);
	if (!strcmp(g.fullname, ""))
		return 0;
	cout << "Please enter the handicap:";
	cin >> g.handicap;
	cin.get();
	return 1;
}

void handicap(golf & g, int hc)
{
	g.handicap = hc;
}

void showgolf(const golf & g)
{
	cout << "fullname:" << g.fullname << endl;
	cout << "handicap:" << g.handicap << endl;
}
/**************************main.cpp*****************************************/
#include "stdafx.h"
#include<iostream>
#include"golf.h"
using namespace std;
const int StructSize = 3;
int main()
{
	golf ann;
	setgolf(ann, "Ann Birdfree", 24);
	showgolf(ann);
	handicap(ann, 12);
	showgolf(ann);
	golf g[StructSize];
	int j;
	for (int i = 0; i < StructSize; i++)
	{
		j = setgolf(g[i]);
		if (j==0)
			break;
		showgolf(g[i]);
	}

	cout << "bye!\n";
	return 0;
}
2、

#include"stdafx.h"
#include<iostream>
#include<string>
using namespace std;

const int ArSize = 10;
void strcount(const string str);

int main()
{
	string input;
	char next;

	cout << "Enter a line:\n";
	getline(cin, input);
	while (input!="")
	{
		strcount(input);
		cout << "Enter next line:(empty line to quit)\n";
		getline(cin, input);
	}

	cout << "Bye\n";
	return 0;
}

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

#include"stdafx.h"
#include<iostream>
#include<string>

struct chaff
{
	char dross[20];
	int slag;
};

using namespace std;


int main()
{
	chaff *p = new chaff[2];
	strcpy_s(p[0].dross, "fuck");
	p[0].slag = 1;
	strcpy_s(p[1].dross, "what");
	p[1].slag = 2;
	for (int i = 0; i < 2; i++)
		cout << p[i].dross << "," << p[i].slag << endl;
	return 0;
}
4、

/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/15
From:C++ primer plus 第九章编程练习 第4题
*********************************************************************************************/

/***********************************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);

};

/******************Sales.cpp****************************/
#include"stdafx.h"
#include"Sales.h"
#include<iostream>

namespace SALES
{
	using std::cout;
	using std::cin;
	using std::endl;

	double average(double arr[], int ArrSize)
	{
		double s = 0;
		for (int i = 0; i < ArrSize; i++)
			s += arr[i];
		double average = s / ArrSize;
		return average;
	}

	double max(double arr[], int ArrSize)
	{
		double temp = arr[0];
		for (int i = 1; i < ArrSize; i++)
		{
			if (arr[i] > temp)
				temp = arr[i];
		}
		return temp;
	}

	double min(double arr[], int ArrSize)
	{
		double temp = arr[0];
		for (int i = 1; i < ArrSize; i++)
		{
			if (arr[i] < temp)
				temp = arr[i];
		}
		return temp;
	}

	void setSales(Sales & s, const double ar[], int n)
	{
		int m = QUARTERS < n ? QUARTERS : n;
		for (int i = 0; i < m; i++)
		{
			s.sales[i] = ar[i];
		}
		for (int i = m; i < QUARTERS; i++)
			s.sales[i] = 0;
		s.average = average(s.sales, m);
		s.max = max(s.sales, m);
		s.min = min(s.sales, m);
	}

	void setSales(Sales & s)
	{
		cout << "Enter 4 sales:\n";
		for (int i = 0; i < QUARTERS; i++)
		{
			cout << "sales " << i + 1 << " :";
			cin >> s.sales[i];
		}
		s.average = average(s.sales, QUARTERS);
		s.max = max(s.sales, QUARTERS);
		s.min = min(s.sales, QUARTERS);
	}

	void showSales(const Sales & s)
	{
		cout << "sales:\n";
		for (int i = 0; i < QUARTERS; i++)
			cout << s.sales[i] << endl;
		cout << "average:" << s.average << endl;
		cout << "max:" << s.max << endl;
		cout << "min:" << s.min << endl;
	}
};
/******************main.cpp****************************/
#include"stdafx.h"
#include"Sales.h"
#include<iostream>

using namespace std;
using namespace SALES;

int main()
{
	Sales s1,s2;
	double ar[3] = { 1.2, 2.0, 3.1 };
	setSales(s1, ar, 3);
	showSales(s1);
	setSales(s2);
	showSales(s2);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值