C++ Primer Plus第二章编程练习

一、第二章

一、第一题

1.编写一个C++程序,它显示您的姓名和地址。

#include<iostream>
using namespace std;
int main()
{
	cout << "xx" << endl;//姓名
	cout << "xx市" << endl;//地址
	return 0;
}

二、第二题

2.编写一个C++程序,它要求用户输人一个以long为单位的距离,然后将它转换为码(一long等于220码)。

#include<iostream>
using namespace std;
int main()
{
	int long1 = 0;
	cin >> long1;
	int ma = 220 * long1;
	cout << ma << endl;
	return 0;
}

三、第三题

3.编写一个C++程序,它使用3个用户定义的函数(包括main),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。

#include<iostream>
using namespace std;
void fun1()
{
	cout << "Three blind mice" << endl;
	
}
void fun2()
{
	cout << "Three blind mice" << endl;

}
void fun3()
{
	cout << "See how they run" << endl;
	
}
void fun4()
{
	cout << "See how they run" << endl;

}
int main()
{
	fun1();
	fun2();
	fun3();
	fun4();
	return 0;
}

四、第四题

4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age:29

#include<iostream>
using namespace std;
int main()
{
	int age = 0;
	cout << "Enter your age:";
	cin >> age;
	int month = 12 * age;
	cout << month << endl;
	return 0;
}

五、第五题

5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求输入摄氏温度值,并显示结果:
Please enter a Celsius value:20
20 degrees Celsius is 68 degrees Fahrenheit.
下面是转换公式:
华氏温度=1.8x摄氏温度+32.0

#include<iostream>
using namespace std;
double fun(double a)
{
	return 1.8 * a + 32.0;
}
int main()
{
	double sheshi = 0;
	cout << "Please enter a Celsius value:";
	cin >> sheshi;
	double z = fun(sheshi);
	cout << sheshi << " degrees Celsius is " << z << " degrees Fahrenheit." << endl;
	return 0;
}

六、第六题

6.编写一个程序,其main调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输人光年值,并显示结果:
Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.
天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球 4.2光年)。请使用double类型(参见输出流成为 程序清单2.4),转换公式为:
1光年=63240天文单位

#include<iostream>
using namespace std;
double fun(double year)
{
	return year * 63240;
}
int main()
{
	double year = 0;
	cout << "Enter the number of light years:";
	cin >> year;
	double a = fun(year);
	cout << year << " light years = " << a << " astronomical units." << endl;
	return 0;
}

七、第七题

7.编写一个程序,要求用户输人小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours:9
Enter the number of minutes: 28
Time:9:28

#include<iostream>
using namespace std;
void fun(int a, int b)
{
	cout << "Time:" << a << ":" << b << endl;
}
int main()
{
	int hours = 0;
	int minutes = 0;
	cout << "Enter the number of hours:";
	cin >> hours;
	cout << "Enter the number of minutes:";
	cin >> minutes;
	fun(hours, minutes);
	return 0;
}

二、第三章

一、第一题

#include <iostream>
using namespace std;
const double a = 12.0;//(1英尺为12英寸)
int main()
{
    int height = 0;
    cout << "输入你的身高(英寸):______\b\b\b\b\b\b";
    cin >> height;
    cout << "身高为 " << height / a << " 英尺 " << height << " 英寸 " << endl;
    return 0;
}

二、第二题

#include <iostream>
#include <math.h>
using namespace std;
const double inches_per_foot = 12.0;//(一英尺为12英寸)
const double metres_per_inch = 0.0254;//(一英寸=0.0254米)
const double pounds_per_kilogram = 2.2;//(一千克=2.2磅)
int main()
{
    double foot, inch, pound;
    cout << "请输入身高(英寸):" << endl;
    cin >> foot;
    cout << "请输入身高(英尺):" << endl;
    cin >> inch;
    cout << "请输入体重(磅)::" << endl;
    cin >> pound;
    double height = (foot * inches_per_foot + inch) * metres_per_inch;
    double weight = pound / pounds_per_kilogram;
    double BMI = weight / pow(height, 2);//pow函数计算身高的二次方
    cout << "BMI指数是 " << BMI << endl;
    return 0;
}

三、第三题

#include <iostream>
using namespace std;
const double degrees_per_minutes = 60.0;//(一度为60分)
const double minutes_per_seconds = 60.0;//(一分为60秒)
const double degrees_per_seconds = 3600.0;//(一度为3600秒)
int main()
{
	double degrees, minutes, seconds, x;
	cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
	cout << "First, enter the degrees: ";
	cin >> degrees;
	cout << "Next, enter the minutes of arc: " ;
	cin >> minutes;
	cout << "Finally, enter the seconds of arc:" ;
	cin >> seconds;
	x = degrees + minutes / degrees_per_minutes + seconds / degrees_per_seconds;
	cout << degrees << " degrees," << minutes << " minutes," << seconds << " seconds" << " = " << x << " degrees" << endl;
	return 0;
}

