前两天有事情去了,没有好好学,之后要补回来
9-1
main.cpp
#include <string.h>
#include <iostream>
#include"golf.h"
const int GolfSize = 3;
int main()
{
golf ann;
setgolf(ann, "Ann Birdfree", 24);
showgolf(ann);
golf andy;
setgolf(andy);
golf g[GolfSize];
int j = 0;
for (int i = 0; i < GolfSize; i++)
{
j = setgolf(g[i]);
if (j == 1)
showgolf(g[i]);
else if (j == 0)
break;
}
return 0;
}
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 <string.h>
#include <iostream>
#include"golf.h"
void setgolf(golf& g, const char* name, int hc)
{
strcpy_s(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf& g)
{
using std::cout;
using std::cin;
cout << "Please enter fullname: \n";
cin.getline(g.fullname, 50);
if (!strcmp(g.fullname, ""))
return 0;
cout << "Please enter handicap: \n";
cin >> g.handicap;
cin.get();
return 1;
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
using std::cout;
cout << "fullname: " << g.fullname << "\n";
cout << "handicap: " << g.handicap << "\n";
}
9-2
需要用getline才能获取空行
#include <iostream>
#include<string>
using namespace std;
void strcount(const string str);
int main()
{
string input;
char next;
cout << "Enter a line:\n";
getline(cin, input);
while (input != "")
{
strcount(input);
cout << "Enter the 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 = 0;
cout << "\"" << str << "\" contains ";
count = str.size();
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
9-3
同样需要stcpy_s
(1)
#include <iostream>
#include <string>
struct chaff
{
char dross[20];
int slag;
};
int main()
{
using namespace std;
chaff* cf = new chaff[2];
strcpy_s(cf[0].dross, "gugugu");
cf[0].slag = 6;
strcpy_s(cf[1].dross, "biubiubiu");
cf[1].slag = 9;
for (int i = 0; i < 2; i++)
{
cout << "dross, slag: " << cf[i].dross << ", " << cf[i].slag << endl;
}
//delete cf;<buffer>指定的是静态内存,不需要用delete释放
return 0;
}
(2)
#include <iostream>
#include <string>
struct chaff
{
char dross[20];
int slag;
};
const int BUF = 512;
const int N = 2;
char buffer[BUF];
int main()
{
using namespace std;
chaff* cf = new (buffer) chaff[N];
strcpy_s(cf[0].dross, "gugugu");
cf[0].slag = 6;
strcpy_s(cf[1].dross, "biubiubiu");
cf[1].slag = 9;
for (int i = 0; i < 2; i++)
{
cout << "dross, slag: " << cf[i].dross << ", " << cf[i].slag << endl;
}
//delete cf;<buffer>指定的是静态内存,不需要用delete释放
return 0;
}
9-4
main.cpp
#include <iostream>
#include"Sales.h"
using namespace SALES;
int main()
{
Sales a, b;
double arr[3] = { 1.1, 22, 4.4 };
setSales(a, arr, 3);
showSales(a);
setSales(b);
showSales(b);
return 0;
}
Sales.cpp
#include <iostream>
#include"Sales.h"
using namespace std;
void SALES::setSales(Sales& s, const double ar[], int n)
{
double sum = 0;
double max = ar[0];
double min = ar[0];
if (n < QUARTERS)
{
for (int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
sum += ar[i];
if (max < ar[i])
max = ar[i];
else if (min > ar[i])
min = ar[i];
}
s.average = sum / n;
s.max = max;
s.min = min;
for (int i = n; i < QUARTERS; i++)
s.sales[i] = 0;
}
else
cout << "too long\n";
}
void SALES::setSales(Sales& s)
{
double sum = 0;
cout << "Enter four numbers:\n";
for (int i = 0; i < QUARTERS; i++)
{
std::cin >> s.sales[i];
}
double max = s.sales[0];
double min = s.sales[0];
for (int i = 0; i < QUARTERS; i++)
{
sum += s.sales[i];
if (max < s.sales[i])
max = s.sales[i];
else if (min > s.sales[i])
min = s.sales[i];
}
s.average = sum / QUARTERS;
s.max = max;
s.min = min;
}
void SALES::showSales(const Sales& s)
{
cout << "Sales" << "\n";
cout << "Average: " << s.average << "\n";
cout << "max: " << s.max << "\n";
cout << "min: " << s.min << "\n";
}
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