C++簡單程式設計-2

來源:互聯網
上載者:User

標籤:函數參數   stream   去除   mes   設計   cti   資料   blank   white   

四、實驗結論

1.

①函式宣告和函數定義各自的作用,二者的區別;

函式宣告:

int  fun(int a, int b);

 

函數定義:

int  fun(int a,int b){      int  c;    c=a+b;    return c;   }    

函式宣告就像函數定義的頭部,當需要調用函數時,如果函數定義用在函數調用的前面,執行到調用的部分就無法調用,會報錯,這時就需要在函數調用前加個函式宣告,或者先寫函數定義後寫函數調用。

② 什麼是形參?什麼是實參?函數參數和傳回值在函數中起到什麼作用?

形參全稱為“形式參數”是在定義函數名和函數體的時候使用的參數,目的是用來接收調用該函數時傳遞的參數。

實參全稱為“實際參數”是在調用時傳遞給函數的參數,即傳遞給被調用函數的值。

函數參數為在函數運算或函數調用中傳遞的參數。

函數傳回值為函數執行完以後return的結果。

③函數參數傳遞過程中,值傳遞和引用傳遞區別是什嗎?

值傳遞僅僅傳遞的是值

引用傳遞,傳遞的是記憶體位址,修改後會改變記憶體位址對應儲存的值。

2.

(1)習題2-28

①用if...else語句進行判斷

 

#include <iostream>
using namespace std; 
int main() 

    char x;
    cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";
    while(x!=‘Q‘)
    { 
      cin>>x;
      if(x==‘A‘)
           cout<<"資料已經增加"<<endl;
       else if(x==‘D‘)
           cout<<"資料已經刪除"<<endl;
       else if(x==‘S‘)
           cout<<"資料已經排除"<<endl;
   }
    return 0; 
}

 

 

 

②用switch語句

 

#include <iostream>using namespace std;  int main()  {      char x;    cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";     while(x!=‘Q‘)    {        cin>>x;        switch(x)        {            case ‘A‘:                cout<<"資料已經增加"<<endl;break;            case ‘B‘:                cout<<"資料已經刪除"<<endl;break;            case ‘S‘:                cout<<"資料已經排除"<<endl;break;         }    }    return 0; } 

 

 

(2)習題2-29

質數為在大於1的自然數中,除了1和它本身以外不再有其他因數的數。

則質數首先應該大於1,然後用從2到這個數-1的所有數去除這個數,若都不能整除,則這個數為質數。

#include <iostream>using namespace std;  int isprime(int x){int ret=1;int i;for(i=2;i<x;i++){if(x%i==0){ret=0;break;}}if(x<=1)ret=0;return ret;}int main()  {      int x=1;;    while(x<=100)    {if(isprime(x)==1)cout<<x<<‘ ‘;x++;}    return 0;  } 

  

 

(3)習題2-32

 

#include <iostream>#include<cstdlib> #include<ctime>using namespace std; int main(){    srand(time(0));    int a=rand()%100;    int s;    int count=0;    while(1)    {        cout<<"請輸入你要猜的數";         cin>>s;        if(s>a)        {            cout<<"偏大了"<<endl;            count++;         }        else if(s<a)        {            cout<<"偏小了"<<endl;            count++;        }        else        {            count++;            cout<<"猜對了!?"<<endl;            break;         }    }    return 0;}

 

(4)習題 2-34

#include <iostream>#include<iomanip>using namespace std; int main(){    enum color{red,yellow,blue,white,black};    color pri;    int i,j,k,n=0,loop;    for(i=red;i<=black;i++)    {        for(j=red;j<=black;j++)        {            if(i!=j)            {                for(k=red;k<=black;k++)                {                    if(k!=i&&k!=j)                    {                        n++;                        cout<<setw(3)<<n;                        for(loop=1;loop<=3;loop++)                        {                            switch(loop)                            {                                case 1:pri=color(i);break;                                case 2:pri=color(j);break;                                case 3:pri=color(k);break;                                default:break;                            }                            switch(pri)                            {                                case red:cout<<setw(8)<<"red";break;                                case yellow:cout<<setw(8)<<"yellow";break;                                case blue:cout<<setw(8)<<"blue";break;                                case white:cout<<setw(8)<<"white";break;                                case black:cout<<setw(8)<<"black";break;                            }                        }                        cout<<endl;                    }                }            }        }    }    return 0;}

 

 

五、實驗總結與體會

c++中,想要輸出控制字元長度,需要用到stew()函數,同時添加#include<iomanip>標頭檔。

第四題中,color(i)是強制類型轉換,使pri的值為i。

想要產生一個隨機數,需要用到srand和rand函數,同時添加#include<cstdlib>和 #include<ctime>標頭檔

例如:

#include <iostream>

#include<cstdlib>

#include<ctime>

int main()

{

  srand(time(0));

  int a=rand()%100;

  return 0;

}

其中a即為一個1~100的隨機整數。

 

C++簡單程式設計-2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.