四、第四题

#include <iostream>
using namespace std;
const int minutes_per_seconds = 60;//(一分=60秒)
const int hours_per_seconds = 3600;//(一小时=3600秒)
const int days_per_seconds = 86400;//(一天=86400秒)
int main()
{
	long long sec = 0;
	cout << "Enter the number of seconds:";
	cin >> sec;
	int days, hours, minutes, seconds;
	days = sec / days_per_seconds;
	hours = sec % days_per_seconds / hours_per_seconds;
	minutes = sec % days_per_seconds % hours_per_seconds / minutes_per_seconds;
	seconds= sec % days_per_seconds % hours_per_seconds % minutes_per_seconds;
	cout << sec << " seconds" << " = " << days << " days," << hours << " hours," << minutes << " minutes," << seconds << " seconds" << endl;
	return 0;
}

五、第五题

#include <iostream>
using namespace std;
int main()
{
	long long world_population = 0;
	long long population = 0;
	cout << "Enter the world's population:" ;
	cin >> world_population;
	cout << "Enter the population of our country:";
	cin >> population;
	double percentage = (double)population / (double)world_population * 100;
	cout << "The population of our country is " << percentage << "% of the world population" << endl;
	return 0;
}

六、第六题

#include <iostream>
using namespace std;
int main()
{
	double kilometre, litre, FCi;
	cout << "请输入公里数:"  ;
	cin >> kilometre;
	cout << "请输入消耗的汽油:" ;
	cin >> litre;
	FCi = kilometre / litre * 100;
	cout << "每一百公里的耗油量:" << FCi << endl;
	return 0;
}

七、第七题

#include <iostream>
using namespace std;
const double mile = 100;
const double liter = 3.875;
int main()
{
    double gasoline;
    cout << "按欧洲风格输入汽车的耗油:" ;
    cin >> gasoline;
    double gallon = 62.14 / (gasoline / 3.875);
    cout << "美式风格的耗油量:" << gallon << endl;
    return 0;
}

三、第四章

一、第一题

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string name1, name2;
    char grade = 0;
    int age = 0;

    cout << "What is your first name?";
    getline(cin, name1);
    cout << "What is your last name?";
    getline(cin, name2);
    cout << "What letrer grade do you deserve?";
    cin >> grade;
    cout << "What is your age?";
    cin >> age;
    cout << "Name:" << name1 << "," << name2 << endl;
    cout << "Grade:" << grade << endl;
    cout << "Age:" << age << endl;
    return 0;
}

第二题

#include <iostream>
#include <string>
using namespace std;
int main()
{    
	string name, dessert;
	cout << "Enter your name:\n";
	getline(cin, name);
	cout << "Enter your favorite dessert:\n";
	getline(cin, dessert);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";
	return 0;
}

第三题

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstring>
using namespace std;
int main()
{    
	const int size = 10;
	char first[size];
	char last[size];
	char name[size * 2];
	cout << "Enter your first name:";
	cin.getline(first, size);
	cout << "Enter your last name:";
	cin.getline(last, size);
	strcpy(name, last);
	strcat(name, ",");
	strcat(name, first);
	cout << "Here's the information in a single string:" << name << endl;
	return 0;
}

第四题

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string first, last, name;
    cout << "Enter your first name:";
    getline(cin, first);
    cout << "Enter your last name:";
    getline(cin, last);
    name = last + ", " + first;
    cout << "Here's the information in a single string:" << name << endl;
 	return 0;
}  

第五题

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
	string brand;
	double weight;
	int calorie;
};
int main()
{
	CandyBar snack = { "Mocha Munch",2.3,350 };
	cout << "品牌:" << snack.brand << " 重量:" << snack.weight << " 卡路里:" << snack.calorie << endl;
}

第六题

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
	string brand;
	double weight;
	int calorie;
};
int main()
{
	CandyBar snack[3] = {
	{"Mocha Munch",2.3,350},
	{"candy", 5.1, 150},
	{"chips", 3.2, 50}
	};
	for (int i = 0; i < 3; i++)
	{
		cout << "品牌:" << snack[i].brand << " 重量:" << snack[i].weight << " 卡路里:" << snack[i].calorie << endl;
	}
	return 0;
}
	

第七题

#include <iostream>
#include <string>
using namespace std;
struct Pizza
{
	string name;
	double diameter;
	double weight;
};
int main()
{
	Pizza pizza;
	cout << "请输入披萨饼公司的名称:";
	getline(cin, pizza.name);
	cout << "请输入披萨饼的直径:";
	cin >> pizza.diameter;
	cout << "请输入披萨饼的重量:";
	cin >> pizza.weight;
	cout << "披萨饼公司的名称:" << pizza.name << " 披萨饼的直径:" << pizza.diameter << " 披萨饼的重量:" << pizza.weight << endl;
	return 0;
}

