C++ Primer Plus笔记: 2023.07.14

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

#include 
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 
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 
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 
#include 
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 
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 
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 
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 
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 
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 
#include 
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 
#include 
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 
#include 
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 ;
}

你可能感兴趣的:(C++,Primer,Plus,c++)