c++第二章2.7编程题

1. 编写一个C++程序,它显示您的姓名和地址。

#include<iostream>

using namespace std;

int main(void)
{
        cout << "My name is YoRHa,l live in Shanghai" << endl;
        return 0;
}

输出结果:

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

#include<iostream>

using namespace std;
double long_to_yard(double long1);
int main(void)
{
        double long1;
        cout << "Gvie long: ";
        cin >> long1;
        double yard1;
        yard1 = long_to_yard(long1);
        cout << long1 << " long= " 
             << yard1 << " yard." << endl;
        return 0;
}

double long_to_yard(double long1)
{
        return 200 * long1;
}

输出结果:
 

3.编写一个C++程序,它使用3个用户定义的函数(包括main( )),并生成下面的输出:

Three blind mice
Three blind mice

See how they run

See how they run

include<iostream>

using namespace std;
void print_mice(void);
void print_run(void);
int main(void)
{
        print_mice();
        print_mice();
        print_run();
        print_run();

        return 0;
}

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

void print_run(void)
{
        cout << "See how the run." << endl;
}

输出结果:

4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:Enter your age:29

#include<iostream>

using namespace std;
int age_to_month(int age);

int main(void)
{
        int age;
        cout << "Enter your age: ";
        cin >> age;
        int months;
        months = age_to_month(age);
        cout << "This age include " << months << " month" << endl; 
        return 0;
}

int age_to_month(int age)
{
        return age*12;
}

输出结果:

5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a celsius value: 20
20 degrees celsius is 68 degrees Fahrenheit.

下面是转换公式:华氏温度=1.8×摄氏温度+32.0

#include<iostream>

using namespace std;
double convert(double c);
int main(void)
{
        double c_degree,f_degree;
        cout << "Please enter a Celsius value: ";
        cin >> c_degree;
        f_degree = convert(c_degree);
        cout << c_degree << " degrees Celsius is "
             << f_degree << " degrees Fahrenheit." << endl;
        return 0;
}

double convert(double c)
{
        return 1.8 * c + 32;
}

输出结果:

7.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28

Time:9:28

#include<iostream>

using namespace std;
void show(int h,int m);

int main(void)
{
        int hour,minute;
        cout << "Enter the number of hours: ";
        cin >> hour;
        cout << "Enter the number of minutes: ";
        cin >> minute;
        show(hour,minute);

        return 0;
}

void show(int h,int m)
{
        cout << "Time: " << h <<":" << m << endl;
}

输出结果:


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值