第九章 内存模型与名称空间
第一题:按题目要求补全程序,使用循环填充结构体,对名字输入空时结束,使用三个文件完成题目要求。
//golf.h file1
#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
//golf.cpp file2
#include<iostream>
#include"golf.h"
void setgolf (golf & g,const char * name ,int hc)
{
extern const int Len;
for(int i=0;i<Len;i++)
g.fullname[i]=name[i];
g.handicap=hc;
}
int setgolf (golf & g)
{
int a;
//char name[Len];
//int hc;
std::cout<<"Please Enter the name: ";
std::cin.getline( g.fullname,Len);
//g.fullname[Len]='\0';
a=strlen(g.fullname);
if (a!=0)
{
std::cout<<"Please Enter the handicap: ";
std::cin>>g.handicap;
std::cin.get();
return 1;
}
else
return 0;
}
void handicap (golf & g,int hc)
{
g.handicap=hc;
}
void showgolf (const golf & g)
{
std::cout<<"FullName is : "<<g.fullname<<std::endl;
std::cout<<"Handicap is : "<<g.handicap<<std::endl;
}
//one.cpp file3 , the main function
#include<iostream>
#include"golf.h"
int main()
{
const int man=5;
golf player[man];
int i=1;
int a;
a=setgolf(player[0]);
while(a!=0)
{
if(i<man)
{
a=setgolf(player[i]);
i++;
}
else
break;
}
for (int j=0;j<i-1;j++)
showgolf(player[j]);
if (strlen(player[0].fullname)!=0)
{
std::cout<<"Chage the handicap of "<<"\'"<<player[0].fullname<<"\'"
<<" ,enter the new value: ";
int hc;
std::cin>>hc;
handicap(player[0],hc);
showgolf(player[0]);
}
std::cout<<"Ends."<<std::endl;
system("pause");
return 0;
}
第二题:修改程序清单9.8,使用string 对象替换char数组,并用相关比较函数判断空行。
#include<iostream>
#include<string>
void strcount(const std::string *str);
int main()
{
using namespace std;
string input;
char next;
cout<<"Enter a line :\n";
getline(cin,input);
while(cin)
{
strcount(&input);
cout<<"Enter next line (empty line to quit): \n";
getline(cin,input);
if(!input.compare(""))
break;
}
cout<<"Bye\n";
system("pause");
return 0;
}
void strcount(const std::string * str)
{
using namespace std;
static int total=0;
int count=0;
int len=(*str).size();
char a;
for (int i=0;i<len;i++)
{
char a=(*str)[i];
if(a!=' ')
{
count++;
}
}
total+=count;
cout<<"\""<<*str<<"\" conttains ";
cout<<count<<" characters\n";
cout<<total<<" characters total\n";
}
第三题:根据题目给出的结构体,用两种方法来初始化一个包含两个结构体的数组,并显示结构体内容,法一指定内存分配,法二用常规内存分配。
#include<iostream>
#include<new>
struct chaff
{
char dross[20];
int slag;
};
using namespace std;
const int NUM = 50;
chaff buff[NUM];
int main()
{
chaff *pc;
pc= new(buff) chaff[2];
pc = pc + 1;
char temp[20];
int i = 0;
cout << "Solution 1: " << endl;
for (; i < 2; i++)
{
cout << "Please enter the dross: ";
cin >> temp;
strcpy_s(pc->dross, temp);
cout << "Please enter the slag: ";
cin >> pc->slag;
pc++;
}
for (int j = 0; j < i;j++)
{
pc--; //倒序显示内容,指针返回。
cout << "Dross: " << pc->dross << ". Slag is " << pc->slag << endl;
}
//delete []pc;
cout << endl << endl;
cout << "Solution 2: " << endl;
chaff *pc2 = new chaff[2];
pc2 = pc2 + 1;//倒序显示时,最后PC2--超出分配范围,故先加1;
i = 0;
for(; i < 2; i++)
{
cout << "Please enter the dross: ";
cin >> temp;
strcpy_s(pc2->dross, temp);
cout << "Please enter the slag: ";
cin >> pc2->slag;
pc2++;
}
for (int j = 0; j < i; j++)
{
pc2--; //倒序显示内容,指针返回。
cout << "Dross: " << pc2->dross << ". Slag is " << pc2->slag << endl;
}
delete [] pc2;
cin.get();
system("pause");
return 0;
}
ps:程序最后会中止,不知道原因,以后再改。
第四题:根据题目给出完成程序,并按要求测试。
//SALES.h file1
#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 = 4);
void setSales(Sales &s);
void showSales(const Sales &s);
}
#endif
// SALES.CPP file2
#include<iostream>
#include"SALES.h"
namespace SALES{
void setSales(Sales &s, const double ar[], int n)
{
double sum = 0;
s.max = s.min = ar[0];
for (int i = 0; i < n; i++)
{
s.sales[i] = ar[i];
sum += ar[i];
if (s.max < ar[i])
s.max = ar[i];
if (s.min > ar[i])
s.min = ar[i];
}
s.average = sum / n;
}
void setSales(Sales &s)
{
using std::cout;
using std::cin;
using std::endl;
cout << "Please enter four quarters ." << endl;
for (int i = 0; i < QUARTERS; i++)
{
cout << "Now the " << i+1 << " : ";
cin >> s.sales[i];
}
double sum = 0;
double tempmin = s.sales[0];
double tempmax = s.sales[0];
for (int i = 1; i <QUARTERS; i++)
{
sum += s.sales[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;
s.average = sum / QUARTERS;
}
void showSales(const Sales &s)
{
using std::cout;
using std::cin;
using std::endl;
int n = sizeof(s.sales) / sizeof(double);
cout << "The sales is: ";
for (int i = 0; i < n; i++)
{
cout << s.sales[i] << " ";
}
cout << endl;
cout << "The max value is " << s.max << endl;
cout << "The min value is " << s.min << endl;
cout << "The average value is " << s.average << endl;
}
}
//four.cpp file3
#include<iostream>
#include"SALES.h"
using namespace SALES;
int main()
{
Sales s1,s2;
setSales(s1);
double ar[4] = { 35,75,43.8,72 };
setSales(s2, ar, 4);
showSales(s2);
showSales(s1);
system("pause");
return 0;
}
第九章完成。