• C++ Primer第九章课后编程问题


    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);
    golf.cpp代码
    #include<iostream>
    #include<cstring>
    #include "golf.h"
    using namespace std;
    void setgolf(golf & g, const char * name, int hc)
    {
        strcpy(g.fullname, name);
        g.handicap = hc;
    }
    int setgolf(golf & g)
    {
        cout<< "请输入全名: ";
        cin.getline(g.fullname, Len);
        if (g.fullname[0] == '')
            return 0;
        cout << "Enter handicap value: ";
        while (!(cin >> g.handicap))
        {
            cin.clear();
            cout << "请输入整数:";
        }
        while (cin.get() != '
    ')
            continue;
        return 1;
    }
    void handicap(golf & g, int hc)
    {
        g.handicap = hc;
    }
    void showgolf(const golf & g)
    {
        cout << "Golfer: " << g.fullname << "
    ";
        cout << "Handicap: " << g.handicap << "
    
    ";
    }
    const int Mems = 5;
    int main()
    {
        golf team[Mems];
        cout << "输入 " << Mems << " 球队成员:
    ";
        int i;
        for (i=0; i<Mems; i++)
            if (setgolf(team[i]) == 0)
                break;
        for (int j=0; j<i; j++)
            showgolf(team[j]);
        setgolf(team[0], "Fred Norman", 5);
        showgolf(team[0]);
        handicap(team[0], 4);
        showgolf(team[0]);
        return 0;
    }
    执行结果:


    2、改动程序清单9.8:用string对象取代字符数组.这样,该程序将不再须要检查输入的字符串是否过长,同一时候能够将输入字符串同字符串""进行比較,比推断是否为空行
    代码:
    #include<iostream>
    #include<string>
    using namespace std;
    void strcount(const string &str);
    int main()
    {
        string input;
        cout << "Enter a line:
    ";
        getline(cin, input);
        while ("" != input)
        {
            strcount(input);
            cout << "Enter next line:";
            getline(cin, input);
        }
        cout << "bey!
    ";
    }
    void strcount(const string &str)
    {
        cout << """ << str << "": ";
        int count = str.length();
        cout << count << "  characters
    ";
    }
    执行结果:


    3、

    代码:
    #include<iostream>
    #include<cstring>
    struct chaff
    {
        char dross[20];
        int slag;
    };
    int main()
    {
        using std::cout;
        using std::endl;
        chaff *p1;
        int i;
        p1 = new chaff[2];
        std::strcpy(p1[0].dross, "Tiffany Zhao");
        p1[0].slag=13;
        std::strcpy(p1[1].dross, "guugle");
        p1[1].slag=-23;
        for(i=0; i<2; i++)
            cout << p1[i].dross << ": " << p1[i].slag <<endl;
        delete [] p1;
        return 0;
    }
    执行结果:



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Math 方法的使用
    敏捷开发 故事墙
    利用OpenPOP开源组件实现接收电子邮件功能
    JDBC连接MySql数据库
    【转】"已成功与服务器建立连接,但是在登录前的握手期间发生错误"问题解决方案
    【转】如何让虚拟目录里面的webconfig不继承网站
    【转】JavaScript控制图片放大缩小
    【Wonder原创】CheckBoxList实现单选
    【杂记】ASP.Net Web开发
    【Wonder整理】防止重复提交并弹出半透明对话框
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4803615.html
Copyright © 2020-2023  润新知