原创 C++ Primer Plus 第九章 课后编程练习题1-4

//1题
//golf.cpp
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 40;
struct golf
{
char fullname[Len]; //选手全名
int handicap; //高尔夫球差点
};
//接受: 一个golf类的结构引用, 一个全名指针, 一个代表高尔夫球差点的int类型
//用途: 把接受的全名和差点存储在golf结构中。
//结果: 没有返回值,把给的的信息存储到对应的结构成员中。
void setgolf(golf & g, const char * name, int hc);

//接受: 一个golf类的结构引用。
//用途: 通过询问全名和高尔夫球差点于用户进行互动,并存储用户提供的信息到golf结构中。
//结果:存储成功返回1,否则返回0.
int setgolf(golf & g);

//接受: 一个golf类的结构引用, 一个代表高尔夫球差点的int类型
//用途: 修改新的高尔夫球差点。
//结果: 无返回值,只修改选手现在高尔夫球差点。
void handicap(golf & g, int hc);

//接受: 一个不可修改的golf类型的结构引用。
//用途: 读取结构中选手的信息。
//结果: 无返回值,显示选手的信息。
void showgolf(const golf & g);

#endif

//main.cpp
//结构类: 成员.fullname (char 40) 成员.handicap(int)
//函数类: void setgolf(golf &, const char *name, int handicap) 存储信息
// int setgolf(golf &); 存储信息
// void handicap(golf &, int handicap) 修改高尔夫球差点信息
// void showgolf(const golf &) 查看信息
//常量: const int Len = 40

#include
#include “golf.hpp”
int main()
{
char fullname1[Len];
int handicap1;
int handicap2; //存储需要更新的高尔夫球差点
golf andy,ann;
std::cout <<“Please enter the full name of the contestant: (Direct return exit)\n”;
while (std::cin.getline(fullname1,40) && fullname1[0] != ‘\0’)
{
std::cout <<"Please enter " << fullname1 <<“golf handicap:\n”;
std::cin >> handicap1;
std::cin.get();
setgolf(ann, fullname1, handicap1);
showgolf(ann);
std::cout <<“Please enter the full name of the contestant: (Direct return exit)\n”;
}
std::cout <<“Please input the golf handicap to be modified:\n”;
std::cin >> handicap2;
std::cin.get();
handicap(ann, handicap2);
showgolf(ann);

while (setgolf(andy))
    showgolf(andy);

std::cout <<"Please input the golf handicap to be modified:\n";
std::cin >> handicap2;
handicap(andy, handicap2);
showgolf(andy);
std::cout <<"Bye!\n";

return 0;

}
//golf.cpp
//结构类: 成员.fullname (char 40) 成员.handicap(int)
//函数类: void setgolf(golf &, const char *name, int handicap) 存储信息
// int setgolf(golf &); 存储信息
// void handicap(golf &, int handicap) 修改高尔夫球差点信息
// void showgolf(const golf &) 查看信息
//常量: const int Len = 40
#include
#include “golf.hpp”
void setgolf(golf & g, const char * name, int hc)
{
for (int i = 0; i < Len; i++)
g.fullname[i] = name[i];
g.handicap = hc;
}

int setgolf(golf & g)
{
std::cout <<“Please enter the full name of the contestant: (Direct return exit)\n”;
if(std::cin.getline(g.fullname, Len) && g.fullname[0] != ‘\0’)
{
std::cout <<"Please enter " << g.fullname <<“golf handicap:\n”;
std::cin >> g.handicap;
std::cin.get();
return 1;
}
else
return 0;
}
void handicap(golf & g, int hc)
{
g.handicap = hc;
}

