C++ Primer Plus(第6版)編程練習(xí)-第二章

內(nèi)容僅供參考,如有錯(cuò)誤,歡迎指正 !

1.編寫一個(gè)c++程序,它顯示您的姓名和地址。
#include <iostream>
using namespace std;
int main()
{
    cout << "My name is kunlongMo,and my address is in the Sichuan provence." << endl;
    return 0;
}
2.編寫一個(gè)c++程序,它要求用戶輸入一個(gè)以long為單位的距離,然后將它轉(zhuǎn)換為碼(一long等于200碼)。
#include <iostream>
using namespace std;
int main()
{
    int longN;
    cout << "Please input a distance(long)" << endl;
    cin >> longN;
    cout << "You input " << longN << " long" << endl;
    cout << "It's " << longN * 220 << " 碼";
    return 0;
}
3.編寫一個(gè)c++程序,他使用3個(gè)用戶定義的函數(shù)(包括main(),并生成下面的輸出:

Three blind mice
Three blind mice
See how they run
See how they run

其中一個(gè)函數(shù)要調(diào)用兩次,該函數(shù)生成前兩行;另外一個(gè)函數(shù)也調(diào)用兩次,并生成其余的輸出。
#include <iostream>
using namespace std;
void A_function()
{
    cout << "Three blind mice" << endl;
}
void B_function()
{
    cout << "See how they run" << endl;
}
int main() {
    A_function;
    A_function;
    B_function;
    B_function;
    return 0;
}
4.編寫一個(gè)程序,讓用戶輸入其年齡,然后顯示該年齡包含多少個(gè)月,如下圖所示:

Enter your age:29

#include<iostream>
using namespace std;
int main()
{
    int age = 0;
    cout << "Enter your age: ";
    cin >> age;
    cout << age * 12;
    return 0;
}
5.編寫一個(gè)程序,其中main()調(diào)用一個(gè)用戶定義的的函數(shù)(以攝氏溫度值為參數(shù),并返回相應(yīng)的華氏溫度值)。該程序按下面的格式要求用戶輸入攝氏溫度值,并顯示結(jié)果:

Please enter a Celsius value:20
20 degrees Celsius is 69 degrees Fahrenheit.

下面是轉(zhuǎn)換公式:
華氏溫度=1.8*攝氏溫度+32.0
#include<iostream>
using namespace std;

double CotF(double C)
{
    double F = 1.8 * C + 32;
    return F;
}
int main()
{
    double she,hua;
    cout << "Please enter a Celsius value: ";
    cin >> she;
    hua = CotF(she);
    cout << she << " degrees Celsius is " << hua<< " degree Fahrenheit"<<endl;
    return 0;


 }
6.編寫一個(gè)程序,其main()調(diào)用一個(gè)用戶的函數(shù)(以光年為參數(shù),并返回對(duì)應(yīng)天文單位的值)。該程序按下面的格式要求用戶輸入光年值,并顯示結(jié)果:

Enter the number of light years: 4.2
4.2 light years =265608 astronomical units.

天文單位是從地球到太陽(yáng)的平均距離(約150000000公里或93000000英里),光年是光一年走的距離(約10萬(wàn)億公里或6萬(wàn)億英里)(除太陽(yáng)外,最近的恒星大約離地球4.2光年)。請(qǐng)使用double類型,轉(zhuǎn)換公式為:1光年=63240天文單位。
#include<iostream>
using namespace std;

double CotG(double T)
{
    double G = 63240 * T;
    return G;
}
int main()
{
    double L, A;
    cout << "Enter the number of lignt years: ";
    cin >> L;
    A = CotG(L);
    cout << L << " light years = " << A << " opastronomical units." << endl;
    return 0;
 }
7.編寫一個(gè)程序,要求用戶輸入小時(shí)數(shù)和分鐘數(shù),在main()函數(shù)中,將這兩個(gè)值傳遞給一個(gè)void函數(shù),后者以下面這樣的格式顯示這兩個(gè)值:

Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28

#include<iostream>
using namespace std;
void Time(int hours, int minutes)
{
    cout << "Time: " << hours << ":" << minutes << endl;
}
int main()
{
    int hours, minutes;
    cout << "Enter the number of hours: ";
    cin >> hours;
    cout << "Enter the number of minutes: ";
    cin >> minutes;
    Time(hours, minutes);
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容