C++ Primer第二章课后编程题

本文通过六个实例演示了C++的基本语法和编程技巧,包括简单的输出个人信息、尺寸转换、函数调用、温度单位转换、光年到天文单位的转换以及显示时间。


1、
代码:
#include<iostream>
int main()
{
    using namespace std;
    cout << "My name : guugle" << endl;
    cout << "My address : China Shenzhen" << endl;
    return 0;
}
运行结果:


2、
代码:
#include<iostream>
int main()
{
    using namespace std;
    cout << "请输入尺寸:";
    int lang;
    cin >> lang;
    lang = 220 * lang;
    cout << "转换成码为:" << lang <<endl;
    return 0;
}
运行结果:

3、
代码:
#include<iostream>
using namespace std;
void functionA();
void functionB();
int main()
{
    functionA();
    functionA();
    functionB();
    functionB();
    return 0;
}
void functionA()
{
   cout << "Three blind misc";
   cout << endl;
}
void functionB()
{
   cout << "See how they Run";
   cout << endl;
}
运行结果:


4、
代码:
#include<iostream>
using namespace std;
void hua(int);
int main()
{
    cout << "Please enter a Celsius value:";
    int wendu;
    cin >> wendu;
    hua(wendu);
    return 0;
}
void hua(int wendu)
{
    double huadu = 1.8 * wendu + 32.0;
    cout << wendu << "degrees Velsius is " << huadu << "degrees Fahrenheit" << endl;
}
运行结果:


5、
代码:
#include<iostream>
using namespace std;
double lightyear(double);
int main()
{
    cout << "Enter the number of light years:";
    double years;
    cin >> years;
    double lyear = lightyear(years);
    cout << years <<"light years = " << lyear << "astronomical units.";
    cout << endl;
    return 0;
}
double lightyear(double years)
{
    return years * 63240;
}
运行结果:


6、
代码:
#include<iostream>
using namespace std;
void showTime(int, int);
int main()
{
    cout << "请输入小时:";
    int hours;
    cin >> hours;
    cout << "请输入分钟数:";
    int minutes;
    cin >> minutes;
    showTime(hours, minutes);
    return 0;
}
void showTime(int hours, int minutes)
{
    cout << "Time:" << hours <<":" << minutes << endl;
}
运行结果:


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值