復(fù)習(xí)題
- C++程序的模塊叫什么?
庫(kù)
- 下面的預(yù)處理編譯指令是做什么用的?
#include<iostream>
包含頭文件,使外部程序中可以合法使用頭文件里面的標(biāo)準(zhǔn)函數(shù),例如輸入輸出函數(shù)。
- 下面的語句是做什么用的?
using namespace std;
使用命名空間:頭文件<iostream>定義的所有的函數(shù)、變量和對(duì)象都定義在std命名空間里,要使用頭文件里面的對(duì)象,要先聲明使用std空間。
- 什么語句可以用來打印"Hello, World",然后開始新的一行?
std::cout<<"Hello, World"<<endl;
- 什么語句可以用來創(chuàng)建名為“cheeses”的整數(shù)變量?
int cheeses;
- 什么語句可以將32賦給變量cheeses?
cheeses = 32;
- 什么語句可以用來將鍵盤輸入的值讀入變量cheeses中?
std::cin>>cheeses;
- 什么語句可以用來打印"We have X varieties of cheese",用cheese變量的當(dāng)前值來代替X。
std::cout<<"We have "<<cheese<<" varieties of cheese"
- 下面的函數(shù)原型指出了關(guān)于函數(shù)的哪些信息?
void rattle(int n);
int prune(void);
int froop(double t);函數(shù)froop的參數(shù)是一個(gè)double類型的值,返回值是一個(gè)int類型的值。
void rattle(int n); 函數(shù)rattle的參數(shù)是int類型的值,無返回值。
int prune(void);函數(shù)prune無參數(shù),返回值為int類型。
- 定義函數(shù)時(shí),在什么情況下不必使用關(guān)鍵字return?
返回為空的時(shí)候,也就是說函數(shù)前面有"void"的時(shí)候。
編程練習(xí)
- 編寫一個(gè)C++程序,它顯示您的姓名和住址。
#include<iostream>
using namespace std;
int main(void) {
cout<<"Name: Sommer"<<endl;
cout<<"Adresse: Koethernerstrasse 30 027 10963 Berlin"<<endl;
return 0;
}
- 編寫一個(gè)程序,它要求用戶輸入一個(gè)以浪為單位的距離,然后將它轉(zhuǎn)換為碼。(1浪= 220碼)
#include<iostream>
using namespace std;
int main(){
int a;
cout<<"Enter distance: " << endl;
cin>>a;
cout<<a<<" 浪"<<"="<<220*a<<" 碼"<<endl;
return 0;
}
3.編寫一個(gè)程序,它使用三個(gè)用戶定義的函數(shù)(包括main()),并生成下面的輸出:
Three blind mice
Three blind mice
See how they run
See how they run
其中兩個(gè)函數(shù)分別被調(diào)用兩次。
#include<iostream>
using namespace std;
void func1();
void func2();
int main(){
func1();
func1();
func2();
func2();
return 0;
}
void func1(){
cout<<"Three blind mice"<<endl;
}
void func2(){
cout<<"See how they run"<<endl;
}
- 編寫一個(gè)程序,其中的main()調(diào)用一個(gè)用戶定義的函數(shù)(以攝氏溫度值為參數(shù),并返回相應(yīng)的華氏溫度值)。該程序按下面的格式要求用戶輸入攝氏溫度值,并顯示結(jié)果:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
下面是轉(zhuǎn)換公式:
華氏溫度= 1.8x攝氏溫度+32.0
#include<iostream>
using namespace std;
void func(){
double a;
cout<< "Please enter a Celsius value:";
cin>> a;
cout << a <<" degrees Celsius is "<< a*1.8+32.0<<" degrees Fahrenheit."<<endl;
}
int main(){
func();
return 0;
}
- 編寫一個(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.
天文單位是從地球到太陽的平均距離(約150000000千米或93000000英里),光年是光走一年的距離,約10萬億千米。請(qǐng)使用double類型,轉(zhuǎn)換公式為:1光年=63240天文單位
- 編寫一個(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 func(){
int a,b;
cout<< "Enter the number of hours: ";
cin>> a;
cout<< "Enter the number of minutes: ";
cin>> b;
cout << "Time: " << a <<":"<<b<<endl;
}
int main(){
func();
return 0;
}