C++ Primer第九章課後編程題,primer第九章

來源:互聯網
上載者:User

C++ Primer第九章課後編程題,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] == '\0')        return 0;    cout << "Enter handicap value: ";    while (!(cin >> g.handicap))    {        cin.clear();        cout << "請輸入整數:";    }    while (cin.get() != '\n')        continue;    return 1;}void handicap(golf & g, int hc){    g.handicap = hc;}void showgolf(const golf & g){    cout << "Golfer: " << g.fullname << "\n";    cout << "Handicap: " << g.handicap << "\n\n";}const int Mems = 5;int main(){    golf team[Mems];    cout << "輸入 " << Mems << " 球隊成員:\n";    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:\n";    getline(cin, input);    while ("" != input)    {        strcount(input);        cout << "Enter next line:";        getline(cin, input);    }    cout << "bey!\n";}void strcount(const string &str){    cout << "\"" << str << "\": ";    int count = str.length();    cout << count << "  characters\n";}
運行結果:

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;}
運行結果:



跪《C Primer Plus》中文版(第五版)各章課後編程題答案!

c++primer plus (中文版)第五版課後編程答案哪位大俠分享下//多謝啦俺的QQ號是115425796
 
C primer plus 課後練習題目不理解,解答

它沒有指定行,你就可以把它理解在第一行的第i到j列輸出字元的一個函數。實際上這個函數調用與行無關,假設運行某程式已經輸出3行並且跳到第4行了,再調用這個函數,就是從第四行的i到j列輸出字元。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.