以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆。仅供参考,DEV C++已通过编译运行
。
练习1
//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);
//function part
#include<iostream>
#include<cstring>
#include"p1.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 << "Enter fullname: ";
if (cin.getline(g.fullname, 40) && strlen(g.fullname))//空字符串不等于空格串,空字符串的strlen为0,空格串为1
{
cout << "Enter the handicap: ";
cin >> g.handicap;
cin.get();
return 1;
}
else
{
cout << "error";
return 0;
}
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
using std::cout;
using std::cin;
using std::endl;
cout << " FULLNAME: " << g.fullname << endl;
cout << " HANDICAP: " << g.handicap << endl;
}
#include<iostream>
#include"p1.h"
int main()
{
golf golfar[3];
golf golfacc;
for (int i = 0; i < 3; ++i)
{
if (!setgolf(golfar[i]))
break;
}
setgolf(golfacc, "Ann Birdfree", 24);
showgolf(golfacc);
handicap(golfacc, 30);
showgolf(golfacc);
return 0;
}
练习2
#include<iostream>
#include<string>
#include<cctype>
void strcount(const std::string str);
int main()
{
using namespace std;
string input;
cout << "Enter a line:\n";
getline(cin, input);
while (cin)
{
strcount(input);
cout << "Enter next line(empty line to quit):\n";
getline(cin, input);
}
}
void strcount(const std::string str)
{
using namespace std;
static int total = 0;
int count = 0;
if (str == "")
cout << "BLANK!\n";
else
{
for (int cnt = 0; cnt < str.size(); ++cnt)//str.size()返回所有非‘\0’字符的长度,与题设不符
{
if (isalpha(str[cnt]))
++count;
}
total += count;
cout << "\"" << str << "\" contains ";
cout << count << " characters\n";
cout << total << " characters total\n";
}
}
练习3
方法一
#include<iostream>
struct chaff
{
char dross[20];
int slag;
};
char buffer[500];
int main()
{
using namespace std;
chaff* structsr = new(buffer)chaff[2];
for (int i = 0; i < 2; ++i)
{
cout << "Enter dross[20]: ";
cin.getline(structsr[i].dross, 20);
cout << "Enter slag: ";
cin >> structsr[i].slag;
cin.get();
}
for (int i = 0; i < 2; ++i)
{
cout << "DROSS: " << structsr[i].dross << endl;
cout << "SLAG: " << structsr[i].slag << endl;
}
return 0;
}
方法二
#include<iostream>
struct chaff
{
char dross[20];
int slag;
};
int main()
{
using namespace std;
chaff* ptr = new chaff[2];
for (int i = 0; i < 2; ++i)
{
cout << "Enter dross[20]: ";
cin.getline(ptr[i].dross, 20);
cout << "Enter slag: ";
cin >> ptr[i].slag;
cin.get();
}
for (int i = 0; i < 2; ++i)
{
cout << "DROSS: " << ptr[i].dross << endl;
cout << "SLAG: " << ptr[i].slag << endl;
}
delete[]ptr;
return 0;
}
练习4
//p4.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);
}
//function part
#include<iostream>
#include"p4.h"
namespace SALES
{
void setSales(Sales& s, const double ar[], int n)
{
double sum = 0;
for (int i = 0; i < n; ++i)
{
s.sales[i] = ar[i];
sum += s.sales[i];
}
s.average = sum / n;
double tempmax, tempmin;
tempmax = tempmin = ar[0];
for (int i = 0; i < n; ++i)
{
if (tempmax < s.sales[i])
tempmax = s.sales[i];
if (tempmin > s.sales[i])
tempmin = s.sales[i];
}
s.max = tempmax;
s.min = tempmin;
}
void setSales(Sales& s)
{
using std::cout;
using std::cin;
using std::endl;
int cnt = 0;
cout << "Enter sales array: ";
while (cnt < QUARTERS && cin >> s.sales[cnt] )//顺序不可替换,否则会多一次录入,虽然录入并没有什么用
++cnt;
double sum = 0;
for (int i = 0; i < cnt; ++i)
sum += s.sales[i];
s.average = sum / cnt;
double tempmax, tempmin;
tempmax = tempmin = s.sales[0];
for (int i = 0; i < cnt; ++i)
{
if (tempmax < s.sales[i])
tempmax = s.sales[i];
if (tempmin > s.sales[i])
tempmin = s.sales[i];
}
s.max = tempmax;
s.min = tempmin;
}
void showSales(const Sales& s)
{
using namespace std;
for (int i = 0; i < QURATES ; ++i)
cout << "SALES[" << i << "]: " << s.sales[i] << endl;
cout << "AVERAGE: " << s.average << endl;
cout << "MAX: " << s.max << endl;
cout << "MIN: " << s.min << endl;
}
}
#include<iostream>
#include"p4.h"
int main()
{
using SALES::Sales;
using SALES::setSales;
using SALES::showSales;
Sales test1;
double arr[3] = { 1.3,6.2,2.8 };
setSales(test1, arr, 3);//程序存在瑕疵,原因在于arr小于4个时,show也打印出4个,但是把
showSales(test1);//后续部分都处理成0也貌似有点不妥的样子,所以大概就这样吧:)
Sales test2;
setSales(test2);
showSales(test2);
return 0;
}