C++ PRIMER PLUS(第5版) 第九章 编程练习

//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 "golf.h"  
#include<string>
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 input your name:\n";
	cin.getline(g.fullname, Len);
	cout << "Please input your handicap:\n";
	cin >> g.handicap;
	return g.fullname==NULL ? 0 : 1;
}

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

void showgolf(const golf &g)
{
	cout << "--------------------------------\n";
	cout << "fullname: " << g.fullname << endl;
	cout << "handicap: " << g.handicap << endl;
	cout << "--------------------------------\n";
}
#include "stdafx.h"
#include "golf.h"
#include<iostream>
using namespace std;



int main()
{
	golf g1, g2;
	char name1[] = "Jackson";
	int hc1 = 6;

	setgolf(g1, name1, hc1);
	showgolf(g1);
	setgolf(g2);
	showgolf(g2);
	handicap(g1, 10);
	showgolf(g1);


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

void strcount(const string str);

int main()
{
	string input;
	char next;

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

	return 0;
}

void strcount(const string str)
{
	static int total = 0;
	int count = 0;

	cout << "\"" << str << "\" contains  ";
	count += str.length();
	total += count;
	cout << count << " characters\n";
	cout << total << "characters total\n";
}
#include "stdafx.h"
#include<iostream>
using namespace std;

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

char buffer[500];

int main()
{
	chaff *p;
	p = new (buffer) chaff[2];
	for (int i = 0; i < 2; i++) {
		cout << "Input dross of chaff " << i + 1 <<":\n";
		cin.getline(p[i].dross, 20);
		cout << "Input slag of chaff " << i + 1 << ":\n";
		cin >> p[i].slag;
		cin.get();
	}
	cout << "-------------------------------" << endl;
	for (int i = 0; i < 2; i++)
		cout << "chaff " << i + 1 << ": \n" 
		<< "dross: " << p[i].dross 
		<< " ; slag: " << p[i].slag << endl;
	delete p;
	system("pause");
	return 0;
}
//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);

}
//sale.cpp

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

namespace SALES
{
	void setSales(Sales& s, const double ar[], int n)
	{
		double sum = 0;
		s.max = ar[0];
		s.min = ar[0];
		if(n>=4)
			for (int i = 0; i < 4; i++) 
				s.sales[i] = ar[i];
		else
		{
			for (int i = 0; i < n; i++) 
				s.sales[i] = ar[i];
			for (int i = n; i < 4; i++) 
				s.sales[i] = 0;
		}

		for (int i = 0; i < 4; i++) {
			if (ar[i] > s.max)
				s.max = ar[i];
			if (ar[i] < s.min)
				s.min = ar[i];
			sum += ar[i];
		}
		
		s.average = sum / n;
	}

	void setSales(Sales& s)
	{
		double sum = 0;
		cout << "Input sales for 4 quaters:\n";
		for (int i = 0; i < 4; i++) {
			cin >> s.sales[i];
		}
		s.max = s.sales[0];
		s.min = s.sales[0];
		for (int i = 0; i < 4; i++) {
			if (s.sales[i] > s.max)
				s.max = s.sales[i];
			if (s.sales[i] < s.min)
				s.min = s.sales[i];
			sum += s.sales[i];
		}
		s.average = sum / 4;
	}


	void showSales(const Sales& s) {
		cout << "-----------------------------\n";
		cout << "sales: " ;
		for (int i = 0; i < 4; i++)
			cout << s.sales[i] << ' ';
		cout << endl;
		cout << "average: " << s.average << endl;
		cout << "maximum: " << s.max << endl;
		cout << "minumum: " << s.min << endl;
		cout << "-----------------------------\n";
	}

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


int main()
{
	using namespace SALES;
	Sales s1, s2;
	double ar[5] = { 101,50.4,90.5,98.2,78.6 };
	setSales(s2, ar, 3);
	showSales(s2);
	setSales(s1);
	showSales(s1);
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值