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] == '\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;
}

运行结果:

时间: 2024-08-26 13:33:00

C++ Primer第九章课后编程题的相关文章

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

1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 代码: 头文件golf.h代码: const int Len = 40; struct golf { char fullname[Len]; int handicap; }; void setgolf(golf &

C++ Primer第五章课后编程题

1. 代码 #include<iostream> int main() { using namespace std; int num1; int num2; int total=0; cout << "请输入开始数字\n"; cin >> num1; cout << "请输入结束数字\n"; cin >> num2; for (num1; num1<=num2; num1++) total = num

C++ Primer第四章课后编程题

1. 代码 #include<iostream> #include<string> int main() { using namespace std; string name; string lname; char grade; int age; cout << "What is your first name?"; getline(cin, name); cout << "What is your last name?&quo

C++ Primer第八章课后编程题

1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数.不过,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数).是的,这是一个非常可笑的函数,但它让读者能够使用本章介绍的一些技术.在一个简单的程序中使用该函数,以演示该函数是如何工作的. 代码: #include<iostream> using namespace std; void show(const char *

C Primer Plus (第五版) 第九章 函数 编程练习

第九章 函数 编程练习 设计函数 min(x,y),返回两个double数值中较小的数值,同时用一个简单的驱动程序测试该函数. #include <stdio.h> double min(double x, double y); int main(void) { double a, b; printf("请输入两个数:\n"); scanf("%lf%lf", &a, &b); printf("两个数中较小的数是:%lf\n&q

java语言的科学与艺术 第六章 课后编程

欢迎大家转载,为保留作者成果,转载请注明出处,http://blog.csdn.net/netluoriver,有些文件在资源中也可以下载!如果你没有积分,可以联系我索要! 1. package SixthCharter; /* * File: Poker.java * --------------------------- * 这是第6章的第一题 * Author luoriver */ import acm.program.*; import acm.util.*; public class

C++ PRIMER 第九章

顺序容器:vector list deque 顺序容器适配器: stack queue priority_quequ(没见过,第一轮不管) C<T> c; C c(c2); C c(b,e) ///b e 都是迭代器; c(n,t)///只用于顺序容器; C c(n) ///只用于顺序容器 const list<int>::size_type list_size = 64; list<string> slist(size_type,"eh?"); 支

Learning Perl 第九章习题第二题

把输入文件中的所有Fred换成Larry, 不区分大小写. 知识点 1. 文本文件读写 2. 简单的正则替换 3. unless 的用法 4. $_ 的用法 Learning Perl 第九章习题第二题,布布扣,bubuko.com

C++ Primer章课后编程问题

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 1. 代码 #include<iostream> int main() { using namespace std; int num1; int num2; int total=0; cout <<