【C++】 C++Primer Plus 第六版 编程练习(第二章)

本文提供了一系列基于C++ Primer Plus第六版的编程练习示例,涵盖了基本输入输出、变量操作、函数定义及调用等核心概念。通过实际代码演示了字符串输出、浮点数运算、温度单位转换、距离单位换算等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【C++】 C++Primer Plus 第六版 编程练习(第二章)

练习一

#include <iostream>

using namespace std;

int main()
{
    cout << "ZhangChen" << endl;
    cout << "Beijing" << endl;
    return 0;
}
g++ -o test test.cpp
# 【-o 文件名】 指定编译生成可执行文件的名称

练习二

#include <iostream>

int main()
{
    using namespace std;
    
    float my_long;
    float yard;

    cin >> my_long;
    yard = 220 * my_long;
    cout << my_long << " long = " << yard << " yard." << endl;
    return 0;
}

练习三

#include <iostream>

void Cout1(void);
void Cout2(void);

int main()
{
    Cout1();
    Cout1();
    Cout2();
    Cout2();
}

void Cout1()
{
    std::cout << "Three blind mice" << std::endl;
}

void Cout2()
{
    std::cout << "See how they run" << std::endl;
}

练习四

#include <iostream>

using namespace std;

int main()
{
    int your_age;
    int month;

    cout << "Enter your age:";
    cin >> your_age;

    month = 12 * your_age;
    cout << month << endl;
}

练习五

#include <iostream>

float change(float a);

int main()
{
    using namespace std;

    float a;
    float b;
    cout << "Please enter a Celsius value: ";
    cin >> a;
    b = change(a);
    cout << a << " degrees Celsius is " << b << " degrees Fahrenheit." << endl;
}

float change(float a)
{
    float b;
    b = 1.8 * a + 32.0;
    return b;
}

练习六

#include <iostream>

double change(float a);

int main()
{
    using namespace std;
    
    float a;
    double b;

    cout << "Enter the number of light years: ";
    cin >> a;

    b = change(a);
    cout << a << " light years = " << b << " astronomical units." << endl;
}

double change(float a)
{
    double b;

    b = 63240 * a;
    return b;
}

练习七

#include <iostream>

using namespace std;

void display(int h, int m);

int main()
{
    int h;
    int m;

    cin >> h;
    cin >> m;

    display(h, m);
}

void display(int h, int m)
{
    cout << "Enter the number of hours: " << h << endl;
    cout << "Enter the number of minutes: " << m << endl;
    cout << "Time " << h << ":" << m << endl;
}

结语

如果您有修改意见或问题,欢迎留言或者通过邮箱和我联系。
手打很辛苦,如果我的文章对您有帮助,转载请注明出处。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值