实验4 c语言数组应用编程
task1_1
task1_1源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 4 4 5 void test1(){ 6 int a[N]={1,9,8,4}; 7 int i; 8 9 printf("sizeof(a)=%d\n",sizeof(a)); 10 11 for(i=0;i<N;i++) 12 printf("%p:%d\n",&a[i],a[i]); 13 14 printf("a=%p\n",a); 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 int main() 28 { 29 printf("测试1:int类型一维数组\n"); 30 test1(); 31 32 printf("\n测试2:char类型一维数组\n"); 33 test2(); 34 35 system("pause"); 36 37 return 0; 38 39 }
结果

task1_2
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 2 4 #define M 4 5 6 void test1(){ 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=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 void test2(){ 22 char b[N][M]={{'1','9','8','4'},{'2','0','4','9'}}; 23 int i,j; 24 25 printf("sizeof(b)=%d\n",sizeof(b)); 26 for(i=0;i<N;++i) 27 for(j=0;j<M;++j) 28 printf("%p:%c\n",&b[i][j],b[i][j]); 29 printf("\n"); 30 31 printf("b=%p\n",b); 32 printf("b[0]=%p\n",b[0]); 33 printf("b[1]=%p\n",b[1]); 34 printf("\n"); 35 36 } 37 int main() 38 { 39 printf("测试1:int型两维数组"); 40 test1(); 41 42 printf("\n测试2:char型两维数组"); 43 test2(); 44 system("pause"); 45 return 0; 46 }
结果

task2
源代码
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 }
结果

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

task3_2
源代码
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 // 跳过连续空格 19 while(line[i] == ' ') { 20 word_len = 0; // 单词长度置0,为新单词统计做准备 21 i++; 22 } 23 24 // 在一个单词中,统计当前单词长度 25 while(line[i] != '\0' && line[i] != ' ') { 26 word_len++; 27 i++; 28 } 29 30 // 更新更长单词长度,并,记录最长单词结束位置 31 if(max_len < word_len) { 32 max_len = word_len; 33 end = i; // end保存的是单词结束的下一个坐标位置 34 } 35 36 // 遍历到文本结束时,终止循环 37 if(line[i] == '\0') 38 break; 39 } 40 41 // 输出最长单词 42 printf("最长单词: "); 43 for(i = end - max_len; i < end; ++i) 44 printf("%c", line[i]); 45 printf("\n\n"); 46 } 47 48 return 0; 49 }
结果

task4
源代码
1 #include <stdio.h> 2 #define N 100 3 void dec_to_n(int x, int n); // 函数声明 4 5 int main() { 6 int x; 7 8 printf("输入一个十进制整数: "); 9 while(scanf("%d", &x) != EOF) { 10 dec_to_n(x, 2); // 函数调用: 把x转换成二进制输出 11 dec_to_n(x, 8); // 函数调用: 把x转换成八进制输出 12 dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出 13 14 printf("\n输入一个十进制整数: "); 15 } 16 17 return 0; 18 } 19 20 21 void dec_to_n(int x,int n) { 22 char map[16]={"0123456789ABCDEF"}; 23 char ans[N]; 24 int r; 25 int cnt = 0,i; 26 27 do{ 28 r=x%n; 29 ans[cnt++]=map[r]; 30 x=x/n; 31 32 }while(x!=0); 33 for(i=cnt-1;i>=0;--i) 34 printf("%c",ans[i]); 35 printf("\n"); 36 37 38 }
结果

task5
1 #include <stdio.h> 2 #define N 5 3 4 // 函数声明 5 void input(int x[], int n); 6 void output(int x[], int n); 7 double average(int x[], int n); 8 void bubble_sort(int x[], int n); 9 10 int main() { 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 31 // 函数定义 32 // 输入n个整数保存到整型数组x中 33 void input(int x[], int n) { 34 int i; 35 36 for(i = 0; i < n; ++i) 37 scanf("%d", &x[i]); 38 } 39 40 // 输出整型数组x中n个元素 41 void output(int x[], int n) { 42 int i; 43 44 for(i = 0; i < n; ++i) 45 printf("%d ", x[i]); 46 printf("\n"); 47 } 48 49 double average(int x[],int n) 50 { 51 int i; 52 double avg; 53 int sum = 0; 54 for(i=0;i<n;i++){ 55 sum =sum+x[i]; 56 } 57 avg=1.0*sum/n; 58 59 60 } 61 62 63 64 void bubble_sort(int x[],int n){ 65 66 int temp; 67 int i,j; 68 for(i=0;i<n-1;i++) 69 { 70 for(j=0;j<n-1-i;j++) 71 { 72 if(x[j]<x[j+1]) 73 { 74 temp=x[j]; 75 x[j]=x[j+1]; 76 x[j+1]=temp; 77 } 78 } 79 } 80 }
结果

task6
源代码
1 #include <stdio.h> 2 #include <string.h> 3 #include<stdlib.h> 4 #define N 5 5 #define M 20 6 7 // 函数声明 8 void output(char str[][M], int n); 9 void bubble_sort(char str[][M], int n); 10 11 int main() { 12 char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"}; 13 int i; 14 15 printf("输出初始名单:\n"); 16 output(name, N); 17 18 printf("\n排序中...\n"); 19 bubble_sort(name, N); // 函数调用 20 21 printf("\n按字典序输出名单:\n"); 22 output(name, N); 23 system("pause"); 24 return 0; 25 } 26 27 // 函数定义 28 // 功能:按行输出二维数组中的字符串 29 void output(char str[][M], int n) { 30 int i; 31 32 for(i = 0; i < n; ++i) 33 printf("%s\n", str[i]); 34 } 35 36 void bubble_sort(char str[][M],int n){ 37 int i,j; 38 39 char word[M]; 40 for(i=0;i<n-1;i++) 41 { 42 43 for(j=0;j<n-i-1;j++) 44 { 45 if(strcmp(str[j],str[j+1])>0) 46 { 47 strcpy(word,str[j]); 48 strcpy(str[j],str[j+1]); 49 strcpy(str[j+1],word); 50 51 } 52 } 53 54 } 55 56 }
结果

task7
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 int re( char num[]); 5 6 int main() 7 { 8 char num[150]; 9 while(gets(num)!=NULL) 10 { 11 if(re(num)==1) 12 printf("yes\n"); 13 else 14 printf("NO\n"); 15 } 16 return 0; 17 } 18 int re(char num[]) 19 { 20 int n; 21 n=strlen(num); 22 int i,j; 23 for(i=0;i<n;i++) 24 { 25 for(j=i+1;j<n;j++) 26 if(num[i]==num[j]) 27 { 28 return 1; 29 } 30 } 31 return 0; 32 }
结果

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


浙公网安备 33010602011771号