實驗5 C語言指針應用編程
實驗任務1
task1_1
1 #include <stdio.h> 2 #define N 5 3 4 void input(int x[], int n); 5 void output(int x[], int n); 6 void find_min_max(int x[], int n, int *pmin, int *pmax); 7 8 int main() { 9 int a[N]; 10 int min, max; 11 12 printf("錄入%d個數據:\n", N); 13 input(a, N); 14 15 printf("數據是: \n"); 16 output(a, N); 17 18 printf("數據處理...\n"); 19 find_min_max(a, N, &min, &max); 20 21 printf("輸出結果:\n"); 22 printf("min = %d, max = %d\n", min, max); 23 24 return 0; 25 } 26 27 void input(int x[], int n) { 28 int i; 29 30 for(i = 0; i < n; ++i) 31 scanf("%d", &x[i]); 32 } 33 34 void output(int x[], int n) { 35 int i; 36 37 for(i = 0; i < n; ++i) 38 printf("%d ", x[i]); 39 printf("\n"); 40 } 41 42 void find_min_max(int x[], int n, int *pmin, int *pmax) { 43 int i; 44 45 *pmin = *pmax = x[0]; 46 47 for(i = 1; i < n; ++i) 48 if(x[i] < *pmin) 49 *pmin = x[i]; 50 else if(x[i] > *pmax) 51 *pmax = x[i]; 52 }

find_min_max的功能是通過間接訪問求出五個數據中的最大值和最小值;
執行到line45時,指針變量pmin、pmax都指向x[0]的地址。
task1_2
1 #include <stdio.h> 2 #define N 5 3 4 void input(int x[], int n); 5 void output(int x[], int n); 6 int *find_max(int x[], int n); 7 8 int main() { 9 int a[N]; 10 int *pmax; 11 12 printf("錄入%d個數據:\n", N); 13 input(a, N); 14 15 printf("數據是: \n"); 16 output(a, N); 17 18 printf("數據處理...\n"); 19 pmax = find_max(a, N); 20 21 printf("輸出結果:\n"); 22 printf("max = %d\n", *pmax); 23 24 return 0; 25 } 26 27 void input(int x[], int n) { 28 int i; 29 30 for(i = 0; i < n; ++i) 31 scanf("%d", &x[i]); 32 } 33 34 void output(int x[], int n) { 35 int i; 36 37 for(i = 0; i < n; ++i) 38 printf("%d ", x[i]); 39 printf("\n"); 40 } 41 42 int *find_max(int x[], int n) { 43 int max_index = 0; 44 int i; 45 46 for(i = 1; i < n; ++i) 47 if(x[i] > x[max_index]) 48 max_index = i; 49 50 return &x[max_index]; 51 }

find_max函數返回的是五個數據中最大值的地址;
可以將find_max函數改寫成以下形式:
1 int *find_max(int x[], int n) { 2 int *ptr = &x[0]; 3 int i; 4 for(i = 1; i < n; ++i) 5 if(x[i] > *ptr) 6 ptr = &x[i]; 7 return ptr; 8 }
實驗任務2
task2_1
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 5 int main() { 6 char s1[] = "Learning makes me happy"; 7 char s2[] = "Learning makes me sleepy"; 8 char tmp[N]; 9 10 printf("sizeof(s1) vs. strlen(s1): \n"); 11 printf("sizeof(s1) = %d\n", sizeof(s1)); 12 printf("strlen(s1) = %d\n", strlen(s1)); 13 14 printf("\nbefore swap: \n"); 15 printf("s1: %s\n", s1); 16 printf("s2: %s\n", s2); 17 18 printf("\nswapping...\n"); 19 strcpy(tmp, s1); 20 strcpy(s1, s2); 21 strcpy(s2, tmp); 22 23 printf("\nafter swap: \n"); 24 printf("s1: %s\n", s1); 25 printf("s2: %s\n", s2); 26 27 return 0; 28 }

數組s1的大小是24字節,sizeof(s1)計算的是s1的字節長度,strlen(s1)計算的是s1的實際長度(不會將空字符計算在內);
line6代碼不能替換成以下寫法(數組名即數組首地址,是常量,不能對數組做賦值操作):
1 char s1[]; 2 s1 = "Learning makes me happy";
line19-21執行后,字符數組s1和s2中的內容發生交換。
task2_2
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 5 int main() { 6 char *s1 = "Learning makes me happy"; 7 char *s2 = "Learning makes me sleepy"; 8 char *tmp; 9 10 printf("sizeof(s1) vs. strlen(s1): \n"); 11 printf("sizeof(s1) = %d\n", sizeof(s1)); 12 printf("strlen(s1) = %d\n", strlen(s1)); 13 14 printf("\nbefore swap: \n"); 15 printf("s1: %s\n", s1); 16 printf("s2: %s\n", s2); 17 18 printf("\nswapping...\n"); 19 tmp = s1; 20 s1 = s2; 21 s2 = tmp; 22 23 printf("\nafter swap: \n"); 24 printf("s1: %s\n", s1); 25 printf("s2: %s\n", s2); 26 27 return 0; 28 }

