三、int A[M][N];(一)請利用new的方式,動態產生這個二維陣列;(二)接著利用delete釋放所要的記憶體。(12分)

我覺得這題還蠻難的……

五、試用C++裡function template的方式,寫出一個swap function,作任何兩個相同型態變數的交換功能。(12分)

解:

#include <iostream>
#include <cstdlib>

using namespace std;

template<class T>
void swapF(T &x,T &y)
{
     T temp;
     temp=x;
     x=y;
     y=temp;
}

int main(void)
{
    int m,n;
    m=5;
    n=4;
    
    cout<<"before swap...:"<<m<<", "<<n<<endl;
    swapF(m,n);
    cout<<"after swap...:"<<m<<", "<<n<<endl;
    
    system("pause");
    return 0;
}

 

七、試寫出一個副程式bitsdisplay(unsigned int p),使得這程式能將數字p的每一個bit作輸出。例如:(14分)
bitsdisplay(65534) --- > 1111111111111110

解:

#include <iostream>
#include <cstdlib>

using namespace std;

void bitdisplay(unsigned int p)
{
     int r;
     r=p%2;
     if(p>0)
     {
         bitdisplay(p/2);
         cout<<r;
     }
}

int main(void)
{  
    
    bitdisplay(65534);
    
    system("pause");
    return 0;
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Rinoa 的頭像
    Rinoa

    褪色的世界.斑剝的記憶

    Rinoa 發表在 痞客邦 留言(9) 人氣()