实验4
实验1源代码
1 #include<stdio.h> 2 #define N 4 3 4 void test1() 5 { 6 int a[N] = {1,9,8,4}; 7 int i; 8 9 printf("sizeof(a) = %d\n",sizeof(a)); 10 for(i=1;i<N;++i) 11 printf("%p:%d\n",&a[i],a[i]); 12 printf("a = %p\n",a); 13 14 } 15 void test2() 16 { 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 printf("b = %p\n",b); 25 26 } 27 int main() 28 { 29 printf("测试1:int类型一维数组\n"); 30 test1(); 31 32 printf("\n测试2:char类型一维数组\n"); 33 test2(); 34 35 return 0; 36 }
实验1运行结果

实验1.2源代码
1 #include<stdio.h> 2 #define N 2 3 #define M 4 4 5 void test1() 6 { 7 int a[N][M] = {{1,9,8,4},{2,0,4,9}}; 8 int i,j; 9 10 printf("sizeof(a) = %d\n",sizeof(a)); 11 for(i=1;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 void test2() 22 { 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 int main() 38 { 39 printf("测试1:int类型一维数组\n"); 40 test1(); 41 42 printf("\n测试2:char类型一维数组\n"); 43 test2(); 44 45 return 0; 46 }
实验1.2运行结果

实验2源代码
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 { 12 printf("测试1:int类型一维数组\n"); 13 test1(); 14 15 printf("\n测试2:char类型一维数组\n"); 16 test2(); 17 18 return 0; 19 } 20 21 void test1() 22 { 23 char views1[N] = "hey,C,I hate u."; 24 char views2[N] = "hey,C,I love u."; 25 26 printf("交换前:\n"); 27 puts(views1); 28 puts(views2); 29 30 swap_str(views1,views2); 31 32 printf("交换后:\n"); 33 puts(views1); 34 puts(views2); 35 } 36 void test2() 37 { 38 char views[2][N] = {"hey,C,I hate u.", 39 "hey,C,I love u."}; 40 printf("交换前:\n"); 41 puts(views[0]); 42 puts(views[1]); 43 44 swap_str(views[0],views[1]); 45 46 printf("交换后:\n"); 47 puts(views[0]); 48 puts(views[1]); 49 } 50 void swap_str(char s1[N],char s2[N]) 51 { 52 char tmp[N]; 53 54 strcpy(tmp,s1); 55 strcpy(s1,s2); 56 strcpy(s2,tmp); 57 }
实验2运行结果

实验3源代码
1 #include<stdio.h> 2 #define N 80 3 4 int count(char x[]); 5 int main() 6 { 7 char words[N+1]; 8 int n; 9 10 while(gets(words)!=NULL) 11 { 12 n = count(words); 13 printf("单词数:%d\n\n",n); 14 } 15 return 0; 16 } 17 int count(char x[]) 18 { 19 int i; 20 int word_flag = 0; 21 int number = 0; 22 23 for(i = 0;x[i]!='\0';i++) 24 { 25 if(x[i] == ' '); 26 word_flag = 0; 27 else if(word_flag == 0) 28 { 29 word_flag = 1; 30 number++; 31 } 32 } 33 return number; 34 }
实验3运行结果

实验3.2源代码
1 #include <stdio.h> 2 #define N 1000 3 int main() { 4 char line[N]; 5 int word_len; 6 int max_len; 7 int end; 8 int i; 9 while (gets(line) != NULL) { 10 word_len = 0; 11 max_len = 0; 12 end = 0; 13 i = 0; 14 while (1) { 15 while (line[i] == ' ') { 16 word_len = 0; 17 i++; 18 } 19 while (line[i] != '\0' && line[i] != ' ') { 20 word_len++; 21 i++; 22 } 23 if (max_len < word_len) { 24 max_len = word_len; 25 end = i; 26 } 27 if (line[i] == '\0') 28 break; 29 } 30 31 printf("最长单词: "); 32 for (i = end - max_len; i < end; ++i) 33 printf("%c", line[i]); 34 printf("\n\n"); 35 } 36 return 0; 37 }
实验3.2运行结果

实验4源代码
1 #include<stdio.h> 2 #define N 100 3 void dec_to_n(int x,int n); 4 5 int main() 6 { 7 int x; 8 9 printf("请输入一个十进制数:"); 10 while(scanf("%d",&x)!=EOF) 11 { 12 dec_to_n(x,2); 13 dec_to_n(x,8); 14 dec_to_n(x,16); 15 printf("\n输入一个十进制整数:"); 16 } 17 return 0; 18 } 19 void dec_to_n(int x,int n) 20 { 21 char map[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; 22 char ans[N]; 23 int r; 24 int cnt = 0,i; 25 26 do 27 { 28 r=x%n; 29 ans[cnt++]=map[r]; 30 x=x/n; 31 }while(x!=0); 32 33 for(i = cnt-1;i >= 0; --i) 34 printf("%c",ans[i]); 35 36 printf("\n"); 37 }
实验4运行结果

