C++ Primer Plus 第五章 课后练习

#include <iostream>
using namespace std;
int main()
{	

	int num_1,num_2;
	int sum = 0;
	cout << "请输入第1个数:";
	cin  >> num_1;
	cin.ignore();
	cout << "请输入第2个数:";
	cin  >> num_2;
	
	for(int i = num_1 ;i <= num_2; i++){
		sum = sum + i;
	}
	cout << sum;
    return 0;
}

运行结果:

程序清单5.4

改写后:

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

const int ArSize = 101;      
int main()
{
	//如何创建array对象 设定其长度与元素类型。 
    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;
}

运行结果:

#include <iostream>
using namespace std;   
int main()
{
	int num=1,sum = 0;
	while(num != 0){
		cout << "请输入数字:";
		cin >> num;
		sum = sum + num;
		cout << "输入和为" << sum;
	}	
    return 0;
}

运行结果:

#include <iostream>
using namespace std;   
int main()
{
	double Cleo_M = 100, Doph_M =100;
	int year = 0;
	while(Cleo_M <= Doph_M){
		Cleo_M =  Cleo_M + Cleo_M * 0.05;
		Doph_M =  Doph_M + 100 * 0.10;
		year++ ;
	}
	cout << year <<"年后,Cleo的投资价值超过了Doph" <<endl;
	cout << "Cloe的投资价值为:" << Cleo_M <<endl;
	cout << "Doph的投资价值为:" << Doph_M <<endl;
	return 0;
}

运行结果:

#include "iostream"
using namespace std;
const int month = 12;
const char *months[month] =
    {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };

int main()
{
    int sales[month], sum = 0;
    for (int i = 0; i <month; i++)
    {
        cout << "输入在" << months[i] << "中的C++ for fools的销售量: ";
        cin >> sales[i];
        sum += sales[i];
    }
    cout << "这年的C++ for fools的总销量为" << sum << endl;
    return 0;
}

 

#include "iostream"
using namespace std;
const int month = 12;
const int year = 3;
const char *months[month] =
    {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };

int main()
{
    int sales[year][month], sum = 0;
    for (int j = 1; j <= year ; j++){
    	for (int i = 0; i < month; i++)
    	{
        	cout << "输入在第" << j <<"年的" << months[i] << "中的C++ for fools的销售量: ";
       	 	cin >> sales[j][i];
       	 	sum += sales[j][i];
    	}
	}
    cout << "这三年的C++ for fools的总销量为" << sum << endl;
    return 0;
}

#include <iostream>
using namespace std;
struct car {
	string name;
	int year;
};
int main() {
	cout << "how many cars are there?";
	int n, i;
	cin >> n;
	car* pl = new car[n];
	for (i = 0; i < n; i++) {
		cout << "car:#" << i + 1 << endl << "enter the car's name:" ;
		cin >> pl[i].name;
		cout << "enter the car's producting year:" ;
		cin >> pl[i].year;
	}
	for (i=0;i<n;i++){
		cout << "here is ur collection:\n";
		cout << pl[i].year << " " << pl[i].name;
	}
	delete []pl;
	return 0;
}

 

 这道题实际上是对字符串的比较与读取。

#include <iostream>
#include <cstring>
using namespace std;
int main() {
	int count = 0;
	cout << "Enter words(to stop, type the word done):" << endl;
	char word[20];
	cin >> word;
	while (strcmp(word, "done"))
	{
		count++;
		cin >> word;
	}
	cout << "Your entered a total of "<<count<<" words.";
	return 0;
}

这题要求使用string对象,并且使用关系运算符进行测试比较。

#include <iostream>
#include <cstring>
using namespace std;
int main() {
	int count = 0;
	cout << "Enter words(to stop, type the word done):" << endl;
	string word;
	cin >> word;
	while (word != "done")
	{
		count++;
		cin >> word;
	}
	cout << "Your entered a total of "<<count<<" words.";
	return 0;
}

运行结果

#include <iostream>
#include <cstring>
using namespace std;
int main() {
	int rows = 0;
	cout << "Enter the number of rows:"; 
	cin >> rows;
	for (int i = 1; i <= rows;i++){
		cout << endl;
		for (int j = 1; j <= rows-i ; j++){
			cout << ".";
		}
		for (int r = 1 ;r <= i ;r++){
			cout << "*"; 
		}
	}
	return 0;
}

运行结果:

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值