柚子快報(bào)激活碼778899分享:c++學(xué)習(xí)筆記(更新中)
柚子快報(bào)激活碼778899分享:c++學(xué)習(xí)筆記(更新中)
一. 開(kāi)始學(xué)習(xí)C++
1.1 進(jìn)入C++
myfirst.cpp
#include
int main()
{
using namespace std;
cout << "Come up and C++ me some time.";
cout << endl;
cout << "You won't regret it!" << endl;
return 0;
}
1.1.1 main()函數(shù)
1.1.2 C++預(yù)處理器和iostream文件
如果程序要使用C++輸入或輸出工具,需要提供這樣兩行代碼:
#include
using namespace std;
1.1.3 頭文件名
1.1.4 名稱(chēng)空間
使用編譯指令using可以省略名稱(chēng)空間前綴std::,以下述方式進(jìn)行編碼:
std::cout << "Come up and C++ me some time.";
std::cout << std::endl;
1.1.5 使用cout進(jìn)行C++輸出
cout << "Come up and C++ me some time.";
<<該符號(hào)指出了信息流動(dòng)的路徑。
控制符endl
cout << endl;
endl表示重起一行。
換行符
cout << "Pluto is a dwarf planet.\n";
cout << "Pluto is a dwarf planet." << endl;
\n被視為一個(gè)字符,名為換行符。
1.1.6?C++源代碼的格式化
源代碼中的標(biāo)記和空白
C++源代碼風(fēng)格
·?每條語(yǔ)句占一行。
·?每個(gè)函數(shù)都有一個(gè)開(kāi)始花括號(hào)和一個(gè)結(jié)束花括號(hào),這兩個(gè)花括號(hào)各占一行。
· 函數(shù)中的語(yǔ)句都相對(duì)于花括號(hào)進(jìn)行縮進(jìn)。
· 與函數(shù)名稱(chēng)相關(guān)的圓括號(hào)周?chē)鷽](méi)有空白。
1.2 C++語(yǔ)句
carrot.cpp
#include
int main()
{
using namespace std;
int carrots; //聲明一個(gè)整型變量
carrots = 25; //為變量賦值
cout << "I have ";
cout << carrots;
cout << " carrots."; //輸出變量值
cout << endl;
carrots = carrots - 1; //修改變量值
cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
return 0;
}
1.2.1 聲明語(yǔ)句和變量
int carrots; //聲明一個(gè)整型變量
這條語(yǔ)句提供了兩項(xiàng)信息:需要的內(nèi)存以及該內(nèi)存單元的名稱(chēng)。
1.2.2 賦值語(yǔ)句
carrots = 25; //為變量賦值
賦值語(yǔ)句將值賦給存儲(chǔ)單元,=叫做賦值運(yùn)算符。
1.3 其他C++語(yǔ)句
getinfo.cpp
#include
int main()
{
using namespace std;
int carrots; //聲明一個(gè)整型變量
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "Here are two more. ";
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots." << endl;
return 0;
}
該程序包含兩項(xiàng)新特性:用cin來(lái)讀取鍵盤(pán)輸入以及將四條輸出語(yǔ)句組合成一條。
類(lèi)簡(jiǎn)介
類(lèi)描述了一種數(shù)據(jù)類(lèi)型的全部屬性(包括可使用它執(zhí)行的操作),對(duì)象是根據(jù)這些描述創(chuàng)建的實(shí)體。
1.4? 函數(shù)
1.4.1 使用有返回值的函數(shù)
sqrt.cpp
#include
#include
int main()
{
using namespace std;
double area;
cout << "Enter the floor area, in square feet, of your home: ";
cin >> area;
double side;
side = sqrt(area);
cout << "That's the eqivalent of a square " << side
<< " feet to the side." << endl;
cout << "How fascinating!" << endl;
return 0;
}
1.4.2 函數(shù)變體
有些函數(shù)需要多項(xiàng)信息。
double pow(double, double);
answer = pow(5.0, 8.0);
另外一些函數(shù)不接受任何參數(shù)。
int rand(void);
myGuess = rand();
還有一些函數(shù)沒(méi)有返回值。
void bucks(double);
bucks(1234.56);
1.4.3 用戶(hù)定義的函數(shù)
ourfnuc.cpp
#include
using namespace std;
void simon(int);
int main()
{
simon(3); //調(diào)用simon
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count);
cout << "Done!" << endl;
return 0;
}
void simon(int n)
{
cout << "Simon says touch your toes " << n << " times." << endl;
}
函數(shù)格式
type functionname(argumentlist)
{
statements
}
C++不允許將函數(shù)定義嵌套在另一個(gè)函數(shù)定義中,每個(gè)函數(shù)定義都是獨(dú)立的,所有函數(shù)的創(chuàng)建都是平等的。
函數(shù)頭
C++程序應(yīng)當(dāng)為程序中使用的每個(gè)函數(shù)提供原型。
1.4.4 用戶(hù)定義的有返回值的函數(shù)
#include
using namespace std;
int stonetolb(int);
int main()
{
int stone;
cout << "Enter the weight in stone: ";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << " stone = ";
cout << pounds << " pounds." << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
全部的函數(shù)特性:
? ? ? ? ·有函數(shù)頭和函數(shù)體;
? ? ? ? ·接受一個(gè)參數(shù);
? ? ? ? ·返回一個(gè)值;
? ? ? ? ·需要一個(gè)原型。
1.4.5 在多函數(shù)程序中使用using編譯指令
只讓需要訪問(wèn)名稱(chēng)空間std的函數(shù)訪問(wèn)它是更好的選擇。訪問(wèn)名稱(chēng)空間的方法有多種,下面是其中的四種。
?????????·將using namespace std;放在函數(shù)定義之前,讓文件中所有的函數(shù)都能夠使用名稱(chēng)空間std中所有的元素;
? ? ? ? ·將using namespace std;放在特定的函數(shù)定義中,讓該函數(shù)能夠使用名稱(chēng)空間std中的所有元素;
? ? ? ? ·在特定的函數(shù)中使用類(lèi)似using std::cout;這樣的編譯指令,而不是using namespace std;讓該函數(shù)能夠使用指定的元素,如cout。
? ? ? ? ·完全不使用編譯指令using,而在需要使用名稱(chēng)空間std中的元素時(shí),使用前綴std::,即std:: cout << "I'm using cout and endl from the std namespace" << std::endl;
1.5 編程練習(xí)
1. 顯示姓名地址。
#include
int main()
{
using namespace std;
cout << "My name is Li Hua. " << "And I lvie in Beijing." << endl;
return 0;
}
2. 輸入以long為單位的距離,將它轉(zhuǎn)換為碼(1long等于220碼)。、
#include
int lengthconvert(int);
int main()
{
using namespace std;
int length;
cout << "請(qǐng)輸入距離(long):" ;
cin >> length;
int ma = lengthconvert(length);
cout << length << " long = " << ma << " 碼" << endl;
return 0;
}
int lengthconvert(int len)
{
return 200 * len;
}
3. 使用3個(gè)用戶(hù)定義的函數(shù)(包括main()),并生成下面的輸出:
Three blind mice
Three blind mice
See how they run
See how they run
#include
using namespace std;
void MiceNum();
void MiceAct();
int main()
{
MiceNum();
MiceNum();
MiceAct();
MiceAct();
return 0;
}
void MiceNum()
{
cout << "Three blind mice" << endl;
}
void MiceAct()
{
cout << "See how they run" << endl;
}
4. 輸入年齡,并顯示該年齡包含多少個(gè)月,示例:
Enter your age: 29
29 years = 348 months
#include
int AgetoMonths(int);
int main()
{
using namespace std;
int yourage;
cout << "Enter your age:";
cin >> yourage;
int months = AgetoMonths(yourage);
cout << yourage << " years = " << months << " months" << endl;
return 0;
}
int AgetoMonths(int age)
{
return 12 * age;
}
5. 輸入攝氏溫度,顯示華氏溫度,示例:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
#include
double CeltoFah(double);
int main()
{
using namespace std;
double celdeg;
cout << "Please enter a Celsius value:";
cin >> celdeg;
double fahdeg = CeltoFah(celdeg);
cout << celdeg << " degrees Celsius is ";
cout << fahdeg << " degrees Fahrenheit." << endl;
return 0;
}
double CeltoFah(double Cel)
{
return 32 + Cel * 1.8;
}
6. 編寫(xiě)函數(shù),以光年值為參數(shù),并返回對(duì)應(yīng)天文單位的值,示例:
Enter the number of light years: 4.2
4.2 linght?years = 265608 astronomical units.
#include
double astronom(double);
int main()
{
using namespace std;
double lightyears;
cout << "Enter the number of light years:";
cin >> lightyears;
double astrounits = astronom(lightyears);
cout << lightyears << " light years = ";
cout << astrounits << " astronomical units." << endl;
return 0;
}
double astronom(double ligyear)
{
return 63240 * ligyear;
}
7. 輸入分鐘數(shù)和小時(shí)數(shù),顯示時(shí)間,示例:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include
void Timer(int min, int hour);
int main()
{
using namespace std;
int minutes,hours;
cout << "Enter the number of hours: ";
cin >> hours;
cout << "Enter the number of minutes: ";
cin >> minutes;
Timer(minutes, hours);
return 0;
}
void Timer(int min, int hour)
{
using std::cout;
cout << "Time: " << hour << " : " << min ;
}
柚子快報(bào)激活碼778899分享:c++學(xué)習(xí)筆記(更新中)
參考鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。