第1题:
golf.h
//golf.h
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 4;
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 // !GOLF_H_
golf.app
//golf.app
#include <iostream>
#include "golf.h"
void setgolf(golf& g, const char* name, int hc)
{
strncpy_s(g.fullname, Len, name, Len - 1);
g.handicap = hc;
}
int setgolf(golf& g)
{
using std::cout;
using std::cin;
cout << "Input name: ";
cin.get(g.fullname, Len);
if (strlen(g.fullname)!=0)
{
while (cin.get() != '\n')
{
continue;
}
cout << "Input handicap: ";
cin >> g.handicap;
cin.get();
}
return strlen(g.fullname) == 0 ? 0 : 1;
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
using std::cout;
using std::endl;
cout << "Fullname: " << g.fullname << endl;
cout << "Handicap: " << g.handicap << endl;
}
main.app
//main.app
#include <iostream>
#include "golf.h"
int main()
{
using std::cout;
using std::cin;
using std::endl;
golf enn;
setgolf(enn, "abcdefg", 33);
showgolf(enn);
golf andy;
setgolf(andy);
showgolf(andy);
golf player[5];
int n;
for (n = 0; n < 5; n++)
{
if (setgolf(player[n]) == 0)
{
break;
}
}
for (int i = 0; i < n; i++)
{
showgolf(player[i]);
}
return 0;
}
第2题
#include <iostream>
#include <string>
void strcount(const std::string& str);
int main()
{
using namespace std;
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\n";
}
void strcount(const std::string& str)
{
using namespace std;
static int total = 0;
int count = str.size();
cout << "\"" << str << "\" contains "<< count << " characters\n";
total += count;
cout << total << " characters total\n";
}
第3题
方法一:静态数组
//静态数组
#include <iostream>
#include <new>
const int N = 5;
const int ArSize = 20;
char buffer[1024];
struct chaff
{
char dross[ArSize];
int slag;
};
int main()
{
using std::cout;
using std::cin;
using std::endl;
chaff* p1 = new (buffer) chaff[N]; //定位new运算符
int count = 0;
for (int i = 0; i < N; ++count, i++)
{
char temp[ArSize];
cout << "Enter dross (empty line to quit): ";
cin.get(temp, ArSize);
if (strlen(temp) != 0)
{
while (cin.get() != '\n')
{
continue;
}
}
else if (strlen(temp) == 0)
{
break;
}
strncpy_s(p1[i].dross, ArSize, temp, ArSize - 1);
cout << "Enter slag:";
cin >> p1[i].slag;
cin.get();
}
cout << endl << endl;
for (int i = 0; i < count; i++)
{
cout << "Dross " << i + 1 << ": " << p1[i].dross << endl;
cout << "Slag " << i + 1 << ": " << p1[i].slag << endl;
}
}
方法二:常规new运算符
#include <iostream>
const int N = 5;
const int ArSize = 20;
struct chaff
{
char dross[ArSize];
int slag;
};
int main()
{
using std::cout;
using std::cin;
using std::endl;
char* buffer = new char[1024];
chaff* p1 = new (buffer)chaff[N]; //定位New运算符
int count = 0;
for (int i = 0; i < N; ++count, i++)
{
char temp[ArSize];
cout << "Enter dross (empty line to quit): ";
cin.get(temp, ArSize);
if (strlen(temp) != 0)
{
while (cin.get() != '\n')
{
continue;
}
}
else if (strlen(temp) == 0)
{
break;
}
strncpy_s(p1[i].dross, ArSize, temp, ArSize - 1);
cout << "Enter slag:";
cin >> p1[i].slag;
cin.get();
}
cout << endl << endl;
for (int i = 0; i < count; i++)
{
cout << "Dross " << i + 1 << ": " << p1[i].dross << endl;
cout << "Slag " << i + 1 << ": " << p1[i].slag << endl;
}
delete[] p1;
}
第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& safe_cast, const double ar[], int n);
void setSales(Sales& s);
void showSales(const Sales& s);
}
#endif // !SALES_H_
sales.cpp
#include <iostream>
#include "sales.h"
namespace SALES
{
void setSales(Sales& safe_cast, const double ar[], int n)
{
using std::cout;
if (n > 0 && n <= QUARTERS)
{
double sum = 0;
double temp_max = ar[0];
double temp_min = ar[0];
for (int i = 0; i < n; i++)
{
safe_cast.sales[i] = ar[i];
sum += ar[i];
temp_max = temp_max > ar[i] ? temp_max : ar[i];
temp_min = temp_min < ar[i] ? temp_min : ar[i];
}
for (int i = n; i < QUARTERS; i++)
{
safe_cast.sales[i] = 0;
temp_min = 0;
}
safe_cast.average = sum / QUARTERS;
safe_cast.max = temp_max;
safe_cast.min = temp_min;
}
else
{
cout << "Error times. Exit!\n";
exit(EXIT_FAILURE);
}
}
void setSales(Sales& s)
{
using std::cout;
using std::cin;
int times;
cout << "Input sales times:\n";
cin >> times;
while (times <= 0 || times > QUARTERS)
{
cout << "Error input, Please input a new number 1-4: ";
cin >> times;
}
double* arr = new double[QUARTERS];
for (int i = 0; i < times; i++)
{
cout << "Quarter #" << i + 1 << ": ";
cin >> arr[i];
}
for (int i = times; i < QUARTERS; i++)
{
arr[i] = 0;
}
setSales(s, arr, QUARTERS);
delete[] arr;
}
void showSales(const Sales& s)
{
using std::cout;
using std::endl;
int count = std::size(s.sales);
for (int i = 0; i < count; i++)
{
cout << "Quarter #" << i + 1 << ": " << s.sales[i] << endl;
}
cout << "Average: " << s.average << endl;
cout << "Max: " << s.max << endl;
cout << "Min: " << s.min << endl;
}
}
main.cpp
#include <iostream>
#include "sales.h"
int main()
{
using namespace SALES;
using namespace std;
double a1[1] = { 10 };
double a3[3] = { 100,200,300 };
double a4[4] = { 1000,2000,3000,4000 };
Sales S1, S2;
setSales(S1, a1, 1);
showSales(S1);
setSales(S1, a3, 3);
showSales(S1);
setSales(S1, a4, 4);
showSales(S1);
setSales(S2);
showSales(S2);
}