C++ Primer Plus 第2章 课后编程练习 代码

//第一题
#include <cctype>
#include <iostream>
int main()
{
    using namespace std;
    cout << "Please input the letters(@ to quit):\n";
    char ch;
    cin.get(ch);
    while (ch != '@')
    {
        if (isupper(ch))
        {
            ch = tolower(ch);
        }
        else
        {
            ch = toupper(ch);
        }
        // cout.put(ch);//输出方案1
        cout << ch; // 输出方案2

        cin.get(ch);
    }

    cout << "Bye!" << endl;

    return 0;
}
//第2题
//long to meters
#include <iostream>

int main()
{
    using namespace std;
    cout << "Enter the distance in long:";
    double distance;
    cin >> distance;
    cout << "The distance in meters is " << distance * 220 << endl;

    return 0;
}
//第3题
//two functions in this file
#include <iostream>
using namespace std;
void Myfunc2(void);
void Myfunc1(void);

int main()
{
    Myfunc1();
    Myfunc1();
    Myfunc2();
    Myfunc2();
    return 0;
}

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

void Myfunc2(void)
{
    cout << "See how they run\n";
}
//第4题
//display somebody's months
#include <iostream>

int Agetomonths(int age);
int main()
{
    using namespace std;
    int age;
    cout << "Enter your age please.\n";
    cin >> age;
    cout << "OK,the months of your age is " << Agetomonths(age) << "." << endl;

    return 0;
}

int Agetomonths(int age)
{
    int months = age * 12;
    return months;
}
5//Celsius to Fahrenheit
#include <iostream>
double CelToFar(double calsius);

int main()
{
    using namespace std;
    double celsius;
    cout << "Please enter a Celsius value:";
    cin >> celsius;
    cout << celsius;
    cout << " degrees Celsius is " << CelToFar(celsius) << " degrees Farenheit." << endl;

    return 0;
}

double CelToFar(double celsius)
{
    double fahrenheit = celsius * 1.8 + 32.0;
    return fahrenheit;
}
6//light year to astronomical unit
#include <iostream>
double LigToAst(double lightyear);

int main()
{
    using namespace std;
    double lightyear;
    cout << "Enter the number of light year:";
    cin >> lightyear;
    cout << lightyear;
    cout << " lightyear = " << LigToAst(lightyear) << " astronomical units." << endl;

    return 0;
}

double LigToAst(double lightyear)
{
    double astrunit = lightyear * 63240;
    return astrunit;
}
7//输入一个时间,显示,并且求出与现在的时间差
#include <iostream>
void ShowInputTime(int, int);
void ShowNowTime(void);
int main()
{
    using namespace std;
    ShowNowTime();

    int hours, minutes;
    cout << "Enter the number of hours:";
    cin >> hours;
    cout << "Enter the number of minutes:";
    cin >> minutes;
    ShowInputTime(hours, minutes);

    return 0;
}
void ShowInputTime(int hours, int minutes)
{
    using namespace std;
    cout << "Time: " << hours << ":" << minutes << endl;
}

void ShowNowTime(void)
{
    using namespace std;
    cout << "Now time is " << __TIME__ << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咖啡与乌龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值