C++ Primer Plus 第六版_編程練習(1)(Chapter_two 1-7)

編程工具用得好,搞起研究事半功倍。導師的意見是,學好C++,其他編程都不怕。暫時沒有迫切的實戰需要,於是決定從最基礎的學起,挑了《C++ Primer Plus (第六版)》這本書,開始啃吧。編程練習還是一定要做的,每天一點,記在這裏。

2_1:

#include <iostream>
using namespace std;

int main()
{
    char name = 'k';
    char add_ = 'C';
    cout << name << endl;
    cout << add_ << endl;
    return 0;
}

2_2:

#include <iostream>
using namespace std;
int main()
{
    int long_;
    int ma;
    cout << "please input long_:";
    cin >> long_;
    ma = long_ * 220;
    cout << "the long_ is " << ma << " ma" << endl;
    return 0;
}

2_3:

#include <iostream>
using namespace std;

void function_1();
void function_2();
int main()
{
    int i;
    for (i = 0; i < 2; i++)
    {
        function_1();
        function_2();
    }
    return 0;
}

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

void function_2()
{
    cout << "See how they run" << endl;
}

2_4:

#include<iostream>
using namespace std;
int main()
{
    int year;
    int month;
    cout << "Enter your age:";
    cin >> year;
    month = year * 12;
    cout << "Your age include " << month << " mouth" << endl;
    return 0;
}
#include "iostream"
using namespace std;

float change(float a)
{
    float b;
    b = 1.8*a + 32.0;
    return b;
}

int main()
{
    float sh;
    cout << "Please enter a Celsius value:";
    cin >> sh;
    float h = change(sh);
    cout << sh << " degrees Celsius is " << h << " degrees Fahrenheit." << endl;
    return 0;
}

2_6

#include "iostream"
using namespace std;

double chenge(float a);

int main()
{
    float light_year;
    double astronomical_unite;
    cout << "Enter the number of light years: ";
    cin >> light_year;
    astronomical_unite = chenge(light_year);
    cout << light_year << " light years = " << astronomical_unite << " astronomical unites." << endl;
    return 0;
}

double chenge(float a)
{
    return a * 63240;
}

2_7

#include "iostream"
using namespace std;

void show_time(int a, int b);

int main()
{
    int hours_;
    int minutes_;

    cout << "Enter the number of hours:";
    cin >> hours_;
    cout << "Enter the number of minutes:";
    cin >> minutes_;
    show_time(hours_, minutes_);
    return 0;
}

void show_time(int a, int b)
{
    cout << "Time: " << a << ":" << b << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章