void showgolf(const golf & g)
{
std::cout << g.fullname <<" handicap is = " << g.handicap << std::endl;
}
//2题
#include
#include
void strcount(const std::string &str);
int main()
{
using namespace std;
string input;
cout <<“Enter a line:\n”;
getline(cin,input);
while (input[0] !=’\0’)
{
strcount(input);
cout <<“Enter next line (empty line to quit):\n”;
getline(cin, input);
}
cout <<“Bye\n”;

return 0;

}
void strcount(const std::string &str)
{
using namespace std;
static int total = 0;
int count = 0;

cout <<"\"" << str <<"\" contains ";
for (int i = 0; str[i] != '\0'; i++)
    if (str[i] != ' ')
        count++;
total +=count;
cout << count <<" characters\n";
cout << total <<" characters total\n";

}
//3题
//题目要求用结构数组,我感觉还是直接使用存储结构指针的数组比较方便

#include
#include
struct chaff
{
char dross[20];
int siag;
};
const int MAX = 100;
int main()
{
//pc[] = new char[MAX];
static char pc[MAX]; //静态数组
chaff *chaf[2];
chaf[0] = new (pc) chaff;
chaf[1] = new (pc + sizeof(chaff)) chaff;
strcpy(chaf[0]->dross, “asdfghjkl”);
strcpy(chaf[1]->dross, “lfkjhshafiha”);
chaf[0]->siag = 150;
chaf[1]->siag = 200;
for (int i = 0; i < 2; i++)
{
std::cout <<“chaf[” << i <<"].dross = " << chaf[i]->dross;
std::cout <<" chaf[" << i <<"].siag = " << chaf[i]->siag << std::endl;
}

return 0;

}
//4题
//声明名称空间文件
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
//接受: 一个Sales结构引用,一个double指针,一个int类型
//结果: 把数组存储到结构成员sales数组中,并计算出平均数,最大和最小数存储到相应的变量中。
//非互交
void setSales(Sales & s, const double ar[], int n);

//接受: 一个Sales结构引用
//用途: 和用户互交去得信息
//结果: 把取得的信息存储到结构中
void setSales(Sales & s);

//接受: 一个结构引用
//用途: 读取结构内容并显示
//结果: 显示结构内容
void showSales(const Sales & s);

}
//定义名称空间内容
//名称空间类 SALES
//结构类: SALES::Sales
//结构成员: double sales[QUARTERS]; double average; double max; double min
//函数类: void setSales(Sales &, const double ar[], int n) 非互交
// void setSales(Sales &) 互交
// void showSales(const Sales &) 显示
#include
#include “sales.hpp”
namespace SALES
{
//非互交
void setSales(Sales & s, const double ar[], int n)
{
double total = 0;
for (int i = 0; i < n; i++)
{
s.max = ar[0];
s.min = ar[0];
s.sales[i] = ar[i];
if (ar[i] > s.max)
s.max = ar[i];
if(ar[i] < s.min)
s.min = ar[i];
total += ar[i];
}
s.average = total / n;
}
//互交
void setSales(Sales & s)
{
double total = 0;
std::cout <<“请输入4个浮点数:\n”;
for (int i = 0; i < QUARTERS; i++)
{
std::cin >> s.sales[i];
s.max = s.sales[0];
s.min = s.sales[0];
if (s.sales[i] > s.max)
s.max = s.sales[i];
if (s.sales[i] < s.min)
s.min = s.sales[i];
total += s.sales[i];
}
s.average = total / QUARTERS;
}
//显示
void showSales(const Sales & s)
{
for (int i = 0; i < QUARTERS; i++)
std::cout <<“数组sales[” << i <<"] = " << s.sales[i] << std::endl;
std::cout <<"平均数 = " << s.average << std::endl;
std::cout <<"最大数 = " << s.max << std::endl;
std::cout <<"最小说 = " << s.min << std::endl;
}
}
//主调函数文件
//名称空间类 SALES
//结构类: SALES::Sales
//结构成员: double sales[QUARTERS]; double average; double max; double min
//函数类: void setSales(Sales &, const double ar[], int n) 非互交
// void setSales(Sales &) 互交
// void showSales(const Sales &) 显示
#include
#include “sales.hpp”
int main()
{
using namespace SALES;
Sales sa1, sa2;
setSales(sa1);
showSales(sa1);
setSales(sa2, sa1.sales, QUARTERS);
showSales(sa2);

return 0;

}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/104116882