// 1
// golf.h -- for pe9-1.cpp
static const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
// non-interactive version:
// function sets golf structure to provided name,handicap
// using values passed as arguments to the function
void setgolf(golf &g,const char *name,int hc);
// interactive version:
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);
// function resets handicap to new value_type
void handicap(golf & g,int hc);
// function displays contents of golf structure
void showgolf(const golf & g);
// 1
// golf.cpp
#include <iostream>
#include <cstring>
#include "golf.h"
void setgolf(golf & g,const char * name,int hc)
{
strcpy(g.fullname,name);
g.handicap = hc;
}
int setgolf(golf & g)
{
using std::cin;
using std::cout;
using std::endl;
int hc;
cout << "Please input the name of the golf player: ";
cin.get(g.fullname,Len); // 读取一行
if(cin)
{
cout << "Please input the handicap of the golf player: ";
cin >> hc;
cin.get();
g.handicap = hc;
return 1;
}
else
return 0;
}
void handicap(golf & g,int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
std::cout << "g.hullname is " << g.fullname << ", g.handicap is " << g.handicap << std::endl;
}
// 1
#include <iostream>
#include "golf.h"
int main()
{
using namespace std;
int array_size,real_size;
char opteions;
cout << "Please input the size of array you want: ";
cin >> array_size;
golf g[array_size];
cout << "Please input your choice('1' or '3'): ";
cin >> opteions;
cin.get();
while(opteions != '1' && opteions != '3')
{
cout << "Please input your choice('1' or '3'): ";
cin >> opteions;
cin.get();
}
if(opteions == '1') // use function setgolf(golf & g)
{
int is_ok = 1;
for(int i = 0;i < array_size;i++)
if(is_ok)
{
is_ok = setgolf(g[i]);
real_size = i;
}
else
{
real_size = i - 2;
cin.clear(); // 不加会出错,死循环打印
break;
}
}
else if(opteions == '3') // use function setgolf(golf &g,const char *name,int hc)
{
int cin_ok = 1;
for(int i = 0;i < array_size;i++)
{
if(cin_ok)
{
int hc;
char name[Len];
cout << "Please input the name of the golf player: ";
cin.get(name,Len); // 读取一行
if(cin)
{
cout << "Please input the handicap of the golf player: ";
cin >> hc;
cin.get();
setgolf(g[i],name,hc);
real_size = i;
}
else
{
cin_ok = 0;
cin.clear(); // 不加会出错,死循环打印
real_size = i - 1;
break;
}
}
}
}
int flag;
do
{
cout << "***********************************************************************\n"
<< "****\t\t2 to change\n****\t\t3 to show\n****\t\t4 to quit\n"
<< "***********************************************************************\n";
cin >> flag;
cin.get();
if(flag == 2)
{
int hc,number;
cout << "Please input the number of the array you want to change: ";
cin >> number;
cin.get();
cout << "Please input the handicap of the golf player: ";
cin >> hc;
cin.get();
if(number <= real_size)
handicap(g[number - 1],hc);
}
else if(flag == 3)
{
for(int i = 0;i <= real_size;i++) // show the information of the struct
showgolf(g[i]);
}
}while(flag != 4);
return 0;
}
// 9.8 static.cpp
// static.cpp -- using a static local variable
#include <iostream>
// constants
const int ArSize = 10;
// function prototype
void strcount(const char * str);
int main()
{
using namespace std;
char input[ArSize];
char next;
cout << "Enter a line: \n";
cin.get(input,ArSize);
while(cin)
{
cin.get(next);
while(next != '\n') // string didn't fit!
cin.get(next);
strcount(input);
cout << "Enter next line (empty line to quit): \n";
cin.get(input,ArSize);
}
cout << "Bye.\n";
return 0;
}
void strcount(const char *str)
{
using namespace std;
static int total = 0; // static local variable
int count = 0; // automatic local variable
cout << "\"" << str << "\" contains ";
while(*str++) // go to end of string
count ++;
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
// 2
#include <iostream>
#include <string>
using namespace std;
// function prototype
void strcount(const string);
int main()
{
string input;
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 << "Bye.\n";
return 0;
}
void strcount(const string val)
{
static int total = 0; // static local variable
int count = 0; // automatic local variable
// const char *str = val.c_str(); // string类型转换为char *类型 方法一
// cout << "\"" << str << "\" contains ";
// while(*str++) // go to end of string
// count ++;
cout << "\"" << val << "\" contains ";
count = val.length(); //方法二
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
// 3
#include <iostream>
#include <cstring>
const int Len = 2;
struct chaff
{
char dross[20];
int slag;
};
int main()
{
using namespace std;
//chaff buf[Len]; // 方法一
chaff * buf = new chaff[Len]; // 方法二
chaff * p = new (buf) chaff[Len];
char strdross[20];
for(int i = 0;i < Len;i++)
{
cout << "Please input the dross of chaff " << i + 1 << ": ";
cin >> strdross;
strcpy(p[i].dross,strdross);
cout << "Please input the slag of chaff " << i + 1 << ": ";
cin >> p[i].slag;
}
// cout << "\nthe address of the buf is " << (void*)buf << endl;
// cout << "the address of the p is " << &p[0] << endl;
//循环显示结构体数据内容
int num;
do
{
cout << "Please input the num of chaff you want to show : ";
cin >> num;
while(!cin) // 如果输入不是int型的,会报错,重新输入
{
cin.clear();
while(cin.get() != '\n')
continue;
cout << "Please input the num of chaff you want to show : ";
cin >> num;
}
if(num == 1 || num == 2)
{
cout << "the dross of chaff" << num << "is\t" << p[num - 1].dross << endl;
cout << "the slag of chaff" << num << "is\t" << p[num - 1].slag << endl;
}
}while(num != 0); // 输入为0时退出
delete [] buf; // 释放new申请的内存
return 0;
}