实验4
实验1.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 void test2(){ 16 char b[N]={'1','9','8','4'}; 17 int i; 18 printf("sizeof(b)=%d\n",sizeof(b)); 19 for (i=0;i<N;++i) 20 printf("%p:%c\n",&b[i],b[i]); 21 printf("b=%p\n",b); 22 23 } 24 25 int main(){ 26 printf("测试1:int类型一维数组\n"); 27 test1(); 28 29 printf("\n测试2:char类型一维数组\n"); 30 test2(); 31 32 return 0; 33 }
截图

问题
1:不连续,16,不一样
2.连续,4,不一样
实验1.2
代码
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;i<N;++i) 13 printf("%p: %d\n",&a[i][j],a[i][j]); 14 15 printf("\n"); 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 printf("sizeof(b)=%d\n",sizeof(b)); 25 for (i=0;i<N;++i) 26 for(j=0;j<M;++j) 27 printf("%p:%c\n",&b[i][j],b[i][j]); 28 printf("\n"); 29 printf("b=%p\n",b); 30 printf("b[0]=%p\n",b[0]); 31 printf("b[1]=%p\n",b[1]); 32 33 34 } 35 36 int main(){ 37 printf("测试1:int类型一维数组\n"); 38 test1(); 39 40 printf("\n测试2:char类型一维数组\n"); 41 test2(); 42 43 return 0; 44 }
截图

问题
1.
实验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 printf("测试1;用两个一维char数组,实现两个字符交换\n"); 12 test1(); 13 14 printf("\n测试2;用二维char数组,实现两个字符串交换\n"); 15 test2(); 16 return 0; 17 } 18 19 void test1(){ 20 char views1[N]="hey,C,I hate u."; 21 char views2[N]="hey,C,I love u."; 22 23 printf("交换前;\n"); 24 puts(views1); 25 puts(views2); 26 27 swap_str(views1,views2); 28 29 printf("交换后; \n"); 30 puts(views1); 31 puts(views2); 32 33 swap_str(views1,views2); 34 35 printf("交换后; \n"); 36 puts(views1); 37 puts(views2); 38 39 swap_str(views1,views2); 40 41 printf("交换后; \n"); 42 puts(views1); 43 puts(views2); 44 45 } 46 void test2(){ 47 char views[2][N]={"hey,c,I hate u.", 48 "hey,c,I love u."}; 49 printf("交换前; \n"); 50 puts(views[0]); 51 puts(views[1]); 52 swap_str(views[0],views[1]); 53 printf("交换后; \n"); 54 puts(views[0]); 55 puts(views[1]); 56 57 58 } 59 void swap_str(char s1[N],char s2[N]){ 60 char tmp[N]; 61 strcpy(tmp,s1); 62 strcpy(s1,s2); 63 strcpy(s2,tmp); 64 }
截图、

实验3
3.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 }
截图

3.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 }
截图

实验4
代码
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];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 }
截图
实验5
代码
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 }
截图

实验6
代码
1 #include<stdio.h> 2 #include<string.h> 3 4 #define N 5 5 #define M 20 6 7 void output(char str[][M],int n); 8 void bubble_sort(char str[][M],int n); 9 10 int main() 11 { 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 24 return 0; 25 } 26 //函数定义 功能:按行输出二维数组中的字符串 27 void output(char str[][M],int n) 28 { 29 int i; 30 for(i = 0;i<n;++i) 31 printf("%s\n",str[i]); 32 } 33 void bubble_sort(char str[][M],int n) 34 { 35 int i,j; 36 char s[20]; 37 for(i=0;i<n-1;++i){ 38 for(j=0;j<n-1-i;++j){ 39 if(strcmp(str[j],str[j+1])>0) 40 { 41 strcpy(s,str[j]); 42 strcpy(str[j],str[j+1]); 43 strcpy(str[j+1],s); 44 } 45 } 46 } 47 }
截图

实验7
代码
1 #include<stdio.h> 2 3 int main() 4 { 5 char s[100]; 6 while(scanf("%s",&s)!=EOF) 7 { 8 int i,j,flag = 0; 9 for(i=0;s[i]!='\0';i++){ 10 for(j=i+1;s[j]!='\0';j++){ 11 if(s[i] == s[j]){ 12 flag = 1; 13 } 14 15 } 16 } 17 if(flag == 1) 18 printf("YES\n"); 19 else 20 printf("NO\n"); 21 } 22 return 0; 23 }
截图

实验8
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号