实验4
实验4
实验4.1源码
#include<stdio.h> #include<stdlib.h> #define N 4 void test1() { int a[N] = {1, 9, 8, 4}; int i; // 输出数组a占用的内存字节数 printf("sizeof(a) = %d\n", sizeof(a)); // 输出int类型数组a中每个元素的地址、值 for (i = 0; i < N; ++i) printf("%p: %d\n", &a[i], a[i]); // 输出数组名a对应的值 printf("a = %p\n", a); } void test2() { char b[N] = {'1', '9', '8', '4'}; int i; // 输出数组b占用的内存字节数 printf("sizeof(b) = %d\n", sizeof(b)); // 输出char类型数组b中每个元素的地址、值 for (i = 0; i < N; ++i) printf("%p: %c\n", &b[i], b[i]); // 输出数组名b对应的值 printf("b = %p\n", b); } int main() { printf("测试1: int类型一维数组\n"); test1(); printf("\n测试2: char类型一维数组\n"); test2(); system("pause"); return 0; }
实验4.1运行结果

结论:数组内的元素在内存中是连续存放,数组名代表元素的首地址
实验4.2
实验4.2源码
#include<stdio.h> #include<stdlib.h> #define N 2 #define M 4 void test1() { int a[N][M] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; int i, j; // 输出int类型二维数组a占用的内存字节数 printf("sizeof(a) = %d\n", sizeof(a)); // 输出int类型二维数组a中每个元素的地址、值 for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) printf("%p: %d\n", &a[i][j], a[i][j]); printf("\n"); // 输出int类型二维数组名a, 以及,a[0], a[1]的值 printf("a = %p\n", a); printf("a[0] = %p\n", a[0]); printf("a[1] = %p\n", a[1]); printf("\n"); } void test2() { char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}}; int i, j; // 输出char类型二维数组b占用的内存字节数 printf("sizeof(b) = %d\n", sizeof(b)); // 输出char类型二维数组b中每个元素的地址、值 for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) printf("%p: %c\n", &b[i][j], b[i][j]); printf("\n"); // 输出char类型二维数组名b, 以及,b[0], b[1]的值 printf("b = %p\n", b); printf("b[0] = %p\n", b[0]); printf("b[1] = %p\n", b[1]); } int main() { printf("测试1: int型两维数组"); test1(); printf("\n测试2: char型两维数组"); test2(); system("pause"); return 0; }
实验4.2运行结果

结论:二维数组中的元素在内存中连续存储,且二维数组名以及二维数组中第一个元素都代表了元素的起始地址
实验4.3
实验4.3源码
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 80 void swap_str(char s1[N], char s2[N]); void test1(); void test2(); int main() { printf("测试1: 用两个一维char数组,实现两个字符串交换\n"); test1(); printf("\n测试2: 用二维char数组,实现两个字符串交换\n"); test2(); system("pause"); return 0; } void test1() { char views1[N] = "hey, C, I hate u."; char views2[N] = "hey, C, I love u."; printf("交换前: \n"); puts(views1); puts(views2); swap_str(views1, views2); printf("交换后: \n"); puts(views1); puts(views2); } void test2() { char views[2][N] = {"hey, C, I hate u.", "hey, C, I love u."}; printf("交换前: \n"); puts(views[0]); puts(views[1]); swap_str(views[0], views[1]); printf("交换后: \n"); puts(views[0]); puts(views[1]); } void swap_str(char s1[N], char s2[N]) { char tmp[N]; strcpy(tmp, s1); strcpy(s1, s2); strcpy(s2, tmp); }
实验4.3运行结果

结论:如果一维数组加上[]就代表了元素,二维数组可以看成里面的元素是一维数组,所以加上[]就可以实现一维数组的交换
实验4.4
实验4.4源码
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 80 void swap_str(char s1[N], char s2[N]); void test1(); void test2(); int main() { printf("测试1: 用两个一维char数组,实现两个字符串交换\n"); test1(); printf("\n测试2: 用二维char数组,实现两个字符串交换\n"); test2(); system("pause"); return 0; } void test1() { char views1[N] = "hey, C, I hate u."; char views2[N] = "hey, C, I love u."; printf("交换前: \n"); puts(views1); puts(views2); swap_str(views1, views2); printf("交换后: \n"); puts(views1); puts(views2); } void test2() { char views[2][N] = {"hey, C, I hate u.", "hey, C, I love u."}; printf("交换前: \n"); puts(views[0]); puts(views[1]); swap_str(views[0], views[1]); printf("交换后: \n"); puts(views[0]); puts(views[1]); } void swap_str(char s1[N], char s2[N]) { char tmp[N]; strcpy(tmp, s1); strcpy(s1, s2); strcpy(s2, tmp); }
#include<stdio.h> #include<stdlib.h> #define N 1000 int main() { char line[N]; int word_len; // 记录当前单词长度 int max_len; // 记录最长单词长度 int end; // 记录最长单词结束位置 int i; while(gets(line) != NULL) { word_len = 0; max_len = 0; end = 0; i = 0; while(1) { // 跳过连续空格 while(line[i] == ' ') { word_len = 0; // 单词长度置0,为新单词统计做准备 i++; } // 在一个单词中,统计当前单词长度 while(line[i] != '\0' && line[i] != ' ') { word_len++; i++; } // 更新更长单词长度,并,记录最长单词结束位置 if(max_len < word_len) { max_len = word_len; end = i; // end保存的是单词结束的下一个坐标位置 } // 遍历到文本结束时,终止循环 if(line[i] == '\0') break; } // 输出最长单词 printf("最长单词: "); for(i = end - max_len; i < end; ++i) printf("%c", line[i]); printf("\n\n"); } system("pause"); return 0; }
实验4.4运行结果