第八题

#include <iostream>
#include <string>
using namespace std;
struct Pizza
{
	string name;
	double diameter;
	double weight;
};
int main()
{
	Pizza* p = new Pizza;
	cout << "请输入披萨饼的直径:";
	(cin >> p->diameter).get();
	cout << "请输入披萨饼公司的名称:";
	getline(cin, p->name);
	cout << "请输入披萨饼的重量:";
	cin >> p->weight;
	cout << "披萨饼公司的名称:" << p->name << " 披萨饼的直径:" << p->diameter << " 披萨饼的重量:" << p->weight << endl;
	return 0;
}

第九题

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
	string brand;
	double weight;
	int calorie;
};
int main()
{
	CandyBar* p = new CandyBar[3];
	p[0] = { "Mocha Munch",2.3,350 };
	p[1] = { "candy", 5.1, 150 } ;
	p[2] = { "chips", 3.2, 50 };
	for (int i = 0; i < 3; i++)
	{
		cout << "品牌:" << (p + i)->brand << " 重量:" << (p + i)->weight << " 卡路里:" << (p + i)->calorie << endl;
	}
	return 0;
}

第十题

#include <iostream>
#include <array>
using namespace std;
int main()
{
	const int size = 3;
	double sum = 0;
	array<double, size> arr;
	cout << "输入第一次跑步的成绩:";
	cin >> arr[0];
	cout << "输入第二次跑步的成绩:";
	cin >> arr[1];
	cout << "输入第三次跑步的成绩:";
	cin >> arr[2];
	for (int i = 0; i < size; i++)
	{
		sum = sum + arr[i];
	}
	cout << "次数:" << size << " 平均成绩:" << sum / size << endl;
	return 0;
}

三、第五章

一、第一题

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

二、第二题

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

三、第三题

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

四、第四题

#include<iostream>
using namespace std;
int main()
{
	int year = 0;
	double Daphne = 100;
	double Cleo = 100;
	while (Daphne >= Cleo)
	{
		Daphne += 10;
		Cleo = Cleo + Cleo * 0.05;
		year++;
	}
	cout << year << "年后,Cleo的投资资产超过Daphne" << endl;
	cout << "此时,Daphne的投资资产是:" << Daphne << " Cleo的投资资产是:" << Cleo << endl;
	return 0;
}

五、第五题

