Академический Документы
Профессиональный Документы
Культура Документы
學號 : 姓名 : 分數 :
填充題
問題 1 (10%) 問題 2 (10%)
int main() { int main() {
int a = 5, b = 3; int a = 5, b = 3, c;
a = a + b; c = (a > b) ? a : b;
printf("a=%d\n", a); printf("a=%d\n", a);
a = a % 5; printf("c=%d\n", c);
printf("a=%d\n", a); if (a > b) {
a = a << 2; c = a+b;
printf("a=%d\n", a); a++;
b = b & 0x05; } else {
printf("b=%d\n", b); c = a-b;
b = b | 0x08; b++;
printf("b=%d\n", b); }
} printf("a=%d\n", a);
輸出結果 printf("b=%d\n", b);
a= printf("c=%d\n", c);
a= system("pause");
a= }
b= 輸出結果
b= a=
c=
a=
b=
c=
1
學號 : 姓名 : 分數 :
C 語言的函數設計
主程式 1 :
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int x[] = {3, 7, 4, 2, 6, 5, 8 };
int y[7];
int oddLen;
print_array(x, 7);
odd(x, 7, y, &oddLen);
print_array(y, oddLen);
sort(x, 7);
print_array(x, 7);
int idx3 = binary_search(x, 0, 6, 3);
printf("idx3 = %d\n", idx3);
return 0;
}
問題 3. 請寫出一個函數可以計算 1 + 2 + … + n
(15%)。
以下各題請參考主程式 1 進行撰寫。
2
學號 : 姓名 : 分數 :
動態字串設計
以下是一個動態字串的測試程式 問題 7. 請寫出 StrNew 函數 (15%)
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int size;
char *str;
} Str;
提供下列標準 C 函數介面以供您使用:
1. strlen(str) : 傳回 str 的字串長度
2. strcpy(toStr, fromStr) : 將 fromStr 複製給 toStr
3. int sprintf( char *buffer, const char *format, ... );
像 printf 一樣,但會印在第一個參數的 buffer 字串
中。
3
4