实验1 源代码
1 #include <stdio.h> 2 #define N 4 3 4 void test1() { 5 int a[N] = {1, 9, 8, 4}; 6 int i; 7 8 printf("sizeof(a) = %d\n", sizeof(a)); 9 10 for (i = 0; i < N; ++i) 11 printf("%p: %d\n", &a[i], a[i]); 12 13 printf("a = %p\n", a); 14 } 15 16 void test2() { 17 char b[N] = {'1', '9', '8', '4'}; 18 int i; 19 20 printf("sizeof(b) = %d\n", sizeof(b)); 21 22 for (i = 0; i < N; ++i) 23 printf("%p: %c\n", &b[i], b[i]); 24 25 printf("b = %p\n", b); 26 } 27 28 int main() { 29 printf("测试1: int类型一维数组\n"); 30 test1(); 31 32 printf("\n测试2: char类型一维数组\n"); 33 test2(); 34 35 return 0; 36 }
1 #include <stdio.h> 2 #define N 2 3 #define M 4 4 5 void test1() { 6 int a[N][M] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 7 int i, j; 8 9 printf("sizeof(a) = %d\n", sizeof(a)); 10 11 for (i = 0; i < N; ++i) 12 for (j = 0; j < M; ++j) 13 printf("%p: %d\n", &a[i][j], a[i][j]); 14 printf("\n"); 15 16 printf("a = %p\n", a); 17 printf("a[0] = %p\n", a[0]); 18 printf("a[1] = %p\n", a[1]); 19 printf("\n"); 20 } 21 22 void test2() { 23 char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}}; 24 int i, j; 25 26 printf("sizeof(b) = %d\n", sizeof(b)); 27 28 for (i = 0; i < N; ++i) 29 for (j = 0; j < M; ++j) 30 printf("%p: %c\n", &b[i][j], b[i][j]); 31 printf("\n"); 32 33 printf("b = %p\n", b); 34 printf("b[0] = %p\n", b[0]); 35 printf("b[1] = %p\n", b[1]); 36 } 37 38 int main() { 39 printf("测试1: int型两维数组"); 40 test1(); 41 42 printf("\n测试2: char型两维数组"); 43 test2(); 44 45 return 0; 46 }
实验一 截屏
实验二 源代码
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 80 5 6 void swap_str(char s1[N], char s2[N]); 7 void test1(); 8 void test2(); 9 10 int main() { 11 printf("测试1: 用两个一维char数组,实现两个字符串交换\n"); 12 test1(); 13 14 printf("\n测试2: 用二维char数组,实现两个字符串交换\n"); 15 test2(); 16 17 return 0; 18 } 19 20 void test1() { 21 char views1[N] = "hey, C, I hate u."; 22 char views2[N] = "hey, C, I love u."; 23 24 printf("交换前: \n"); 25 puts(views1); 26 puts(views2); 27 28 swap_str(views1, views2); 29 30 printf("交换后: \n"); 31 puts(views1); 32 puts(views2); 33 } 34 35 void test2() { 36 char views[2][N] = {"hey, C, I hate u.", 37 "hey, C, I love u."}; 38 39 printf("交换前: \n"); 40 puts(views[0]); 41 puts(views[1]); 42 43 swap_str(views[0], views[1]); 44 45 printf("交换后: \n"); 46 puts(views[0]); 47 puts(views[1]); 48 } 49 50 void swap_str(char s1[N], char s2[N]) { 51 char tmp[N]; 52 53 strcpy(tmp, s1); 54 strcpy(s1, s2); 55 strcpy(s2, tmp); 56 }
实验二 截屏
实验三 源代码
1 #include <stdio.h> 2 #define N 80 3 4 int count(char x[]); 5 6 int main() { 7 char words[N+1]; 8 int n; 9 10 while(gets(words) != NULL) { 11 n = count(words); 12 printf("单词数: %d\n\n", n); 13 } 14 15 return 0; 16 } 17 18 int count(char x[]) { 19 int i; 20 int word_flag = 0; 21 int number = 0; 22 23 for(i = 0; x[i] != '\0'; i++) { 24 if(x[i] == ' ') 25 word_flag = 0; 26 else if(word_flag == 0) { 27 word_flag = 1; 28 number++; 29 } 30 } 31 32 return number; 33 }
1 #include <stdio.h> 2 #define N 1000 3 4 int main() { 5 char line[N]; 6 int word_len; 7 int max_len; 8 int end; 9 int i; 10 11 while(gets(line) != NULL) { 12 word_len = 0; 13 max_len = 0; 14 end = 0; 15 16 i = 0; 17 while(1) { 18 while(line[i] == ' ') { 19 word_len = 0; 20 i++; 21 } 22 23 while(line[i] != '\0' && line[i] != ' ') { 24 word_len++; 25 i++; 26 } 27 28 if(max_len < word_len) { 29 max_len = word_len; 30 end = i; 31 } 32 33 if(line[i] == '\0') 34 break; 35 } 36 37 printf("最长单词: "); 38 for(i = end - max_len; i < end; ++i) 39 printf("%c", line[i]); 40 printf("\n\n"); 41 } 42 43 return 0; 44 }
实验三 截屏
实验四 源代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 100 4 5 void dec_to_n(int x, int n); 6 7 int main() { 8 int x; 9 10 printf("输入一个十进制整数: "); 11 while(scanf("%d", &x) != EOF) { 12 dec_to_n(x, 2); 13 dec_to_n(x, 8); 14 dec_to_n(x, 16); 15 16 printf("\n输入一个十进制整数: "); 17 } 18 19 return 0; 20 } 21 22 23 void dec_to_n(int x, int n) 24 { 25 int a[N]; 26 char t; 27 int i = 1; 28 29 memset(a,'\0',sizeof(a)); 30 while(x >= n) 31 { 32 a[i] = x%n; 33 x /= n; 34 i++; 35 } 36 a[i] = x; 37 38 for(;i > 0;i--) 39 { 40 if(a[i] == 10) 41 { 42 t = 'A'; 43 printf("%c",t); 44 } 45 else if(a[i] > 10) 46 { 47 t = 'A'+a[i]-10; 48 printf("%c",t); 49 } 50 else 51 { 52 printf("%d",a[i]); 53 } 54 } 55 printf("\n"); 56 }
实验四 截屏
实验五 源代码
1 #include <stdio.h> 2 #define N 5 3 4 void input(int x[], int n); 5 void output(int x[], int n); 6 double average(int x[], int n); 7 void bubble_sort(int x[], int n); 8 9 int main() { 10 int scores[N]; 11 double ave; 12 13 printf("录入%d个分数:\n", N); 14 input(scores, N); 15 16 printf("\n输出课程分数: \n"); 17 output(scores, N); 18 19 printf("\n课程分数处理: 计算均分、排序...\n"); 20 ave = average(scores, N); 21 bubble_sort(scores, N); 22 23 printf("\n输出课程均分: %.2f\n", ave); 24 printf("\n输出课程分数(高->低):\n"); 25 output(scores, N); 26 27 return 0; 28 } 29 30 31 void input(int x[], int n) { 32 int i; 33 34 for(i = 0; i < n; ++i) 35 scanf("%d", &x[i]); 36 } 37 38 void output(int x[], int n) { 39 int i; 40 41 for(i = 0; i < n; ++i) 42 printf("%d ", x[i]); 43 printf("\n"); 44 } 45 46 double average(int x[], int n) 47 { 48 int i, sum = 0; 49 double ave; 50 51 for(i = 0;i < n; i++) 52 { 53 sum += x[i]; 54 } 55 ave = 1.0*sum/n; 56 57 return ave; 58 } 59 60 void bubble_sort(int x[], int n) 61 { 62 int i, j, t; 63 for(i = 0;i < n-1;i++) 64 { 65 for(j = 0;j < n-1-i;j++) 66 { 67 if(x[j] < x[j+1]) 68 { 69 t = x[j]; 70 x[j] = x[j+1]; 71 x[j+1] = t; 72 } 73 } 74 } 75 }
实验五 截屏
实验六 源代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 5 4 #define M 20 5 6 void output(char str[][M], int n); 7 void bubble_sort(char str[][M], int n); 8 9 int main() { 10 char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"}; 11 int i; 12 13 printf("输出初始名单:\n"); 14 output(name, N); 15 16 printf("\n排序中...\n"); 17 bubble_sort(name, N); 18 19 printf("\n按字典序输出名单:\n"); 20 output(name, N); 21 22 return 0; 23 } 24 25 26 void output(char str[][M], int n) { 27 int i; 28 29 for(i = 0; i < n; ++i) 30 printf("%s\n", str[i]); 31 } 32 33 void bubble_sort(char str[][M], int n) 34 { 35 int i, j; 36 char t[M]; 37 for(i = 0;i < n-1;i++) 38 { 39 for(j = 0;j < n-i-1;j++) 40 { 41 if(strcmp(str[j],str[j+1]) > 0) 42 { 43 strcpy(t,str[j]); 44 strcpy(str[j],str[j+1]); 45 strcpy(str[j+1],t); 46 } 47 } 48 } 49 }
实验六 截屏
试验七 源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define N 200 5 6 char x[N]; 7 8 int main() 9 { 10 int i, j; 11 12 memset(x,'\0',N); 13 while(fgets(x,N,stdin) != NULL) 14 { 15 for(i = 0;i < strlen(x)-1;i++) 16 { 17 for(j = i+1;j < strlen(x);j++) 18 { 19 if(x[i] == x[j]) 20 { 21 printf("YES\n"); 22 goto out; 23 } 24 } 25 } 26 printf("NO\n"); 27 out: 28 memset(x,'\0',N); 29 fgets(x,N,stdin); 30 } 31 32 system("pause"); 33 return 0; 34 }
试验七 截屏
实验八 源代码
1 #include <stdio.h> 2 #define N 100 3 #define M 4 4 5 void output(int x[][N], int n); 6 void rotate_to_right(int x[][N], int n); 7 8 9 int main() { 10 int t[][N] = {{21, 12, 13, 24}, 11 {25, 16, 47, 38}, 12 {29, 11, 32, 54}, 13 {42, 21, 33, 10}}; 14 15 printf("原始矩阵:\n"); 16 output(t, M); 17 18 rotate_to_right(t, M); 19 20 printf("变换后矩阵:\n"); 21 output(t, M); 22 23 return 0; 24 } 25 26 27 void output(int x[][N], int n) { 28 int i, j; 29 30 for (i = 0; i < n; ++i) { 31 for (j = 0; j < n; ++j) 32 printf("%4d", x[i][j]); 33 34 printf("\n"); 35 } 36 } 37 38 void rotate_to_right(int x[][N], int n) 39 { 40 int i, t, j; 41 for(i = 0;i < n;i++) 42 { 43 t = x[i][n-1]; 44 for(j = n-1;j >= 1;j--) 45 { 46 x[i][j] = x[i][j-1]; 47 } 48 x[i][0] = t; 49 } 50 }
实验八 截屏