#include<iostream>
#include<string>
using namespace std;
const int Arraysize = 12;
int main()
{
	string month[Arraysize] = { "一月", "二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
	int sales[Arraysize];
	int sum = 0;
	for(int i = 0;i < Arraysize;i++)
	{
		cout << "请输入" << month[i] << "的销售情况:" ;
		cin >> sales[i];
		sum +=sales[i];
	}
	cout << "这一年的销售情况是" << sum << endl;
	return 0;
}

六、第六题

#include<iostream>
#include<string>
using namespace std;
const int Arraysize = 12;
int main()
{
	string year[3] = {"第一年","第二年","第三年"};
	string month[Arraysize] = { "一月", "二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
	int sales[3][Arraysize]={0};
	int sums[3]={0};
	int sum = 0;
	for(int i = 0;i < 3;i++)
	{
		
		for(int j = 0;j < Arraysize;j++)
		{
			cout << "请输入" << year[i]<<"第"<<month[j] << "的销售情况:";
			cin >> sales[i][j];
			sums[i] += sales[i][j];
		}
		cout << year[i] << "总的销售情况:" <<sums[i] <<endl;
		sum = sum + sums[i];
	}
	cout << "这三年的销售情况是:" << sum << endl;
	return 0;
}

七、第七题

#include<iostream>
#include<string>
using namespace std;
struct Car
{
	string make;
	int year;
};
int main()
{
	cout << "How many cars do you wish to catalog?";
	int size;
	cin >> size;
	cin.get();
	Car* p = new Car[size];
	for (int i = 0; i < size; i++)
	{
		cout << "Car #" << i + 1 << ":" << endl;
		cout << "Please enter the make:";
		getline(cin, (p + i)->make);
		cout << "Please enter the year made:";
		cin >> (p + i)->year;
		cin.get();
	}
	cout << "Here is your collection:" << endl;
	for (int i = 0; i < size; i++)
	{
		cout << (p + i)->year << " " << (p + i)->make << endl;
	}
	delete[]p;
	return 0;
}

八、第八题

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

int main()
{
	int count = 0;
	char words[100];
	cout << "Enter words (to stop,type the word done):" << endl;
	cin >> words;
	for (; strcmp(words, "done");)
	{
		count++;
		cin >> words;
	}
	cout << "You entered a total of " << count << " words" << endl;
	return 0;
}

九、第九题

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

十、第十题

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

	cout << "Enter number of rows:" ;
	cin >> num;
	for (int i = 1; i <= num; i++)
	{
		for (int j = 1; j <= num - i; j++)
		{
			cout << ".";
		}
		for (int k = 0; k < i; k++)
		{
			cout << "*";
		}
		cout << endl;
	}
	return 0;
}

第六章

一、第一题

#include<iostream>
#include<cctype>
using namespace std;
int main()
{
	char ch;
	cout << "请输入一个字符(输入@字符退出):" << endl;
	cin.get(ch);//可以接受任意一个字符包括换行 空格
	while (ch != '@')
	{
		if (isupper(ch))//如果是大写为真
		{
			ch = tolower(ch);//就转换就小写
		}
		else if (islower(ch))//如果是小写为真
		{
			ch = toupper(ch);//就转换就大写	
		}
		if (!isdigit(ch))//如果不是数字就返回一个真的值
		{
			cout << ch;//打印这个字符
		}
		cin.get(ch);
	}
	return 0;
}

二、第二题

#include<iostream>
using namespace std;
const int Size = 10;
int main()
{
	double donation[Size];
	double count = 0;
	cout << "请输入捐款数:" << endl;
	cout << "你最多可以输入" << Size << "次(若输入不为数字,则退出)" << endl;
	cout << "捐款数 #1: ";
	int i = 0;
	while (i<Size && cin >> donation[i])
	{
		if (++i < Size)
		{
			cout << "捐款数 #" << i + 1 << ": ";
		}
	}
	double total = 0;
	for (int j = 0; j < i; j++)
	{
		total += donation[j];

	}
	double avg = total / i;

	if (i == 0)
	{
		cout << "没有捐款数" << endl;
	}
	else
	{
		for (int k = 0; k < i; k++)
		{
			if (donation[k] > avg)
			{
				++count;
			}
		}
		cout << "捐款数的平均值是:" << avg << " 有" << count << "次捐款数大于平均值" << endl;
	}
	return 0;
}

三、第三题

#include<iostream>
using namespace std;
const int Size = 10;
void menu()
{
	cout << "Please enter one of the following choices:" << endl;
	cout << "c) carnivore p) pianist" << endl;
	cout << "t) tree g) game" << endl;
}
int main()
{
	menu();
	char ch;
	cin >> ch;
	while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
	{
		cout << "Please enter a c, p, t, or g: ";
		cin >> ch;
	}
	switch (ch)
	{
	case 'c':cout << "A maple is a carnivore." << endl; break;
	case 'p':cout << "A maple is a pianist." << endl; break;
	case 't':cout << "A maple is a tree." << endl; break;
	case 'g':cout << "A maple is a game." << endl; break;
	}
	return 0;
}

四、第四题

#include<iostream>
using namespace std;
const int strsize = 100;
struct bop {
	char fullname[strsize]; // real name
	char title[strsize]; // job title 
	char bopname[strsize]; // secret Bop name 
	int preference; //0=fullname,1=title,2=bopname 
};
void menu()
{
	cout << "a.display by name  b.display by title" << endl;
	cout << "c.display by bopname d.display by preference " << endl;
	cout << "q.quit" << endl;

}
bop bops[5] = { {"Wimp Macho", "Junior Programmer", "WM", 0},
			   {"Raki Rhodes", "Junior Programmer", "RR", 1},
			   {"Celia Laiter", "Junior Programmer", "CL", 2},
			   {"Hoppy Hipman", "Analyst Trainee", "HH", 1},
			   {"Pat Hand", "Trainer", "PH", 2} };
void showname()
{
	for (int i = 0; i < 5; i++)
	{
		cout << bops[i].fullname << endl;
	}
}
void showtitle()
{
	for (int i = 0; i < 5; i++)
	{
		cout << bops[i].title << endl;
	}
}
void showbopname()
{
	for (int i = 0; i < 5; i++)
	{
		cout << bops[i].bopname << endl;
	}
}
void showpreference()
{
	for (int i = 0; i < 5; i++)
	{
		if (bops[i].preference == 0)
		{
			cout << bops[i].fullname << endl;
		}
		else if (bops[i].preference == 1)
		{
			cout << bops[i].title << endl;
		}
		else
		{
			cout << bops[i].bopname << endl;
		}
	}
}
int main()
{
	menu();
	
	char ch;
	cout << "Enter your choice:";
	cin >> ch;
	while (ch != 'q')//等于q就退出循环 是其他字符就走default
	{
		
		switch (ch)
		{
		case 'a':showname(); break;
		case 'b':showtitle(); break;
		case 'c':showbopname(); break;
		case 'd':showpreference(); break;
		default:cout << "输入错误,重新输入" << endl;
		}

		cout << "Next choice:";
		cin >> ch;
	}
	cout << "Bye!" << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值