内存模型和名称空间复习题(c++ prime plus )

第一题

注意到setgolf()被重载,可以这样使用其第一个版本:

golf ann;

setgolf(ann, "Ann Birdfree", 24);

上述函数调用提供了存储在ann结构中的信息。可以这样使用其第二个版本:

golf andy;

setgolf(andy);

上述函数将提示用户输入姓名和等级,并将它们存储在andy结构中。这个函数可以(但是不一定必须)在内部使用第一个版本。

根据这个头文件,创建一个多文件程序。其中的一个文件名为golf.cpp,它提供了与头文件中的原型匹配的函数定义;另一个文件应包含main(),并演示原型化函数的所有特性。例如,包含一个让用户输入的循环,并使用输入的数据来填充一个由golf结构组成的数组,数组被填满或用户将高尔夫选手的姓名设置为空字符串时,循环将结束。main()函数只使用头文件中原型化的函数来访问golf结构。

 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 showgolf(const golf & g);

 

Golf.cpp

 

#include <iostream>
#include "golf.h"
#include <cstring>
#include <string>

int i=0;
int main()
{
    golf H;
    golf kl[3];
    setgolf(H,"whp",12);
    showgolf(H);
    while(i < 3&&setgolf(kl[i]))
    {
        setgolf(kl[i]);
        showgolf(kl[i]);
        i++;
       
    } 
    return 0;
}

void setgolf(golf & g,const char * name,int hc)
{
    
    strcpy(g.fullname, name);
    g.handicap=hc;
}

int setgolf(golf & g)
{
    using namespace std;
    cout<<"输入name和handcip"<<endl;
    cin.getline(g.fullname,Len);
    cout<<"输入name和handcip"<<endl;
    cin>>g.handicap;
    cin.get();
    return 1;

}
void showgolf(const golf & g)
{
    using namespace std;
        cout << "Here is the golf contents:\n";
        cout << "Name: " << g.fullname<<endl;
        cout << "Handicap: " << g.handicap << endl;
}

 第二题:

修改程序清单9.9:用string对象代替字符数组。这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串“”进行比较,以判断是否为空行。

#include <iostream>
#include <string>
#include <cstring>



using namespace std;

void  strucount(const string str);


int main()
{
    string str;
    
    char next;
    cout<<"Enter a line"<<endl;
    getline(cin,str);
    while(cin)
    {
        strucount(str);
        cout<<"Enter next line(empty line to quit"<<endl;
        getline(cin,str);
        if(str=="")
            break;
    }
    cout<<"Bye"<<endl;
    return 0;
}




void strucount(const string str)
{
    using namespace std;
    static int total=0;
    int count=0;
    cout<<"\""<<str<<"\"contains";
    while(str[count])
    {
        count++;
    }
    total+=count;
    cout<<count<<"characters\n";
    cout<<total<<"characters total\n";
}

第三题:

编写一个程序,使用定位new运算符将一个包含两个这种结构的数组放在一个缓冲区中。然后,给结构的成员赋值(对于char数组,使用函数strcpy()),并使用一个循环来显示内容。一种方法是像程序清单9.10那样将一个静态数组用作缓冲区;另一种方法是使用常规new运算符来分配缓冲区

 

#include <iostream>
#include <new>
#include <string>
#include <cstring>


using namespace std;
const int BUF=512;

struct chaff
{
    char dross[20];
    int slag;
};

int main()
{
    
    char buffer[BUF];
    chaff *p1=new (buffer) chaff[3];
    char * p2=new char [BUF];
    chaff * p3=new(p2) chaff[3];
    char dross[20]={0};
    int slag=0,i=0;
    while(i<3)
    {
        cout<<"请输入你的dross和slag"<<endl;
        cin.getline(dross,20);
        cout<<"请输入你的dross和slag"<<endl;
        cin>>slag;
        cin.ignore();
        strcpy(p1[i].dross,dross);
        strcpy(p3[i].dross,dross);
        p1[i].slag=p3[i].slag=slag;
        i++;
        
    }
    i=0;
    while(i<3)
    {
        cout<<p1[i].dross<<p1[i].slag<<endl;
        cout<<p3[i].dross<<p3[i].slag<<endl;
        i++;
    }
}

 

posted @ 2022-07-22 14:12  术术子  阅读(27)  评论(0编辑  收藏  举报