聯發科技09年手機軟件開發筆試題

1. 編寫一個函數,實現將ASCII碼字符串轉化爲帶符號的整數。
    int StrToInt (const char* pStr);
   傳遞給StrToInt的字符串只包含數字和‘-’(負號),是一個格式正確的整數值。
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int StrtoInt(const char *str)
  5. {
  6.     int Result = 0;
  7.     int i = 0;
  8.     bool m_bMinus = false;
  9.  
  10.     if (*str == '-')
  11.     {
  12.         m_bMinus = true;
  13.         i++;
  14.     }
  15.  
  16.     while (*(str + i) != '\0')
  17.     {
  18.         Result = Result * 10 + *(str+i) - '0';
  19.         i++;
  20.     }
  21.  
  22.     if (m_bMinus)
  23.     {
  24.         Result = 0 - Result;
  25.     }
  26.  
  27.     return Result;
  28. }
  29.  
  30. int main()
  31. {
  32.     char str[] = "-54567";
  33.     int r;
  34.     r = StrtoInt(str);
  35.     cout<<r<<endl;
  36.  
  37.     return 0;
  38. }
2. 編寫一個函數,實現將一個整數轉換爲ASCII碼字符串。
   int IntToStr (int num, char* pStr);
   已知條件:傳遞給IntToStr函數的緩衝區的長度是可以容納int整數範圍內的數。
 
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. int IntToStr(int num, char *pStr)
  6. {
  7.     int m_bMinus = false;
  8.     int i = 0;
  9.     int len;
  10.     char *tmp = pStr;
  11.  
  12.     if (num < 0)
  13.     {
  14.         m_bMinus = true;
  15.         num = abs(num);
  16.     }
  17.  
  18.     while (num)
  19.     {
  20.         tmp[i++] = num % 10 + '0';
  21.         num /= 10;
  22.     }
  23.     tmp[i] = '\0';
  24.  
  25.     len = strlen(tmp);
  26.     for (int j = 0; j < len/2; j++)
  27.     {
  28.         char ch;
  29.         ch = tmp[j];
  30.         tmp[j] = tmp[len - 1 - j];
  31.         tmp[len -1 - j] = ch;
  32.     }
  33.  
  34.     if (m_bMinus)
  35.     {
  36.         tmp[len+1] = '\0';
  37.         for (int k = len - 1; k >= 0; k--)
  38.         {
  39.             tmp[k+1] = tmp[k];
  40.         }
  41.         tmp[0] = '-';
  42.     }
  43.  
  44.     return 0;
  45. }
  46.  
  47. int main()
  48. {
  49.     char pStr[20];
  50.     IntToStr(-12345, pStr);
  51.     cout<<pStr<<endl;
  52.  
  53.     return 0;
  54. }
3. 編程實現二分法搜索函數,該函數對一個排序好的整數數組進行二分搜索(binary Search),函數的原型以下:    int BinarySearch(const int* array, int lower, int upper, int target);
其中lower、upper分別是須要進行搜索的開始和結束的索引值,請分別使用遞歸和非遞歸兩種方法實現該函數。
非遞歸:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int BinarySearch(const int* array, int lower, int upper, int target)
  5. {
  6.     int mid;
  7.     int index = -1;
  8.     while (lower <= upper)
  9.     {
  10.         mid = (lower + upper) / 2;
  11.         if (array[mid] == target)
  12.         {
  13.             index = mid;
  14.             return index;
  15.         }
  16.         else if (array[mid] < target)
  17.         {
  18.             lower = mid + 1;
  19.         }
  20.         else if (array[mid] > target)
  21.         {
  22.             upper = mid - 1;
  23.         }
  24.     }
  25.  
  26.     return index;
  27. }
  28.  
  29. int main()
  30. {
  31.     int arr[10] = {2,5,7,10,12,15,17,19,25,58};
  32.     int index;
  33.     index = BinarySearch(arr, 0, 9, 15);
  34.     cout<<index<<" "<<arr[index]<<endl;
  35.  
  36.     return 0;
  37. }
遞歸:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int BinarySearch(const int* array, int lower, int upper, int target)
  5. {
  6.     int mid, index = -1;
  7.  
  8.     while (lower <= upper)
  9.     {
  10.         mid = (lower + upper) / 2;
  11.         if (array[mid] == target)
  12.         {
  13.             index = mid;
  14.             return index;
  15.         }
  16.         else if (array[mid] < target)
  17.         {
  18.             return BinarySearch(array, mid + 1, upper, target);
  19.         }
  20.         else if (array[mid] > target)
  21.         {
  22.             return BinarySearch(array, lower, mid - 1, target);
  23.         }
  24.     }
  25.  
  26.     return index;
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     int arr[10] = {2,5,7,10,12,15,17,19,25,58};
  33.     int index;
  34.     index = BinarySearch(arr, 0, 9, 17);
  35.     cout<<index<<" "<<arr[index]<<endl;
  36.  
  37.     return 0;
  38. }