指針變量s1里面存放的是指向字符串常量的地址, sizeof(s1) 計算的是指針變量s1所占內存大小,strlen(s1) 統計的是s1指向的字符串的實際長度。
line6代碼可以替換成以下形式:
1 char *s1; 2 s1 = "Learning makes me happy";
這兩行代碼定義了一個指針變量,并使該指針變量指向了一個字符串常量。
line19-line21交換的是兩個字符串常量的地址,字符串常量"Learning makes me happy"和字符串常量"Learning makes me sleepy"在內存存儲單元中沒有交換。
實驗任務3
1 #include <stdio.h> 2 3 #include <stdio.h> 4 5 int main() { 6 int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 7 int i, j; 8 int *ptr1; // 指針變量,存放int類型數據的地址 9 int(*ptr2)[4]; // 指針變量,指向包含4個int元素的一維數組 10 11 printf("輸出1: 使用數組名、下標直接訪問二維數組元素\n"); 12 for (i = 0; i < 2; ++i) { 13 for (j = 0; j < 4; ++j) 14 printf("%d ", x[i][j]); 15 printf("\n"); 16 } 17 18 printf("\n輸出2: 使用指向元素的指針變量ptr1間接訪問二維數組元素\n"); 19 for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { 20 printf("%d ", *ptr1); 21 22 if ((i + 1) % 4 == 0) 23 printf("\n"); 24 } 25 26 printf("\n輸出3: 使用指向一維數組的指針變量ptr2間接訪問二維數組元素\n"); 27 for (ptr2 = x; ptr2 < x + 2; ++ptr2) { 28 for (j = 0; j < 4; ++j) 29 printf("%d ", *(*ptr2 + j)); 30 printf("\n"); 31 } 32 33 return 0; 34 }

int (*ptr)[4]; 中,標識符ptr表示指向一個包含四個整形元素的指針。
int *ptr[4]; 中,標識符ptr表示的是一個含四個元素的數組,該數組的元素是指向整形變量的指針。
實驗任務4
task4_1
1 #include <stdio.h> 2 #define N 80 3 4 void replace(char *str, char old_char, char new_char); // 函數聲明 5 6 int main() { 7 char text[N] = "c programming is difficult or not, it is a question."; 8 9 printf("原始文本: \n"); 10 printf("%s\n", text); 11 12 replace(text, 'i', '*'); // 函數調用 注意字符形參寫法,單引號不能少 13 14 printf("處理后文本: \n"); 15 printf("%s\n", text); 16 17 return 0; 18 } 19 20 // 函數定義 21 void replace(char *str, char old_char, char new_char) { 22 int i; 23 24 while(*str) { 25 if(*str == old_char) 26 *str = new_char; 27 str++; 28 } 29 }

replace的功能是將所有的'i'改成'*';
line24, 圓括號里循環條件可以改寫成 *str != '\0'。
task4_2
1 #include <stdio.h> 2 #define N 80 3 4 void str_trunc(char *str, char x); 5 6 int main() { 7 char str[N]; 8 char ch; 9 10 printf("輸入字符串: "); 11 gets(str); 12 13 printf("輸入一個字符: "); 14 ch = getchar(); 15 16 printf("截斷處理...\n"); 17 str_trunc(str, ch); 18 19 printf("截斷處理后的字符串: %s\n", str); 20 21 return 0; 22 } 23 24 void str_trunc(char *str, char x) { 25 while(*str) { 26 if(*str == x) 27 *str='\0'; // blank1 28 29 str++; // blank2 30 } 31 32 }