实验4.4
实验4.4源码
//把十进制数转化为n进制数 #include<stdio.h> #include<stdlib.h> void dec_to_n(int x,int n); int main() { int x; printf("请输入一个十进制整数:"); while(scanf("%d",&x)!=EOF) { dec_to_n(x,2); dec_to_n(x,8); dec_to_n(x,16); } system("pause"); return 0; } void dec_to_n(int x,int n) { char a[100]; int i; for(i=0;x>0;i++) { if(x%n>=0&&x%n<10) {a[i]=x%n+'0';} else { a[i]=x%n-10+'A'; } x/=n; } for(i=i-1;i>=0;i--) { printf("%c",a[i]); } printf("\n"); }
实验4.4运行结果

实验4.5
实验4.5源码
#include<stdio.h> #include<stdlib.h> #define N 5 // 函数声明 void input(int x[], int n); void output(int x[], int n); double average(int x[], int n); void bubble_sort(int x[], int n); int main() { int scores[N]; double ave; printf("录入%d个分数:\n", N); input(scores, N); printf("\n输出课程分数: \n"); output(scores, N); printf("\n课程分数处理: 计算均分、排序...\n"); ave = average(scores, N); bubble_sort(scores, N); printf("\n输出课程均分: %.2f\n", ave); printf("\n输出课程分数(高->低):\n"); output(scores, N); system("pause"); return 0; } // 函数定义 // 输入n个整数保存到整型数组x中 void input(int x[], int n) { int i; for(i = 0; i < n; ++i) scanf("%d", &x[i]); } // 输出整型数组x中n个元素 void output(int x[], int n) { int i; for(i = 0; i < n; ++i) printf("%d ", x[i]); printf("\n"); } // 计算整型数组x中n个元素均值,并返回 // 补足函数average()实现 // ××× double average(int x[],int n) { double sum=0,avg; int i; for(i=0;i<n;i++) { sum+=x[i]; } avg=sum/n; return avg; } // 对整型数组x中的n个元素降序排序 // 补足函数bubble_sort()实现 // ××× void bubble_sort(int x[],int n) { int i,j,max; max=x[0]; for(i=0;i<n-1;i++) { for(j=i+1;j<n-1;j++) { if(x[i]<x[j]) { max=x[j]; x[j]=x[i]; x[i]=max; } } } }
实验4.5运行结果

实验4.6
实验4.6源码
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 5 #define M 20 // 函数声明 void output(char str[][M], int n); void bubble_sort(char str[][M], int n); int main() { char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"}; int i; printf("输出初始名单:\n"); output(name, N); printf("\n排序中...\n"); bubble_sort(name, N); // 函数调用 printf("\n按字典序输出名单:\n"); output(name, N); system("pause"); return 0; } // 函数定义 // 功能:按行输出二维数组中的字符串 void output(char str[][M], int n) { int i; for(i = 0; i < n; ++i) printf("%s\n", str[i]); } // 函数定义 // 功能:使用冒泡排序算法对二维数组str中的n个字符串按字典序排序 // 补足函数bubble_sort()实现 // ××× void bubble_sort(char str[][M],int n) { int i,j; char t[M]; for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(strcmp(str[j],str[j+1])>0) { strcpy(t,str[j+1]); strcpy(str[j+1],str[j]); strcpy(str[j],t); } } } }
实验4.6运行结果

实验4.7
实验4.7源码
#include<stdio.h> #include<stdlib.h> #define N 10000 void cmp(char *a,int n); int main() { char a[N]; while(gets(a)!=NULL) { cmp(a,N); puts(a); } system("pause"); return 0; } void cmp(char *a,int n) { int i,j; int flag=0; for(i=0;i<n&&a[i];i++) { for(j=i+1;j<n&&a[j];j++) { if(a[i]==a[j]) flag=1; } } if(flag==1) { printf("YES\n"); } else printf("NO\n"); }
实验4.7运行结果

实验4.8
实验4.8源码
#include<stdio.h> #include<stdlib.h> #define N 100 #define M 4 // 函数声明 void output(int x[][N], int n); void rotate_to_right(int x[][N], int n); int main() { int t[][N] = {{21, 12, 13, 24}, {25, 16, 47, 38}, {29, 11, 32, 54}, {42, 21, 33, 10}}; printf("原始矩阵:\n"); output(t, M); // 函数调用 rotate_to_right(t, M); // 函数调用 printf("变换后矩阵:\n"); output(t, M); // 函数调用 system("pause"); return 0; } // 函数定义 // 功能: 输出一个n*n的矩阵x void output(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) printf("%4d", x[i][j]); printf("\n"); } } // 待补足3:函数rotate_to_right()定义 // 功能: 把一个n*n的矩阵x,每一列向右移, 最右边被移出去的一列绕回左边 // xxx void rotate_to_right(int x[][M],int n) { int t; int i,j; for(i=0;i<n;i++) { t=x[i][n-1]; for(j=1;j<n-1;j++) { x[i][j+1]=x[i][j]; } x[i][0]=t; } }
实验4.8运行结果

posted on 2023-11-13 20:33 Awayfyforever 阅读(14) 评论(0) 收藏 举报
浙公网安备 33010602011771号