C++ primer plus 第六版 第五章 编程练习答案

第五章 编程练习答案

1.

#include<iostream>

int main()
{
    using namespace std;
    int i, a, b, sum;

    cout << "Enter the smaller number: " ;
    cin >> a;
    cout << "Enter the biger number: " ;
    cin >> b;   
    sum = 0;
    for(i = a ;i <= b ;i++)
        sum += i;
    cout << "The sum is: " << sum << endl; 
    return 0;

}

2.

第二题要使用模板类array(C++11),编译器版本比较老不支持

3.

#include<iostream>

int main()
{
    using namespace std;
    int n, sum = 0;
    cin >> n;
    while(n!=0){
        sum += n;       
    cout << "The sum is: " << sum << endl; 
    cin >> n;       
    }   
    return 0;   
}

4.

#include<iostream>

int main()
{
    using namespace std;
    const double money = 100;
    const double D_interest = 0.1;
    const double C_interest = 0.05;
    int i;
    double Daphne, Cleo;
    Daphne = money;
    Cleo = money;

    for(i=0 ;Daphne>=Cleo ;i++)
    {
        Daphne += D_interest * money;
        Cleo += C_interest * Cleo;
    }
    cout << "Year:" << i << endl;
    cout << "Daphne's money: " << Daphne << endl;
    cout << "Cleo's money: " << Cleo << endl;
    return 0;   
}

5.

#include<iostream>
#include <string>

int main()
{
    using namespace std;

    int i, sum = 0;
    int sales[12];
    const string months[] = {"1月", "2月", "3月", "4月", "5月", 
    "6月", "7月", "8月", "9月", "10月", "11月", "12月"};

    cout << "输入每月销售量: " << endl;
    for(i = 0; i < 12; i++)
    {
        cout << months[i] << ":";
        cin >> sales[i];
        sum += sales[i];
    }
    cout << "这一年的销售数量:" << sum << endl; 
    return 0;
}

6.

#include<iostream>
#include <string>

int main()
{
    using namespace std;

    int i, j, all = 0;
    int sum[3]={0, 0, 0}; 
    int sales[3][12];//二维数组 
    const string years[] = {"第一年", "第二年", "第三年"};
    const string months[] = {"1月", "2月", "3月", "4月", "5月", 
    "6月", "7月", "8月", "9月", "10月", "11月", "12月"};

    cout << "输入三年中每月销售量: " << endl;
    for(j = 0;j < 3; j++){
        for(i = 0;i < 12; i++){
            cout << years[j] << months[i] << "销售量:";
            cin >> sales[j][i];
            sum[j] += sales[j][i];
        }
        all += sum[j];
    }

    for(j = 0;j < 3; j++)
        cout << years[j] << "销售总量:" << sum[j] << endl;
    cout << "这三年的销售数量:" << all << endl;

    return 0;
}

7.

#include<iostream>
struct car
{
    char name[20];
    int year;

};

int main()
{
    using namespace std;
    int i, n;
    cout << "How many cars do you wish to catalog?";
    cin >> n;
    cin.get();

    car* ps = new car[n];//创建动态数组   

    for(i = 0; i < n; i++){
        cout << "Car #" << i+1 <<": \n";
        cout << "Please enter the make: ";
        cin.get(ps[i].name, 20);//读取字符串 
        cout << "Please enter the year made: ";
        cin >> ps[i].year;
        cin.get();//读取数值                    
    }

    cout << "Here is your collection: \n";
    for(i = 0; i < n; i++)
        cout << ps[i].year << " " << ps[i].name << endl;

    delete ps;//释放指针指向的内存 
    return 0;

}

8.

#include<iostream>
#include<cstring>

int main()
{
    using namespace std;
    char ch[30];
    int count = 0;
//const char* const Done = "done";另一种方法,设定常量字符 

    cout << "Enter words (to stop, type the word done):" << endl;
    while(strcmp(ch, "done"))
    {
        count++;
        cin >> ch;
    }

    cout << "You entered a total of " << count-1 << " words." << endl;
    return 0;

}

9.

#include<iostream>
#include<cstring>

int main()
{
    using namespace std;
    string ch;
    int count = 0;
    string Done = "done";

    cout << "Enter words (to stop, type the word done):" << endl;
    while(Done != ch)
    {
        count++;
        cin >> ch;

    }

    cout << "You entered a total of " << count-1 << " words." << endl;
    return 0;

}

10.

#include<iostream>

int main()
{
    using namespace std;
    int i, j, n;

    cout << "Enter number of rows: ";
    cin >> n;

    for(i = 1; i<=n; i++){
        for(j=0;j<n-i;j++)
            cout << ".";
        for( ; j<n; j++)
            cout << "*" ;
        cout << endl;   
    }
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
《C Primer Plus第六版》是一本面向初学者的C语言教材,由Stephen Prata撰写。这本书深入浅出地介绍了C语言的基本概念、语法和应用,给读者提供了扎实的编程基础。 该书共分为27章,每章都有清晰的目标、易于理解的示例和练习题。从第一章的入门介绍开始,到最后一章的高级主题讨论,书中的内容依次递进,系统完整地覆盖了C语言的方方面面。本书有助于读者逐步掌握C语言的基础知识,从简单的输出语句到复杂的函数调用和指针使用。 《C Primer Plus第六版》的特点是其清晰的讲解风格和丰富的实例。作者通过通俗易懂的语言和生动形象的例子,帮助读者理解和掌握C语言的各种概念和语法。此外,书中还提供了许多练习题编程项目,供读者巩固所学知识和提高编程能力。 作为一本经典的C语言教材,《C Primer Plus第六版》被广泛用于学校和个人学习。它不仅适用于初学者,也对有一定编程基础的读者有所帮助。读者通过学习本书,可以建立起对C语言编程的扎实基础,为深入学习其他编程语言打下坚实的基础。 综上所述,《C Primer Plus第六版》是一本权威、经典的C语言教材,通过清晰的讲解和丰富多样的示例帮助读者深入理解C语言的基本概念和应用。无论是初学者还是有一定编程基础的读者,都可以从中获益,打下良好的编程基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值