實驗任務5
task5_1
1 #include <stdio.h> 2 #include <string.h> 3 void sort(char *name[], int n); 4 5 int main() { 6 char *course[4] = {"C Program", 7 "C++ Object Oriented Program", 8 "Operating System", 9 "Data Structure and Algorithms"}; 10 int i; 11 12 sort(course, 4); 13 14 for (i = 0; i < 4; i++) 15 printf("%s\n", course[i]); 16 17 return 0; 18 } 19 20 void sort(char *name[], int n) { 21 int i, j; 22 char *tmp; 23 24 for (i = 0; i < n - 1; ++i) 25 for (j = 0; j < n - 1 - i; ++j) 26 if (strcmp(name[j], name[j + 1]) > 0) { 27 tmp = name[j]; 28 name[j] = name[j + 1]; 29 name[j + 1] = tmp; 30 } 31 }

task5_2
1 #include <stdio.h> 2 #include <string.h> 3 void sort(char *name[], int n); 4 5 int main() { 6 char *course[4] = {"C Program", 7 "C++ Object Oriented Program", 8 "Operating System", 9 "Data Structure and Algorithms"}; 10 int i; 11 12 sort(course, 4); 13 for (i = 0; i < 4; i++) 14 printf("%s\n", course[i]); 15 16 return 0; 17 } 18 19 void sort(char *name[], int n) { 20 int i, j, k; 21 char *tmp; 22 23 for (i = 0; i < n - 1; i++) { 24 k = i; 25 for (j = i + 1; j < n; j++) 26 if (strcmp(name[j], name[k]) < 0) 27 k = j; 28 29 if (k != i) { 30 tmp = name[i]; 31 name[i] = name[k]; 32 name[k] = tmp; 33 } 34 } 35 }

這兩種算法實現中交換的是內存中字符串的存儲位置。
實驗任務6
1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 5 int check_id(char *str); // 函數聲明 6 7 int main() { 8 char *pid[N] = {"31010120000721656X", 9 "330106199609203301", 10 "53010220051126571", 11 "510104199211197977", 12 "53010220051126133Y"}; 13 int i; 14 15 for (i = 0; i < N; ++i) 16 if (check_id(pid[i])) // 函數調用 17 printf("%s\tTrue\n", pid[i]); 18 else 19 printf("%s\tFalse\n", pid[i]); 20 21 return 0; 22 } 23 24 // 函數定義 25 // 功能: 檢查指針str指向的身份證號碼串形式上是否合法。 26 // 形式合法,返回1,否則,返回0 27 int check_id(char *str) { 28 int i; 29 int len = strlen(str); 30 31 if (len == 18) { 32 for (int i = 0; i < len; i++) { 33 if (!((str[i] >= '0' && str[i] <= '9') || (i == len - 1 && str[i] == 'X'))) { 34 return 0; 35 } 36 } 37 return 1; 38 } 39 else { 40 return 0; 41 } 42 }

實驗任務7
1 #include <stdio.h> 2 #define N 80 3 void encoder(char *str); // 函數聲明 4 void decoder(char *str); // 函數聲明 5 6 int main() { 7 char words[N]; 8 9 printf("輸入英文文本: "); 10 gets(words); 11 12 printf("編碼后的英文文本: "); 13 encoder(words); // 函數調用 14 printf("%s\n", words); 15 16 printf("對編碼后的英文文本解碼: "); 17 decoder(words); // 函數調用 18 printf("%s\n", words); 19 return 0; 20 } 21 22 /*函數定義 23 功能:對s指向的字符串進行編碼處理 24 編碼規則: 25 對于a~z或A~Z之間的字母字符,用其后的字符替換; 其中,z用a替換,Z用A替換 26 其它非字母字符,保持不變 27 */ 28 void encoder(char *str) { 29 // 補足函數實現 30 // ××× 31 while(*str){ 32 if(*str >= 'A' && *str <='z') 33 if(*str == 'z' || *str == 'Z') 34 *str -= 32; 35 else 36 *str += 1; 37 str++; 38 } 39 } 40 41 /*函數定義 42 功能:對s指向的字符串進行解碼處理 43 解碼規則: 44 對于a~z或A~Z之間的字母字符,用其前面的字符替換; 其中,a用z替換,A用Z替換 45 其它非字母字符,保持不變 46 */ 47 void decoder(char *str) { 48 // 補足函數實現 49 // ××× 50 while(*str){ 51 if(*str >= 'A' && *str <='z') 52 if(*str == 'a' || *str == 'A') 53 *str += 32; 54 else 55 *str -= 1; 56 str++; 57 } 58 }



實驗任務8
1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) { 4 int i; 5 6 for(i = 1; i < argc; ++i) 7 printf("hello, %s\n", argv[i]); 8 9 return 0; 10 }


posted on 2024-05-22 20:04 SilverBullet4869 閱讀(58) 評論(0) 收藏 舉報
浙公網安備 33010602011771號