//1
//golf.h
//for golf.cpp
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 & g1, const char * name, int hc)
{
for (int i = 0; i < strlen(name)+1; ++i) {
g1.fullname[i] = name[i];
}
g1.handicap = hc;
}
int setgolf(golf & g1)
{
using namespace std;
cout << "please enter the name: ";
cin.getline(g1.fullname, 40);
if (strlen(g1.fullname))
cout << "enter the hc: ",
cin >> g1.handicap,
cin.get();
return 0;
}
void handicap(golf & g1, int hc)
{
g1.handicap = hc;
}
void showgolf(const golf & g1)
{
using namespace std;
cout << "====the golf info====\n";
cout << "the name is: " << g1.fullname << "\n";
cout << "the hc is :" << g1.handicap << "\n";
}
//main.cpp
#include<iostream>
#include<iomanip>
#include"golf.h"
int main()
{
using namespace std;
int n,hc;
golf ann;
setgolf(ann, "Ann Birdfree", 24);
showgolf(ann);
cout << "please input the nums which you want to log: ";
cin >> n;
cin.get();
golf *g = new golf[n+1];
for (int i = 0; i < n; ++i) {
cout << "#" << i + 1 << endl;
setgolf(g[i]);
showgolf(g[i]);
}
system("pause");
return 0;
}
//2
#include<iostream>
#include<string>
using namespace std;
void strcount(const string s);
int main()
{
string input;
string 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 << "It's empty line.Done.\n";
system("pause");
return 0;
}
void strcount(const string s)
{
static int total = 0;
int count = 0;
cout << "\"" << s << "\" contains ";
count = s.length();
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
//3
#include<iostream>
#include<string>
using namespace std;
const int BUF = 50;
const int n = 5;
struct chaff {
char dross[20];
int slag;
};
chaff buf[BUF];
int main()
{
chaff *p1, *p2;
p1 = new chaff[n];
p2 = new (buf) chaff[n];
cout << "please input the p1 info: \n";
cin.getline(p1->dross, 20);
cin >> p1->slag;
char ch[BUF] = "we are the world";
strcpy_s(p2->dross, strlen(ch) + 1, ch);
p2->slag = strlen(ch);
cout << "the new chaff[] form address: " << p1 << " Dross is: " << p1->dross <<" Slag is: " <<p1->slag<<endl;
cout << "the new chaff[] form address: " << p2 << " Dross is: " << p2->dross << " Slag is: " << p2->slag<<endl;
system("pause");
return 0;
}
//4
//sales.h
namespace SALES
{
const int QUAETERS = 4;
struct sales
{
double sale[QUAETERS];
double average;
double max;
double min;
};
void setsales(sales &s, const double ar[], int n);
void setsales(sales &s);
void showsales(const sales &s);
template <class T>
int arrlen(T& array)
{
return (sizeof(array) / sizeof(array[0]));
}
}
//sales.cpp
#include<iostream>
#include"sales.h"
namespace SALES
{
void setsales(sales &s, const double ar[], int n = SALES::QUAETERS)
{
double sum = 0;
s.max = -99999999;
s.min = 99999999;
for (int i = 0; i < n+1; ++i)
{
s.sale[i] = ar[i];
sum += s.sale[i];
s.max = ar[i] > s.max ? ar[i] : s.max;
s.min = ar[i] < s.min ? ar[i] : s.min;
}
s.average = sum / n;
}
void setsales(sales &s)
{
s.max = -99999999;
s.min = 99999999;
std::cout << "please input the array size: ";
int sum = 0;
int n;
std::cin >> n;
std::cout << "please input the data: ";
for (int i = 0; i < n; ++i)
std::cin >> s.sale[i],
sum += s.sale[i];
for (int i = 0; i < n; ++i) {
s.max = s.sale[i] > s.max ? s.sale[i] : s.max;
s.min = s.sale[i] < s.min ? s.sale[i] : s.min;
}
s.average = sum / n;
}
void showsales(const sales &s)
{
std::cout << "*****the info*****\n";
std::cout << "the sales:" << std::endl;
for (int i = 0; i < arrlen(s.sale); ++i)
std::cout << s.sale[i] << "\t\n";
std::cout << "the average: " << s.average << "\n";
std::cout << "the min: " << s.min << "\n";
std::cout << "the max: " << s.max << "\n";
}
}
//main.cpp
#include<iostream>
#include<string>
#include"sales.h"
int main()
{
double a[4] = { 1,2,3,4 };
SALES::sales s1;
SALES::sales s2;
SALES::setsales(s1, a, 3);
SALES::showsales(s1);
SALES::setsales(s2);
SALES::showsales(s2);
system("pause");
return 0;
}
C++ Primer Plus 第六版 第九章 练习题参考答案
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...