C++ primer plus 第六版编程练习题参考代码(第二章)

如题,从Python的语言转入C ++语言学习,记录下自己的习题练习!

每个题目后,附上调试结果,大家可以直接复制代码自行编译!

纯参考,希望大家提出批评!

练习一:编写一个C ++程序,显示您的姓名。

#include "pch.h"
#include 

using namespace std;

int main()
{
	cout << "my name is huangchao, and i live in xi'an! " << endl;
	return 0;
}

 C++ primer plus 第六版编程练习题参考代码(第二章)_第1张图片

练习二:

#include "pch.h"
#include 

//将long转换成ma,1long=30ma

using namespace std;
float conv(float);

int main()
{
	float changdu;
	cout << "please enter the changdu:  " << endl;
	cin >> changdu;
	float ma = conv(changdu);
	cout << changdu << " chang is ";
	cout << ma << " ma " << endl;
	return 0;
}

float conv(float unit1)
{
	return 30 * unit1;
}

C++ primer plus 第六版编程练习题参考代码(第二章)_第2张图片

练习三:

#include "pch.h"
#include 

void fuc_1();
void fuc_2();

using namespace std;

int main()
{
	fuc_1();
	fuc_1();
	fuc_2();
	fuc_2();
	return 0;
}

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

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

C++ primer plus 第六版编程练习题参考代码(第二章)_第3张图片

练习四:

#include "pch.h"
#include 
int fuc_1(int);
using namespace std;


int main()
{
	int age;
	cout << "Enter your age: ";
	cin >> age;
	int month = fuc_1(age);
	cout << "Your are " << age << "years; ";
	cout << "and equal to " << month << " month! " << endl;
	return 0;
}

int fuc_1(int n)
{
	int months = n * 12;
	return months;
}

C++ primer plus 第六版编程练习题参考代码(第二章)_第4张图片

练习五:

#include "pch.h"
#include 
float fuc_1(float);
using namespace std;


int main()
{
	int cels;
	cout << "Please enter a Celsius value: ";
	cin >> cels;
	int fahr = fuc_1(cels);
	cout << cels << " degrees celsius is " << fahr << " degrees Fahrenheit" << endl;
	return 0;
}

float fuc_1(float n)
{
	int fahr = n * 1.8+32.0;
	return fahr;
}

C++ primer plus 第六版编程练习题参考代码(第二章)_第5张图片

练习六:

#include "pch.h"
#include 
float fuc_1(float);
using namespace std;


int main()
{
	float guangnian;
	cout << "Enter a number of light years: ";
	cin >> guangnian;
	int tianwen = fuc_1(guangnian);
	cout << guangnian << " light years = " << tianwen << " astronomical units." << endl;
	return 0;
}

float fuc_1(float n)
{
	int unit = n * 63240;
	return unit;
}

C++ primer plus 第六版编程练习题参考代码(第二章)_第6张图片

练习七:

#include "pch.h"
#include 

void fuc_1(int,int);
using namespace std;

int main()
{
	cout << "Enter the number of hours: ";
	int time;
	cin >> time;

	cout << "Enter the number of minutes: ";
	int minutes;
	cin >> minutes;

	fuc_1(time, minutes);
	return 0;
}

void fuc_1(int m, int n)
{
	cout << "Time: " << m <<":"<< n ;
}

C++ primer plus 第六版编程练习题参考代码(第二章)_第7张图片

 

实际上,上述代码还有很多缺点。再学习了第三章之后,发现之前程序里的变量定义都未初始化,这是很不好的习惯。

最后贴上个人的公众号,分享各种好玩的软件!

C++ primer plus 第六版编程练习题参考代码(第二章)_第8张图片

你可能感兴趣的:(C,编程,博客)