(1)風色從零單排《C++ Primer》 一個簡單的c++程序


從零單排《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 times
55 occurs 2 times
62 occurs 1 times
100 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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章