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

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

练习一

#include <iostream>

using namespace std;

int main()
{
    cout << "ZhangChen" << endl;
    cout << "Beijing" << endl;
}
g++ -o 1 1.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;
}

练习三

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

结语

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

发布了57 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Zhang_Chen_/article/details/99943034