(一)《C++ Primer Plus》 | 第 2 章:开始学习 C++

(1)编写 C++ 程序显示姓名和地址。

#include <iostream>

using namespace std;

int main()
{
	cout << "My name is zhangts20." << endl;
	cout << "I live in Sichuan." << endl;
	return 0;
}

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

#include <iostream>

using namespace std;

int main()
{
    cout << "Please enter the distance in long: ";
    int dist;
    cin >> dist;
    cout << dist << " longs are " << dist * 220 << " yards." << endl;
    return 0;
}

(3)编写 C++ 程序,它使用 3 个用户定义的函数(包括 main 函数)产生输出。

#include <iostream>

using namespace std;

void func1();

void func2();

int main()
{
    func1();
    func1();
    func2();
    func2();
    return 0;
}

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

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

(4)编写 C++ 程序,让用户输入年龄,然后显示该年龄包含多少个月。

#include <iostream>

using namespace std;

int main()
{
    cout << "Enter your age: ";
    int age;
    cin >> age;
    cout << age << " years contain " << age * 12 << " months." << endl;
    return 0;
}

(5)编写 C++ 程序,其中 main 函数中调用自定义函数,该函数以摄氏度值为参数并返回相应的华氏摄氏度。

#include <iostream>

using namespace std;

double convert(double);

int main()
{
    cout << "Please enter a Celsius value: ";
    double c;
    cin >> c;
    cout << c << " degrees Celsius is " << convert(c) << " degrees Fahrenheit." << endl;
    return 0;
}

double convert(double v)
{
    return 1.8 * v + 32.0;
}

(6)编写 C++ 程序,其中 main 函数中调用自定义函数,该函数以光年值为参数,并返回相应的天文单位值。

#include <iostream>

using namespace std;

double convert(double);

int main()
{
    cout << "Please enter the number of light years: ";
    double c;
    cin >> c;
    cout << c << " light years = " << convert(c) << " astronomical units." << endl;
    return 0;
}

double convert(double v)
{
    return 63240 * v;
}

(7)编写 C++ 程序,要求用户输入小时数和分钟数,在 main 函数中,将这个两个值传递给一个 void 函数并完成打印。

#include <iostream>

using namespace std;

void show(int, int);

int main()
{
    cout << "Enter the number of hours: ";
    int hours;
    cin >> hours;
    cout << "Enter the number of minutes: ";
    int minutes;
    cin >> minutes;
    show(hours, minutes);
    return 0;
}

void show(int hs, int ms)
{
    cout << "Time: " << hs << ":" << ms << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值