MENU

C++丨函数练习题

本来想继续学下一章, 但是这周忘记带书了, 所以复习一下之前学的一点基础

① 输出名称与地址.

#include <iostream>
using namespace std;
int main()
{
    cout << "姓名:keith" << endl
         << "地址:温州" << endl;
    return 0;
}

② 转换long(单位)成ma, 1long = 220ma.

#include <iostream>
using namespace std;
int change(int);
int main()
{
    int longs;
    cout << "Enrter the number of long: ";
    cin >> longs;
    int ma = change(longs);
    cout << longs << " long = " << ma << " ma" << endl;
}

int change(int x)
{
    return x * 220;
}

③ 用函数输出"Three blind mice"与"See how they run"两次.

#include <iostream>
using namespace std;
char say1(void);
char say2(void);
int main()
{
    char t1 = say1();
    char t2 = say2();
    cout << t1
         << t2;
    return 0;
}

char say1(void)
{
    cout << "Three blind mice" << endl
         << "Three blind mice" << endl;
    return 1;
}

char say2(void)
{
    cout << "See how they run" << endl
         << "See how they run" << endl;
    return 2;
}

④ 转换年龄成月龄.

#include <iostream>
using namespace std;
int change(int);

int main()
{
    int age;
    cout << "Enter your age: "; 
    cin >> age;

    int mouth = change(age);
    cout << "Your living month: "
         << mouth;
}

int change(int month)
{
    return month * 12;
}

⑤ 转换摄氏度成华氏度.

#include <iostream>
using namespace std;
double change(double);

int main()
{
    double Fahrenheit, Celsius;
    cout << "Please enter a Celsius value: "; 
    cin >> Celsius;

    Fahrenheit = change(Celsius);
    cout << Celsius << " degrees Celsius is " << Fahrenheit << " degrees Fahrenheit." << endl;
}

double change(double n)
{
    return n * 1.8 + 32;
}

⑥ 转换光年为天文单位(1天文单位 = 63240).

#include <iostream>
using namespace std; \
double change(double);
int main()
{
    double light_year;
    cout << "Enter the number of light years: "; 
    cin >> light_year;

    double au = change(light_year);
    cout << light_year << " light years = " << au << " astronmical units.";
}

double change(double n)
{
    return n * 63240;
}

⑦ 用户输入小时与分钟, 转换为小时:分钟形式.

#include <iostream>
using namespace std;
int time(int, int);
int main()
{
    int h, min;
    char Time;
    cout << "Enrter the number of hours: ";
    cin >> h;
    cout << "Enrter the number of minutes: ";
    cin >> min;
    Time = time(h, min);
    cout << Time;
}

int time(int x, int y)
{
    cout << "time: " << x << ":" << y << endl;
    return 0;
}

之前一直都是在看书学习的, 也很少开电脑写练习题, 初次上手真的感受到麻烦, VS2019就不说了, 安装比以前算是简单多了了, 但还是麻烦(我懒), 而且有时一个符号打错, 花了好久才发现, 浪费巨多时间, 总之... 实践出真理吧!

Last Modified: April 27, 2019
Archives QR Code Tip
QR Code for this page
Tipping QR Code
Leave a Comment