//本章程序需分多文件运行,请读者注意;
//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽所能修改;
//附上课后编程练习题目;
//若是对您有用的话请点赞或分享提供给它人;
//9.6 - 1(main).cpp
#include <iostream>
#include "golf.h"
int main()
{
using namespace std;
int i;
int sum = 0;
golf andy[Len];
setgolf(andy[0], "Ann Birdfree", 24); //演示第一个setgolf函数的特性;
cout << "Starting output:" << endl;
showgolf(andy[0]); //演示showgolf函数的功能;
handicap(andy[0], 666); //演示handicap函数功能;
cout << "Changing handicap:" << endl;
showgolf(andy[0]);
cout.put('\n');
for (i = 0; i < Len; i++)
{
cout << "Please enter andy #" << i + 1 << ": " << endl;
if (0 == setgolf(andy[i])) //演示第二个setgolf函数功能;
{
break;
}
++sum;
}
if (sum > 0)
{
cout << "Ending output:" << endl;
for (i = 0; i < sum; i++)
{
showgolf(andy[i]);
}
}
cout << "Bye." << endl;
return 0;
}
//9.6 - 1(golf).cpp
#include <iostream>
#include <cstring>
#include "golf.h"
void setgolf(golf &g, const char *name, int hc)
{
strncpy(g.fullname, name, Len);
g.fullname[Len - 1] = '\0';
g.handicap = hc;
return;
}
int setgolf(golf &g)
{
using namespace std;
cout << "Please enter the fullname(enter to quit): ";
cin.getline(g.fullname, Len);
if (0 == strcmp(g.fullname, "\0"))
{
return 0;
}
cout << "Please enter the handicap: ";
while (!(cin >> g.handicap))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Please enter an number: ";
}
cin.get();
return 1;
}
void handicap(golf &g, int hc)
{
g.handicap = hc;
return;
}
void showgolf(const golf &g)
{
using namespace std;
cout << "Name: " << g.fullname << endl;
cout << "Handicap: " << g.handicap << endl;
return;
}
//9.6 - 1(golf).h
#ifndef GOLF_H_
#define 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);
#endif
//--------------------------------------------------------------------
//9.6 - 2.cpp
扫描二维码关注公众号,回复:
12354432 查看本文章
#include <iostream>
#include <string>
using namespace std;
void strcount(string &str);
int main()
{
string input;
cout << "Enter a line:\n";
while (getline(cin, input), input != "")
{
strcount(input);
cout << "Enter next line (empty line to quit):\n";
}
cout << "Bye" << endl;
return 0;
}
void strcount(string &str)
{
int i = 0;
int count = 0;
static int total = 0;
cout << "\"" << str << "\" contains ";
while (str[i++] != '\0')
{
count++;
}
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
return;
}
//--------------------------------------------------------------------
//9.6 - 3.cpp
#include <iostream>
#include <cstring>
#include <new>
const int BUF = 512;
static char buffer[BUF];
struct chaff
{
char dross[20];
int slag;
};
int main()
{
using namespace std;
char temp[20];
chaff *str = new (buffer) chaff[2];
for (int i = 0; i < 2; i++)
{
cout << "Enter the content of dross #" << i + 1 << ": ";
cin.getline(temp, 20);
strcpy(str[i].dross, temp);
cout << "Enter the content of slag #" << i + 1 << ": ";
while (!(cin >> str[i].slag))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Please enter a number: ";
}
cin.get();
}
for (int i = 0; i < 2; i++)
{
cout << "Chaff #" << i + 1 << ": " << endl;
cout << "Dross: " << str[i].dross << endl;
cout << "Slag: " << str[i].slag << endl;
}
return 0;
}
//--------------------------------------------------------------------
//9.6 - 4(main).cpp
#include <iostream>
#include "sales.h"
int main()
{
SALES::Sales objects[2];
const double temp[4] = {
1.0, 2.0, 3.0, 4.0};
SALES::setSales(objects[0]);
SALES::setSales(objects[1], temp, 4);
SALES::showSales(objects[0]);
SALES::showSales(objects[1]);
std::cout << "Bye." << std::endl;
return 0;
}
//9.6 - 4(sales).cpp
#include <iostream>
#include "sales.h"
namespace SALES
{
void setSales(Sales &s, const double ar[], int n)
{
double total = 0.0;
double max = ar[0];
double min = ar[0];
for (int i = 1; i < n; i++)
{
s.sales[i] = ar[i];
total += ar[i];
if (ar[i] > max)
{
max = ar[i];
}
if (ar[i] < min)
{
min = ar[i];
}
}
s.min = min;
s.max = max;
s.average = total / n;
return;
}
void setSales(Sales &s)
{
using namespace std;
int len;
cout << "Enter the length of sales(<= 4 and > 0): ";
while (!(cin >> len) || len > 4 || len <= 0)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Please enter a number(<= 4 and > 0): ";
}
double *temp = new double[len];
cout << "Please enter the sales:" << endl;
for (int i = 0; i < len; i++)
{
cout << "Please enter the content #" << i + 1 << ": ";
while (!(cin >> temp[i]))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Please enter a number: ";
}
}
setSales(s, temp, len);
delete[] temp;
return;
}
void showSales(const Sales &s)
{
std::cout << "Sales average: " << s.average << std::endl;
std::cout << "Sales max: " << s.max << std::endl;
std::cout << "Sales min: " << s.min << std::endl;
return;
}
}
//9.6 - 4(sales).h
#ifndef SALES_H_
#define 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);
}
#endif
//--------------------------------------------------------------------
//------------------------------------------2020年9月28日 ----------------------------------------------;