实验5源代码
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 { 11 int scores[N]; 12 double ave; 13 14 printf("录入%d个分数:\n",N); 15 input(scores,N); 16 17 printf("\n输出课程分数:\n"); 18 output(scores,N); 19 20 printf("\n课程分数处理:计算均分、排序...\n"); 21 ave = average(scores,N); 22 bubble_sort(scores,N); 23 24 printf("\n输出课程均分:%.2f\n",ave); 25 printf("\n输出课程分数(高—>低):\n"); 26 output(scores,N); 27 28 return 0; 29 } 30 void input(int x[],int n) 31 { 32 int i; 33 34 for(i = 0;i < n; ++i) 35 scanf("%d",&x[i]); 36 } 37 void output(int x[],int n) 38 { 39 int i; 40 41 for(i = 0;i < n; ++i) 42 printf("%d",x[i]); 43 printf("\n"); 44 } 45 double average(int x[],int n) 46 { 47 double sum=0; 48 double ave; 49 int cnt; 50 51 for(cnt = 0;cnt < n; cnt++) 52 sum += x[cnt]; 53 54 ave = sum/n; 55 return ave; 56 } 57 void bubble_sort(int x[],int n) 58 { 59 int round; 60 for (int cnt = 0; cnt < n; cnt++) 61 { 62 round = n - cnt; 63 for (int cnt = 0; cnt < round; cnt++) 64 { 65 if (x[cnt] <= x[cnt + 1]) 66 { 67 int i = x[cnt]; 68 x[cnt] = x[cnt + 1]; 69 x[cnt + 1] = i; 70 } 71 } 72 } 73 }
实验5运行结果

实验6源代码
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 int main() 9 { 10 char name[][M] = { "Bob", "Bill", "Joseph", "Taylor", "George" }; 11 int i; 12 printf("输出初始名单:\n"); 13 output(name, N); 14 printf("\n排序中...\n"); 15 bubble_sort(name, N); // 函数调用 16 printf("\n按字典序输出名单:\n"); 17 output(name, N); 18 return 0; 19 } 20 void output(char str[][M], int n) 21 { 22 int i; 23 for (i = 0; i < n; ++i) 24 printf("%s\n", str[i]); 25 } 26 27 void bubble_sort(char str[][M], int n) { 28 int round; 29 for (int cnt = 0; cnt < n; cnt++) { 30 round = n - cnt; 31 for (int cnt = 0; cnt < round; cnt++) { 32 if (strcmp(str[cnt], str[cnt + 1]) > 0) { 33 char i[M]; 34 strcpy(i, str[cnt + 1]); 35 strcpy(str[cnt + 1], str[cnt]); 36 strcpy(str[cnt], i); 37 } 38 }
实验6运行结果

实验7源代码
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 char a[110]; 6 while (scanf("%s", a) != EOF) { 7 int flag = 0; 8 for (int n = 0; n < strlen(a); n++) { 9 for (int s = n + 1; s < strlen(a); s++) { 10 if (a[n] == a[s]) { 11 flag = 1; 12 break; 13 } 14 } 15 if (flag == 1) { 16 break; 17 } 18 } 19 if (flag == 1) { 20 printf("YES\n\n"); 21 } 22 else if (flag == 0) { 23 printf("NO\n\n"); 24 } 25 } 26 return 0; 27 }
实验7运行结果

实验8源代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 100 4 #define M 4 5 void output(int x[][N], int n); 6 void rotate_to_right(int x[][N], int n); 7 int main() { 8 int t[][N] = { {21, 12, 13, 24}, 9 {25, 16, 47, 38}, 10 {29, 11, 32, 54}, 11 {42, 21, 33, 10} }; 12 printf("原始矩阵:\n"); 13 output(t, M); 14 rotate_to_right(t, M); 15 printf("变换后矩阵:\n"); 16 output(t, M); 17 return 0; 18 } 19 20 21 void output(int x[][N], int n) { 22 int i, j; 23 for (i = 0; i < n; ++i) { 24 for (j = 0; j < n; ++j) 25 printf("%4d", x[i][j]); 26 printf("\n"); 27 } 28 } 29 30 void rotate_to_right(int x[][N], int n) { 31 int mid[M]; 32 for (int j = 0; j < n; j++) { 33 mid[j] = x[j][M - 1]; 34 } 35 for (int j = 0; j < n; j++) { 36 for (int i = n-1; i >= 1; i--) { 37 x[j][i] = x[j][i - 1]; 38 } 39 } 40 for (int i = 0; i < n; i++) { 41 x[i][0] = mid[i]; 42 } 43 }
实验8运行结果


浙公网安备 33010602011771号