您的位置:首页 > 编程语言 > C语言/C++

【学习C++】C++ Primer Plus (第六版)第九章编程练习1-4

2016-04-27 16:44 555 查看
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 <iostream>
#include "golf.h"
void setgolf(golf & g, const char * name, int hc){
g.handicap = hc; \
int len = strlen(name);
for (int i = 0; i < Len; i++){
g.fullname[i] = name[i];
}
}
int setgolf(golf & g){
std::cout << "Please enter the fullname : ";
char name[Len];
std::cin.getline(name, Len);
std::cout << "Please enter handicap : ";
int han;
std::cin >> han;
std::cin.get();
setgolf(g, name, han);
if (name[0] == '\0')
return 0;
else
return 1;
}
void handicap(golf & g, int hc){
g.handicap = hc;
}
void showgolf(const golf & g){
std::cout << g.fullname << ", "<<g.handicap<<std::endl;
}
//main.cpp
#include <iostream>
#include "golf.h"
int main(){
golf gl[3];
for (int i = 0; i < 3; i++){
if (setgolf(gl[i]) == 0)
break;
}
for (int i = 0; i < 3; i++)
showgolf(gl[i]);
for (int i = 0; i < 3; i++){
handicap(gl[i], 1);
showgolf(gl[i]);
}
std:: cin.get();
return 0;
}

2.#include <iostream>
#include <string>
using namespace std;
void strcount(const string);
int main()
{
string input;
cout << "Enter a line: \n";
getline(cin, input);
while (input != "\0"){
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 = str.size();
total += count;
cout << "\"" << str << "\"contains ";
cout << count << " characters\n";
cout << total << " charaters total\n";
}3.
#include <iostream>
struct chaff
{
char dross[20];
int slag;
};
char buffer[60];
using namespace std;
int main()
{
chaff *ch = new(buffer) chaff[2];
for (int i = 0; i < 2; i++){
char cha[20];
cin.get(cha, 20);
while (cin.get() != '\n')
continue;
strcpy(ch[i].dross, cha);
int a;
cin >> a;
ch[i].slag = a;
cin.get();
}
for (int i = 0; i < 2; i++){
cout << ch[i].dross << " ," << ch[i].slag;
cout << "  address : " << &ch[i] << endl;
}
cout << "buffer address : " << (void *) buffer << endl;
cin.get();
return 0;
}
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 <iostream>
#include "sales.h"
namespace SALES
{
void setSales(Sales & s, const double ar [], int n){
int times = QUARTERS < n ? QUARTERS : n;
double total = 0;
double ma=ar[0], mi=ar[0];
for (int i = 0; i < times; i++){
s.sales[i] = ar[i];
total += s.sales[i];
if (ma < ar[i])
ma = ar[i];
if (mi>ar[i])
mi = ar[i];
}
for (int i = times; i < QUARTERS; i++){
s.sales[i] = 0;
}
s.average = total / times;
s.max = ma;
s.min = mi;
}
void setSales(Sales & s){
double total=0;
for (int i = 0; i < QUARTERS; i++){
std::cin >> s.sales[i];
total += s.sales[i];
}
double ma = s.sales[0],mi=s.sales[0];
for (int i = 0; i < QUARTERS; i++){
if (ma < s.sales[i])
ma = s.sales[i];
if (mi>s.sales[i])
mi = s.sales[i];
}
s.average = total / 4;
s.max = ma;
s.min = mi;
}
void showSales(const Sales & s){
std::cout << "values:\n";
for (int i = 0; i < 4; i++)
std::cout << s.sales[i]<<std::endl;
std::cout <<"average: "<<s.average << std::endl;
std::cout << "max: "<<s.max << std::endl;
std::cout << "min: "<<s.min << std::endl;
}
}
//main.cpp
#include <iostream>
#include "sales.h"
int main()
{
SALES::Sales a, b;
double ar[3] = { 0.1, 0.2, 0.3 };
SALES::setSales(a, ar, 3);
SALES::showSales(a);
std::cout << "Please input 4 numbers: \n";
SALES::setSales(b);
SALES::showSales(b);
std::cin.get();
std::cin.get();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++