標籤:ica center tor sam 哪些 file if語句 運行 values
從零單排《C++ Primer》
——(1)一個簡單的c++程式
本次學習收穫
0、寫在前面
風色以前上過C++的課程。然而當時並沒有認真去學,基本不能使用c++來作項目開發。這次又一次學習c++,首先會閱讀c++Prime英文版第五版。希望能夠為以後的學習打下堅實的基礎。
1、程式
一個簡單的c++程式(P17),協助我們高速瞭解c++的代碼風格,並通過分析代碼學習c++
程式意圖:在終端上輸入一組數字,輸出不同數字輸出的次數。同樣的數字必須是連續的。
如果輸入:
42 42 42 42 55 55 62 100 100 100
終端應該輸出:
42 occurs 5 times55 occurs 2 times62 occurs 1 times100 occurs 3 times
代碼:
#include <iostream>int main(){ //currVal is the number we are counting;we will read new value into val. currVal用來存放當前計算的資料。新讀入的資料會放入val int currVal = 0, val = 0; //read first number and ensure that we have data to process. 為確保有可操作的資料,先讀入一個 if(std::cin >> currVal){ int cnt = 1; //store the count for the current value we‘re processing. cnt用來計數。當前存放的資料出現了多少次while (std::cin >> val){ // read the remaining numbers.讀剩下的資料 if(val == currVal) //if the values are the same.假設新讀入的資料。和當前計算的資料是一樣的++cnt; else { std::cout<< currVal << " occurs " << cnt <<"times" << std::endl;currVal = val; //remember the new value cnt = 1; }} // while loop ends here. while迴圈結束 } // outermost if statement ends here . if結束 return 0;}
2、程式碼分析:
2.1函數:
每一個c++程式擁有至少一個函數。當中一個函數必須叫作main。
他是作業系統執行c++程式時程式的入口。在上面的程式中,我們也自訂了一個main函數。它是:
</pre><pre name="code" class="cpp">int main(){ //currVal is the number we are counting;we will read new value into val. currVal用來存放當前計算的資料,新讀入的資料會放入val ...... return 0;}一個函數由4個部分組成:
1)傳回型別
在這個函數中。它的傳回型別是int 型。表示整數。
2)函數名
在這個函數中為main
3)參數(能夠為空白)
參數填在main後面的()內。改函數的參數為空白
4)函數主體
花括弧及以內的部分
這個函數最後一條語句:return 。
這是一條能夠用來結束函數的語句。在這個範例中,return能夠返回一個值給函數的調用者。返回的值必須喝函數的傳回型別一致。在這個範例中。函數返回了一個整數型0。
2.2使用Library(函數庫):
程式第一行:
#include<iostream>
在程式實現時,我們不僅會自訂一些函數。通常也會使用一些別人已經寫好的函數來簡化我們的編碼。使用別人的函數必須引入相應的函數庫的標頭檔。
程式迪一行快速編譯器我們要使用iostream libraary。
<>裡面的名稱是我們要引入的標頭檔。
這裡我們引入了iostream,以便實如今終端上的輸入輸出操作。<>表示引入的是標準檔案,要引入自己的檔案,使用""。
2.3凝視:
凝視掉的內容,編譯器時不會啟動並執行,凝視用來方便別人閱讀代碼。
c++有兩種凝視方法
1)單行凝視 //
2)c凝視 /* */
2.4標準輸入與輸出:
這個範例中使用了iostream library來處理標準輸入和輸出。它包括了istream和ostream,分別代表input streams(輸入資料流) 和 output streams(輸出資料流)。Stream(流)表示從輸入輸出裝置讀取/寫入一系列有順序的字元。
std::cin >> currVal
從終端上讀入一個資料,存放在變數currVal上。currVal變數用來存放當前計算的數字。
std::cout<< currVal << " occurs " << cnt <<"times" << std::endl;
從終端上輸出當前數字共出現了多少次。
變數cnt記錄了currVal所存放的數字出現的次數。
endl表示換行。
輸入輸出能夠這樣連續使用:
int v1 =0, v2 = 0;std::cin>>v1>>v2;std::cout<<v1<<v2<<std::endl;
這是由於>>(<<)會返回它左邊的運算元(operand)
2.5命名空間:
假設引入了不同庫。而庫裡面假設有相同名稱的函數,調用的時候這個函數的時候。編譯器就會不知道調用哪個。
為瞭解決這個衝突。c++使用了命名空間。注意到
std::cin >> currVal
裡面的std::,表示使用標準庫裡面的cin函數。
2.6IF語句以及IF語句與輸入語句的組合使用:
if(條件陳述式){ //運行內容}
首先運行在()內的語句條件陳述式,僅僅有條件陳述式為真時才運行{ } 內的內容。常見的使用方法有if( a == b) , 假設a等於b。則a==b為真。運行{}內容,否則a==b為false,不運行{}內.其實,條件陳述式非0即為真。
在這個範例中,有
if(std::cin >> currVal)
注意到,假設std::cin>>currVal成功讀取到資料,則為真。
2.7WHILE語句以及IF語句與輸入語句的組合使用:
while(條件陳述式){ //運行內容}首先運行條件陳述式,假設條件為真,則會運行{}內的內行,接著繼續運行條件陳述式,假設繼續為真,則繼續運行{}內容,如此迴圈。直到條件運行後為假。
結合if語句裡所述,在不知道有多少資料要讀取時。能夠編寫例如以下語句
while(std::cin>>val){}
當istream無效時,std::cin>>currVal讀取失敗,條件為假。
無效的情況有
1)當我們輸入end-of-file(eof)時 (Dos/Windows:ctrl+z Linux:ctrl+d)
2)輸入了非法資料,如資料為空白,或者類型不符
3、作業:
p9
/** Exercise 1.3: Write a program to print Hello,World on the standard output* 寫一個程式實如今標準輸出中列印Hello,World*/#include<iostream>int main(){ std::cout<<"Hello,World"<<std::endl; return 0; }
p11
/** Exercise 1.8:Indicate which,if any,of the following output statements are legal:* 指出哪些輸出語句是合法的 */#include <iostream>int main(){ std::cout<<"/*";//合法輸出為/*// std::cout<<"*/";//合法輸出為*/// std::cout<</*"*/"*/;//非法 最後缺少一個"// std::cout<</* "*/" /* "/*" */;//合法 輸
p13
/** Exercise 1.9: Write a program that uses a while to sum the numbers from 50 to 100* 用while計算50到100的累加值*/#include <iostream>int main(){ int sum = 0,val = 50; while(val<101){ sum += val; ++val; } std::cout<<"The sum of 50 to 100 inclusive is "<< sum <<std::endl; return 0; }
/**Exercise 1.11 *Write a program that prompts the user for two integers.Print each number in the range specified by thoe two intergers*提示使用者輸入兩個整數。列印著兩個整數翻飛內的數。*/#include<iostream>int main(){ int val1 = 0, val2 = 0, temp = 0; std::cout<<"please input two intergers"<<std::endl; if(std::cin>>val1){if(std::cin>>temp){ if(temp>val1)val2 = temp; else{ val2 = val1; val1 = temp; } while(val1<=val2){ std::cout<<val1<<std::endl; ++val1; }} } return 0;}
(1)風色從零單排《C++ Primer》 一個簡單的c++程式