跟着c++Primer Plus 第六版 学编程----循环和关系表达式 5.9编程练习

跟着c++Primer Plus 第六版 学编程----循环和关系表达式 5.9编程练习

1、编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间的所有整数之和。这里假设先输入最小整数。

#include <iostream>
int main()
{
	using namespace std;
	int i, j, n;
	n = 0;
	cin >> i >> j;
	for (; i <= j; i++)
		n += i;
	cout << n << endl;
	return 0;
}

在这里插入图片描述

2、使用 array 对象和 long double 重新程序清单5.4,并计算100!的值。

#include <iostream>
#include<array>
const int ArSize = 101;
int main()
{
	using namespace std;
	array<long double, ArSize>factorials;
	factorials[0] = factorials[1] = 1L;
	for (int i = 2; i < ArSize; i++)
		factorials[i] = i * factorials[i - 1];
	for (int i = 0; i < 16; i++)
		cout << i << "! = " << factorials[i] << endl;
	cout << "100! = " << factorials[100] << endl;
	return 0;
}

在这里插入图片描述

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

#include <iostream>
const int ArSize = 101;
int main()
{
	using namespace std;
	int i;
	cout << "Enter num: ";
	cin >> i;
	while (i != 0)
	{
		cout << "程序在此处:" << i << endl;
		cout << "Enter num: ";
		cin >> i;
	}
	return 0;
}

在这里插入图片描述

4、Daphne 以10%的单利投资了100美元。也就是说每一年的利润都是投资额的10%,即每年10美元:利息 = 0.10 x 原始存款。而 Cleo 以5%的复利投资了100美元:利息 = 0.05 x 当前存款。请编写一个程序,计算多少年后,Cleo 投资价值才能超过Daphne 的投资价值,并显示两人的投资价值。

#include <iostream>
int main()
{
	using namespace std;
	double Daphne, Cleo;
	int count;
	count = 0;
	for (Daphne = 100, Cleo = 100; Cleo <= Daphne; Daphne += 10, Cleo +=( 0.05* Cleo))
 count++;
		cout<<"after "<< count <<" years" <<endl;
		cout <<"Daphne = "<< Daphne << " ," <<"Cleo = "<< Cleo << endl;

	return 0;
}

在这里插入图片描述

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int ArSize = 12;
    const string months[ArSize] =
    {
        "January", "February","March",
        "April", "May", "June", "July",
        "August","September", "October",
        "November", "December"
    };
    int sum, sales[ArSize];
    sum = 0;//sum必须初始化,不然编译系统会报错

    for (int i = 0; i < ArSize; i++)
    {
        cout << "Please enter number of books sold (";
        cout << months[i] << "): ";
        cin >> sales[i];
    }
    for (int i = 0; i < ArSize; i++)
    {
        sum += sales[i];
    }
    cout << "A total of " << sum << " <<C++ For Fools>> books were sold in a year." << endl;

    return 0;
}

在这里插入图片描述

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int ArSize_0 = 3;
    const int ArSize_1 = 12;
    const string months[ArSize_1] =
    {
        "January", "February","March",
        "April", "May", "June", "July",
        "August","September", "October",
        "November", "December"
    };
    int sum, sale[ArSize_0][ArSize_1];
    int i, j;
    int Sale_First_Year, Sale_Second_Year, Sale_Threeth_Year;
    Sale_First_Year = Sale_Second_Year = Sale_Threeth_Year = 0;
    sum = 0;//sum必须初始化,不然编译系统会报错

    for (i = 0; i < ArSize_0; i++)
    {
        cout << "Number "<<i+1<<" year:" << endl;
        for (j = 0; j < ArSize_1; j++)
        {
                cout << "Please enter number of books sold (";
                cout << months[i] << "): ";
                cin >> sale[i][j];
        }
    }
    for (i = 0; i < ArSize_0; i++)
    {
        for (j = 0; j < ArSize_1; j++)
        {
            if (i == 0)
                Sale_First_Year += sale[i][j];
            if (i == 1)
                Sale_Second_Year += sale[i][j];
            if (i == 2)
                Sale_Threeth_Year += sale[i][j];
        }
    }
    int All_Sale = Sale_First_Year + Sale_Second_Year + Sale_Threeth_Year;
    cout << "First year sale book: " << Sale_First_Year << endl;
    cout << "Second year sale book: " << Sale_Second_Year << endl;
    cout << "Threeth year sale book: " << Sale_Threeth_Year << endl;
    cout << "Three years sale book: " << All_Sale << endl;
    return 0;
}

在这里插入图片描述

7、设计一个名为 car 的结构,用它储存下述有关汽车的信息:生产商、生产年份。编写一个程序,向用户询问有多少辆车。随后,程序使用 new 来创建一个相应数量的 car 结构组成的动态数组。接下来程序提示用户输入每辆车的生产商和年份信息。

#include <iostream>
int main()
{
	using namespace std;
	const int ArSize = 20;
	int ENTER;
	struct car
	{
		char Car_Name[20];
		int Car_Year;
	};
	cout << "How meny cars do you wish catalog?  ";
	cin >> ENTER;
	
	car* Pcar = new car[ENTER];
	for(int i = 0;i <ENTER;i++)
	{
		cout << "Car #" << i+1 << endl;
		cin.get();
		cout << "Please enter the make : ";
		cin.getline(Pcar[i].Car_Name, ArSize);
		cout << "Please enter the year made: ";
		cin >> Pcar[i].Car_Year;
	}
	for (int i = 0; i < ENTER; i++)
	{
		cout << Pcar[i].Car_Year << " " << Pcar[i].Car_Name << endl;
	}

	return 0;
}

在这里插入图片描述

8、编写一个程序,它使用一个 char 数组和循环来每次读取一个单词,直到用户输入 done 为止。随后,该程序指出用户输入了多少单词。下面是该程序的运行情况:

在这里插入图片描述

#include <iostream>
#include <string>
int main()
{
	using namespace std;
	int count = 0;
	char s1 = 'done';
	char Search[20];
	cout << "Enter words (to stop, type the word done):" << endl;
	while (strcmp(Search, "done") != 0)//通过比较两个数组字符串比较判断循环的条件
	{
		cin >> Search;
		++count;
	}
	cout << "You entered a total of " << count - 1 << " words." << endl;
	return 0;
}

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string Search;
    unsigned int count = 0;

    cout << "Enter words (to stop, type the word done):" << endl;
    while (cin >> Search)
    {
        if ("done" == Search)
        {
            break;
        }
        ++count;
    }
    cout << "You entered a total of " << count << " words." << endl;

    return 0;
}

10、编写一个嵌套循环程序,要求用户输入一个值,指出要显示多少行。然后,将程序显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推星号不够加句点。

在这里插入图片描述

#include <iostream>
#include <string>
int main()
{
	using namespace std;
	int Enter;
	cout << "Enter number of rows: ";
	cin >> Enter;
	for (int i = Enter; i >= 0; i--)
	{
		for (int j = 0; j < Enter; j++)
			{
			if (j < i)
			{
				cout << ". ";
			}
			else
				cout << "* ";
			}
		cout << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太阳请了个假

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值