C++ primer plus 第五章编程练习

编程练习
1.编写一个要求用户输入两个整数的程序。该程序将计算并输出
这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较
小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有
整数的和为44。

#include <iostream>
using namespace std;

int main() {   
    int x, y;
    cout << "please enter two integer x,y and x < y: ";
    cin >> x >> y;
    int sum = 0;
    while (x <= y) {
        sum += x;
        x++;
    }
    cout << "the sum is: " << sum << endl;
    return 0;
}


2.使用array对象(而不是数组)和long double(而不是long long)
重新编写程序清单5.4,并计算100!的值。


#include<array>
#include <iostream>

using namespace std;

const int ArSize = 101;

int main() {   
    array<long double, ArSize> factorials;
    factorials[0] = factorials[1] = 1;
    for (int i = 2; i < ArSize; i++)
        factorials[i] = i * factorials[i - 1];
    for (int i = 0; i < ArSize; i++)
        cout << i << "! = " << factorials[i] << endl;
    return 0;
}


3.编写一个要求用户输入数字的程序。每次输入后,程序都将报
告到目前为止,所有输入的累计和。当用户输入0时,程序结束。

#include <iostream>

using namespace std;

int main() {   
    double x, sum=0;
    do {
        cout << "please enter a number(enter 0 to stop) : ";
        cin >> x;
        sum += x;
        cout << "sum = " << sum << endl;
    } while (x != 0);
    return 0;
}


4.Daphne以10%的单利投资了100美元。也就是说,每一年的利润
都是投资额的10%,即每年10美元:
利息 = 0.10×原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款
(包括获得的利息)的5%,:
利息 = 0.05×当前存款
Cleo在第一年投资100美元的盈利是5%—得到了105美元。下一年
的盈利是105美元的5%—即5.25美元,依此类推。请编写一个程序,计
算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时
两个人的投资价值。

#include <iostream>

using namespace std;

int main() {   
    double Daphne_value = 100;
    double Cleo_value = 100;
    int i;
    for (i = 0; Cleo_value <= Daphne_value; i++) {
        Daphne_value += (100 * 0.1);
        Cleo_value *= 1.05;
    };
    cout << "year: " << i << "\nDaphne's desipote: " << Daphne_value << "\nCleo's desipote: " << Cleo_value << endl;
}


5.假设要销售《C++ For Fools》一书。请编写一个程序,输入全
年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使
用初始化为月份字符串的char *数组(或string对象数组)逐月进行提
示,并将输入的数据储存在一个int数组中。然后,程序计算数组中各元
素的总数,并报告这一年的销售情况。


#include<string>
#include <iostream>

using namespace std;

int main() {   
    string month[12] = { "january","February","March","April",
                        "May","june","july","August","September",
                        "october","November","December" };
    int num_book[12], sum = 0;
    for (int i = 0; i < 12; i++) {
        cout << "please enter the sales volume of books in " << month[i] << " : ";
        cin >> num_book[i];
    }
    for (int i = 0; i < 12; i++) {
        sum += num_book[i];
    }
    cout << "the total book sales for this year: " << sum;
    return 0;
}


6.完成编程练习5,但这一次使用一个二维数组来存储输入—3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。


#include<string>
#include <iostream>

using namespace std;

int main() {
    //月份,年字符数组
    string month[12] = { "january","February","March","April",
                        "May","june","july","August","September",
                        "october","November","December" };
    string year[3] = { "first year","second year","third year" };
    int num_book[12][3], sum = 0;//存储销售量

    //输入
    for (int i = 0; i < 3; i++) 
        for (int j = 0; j < 12; j++) {
            cout << "please enter the sales volume of books in " <<year[i]<<" and " << month[j] << " : ";
            cin >> num_book[i][j];
        }
    //输出
    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < 12; i++) {
            sum += num_book[j][i];
        }
   }
   
    cout << "the total book sales for three year: " << sum;
    return 0;
}


7.设计一个名为car的结构,用它存储下述有关汽车的信息:生产
商(存储在字符数组或string对象中的字符串)、生产年份(整数)。
编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建
一个由相应数量的car结构组成的动态数组。接下来,程序提示用户输
入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需
要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,
程序将显示每个结构的内容。该程序的运行情况如下:

#include<string>
#include <iostream>

using namespace std;

struct car {
    string producer_of_car;
    int production_year;
};
int main() {
    int n;
    cout << "How many cars do you wish to catalog? ";
    cin >> n;
    cin.get();//吸收换行符
    car *c = new car[n];//分配内存

    for (int i = 0; i < n; i++) {
        cout << "Car #" << i+1 << " : " << endl;
        cout << "Please enter the make: ";
        getline(cin, c[i].producer_of_car);
        cout << "Please enter the year made: ";
        cin >> c[i].production_year;
        cin.get();//吸收换行符
    }
    
    cout << "Here is your collection: " << endl;
    for (int i = 0; i < n; i++) {
        cout << c[i].production_year << " " << c[i].producer_of_car << endl;
    }

    delete[]c;//释放内存
    return 0;

}


8.编写一个程序,它使用一个char数组和循环来每次读取一个单
词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词
(不包括done在内)。下面是该程序的运行情况:
您应在程序中包含头文件cstring,并使用函数strcmp( )来进行比较
测试。

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

using namespace std;


int main() {
    char str[10];
    int sum = 0;
    cout << "Enter words (tp stop, type the word done) : " << endl;

    do {
        *str = {NULL};//初始化字符串
        for (int i = 0; i < 10; i++) {
            cin.get(str[i]);//使用cin.get能够识别一些分隔字符,如空格换行符制表符。
            if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
                str[i] = '\0';//这一步是关键,要把读的无效字符去掉
                break;
            }        
        }
        sum++;
    } while (strcmp(str, "done")!=0);

    cout<<"You entered a total of "<<sum-1<<" words.";
    return 0;
}

9.编写一个满足前一个练习中描述的程序,但使用string对象而不
是字符数组。请在程序中包含头文件string,并使用关系运算符来进行
比较测试。

#include<string>
#include <iostream>

using namespace std;


int main() {
    string str;
    int sum = 0;
    cout << "Enter words (tp stop, type the word done) : " << endl;

    do {
        str = {NULL};//初始化字符串
        cin >> str;
        sum++;
    } while (str!="done");

    cout<<"You entered a total of "<<sum-1<<" words.";

    return 0;
}


10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要
显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个
星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户
指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:

#include <iostream>

using namespace std;

int main() {
	int n;
	cout << "Enter number of row: ";
	cin >> n;
	for (int i = 1; i < n+1; i++) {
		for (int j = 0; j < n - i; j++)
			cout << ".";
		for (int k = 0; k < i; k++)
			cout << "*";
		cout << endl;
	}
	return 0;
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值