C++ Primer Plus笔记: 2023.07.14

这篇文章包含一系列编程题目,涉及计算两数之间所有整数之和,计算阶乘,累加输入的数字,模拟金钱增长比较,书籍销售统计,多年度销售对比以及汽车目录管理。主要涵盖了循环控制结构,数组操作,字符串处理和内存管理等编程基础概念。
摘要由CSDN通过智能技术生成

第五章编程题:
(1)
第一种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please input two integers: " << endl ;
	int a, b ;
	cin >> a ;
	cin >> b ;
	cout << "The sum of numbers between them: " ;
	for (int i = (a+1); i<=b; i++)
		a+=i ;
	cout << a << endl ;
	return 0 ;
}

第二种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please enter two integers: " << endl ;
	int a, b, c;
	cin >> a ;
	cin >> b ;
	c = a ;
	while ( c < b )
	{
		c++ ;
		a += c ;
	}
	cout << "The sum of these numbers between them: " ;
	cout << a << endl ;
	return 0 ;
}

第三种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please input two integers: " << endl ;
	int a, b, c ;
	cin >> a ;
	cin >> b ;
	c = a ;
	do
	{
		a += ++c ;
	} while (c < b) ;

	cout << "The sum of numbers between them: " << endl ;
	cout << a << endl ;
	return 0 ;
}

(2)

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

const int ArraySize = 100 ;

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

(3)
第一种解法:

#include <iostream>
using namespace std ;

int main()
{
	int a=0, b ;
	cout << "Please enter numbers casually: " << endl ;
	cin >> b ;
	while ( b != 0)
	{
		cout << "Now the sum = "
			 << (a+=b) << endl ;
		cin >> b ;
	}
	cout << "That's the end! " << endl ;
}

第二种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please enter numbers casually: " << endl ;
	int a=0, b ;
	do
    {
		cin >> b ;
		a += b ;
		cout << "The sum of them = "
			 << a << endl ;
	} while ( b != 0 ) ;
	cout << "That's the end!" << endl ;
}

第三种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please enter numbers casually: " << endl ;
	int a = 0, b ;
	for (cin >> b; (b != 0)&&(a+=b);)
	{
		cout << "The sum of them = " << a << endl ;
		cin >> b ;
	}
	cout << "That's the end ! " << endl ;
	return 0 ;
}

(4)
第一种解法:

#include <iostream>
using namespace std ;

int main()
{
	double Daphne, Cleo, i=0 ;
	Daphne = Cleo = 100 ;
	while (!(Cleo > Daphne))
	{
		i++ ;

		Daphne += 100 * 0.10 ;
		Cleo += 0.05 * Cleo ;

		cout << "Now Daphne have " << Daphne << " dallors.\n"
			 << "Cleo have " << Cleo << " dallors.\n" ;
	}
	cout << "After " << i << " years, Cleo's money beyond Daphne's.\n" ;
}

第二种解法:

#include <iostream>
using namespace std ;

int main()
{
	double Daphne, Cleo, i = 0;
	for (Daphne = Cleo = 100;
		 Cleo <= Daphne ;
		 i++, Daphne += 0.10 * 100, Cleo += 0.05 * Cleo) ;
	cout << "After " << i << " years, "
		 << "Cleo's money beyond Daphne's.\n"
		 << "Now Daphne have " << Daphne << " dollars.\n"
		 << "Cleo have " << Cleo << "dollars.\n" ;
}

(5)

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

int main()
{
	string month[12] = {"Jenuary", "Febrary", "March", "April",
						"May", "June", "July", "August", "September",
						"October", "November", "December"} ;
	int books[12] = {0}, sum = 0 ;
	cout << "Enter how much books have sold: " << endl ;

	for(int i=1; i<=12; i++)
	{
		cout << month[i-1] << " : " ;
		cin >> books[i-1] ;
		sum += books[i-1] ;
	}

	cout << "This years have sold " << sum << " \"C++ for Fools\".\n" ;
}

(6)

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

int main()
{
	string month[12] = {"Jenuary", "Febrary", "March", "April", "May",
			    "June", "July", "August", "September", "October",
			    "November", "December"} ;
	int books[3][12] = {0} ;
	int sum[3] = {0} ;
	
	for (int i=1; i<=3; i++)
	{
		cout << "In year " << i << " :\n" ;
		for (int j=1; j<=12; j++)
		{
			cout << month[j-1] << " : " ;
			cin >> books[i-1][j-1] ;
			sum[i-1] += books[i-1][j-1] ;
		}
		cout << "The sum of this year = " << sum[i-1] << endl ;
	}
	cout << "The sum of 3 years = " << sum[0] + sum[1] + sum[2]
	     << endl ;
	return 0 ;
}

(7)

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

struct Car{
	string brand ;
	int year ;
} ;

int main()
{
	int c ;
	cout << "How many cars do you wish to catalog? " ;
	cin >> c ;
	Car* k = new Car[c] ;
	
	for (int i=1; i<=c; i++)
	{
		cout << "Car #" << i << ":\n" ;
		cout << "Please enter the make: " ;
		cin.get() ;                      // 这是非常必要的,用来消除换行符
		getline(cin, k[i-1].brand) ;
		cout << "Please enter the year made: " ;
                cin >> k[i-1].year ;
	}
	
	cout << "Here is your collection: " << endl ;

	for (int i=1; i<=c; i++)
 	{
                cout << k[i-1].year << " " << k[i-1].brand << endl ;
        }
	
	delete [] k ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值