4. 下面這個程序執行後會有什麼錯誤或者效果
         #define MAX 255
           int main(void)
{
    unsigned char I;
    unsigned char A[MAX];
   
    for(i=0; i<=MAX; i++)
    {
        A[i] = i;
    }
   
    Return 0;
}
存在兩個錯誤,第一:這將是一個無限循環,由於i是無符號的字符型,範圍爲0—255,當i=255後加1,i又等於了0,因而又知足了循環條件。第二,數組下標訪問越界。
5. 在32位小端字節序系統裏,
char array[16] ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char* cp = array;
int* ip = (int*)array;
問:(1)*(cp+2)=?
    (2)*(ip+2)=?
這 涉及指針的運算,當指針p+n時其計算爲:p+ n * sizeof(指針指向的類型),因爲cp指向的類型爲char,因而cp+2 = cp + 2 * sizeof(char),就是至關於cp的初始值向前移動兩個字節,因而*(cp+2) = 2;而ip指向的類型爲int,因而ip+2 = ip + 2 * sizeof(int) = ip + 8;就是至關於ip的初始值向前移動了8個字節,因而
*(ip+2)=8。
 
6. c是一個char類型的變量,c的取值範圍是多少?若是c的值是0xF0,那麼 c>>3=? 請給出10進制的答案。
   c的取值範圍:-128——127;
   c>>3=-1,緣由:
   c = 0xF0的二進制表示爲11110000,右移三位同時由於是帶符號的char,因而右移後高位補1,因而獲得11111110,最高位爲1,因而是負 數,負數是以補碼的形式存儲,因而按求負數補碼的反步驟計算該負數,對11111110 - 1 = 11111101,在對11111101 取反爲 00000010=2,所以爲-2。
 
7. 下面函數實現有問題嗎? 若是有問題,實際運行中調用下面函數結果正確,並無出現問題,爲何?
int ExchangeValue(int* pa, int* pb)
{
    int* pt;
    if(pa == NULL || pb == NULL)
        return 0;
 
    *pt = *pa;
    *pa = *pb;
    *pb = *pt;
    return 1;
}
有問題:函數中並未對指針pt賦值,讓其指向一個整形變量就使用了。
 
8.
    int i1;
    const char* p1 = 「AAA」;
    int main()
{
    static int i2;
    int i3;
    int* i4 = malloc(sizeof(int));
    ........ 
}
上面程序中的變量(i1,p1,i2,i3,i4)分別存在於哪些內存存貯位置(數據段、堆棧或堆)?
i1,i2,i3,*i4的值是什麼?
i1位於數據段;p1位於數據段;i2位於數據段;i3位於堆棧;i4位於堆。
i1 = 0; i2 = 0;i3隨機的;i4是一個指向int類型的地址。
 
9.
下面哪組語句沒有編譯和運行的錯誤?若是有錯誤的組,請說明哪些語句是錯誤的,爲何?
A.     const int a; (1)   //const常量聲明的同時必須初始化
B.     char* pa = 「AAA」(1)
const char* pb = 「BBB」; (2)
pb = pa; (3)
C. char* pa = 「AAA」; (1)
   Char* const pb = 「BBB」; (2)
   Char* const pc = (char*) malloc(4); (3)
   *pb = ‘A’; (4)
   *(pc+1) = ‘A’;(5)
   Pb = pa;        (6)   //錯,由於pc是常量指針,所以不能夠改變指向
D. char* pa; (1)
const char* const pb = 」BBB」; (2)
char* const pc; (3)  //錯,此時pc被定義爲常量指針,在此時必須初始化
pa = 「AAA」;(4)
pc = pa;(5)   //錯,由於pc是常量指針,不能改變指向
 
 
10.用變量a給出下面的定義: 舉例一個整型數  int a;
(1)  一個指向指針的指針,它指向的指針是指向一個整型數   //int **a
(2)  一個有10個整型數的數組         //int a[10]
(3)  一個有10個指針的數組,該指針是指向一個整型數    //int *a[10]
(4)  一個指向有10個整型數組的指針          //int (*a)[10]
(5)  一個指向函數的指針,該函數有一個整型參數並返回一個整型數   //int (*a)(int)
(6)  一個有10個指針的數組,該指針指向一個函數,該函數有一個整型參數並返回一個整型數
    //int (*a[10])(int)
 
11.下面是一個用來刪除單向鏈表頭節點的函數,請找出其中程序的漏洞並加以糾正。
        void RemoveHead (node* head)
        {
            free(head);   /* Line1 */
            head = head->next;  /Line2 */
        }
錯位在於:已經將head free以後,後面的head->next將出錯。
修改成:
     void RemoveHead(node *head)
     {
         p = head;
         head = head->next;
         free(p);
     }
相關文章
相關標籤/搜索