1. 編寫一個函數,實現將ASCII碼字符串轉化爲帶符號的整數。
int StrToInt (const char* pStr);
傳遞給StrToInt的字符串只包含數字和‘-’(負號),是一個格式正確的整數值。
- #include <iostream>
- using namespace std;
-
- int StrtoInt(const char *str)
- {
- int Result = 0;
- int i = 0;
- bool m_bMinus = false;
-
- if (*str == '-')
- {
- m_bMinus = true;
- i++;
- }
-
- while (*(str + i) != '\0')
- {
- Result = Result * 10 + *(str+i) - '0';
- i++;
- }
-
- if (m_bMinus)
- {
- Result = 0 - Result;
- }
-
- return Result;
- }
-
- int main()
- {
- char str[] = "-54567";
- int r;
- r = StrtoInt(str);
- cout<<r<<endl;
-
- return 0;
- }
2. 編寫一個函數,實現將一個整數轉換爲ASCII碼字符串。
int IntToStr (int num, char* pStr);
已知條件:傳遞給IntToStr函數的緩衝區的長度是可以容納int整數範圍內的數。
- #include <iostream>
- #include <math.h>
- using namespace std;
-
- int IntToStr(int num, char *pStr)
- {
- int m_bMinus = false;
- int i = 0;
- int len;
- char *tmp = pStr;
-
- if (num < 0)
- {
- m_bMinus = true;
- num = abs(num);
- }
-
- while (num)
- {
- tmp[i++] = num % 10 + '0';
- num /= 10;
- }
- tmp[i] = '\0';
-
- len = strlen(tmp);
- for (int j = 0; j < len/2; j++)
- {
- char ch;
- ch = tmp[j];
- tmp[j] = tmp[len - 1 - j];
- tmp[len -1 - j] = ch;
- }
-
- if (m_bMinus)
- {
- tmp[len+1] = '\0';
- for (int k = len - 1; k >= 0; k--)
- {
- tmp[k+1] = tmp[k];
- }
- tmp[0] = '-';
- }
-
- return 0;
- }
-
- int main()
- {
- char pStr[20];
- IntToStr(-12345, pStr);
- cout<<pStr<<endl;
-
- return 0;
- }
3. 編程實現二分法搜索函數,該函數對一個排序好的整數數組進行二分搜索(binary Search),函數的原型以下: int BinarySearch(const int* array, int lower, int upper, int target);
其中lower、upper分別是須要進行搜索的開始和結束的索引值,請分別使用遞歸和非遞歸兩種方法實現該函數。
非遞歸:
- #include <iostream>
- using namespace std;
-
- int BinarySearch(const int* array, int lower, int upper, int target)
- {
- int mid;
- int index = -1;
- while (lower <= upper)
- {
- mid = (lower + upper) / 2;
- if (array[mid] == target)
- {
- index = mid;
- return index;
- }
- else if (array[mid] < target)
- {
- lower = mid + 1;
- }
- else if (array[mid] > target)
- {
- upper = mid - 1;
- }
- }
-
- return index;
- }
-
- int main()
- {
- int arr[10] = {2,5,7,10,12,15,17,19,25,58};
- int index;
- index = BinarySearch(arr, 0, 9, 15);
- cout<<index<<" "<<arr[index]<<endl;
-
- return 0;
- }
遞歸:
- #include <iostream>
- using namespace std;
-
- int BinarySearch(const int* array, int lower, int upper, int target)
- {
- int mid, index = -1;
-
- while (lower <= upper)
- {
- mid = (lower + upper) / 2;
- if (array[mid] == target)
- {
- index = mid;
- return index;
- }
- else if (array[mid] < target)
- {
- return BinarySearch(array, mid + 1, upper, target);
- }
- else if (array[mid] > target)
- {
- return BinarySearch(array, lower, mid - 1, target);
- }
- }
-
- return index;
- }
-
-
- int main()
- {
- int arr[10] = {2,5,7,10,12,15,17,19,25,58};
- int index;
- index = BinarySearch(arr, 0, 9, 17);
- cout<<index<<" "<<arr[index]<<endl;
-
- return 0;
- }
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);
}