IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c++ primer plus 第二章课后习题 -> 正文阅读

[C++知识库]c++ primer plus 第二章课后习题

1.编写一个程序,它显示您的姓名和年龄
自己写的

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

int main() {
	
	char str[]="";
	cout << "请输入您的姓名:" << endl;
	cin >> str;
	cout << "您的姓名是 " << str << endl;
	

	int age;
	cout << "请输入您的年龄" << endl;
	cin >> age;
	cout << "您的年龄是 " << age << endl;
	system("pause");

}

网上的参考

#include <iostream>
using namespace std;

int main()
{
    cout << "My name is C++ Primer Plus." << endl;
    cout << "My address is in library." << endl;

    return 0;
}

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

#include <iostream>
using namespace std;

int main()
{
	double Distance;
	cout << "请输入距离:(单位long)" << endl;
	cin >> Distance;
	cout << "等于:" << Distance * 220 << "码" << endl;
	system("pause");
}

网上的参考1

#include <iostream>

int main(void)
{
	using namespace std;
	double distance;
	cout << "Enter the distance (in long):";
	cin >> distance;
	cout << "The distance" << distance << "long" << "equals"
		<< 220 * distance << "yard." << endl;
	return 0;
}

网上的参考2

#include <iostream>
using namespace std;

int main()
{
	double long_distance;

	cout << "Enter a distance for long unit: ";
	cin >> long_distance;

	cout << long_distance << " long distance ";
	cout << "is equal to ";
	long_distance *= 220.0;
	cout << long_distance << " yard distance." << endl;
system("pause");
	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;
int main() {
	for (int i = 0; i < 2; i++) {
		cout << "Three blind mice" << endl;
	}
	for (int j = 0; j < 2; j++) {
		cout << "See how they run" << endl;
	}
	system("pause");
	return 0;
}

网上的参考1

#include <iostream>
void print_mice(void);
void print_run(void);
using namespace std;

int main(void)
{
	print_mice();
	print_mice();
	print_run();
	print_run();

	return 0;
}

void print_mice(void)
{
	cout << "Three blind mice." << endl;
}

void print_run(void)
{
	cout << "See how they run." << endl;
}


网上的参考2

#include<iostream>
void A(int);
void B(int);
using namespace std;
	void A(int n)
	{
		int i;
		for (i = 0; i < n; i++)
			cout << "Three blind mice" << endl;

	}

	void B(int n)
	{
		int i;
		for (i = 0; i < n; i++)
			cout << "See how they run" << endl;

	}
	int main()
	{

		A(2);
		B(2);
		system("pause");
	}

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

自己写的

#include <iostream>
using namespace std;

int main()
{
	int Age;
	cout << "请输入年龄:(岁)" << endl;
	cin >> Age;
	cout << "该年龄包含:" << Age * 12 << "个月" << endl;
	system("pause");
}

网上的参考1

#include <iostream>
using namespace std;

int main(void)
{
	int years;
	cout << "Enter your age:";
	cin >> years;
	cout << "You are " << years << " years old,equals = "
		<< years * 12 << " months old." << endl;

}

网上的参考2

#include <iostream>
using namespace std;

int main()
{
	int age_total_months;

	cout << "Enter your age: ";
	cin >> age_total_months;
	cout << "Your age includes " << age_total_months * 12 << " months." << endl;
	system("pause");
	return 0;
}

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

#include<iostream>
using namespace std;

int main() {

	double Celsius;
	cout << "Please enter a Celsius value:";
	cin >> Celsius;
	cout << Celsius << " degree Celsius is " << Celsius * 1.8 + 32.0
		<< " degree Fahrenheit" << endl;

	system("pause");
	return 0;
}

网上的参考1

#include <iostream>
using namespace std;
double convert(double c);
double convert(double c)
{
	return 1.8 * c + 32.0;
}

int main(void)
{
	double c_degree, f_degree;
	cout << "Please enter a Celsius value:";
	cin >> c_degree;
	f_degree = convert(c_degree);
	cout << c_degree << " degrees Celsius is " << f_degree
		<< "degrees Fahrenheit" << endl;
	return 0;

}

网上的参考2

#include <iostream>
using namespace std;

double temperature(double temp);

int main()
{
	double celsius;

	cout << "Please enter a Celsius value: ";
	cin >> celsius;
	cout << celsius << " degrees Celsius is ";
	cout << temperature(celsius);
	cout << " degrees Fahrenheit." << endl;

	return 0;
}

double temperature(double temp)
{
	return 1.8 * temp + 32.0;
}

网上的参考3

#include <iostream>
using namespace std;
double celsiustofahrenheit(double);
int main()
{
	double  Celsius;
	cout << "please enter a Celsius value:";
	cin >> Celsius;
	double Fahrenheit = celsiustofahrenheit(Celsius);
	cout << Celsius << " degrees Celsius is "<<Fahrenheit<<" degrees Fahrenheit" << endl;
}


double celsiustofahrenheit(double Celsius)
{
	return 1.8 * Celsius + 32.0;
}

6.编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值。)该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years:4.2
4.2 light years = 265608 astronomical inits.
使用double类型,转换公式为: 1光年 = 63240天文单位

自己写的

#include<iostream>
using namespace std;

int main() {

	double LightYears;
	cout << "Enter the number of light years:";
	cin >> LightYears;
	cout << LightYears << " light years = " << LightYears * 63240 
		<< " astronomical units" << endl;

	system("pause");
	return 0;
}

网上的参考1

#include <iostream>
using namespace std;
//函数声明
double converet(double light);

//函数定义
double converet(double light)
{
	return 63240 * light;
}

//函数调用
int main(void)
{
	double light_years, astro_years;
	cout << "Enter the number of light years:";
	cin >> light_years;
	astro_years = converet(light_years);

	cout << light_years << " light years = "
		<< astro_years << " astronomical units." << endl;
	return 0;
}

网上的参考2

#include <iostream>
using namespace std;

double transform(double temp);//函数声明

int main()//函数调用
{
	double light_years;

	cout << "Enter the number of light years: ";
	cin >> light_years;
	cout << light_years << " light years = ";
	cout << transform(light_years);
	cout << " astronomical units." << endl;

	return 0;
}

double transform(double temp)//函数定义
{
	return temp * 63240.0;
}

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

自己写的

#include<iostream>
using namespace std;

int main() {

	int Hours;
	cout << "Enter the number of light hours:";
	cin >> Hours;
	int Minutes;
	cout << "Enter the number of light minutes:";
	cin >> Minutes;
	cout << "Time:" << Hours <<  ":"<<Minutes << endl;

	system("pause");
	return 0;
}

网上的参考1

#include <iostream>
using namespace std;
//函数声明
void display_time(int h, int m);

//函数定义
void display_time(int h, int m) 
{
	cout << "Time: " << h << ":" << m << endl;
}

//函数调用
int main(void)
{
	int hours, minutes;
	cout << "Enter the number of hour:";
	cin >> hours;
	cout << "Enter the number of minutes:";
	cin >> minutes;
	
	display_time(hours, minutes);

	return 0;
}

网上的参考2

#include <iostream>
using namespace std;

void show_time(int hour, int minute);

int main()
{
	int hour, minute;

	cout << "Enter the number of hours: ";
	cin >> hour;
	cout << "Enter the number of minutes: ";
	cin >> minute;
	show_time(hour, minute);

	return 0;
}

void show_time(int hour, int minute)
{
	cout << "Time: " << hour << ":" << minute << endl;
	return;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-14 13:50:50  更:2021-08-14 13:52:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 9:09:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码