欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++ Primer Plus(第六版) 第五章 编程练习

程序员文章站 2023-12-22 21:31:28
...

编程环境:Visual Studio 2017

1:

//practice 1
#include<iostream>
using namespace std;

int main()
{
	int a, b, sum;
	a = b = sum = 0;

	cout << "Enter two integer: ";
	cin >> a >> b;

	for (int i = a; i <= b; ++i)
	{
		sum += i;
	}

	cout << "The sum of all integers between " << a << " and " << b << " is " << sum << endl;

	system("pause");
	return 0;
}

2: 

//practice 2
#include<iostream>
#include<array>
//#include<iomanip>		setprecision需要的头文件
using namespace std;

const int ArSize = 16;
int main()
{
	array<long double, ArSize> factorials;
	factorials[0] = factorials[1] = 1.0;
	for (int i = 2; i < ArSize; ++i)
	{
		factorials[i] = i * factorials[i - 1];
	}

	//cout << fixed;				强制使用小数输入,防止使用科学计数法
	//cout << setprecision(0);		控制显示的精度,控制小数点后面的位数

	for (int i = 0; i < ArSize; ++i)
	{
		cout << i << "! = " << factorials[i] << endl;
	}

	system("pause");
	return 0;
}

 3:

//practice 3
#include<iostream>
using namespace std;

int main()
{
	int number = 0, sum = 0;

	cout << "Enter a number( The 0 indicates the end ): ";
	cin >> number;

	while (number != 0)
	{
		sum += number;
		cout << "The accumulative total is : " << sum << endl;
		cout << "Enter a number( The 0 indicates the end ): ";
		cin >> number;
	}

	return 0;
}

4: 

//practice 4
#include<iostream>
using namespace std;

int main()
{
	double Daphne_account = 100;
	double Cleo_account = 100;
	int years = 0;

	while (Cleo_account <= Daphne_account)
	{
		Daphne_account += 10.0;
		Cleo_account *= 1.05;
		++years;
	}

	cout << "After " << years << " years.\n";
	cout << "Daphne's investment value is " << Daphne_account << endl;
	cout << "Cleo's investment value is " << Cleo_account << endl;

	system("pause");
	return 0;
}

5: 

//practice 5
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string months[12] = {"January","February","March","April","May","June","July","August","September","October","November","December" };
	int sales[12] = { 0 };
	int total_sales = 0;

	cout << "Enter sales per month: \n";

	for (int i = 0; i < 12; ++i)
	{
		cout << months[i] << ": ";
		cin >> sales[i];
		total_sales += sales[i];
	}

	cout << "\nThe total sales is " << total_sales << endl << endl;

	for (int i = 0; i < 12; ++i)
	{
		cout << months[i] << ": " << sales[i] << endl;
	}
	system("pause");
	return 0;
}

 6:

//practice 6
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string months[12] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
	int sales[3][12] = { 0 };
	int total_sales[3] = { 0 };

	for (int i = 0; i < 3; ++i)
	{
		cout << "\nEnter sales per month in " << i + 1 << "th year: \n";
		for (int j = 0; j < 12; ++j)
		{
			cout << months[j] << ": ";
			cin >> sales[i][j];
			total_sales[i] += sales[i][j];
		}
	}

	for (int i = 0; i < 3; ++i)
	{
		cout << i + 1 << "th year total sales is " << total_sales[i] << endl;
	}

	cout << "Three years total sales is " << total_sales[0] + total_sales[1] + total_sales[2] << endl;

	system("pause");
	return 0;
}

 7:

//practice 7
#include<iostream>
#include<string>
using namespace std;

struct car
{
	string manufacturer;
	int pro_year;
};

int main()
{
	int n = 0;
	cout << "How many cars do you wish to catalog? ";
	cin >> n;
	cin.get();

	car *pcar = new car[n];
	for (int i = 0; i < n; i++)
	{
		cout << "Car #" << i + 1 << ":" << endl;
		cout << "Please enter the manufacturer: ";
		getline(cin, pcar[i].manufacturer);

		cout << "Please enter the year made: ";
		cin >> pcar[i].pro_year;
		cin.get();
	}

	cout << "Here is your collection:" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << pcar[i].pro_year << " " << pcar[i].manufacturer << endl;
	}

	system("pause");
	return 0;
}    

 8:

//practice 8
#include<iostream>
using namespace std;

int main()
{
	int n = 0;
	char word[20];

	cout << "Enter words (to stop, type the word done): " << endl;
	cin >> word;
	while (strcmp(word, "done"))
	{
		n++;
		cin >> word;
	}

	cout << "You entered a total of " << n << " words." << endl;
	system("pause");
	return 0;
}

 9:

//practice 9
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int n = 0;
	string word;

	cout << "Enter words (to stop, type the word done): " << endl;
	cin >> word;
	while (word != "done")
	{
		n++;
		cin >> word;
	}

	cout << "You entered a total of " << n << " words." << endl;
	system("pause");
	return 0;
}

 10:

//practice 10
#include<iostream>
using namespace std;

int main()
{
	int n = 0;
	cout << "Enter number of rows: ";
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n - i; j++)
		{
			cout << ".";
		}
		for (int j = 0; j < i; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

才疏学浅,如有错误或者改进的建议还请大牛多多指出。

继续学习,慢慢更新。

相关标签: C C Primer Plus

上一篇:

下一篇: