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

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

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

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

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

#include "pch.h"
#include <iostream>

using namespace std;

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

 

练习二:

#include "pch.h"
#include <iostream>

//将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;
}

练习三:

#include "pch.h"
#include <iostream>

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

练习四:

#include "pch.h"
#include <iostream>
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;
}

练习五:

#include "pch.h"
#include <iostream>
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;
}

练习六:

#include "pch.h"
#include <iostream>
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;
}

练习七:

#include "pch.h"
#include <iostream>

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

 

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值