C++第二章习题

在这里插入图片描述
1.

#include <iostream>

int main()
{
    using namespace std;
    char first[20], last[20];
    cout << "Enter your first name:\n";
    cin >> first;
    cout << "Enter your last name:" << endl;
    cin >> last;
    cout << "Hi, " << first << ' ' << last << endl;
    return 0;
}
Enter your first name:
Man
Enter your last name:
Wu
Hi, Man Wu
#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter a distance in long.\n";
    float distance;
    cin >> distance;
    cout << distance << " long equals to " << distance * 220 << " yard.\n";
    return 0;
}

可以看到float只有6位有效位(和小数点没关系!)

Enter a distance in long.
2.111144141
2.11114 long equals to 464.452 yard.
#include <iostream>
void mice();
void run();

int main()
{
    mice();
    mice();
    run();
    return 0;
}

void mice()
{
    std::cout << "Three blind mice\n";
}

void run()
{
    std::cout << "See how they run\nSee how they run\n";
}

记住函数原型,然后这次main不需要using指令,子函数里我觉得只用cout不需要名称空间

Three blind mice
Three blind mice
See how they run
See how they run
#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter your age:\n";
    int age;
    cin >> age;
    cout << "Your age contains " << age * 12 << " months.\n";
}
Enter your age:
23
Your age contains 276 months.

在这里插入图片描述

#include <iostream>
float tempTransform(float celsius);
int main()
{
    std::cout << "Enter a temperature in Celsius:\n";
    float celsius, fahrenheit;
    std::cin >> celsius;
    fahrenheit = tempTransform(celsius);
    std::cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit.\n";
    return 0;
}

float tempTransform(float celsius)
{
    float f;
    f = 9 * celsius / 5 + 32;
    return f;
}

即使是float,也没有默认加小数点哦

Enter a temperature in Celsius:
20
20 degrees Celsius is 68 degrees Fahrenheit.

在这里插入图片描述

#include <iostream>
double LightToAstro(double light);
int main()
{
    using namespace std;
    cout << "Enter the number of light years:\n";
    double light, astro;
    cin >> light;
    astro = LightToAstro(light);
    cout << light << " light years = " << astro << " astronomical units.\n";

    return 0;
}

double LightToAstro(double light)
{
    return light * 63240;
}
Enter the number of light years:
4.2
4.2 light years = 265608 astronomical units.

在这里插入图片描述

#include <iostream>
void showTime(int hours, int minutes);
int main()
{
    std::cout << "Enter the number of hours:\n";
    int hours, minutes;
    std::cin >> hours;
    std::cout << "Enter the number of minutes:\n";
    std::cin >> minutes;
    showTime(hours, minutes);
    return 0;
}

void showTime(int hours, int minutes)
{
    std::cout << "Time: " << hours << ":" << minutes << std::endl;//注意endl也要有前缀啊
}
Enter the number of hours:
9
Enter the number of minutes:
28
Time: 9:28
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值