看板 Programming
作者 abc1231qa(替咖~拉拉拉~)
標題 [轉錄][轉錄][閒聊] 大一程設期中重點整理
時間 2010年11月12日 Fri. AM 02:38:10


※ 本文轉錄自看板 Test

看板 abc1231qa
作者 abc1231qa (替咖~拉拉拉~)
標題 [閒聊] 大一程設期中重點整理
時間 2010年11月12日 Fri. AM 01:31:28


首先

進考場第一件事 在dev輸入以下程式碼:

#include<iostream>
using namespace std;
int main()
{
    system("pause");
    return 0 ;
}

因為每題都會用到 所以先打好方便複製貼上~


接著是看題目 把題目都看清楚

會的先寫 不一定要照題號寫~~~


以下是一些重點整理


目錄:
[#index]
[url=#ch1]1.亂數[/url]

[url=#ch2]2.for迴圈的用法[/url]

[url=#ch3]3.畫圖形[/url]

[url=#ch4]4.基礎function的用法[/url]

[url=#ch5]5.費式數列(recursive)[/url]

[url=#ch6]6.switch-case & enum[/url]

[url=#ch7]7.call by value & call by reference[/url]

浮動目錄:
  [url=#ch1]1.亂數[/url]
  [url=#ch2]2.for迴圈的用法[/url]
  [url=#ch3]3.畫圖形[/url]
  [url=#ch4]4.基礎function的用法[/url]
  [url=#ch5]5.費式數列(recursive)[/url]
  [url=#ch6]6.switch-case & enum[/url]
  [url=#ch7]7.call by value & call by reference[/url]

















[#ch1]1.亂數
	
	
	
	
[url=#index][↑回目錄][/url]




#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    srand(time(0));//記得include<ctime>
    int x = rand()%5;//x等於除以五的餘數 也就是01234
    cout<<"x = "<<x<<endl;
    system("pause");
    return 0 ;
}




[#ch2]2.for迴圈的用法
	
	
	
[url=#index][↑回目錄][/url]




#include<iostream>
using namespace std;
int main()
{
    int x = 0;
    int sum = 0;//養成初始化的好習慣,將宣告的變數設為0,避免有奇怪的錯誤
    cout<<"請輸入for迴圈想要跑幾次?";
    cin>> x;
    for(int i =0 ; i<x ;i++) //x輸入幾次迴圈就執行幾次
    {
            sum = sum+i;//例:x=5 0+1+2+3+4 總共5次
    }
    cout<<"sum = "<<sum<<endl;
    //遞減其實也可以用"無敵for迴圈"來完成
    //[註]:無敵for迴圈就是我將 for(int i =0 ; i<x ;i++)用法亂取的名字
   
    for(int i = 0 ; i < sum ; i++)//改變x的地方來改變迴圈跑的次數 寫sum 就是跑"sum的值"次
    {   
        x = x-1;
        cout<<x<<" ";//如果x是5 sum是10 那就是4 3 2 1 0 -1 -2 -3 -4 -5
    }
    cout<<endl; 
    system("pause");
    return 0 ;
}




[#ch3]3.畫圖形
	
	
	
[url=#index][↑回目錄][/url]




#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    int n = 0;
    cout<<"請輸入邊長 : ";
    cin>>n;
    for(int i =0 ; i<n ;i++)
    {
        for(int j = 0 ; j < n ;j++)
        {
            cout<<"*";
        }
        cout<<"\n";
    }
    cout<<"\n=========================================\n";
    //畫中空:想法是if某些範圍我畫* 剩下我畫空白
    for(int i =0 ; i<n ;i++)
    {
        for(int j = 0 ; j < n ;j++)
        {
            if(i<2||j<2||i>=n-2||j>=n-2)//這裡的2是邊框的厚度
            {
                cout<<"*";
            }
            else
            {
                cout<<" ";
            }
        }
        cout<<"\n";
    }
    cout<<"\n=========================================\n";
    //畫金字塔(底是n顆星星)
    //每行分成三部分 1.空白2.*的左半部3.*的右半部
    for(int i = 0 ; i <(n/2)+1 ; i++) 
    {
        for(int j = 0 ; j<n/2-i;j++)
        {
            cout<<" ";
        }
        for(int j = 0 ; j<i+1;j++)
        {
            cout<<"*";
        }
        for(int j = 0 ; j<i;j++)
        {
            cout<<"*";
        }
        cout<<"\n" ;
    }
    cout<<"\n=========================================\n";
    //畫金字塔(n層)
    //每行分成三部分 1.空白2.*的左半部3.*的右半部
    for(int i = 0 ; i <n; i++) 
    {
        for(int j = 0 ; j<n-i-1;j++)
        {
            cout<<" ";
        }
        for(int j = 0 ; j<i+1;j++)
        {
            cout<<"*";
        }
        for(int j = 0 ; j<i;j++)
        {
            cout<<"*";
        }
        cout<<"\n" ;
    }
    cout<<"\n=========================================\n";
    //畫菱形(直徑是n顆星星=n層)
    //每行分成三部分 1.空白2.*的左半部3.*的右半部
    for(int i = 0 ; i < n ; i++) 
    {
        if(i<=n/2)
        {
            for(int j = 0 ; j<n/2-i;j++)
            {
                cout<<" ";
            }
            for(int j = 0 ; j<i+1;j++)
            {
                cout<<"*";
            }
            for(int j = 0 ; j<i;j++)
            {
                cout<<"*";
            }
        }
        else
        {
            for(int j = 0 ; j<i-n/2;j++)
            {
                cout<<" ";
            }
            for(int j = 0 ; j<n-i;j++)
            {
                cout<<"*";
            }
            for(int j = 0 ; j<n-i-1;j++)
            {
                cout<<"*";
            }
        }
        cout<<"\n" ;
    }
    cout<<"\n=========================================\n";
    //畫空心菱形(直徑是n顆星星=n層)
    //另外一種畫菱形的方法一行分成四部分1.空格2.*3.空格 4.*
    for(int i = 0 ; i < n ; i++) 
    {
        if(i<=n/2)
        {
            for(int j = 0 ; j<n/2-i;j++)
            {
                cout<<" ";
            }
            cout<<"*";
            for(int j = 0 ; j<i*2-1;j++)
            {
                cout<<" ";
            }
            if(i!=0)
            {
                cout<<"*";
            }
        }
        else
        {
            for(int j = 0 ; j<i-n/2;j++)
            {
                cout<<" ";
            }
            cout<<"*";
            for(int j = 0 ; j<(n-i-1)*2-1;j++)
            {
                cout<<" ";
            }
            if(i!=n-1)
            {
                cout<<"*";
            }
        } 
        cout<<"\n" ;
    }


   
    system("pause");
    return 0 ;
}




[#ch4]4.基礎function的用法
	
	
[url=#index][↑回目錄][/url]




#include<iostream>
#include<ctime>
using namespace std;

void func1()
{
     cout<<"第1種function:什麼都沒有\n";
}

int func2()
{
     cout<<"第2種function:回傳變數型\n";
     int x = 55;
     return x;//其實func1是回傳void void是"無"的意思
}

void func3(int x)//這裡的x跟func2沒關係
{
     cout<<"第3種function:接收變數型\n";
     cout<<"x = "<<x<<endl;
}

int func4(int x)//這裡的x跟func2,func3沒關係
{
     cout<<"第4種function:綜合型\n";
     return ( (x*1100) + (x+1)*11);
}

int main()
{
    func1();
   
    int number = 0;   
    number = func2();//func2用法1 : 將值回傳到變數中
    cout<<"number = "<<number<<endl;
    cout<<"func2() = "<<func2()<<endl;//func2用法2 : 寫在cout裡面
   
    func3(66);
   
    cout<<"func4() = "<<func4(5)<<endl;
   
    system("pause");
    return 0 ;
}




[#ch5]5.費式數列(recursive)
	
	
[url=#index][↑回目錄][/url]




#include<iostream>
#include<ctime>
using namespace std;

int fib(int n)
{
    if(n==0)
    {
        return 0;//n=0的值看題目而定
    }
    else if(n==1)
    {
         return 1;//n=1也是
    }
    else
    {
        return fib(n-1)+fib(n-2);//recursive : 函數自己呼叫自己 , 不理解就用背的吧
    }
}


int main()
{
    int i =0;
    cout<<"請輸入要找第幾個費式數列 : ";
    cin>>i;
    cout<<"result = "<<fib(i)<<endl;
    system("pause");
    return 0 ;
}




[#ch6]6.switch-case & enum
	
	
[url=#index][↑回目錄][/url]




#include<iostream>
#include<ctime>
using namespace std;
int main()
{
    int x = -1; 
    enum EType { T1=1,T2,T3};
    /*
    以上這行等同於
    int T1 = 1;
    int T2 = 2;
    int T3 = 3;
    */
    while(x!=0)
    {
        cout<<"請輸入數字(0結束) : \n" ;
        cin >> x;
        switch (x)
        {
          case 0:
          {
            cout << "程式即將結束." << endl;
            break;
          }     
          case T1:
          {
            cout << "A" << endl;
            break;
          }
          case T2:
          {
            cout << "B" << endl;
            break;
          }
          case T3:
          {
            cout << "C" << endl;
            break;
          }
          default:
          {
            cout << "輸入錯誤." << endl;
            break;
          }
        }
    }
    system("pause");
    return 0 ;
}




[#ch7]7.call by value & call by reference[url=#index][↑回目錄][/url]




請看下面這三篇~

[C++] Call by Value

Call by Address (Call by Pointer)

Call by Reference























--
免責聲明第五條:
  本人在此留言(包括但不限於漢字、注音、拉丁字母、斯拉夫字母,日語假名,
阿拉伯字母,單字、句子、圖片、影像、錄音,以及前述之各種任意組合等等)均為
隨意敲擊鍵盤所出,用於檢驗本人電腦鍵盤輸入、螢幕顯示的性能、光電性能,並不
代表本人局部或全部同意、支持或者反對原po觀點。如需要詳查請直接與鍵盤發明者
及生產外商法人代表連繫。
※ 編輯: abc1231qa  時間: 2010-11-12 02:10:38  來自: 114-44-202-60.dynamic.hinet.net

--
--
※ 來源: Disp BBS 看板: Programming 文章位址: http://disp.cc/b/33-KhA
※ 作者: abc1231qa  時間: 2010-11-12 02:38:10  來自: 114-44-202-60.dynamic.hinet.net
--
abc1231qa: [轉錄][轉錄][閒聊] 大一程設期中重點整理 - Programming板