实验5_c语言指针应用编程
task1_1
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #define N 5 void input(int x[], int n); void output(int x[], int n); void find_min_max(int x[], int n, int* pmin, int* pmax); int main() { int a[N]; int min, max; printf("录入%d个数据:\n", N); input(a, N); printf("数据是:\n"); output(a, N); printf("数据处理...\n"); find_min_max(a, N, &min, &max); printf("输出结果:\n"); printf("min=%d,max=%d\n", min, max); return 0; } void input(int x[],int n) { int i; for (i = 0; i < n; ++i) scanf("%d", &x[i]); } void output(int x[], int n) { int i; for (i = 0; i < n; ++i) printf("%d", x[i]); printf("\n"); } void find_min_max(int x[], int n, int* pmin, int* pmax) { int i; *pmin = *pmax = x[0]; for (i = 1; i < n; ++i) if (x[i] < *pmin) *pmin = x[i]; else if (x[i] > *pmax) *pmax = x[i]; } /*函数 find_min_max 实现的功能是:找到数组x[N]中最小和最大的数,并将它们的地址保存并返回*/ /*pmin-->pmax,pmax-->x*/
函数 find_min_max 实现的功能是:找到数组x[N]中最小和最大的数,并将它们的地址保存并返回
pmin-->pmax,pmax-->x
task1_2
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #define N 5 void input(int x[], int n); void output(int x[], int n); int* find_max(int x[], int n); int main() { int a[N]; int* pmax; printf("录入%d个数据:\n", N); input(a, N); printf("数据是: \n"); output(a, N); printf("数据处理...\n"); pmax = find_max(a, N); printf("输出结果:\n"); printf("max = %d\n", *pmax); return 0; } void input(int x[], int n) { int i; for (i = 0; i < n; ++i) scanf("%d", &x[i]); } void output(int x[], int n) { int i; for (i = 0; i < n; ++i) printf("%d ", x[i]); printf("\n"); } //int* find_max(int x[], int n) //{ // int max_index = 0; // int i; // for (i = 1; i < n; ++1) // if (x[i] > x[max_index]) // max_index = i; // return &x[max_index]; //} /*返回最大数的地址*/ /*可以的,ptr保存的还是最大数的地址*/ int* find_max(int x[], int n) { int* ptr = &x[0]; int i; for (i = 1; i < n; ++i) if (x[i] > *ptr) ptr = &x[i]; return ptr; }
返回最大数的地址
可以的,ptr保存的还是最大数的地址
task2_1
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #define N 80 int main() { char s1[] = "Learning makes me happy"; char s2[] = "Learning makes me sleepy"; char tmp[N]; printf("sizeof(s1) vs. strlen(s1):\n"); printf("sizeof(s1) = %d\n", sizeof(s1)); printf("strlen(s1) = %d\n", strlen(s1)); printf("\nbefore swap:\n"); printf("s1: %s\n", s1); printf("s2: %s\n", s2); printf("\nswapping...\n"); strcpy(tmp, s1); strcpy(s1, s2); strcpy(s2, tmp); printf("\nafter swap: \n"); printf("s1: %s\n", s1); printf("s2: %s\n", s2); return 0; }
sizeof计算的是字符串带上'\0'的结果,而strlen是不带'\0'的结果
不行,s1是一个地址常量,无法给一个无符号整型常量赋值
应该没有,显示栈堆调用异常Run-Time Check Failure #2 - Stack around the variable 's1' was corrupted.
task2_2
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #define N 80 int main() { //const char *s1 = "Learning makes me happy"; const char* s1; s1 = "Learning makes me happy"; const char *s2 = "Learning makes me sleepy"; const char *tmp; printf("sizeof(s1) vs. strlen(s1): \n"); printf("sizeof(s1) = %d\n",sizeof(s1)); printf("strlen(s1) = %d\n",strlen(s1)); printf("\nbefore swap: \n"); printf("s1: %s\n", s1); printf("s2: %s\n", s2); printf("\nswapping...\n"); tmp = s1; s1 = s2; s2 = tmp; printf("\nafter swap: \n"); printf("s1: %s\n", s1); printf("s2: %s\n", s2); return 0; }
s1中存放"Learning makes me happy"的初始地址,sizeof(s1) 计算的是存放地址所需要的内存(8),strlen(s1) 统计的是s1所指向的const char类型的大小
可以的,前者表示的是s1数组的首地址(地址常量无法改变),这里的s1表示的是一个指针,让这个指针指向const char类型的首地址
交换的是指针的指向,实际的字符串在内存存储单元内没有交换
task3
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int main() { int x[2][4] = { {1,9,8,4},{2,0,4,9} }; int i, j; int* ptr1; int(*ptr2)[4]; printf("输出1:使用数组名、下标直接访问二维数组元素\n"); for (i = 0; i < 2; ++i) { for (j = 0; j < 4; ++j) printf("%d", x[i][j]); printf("\n"); } printf("\n输出2:使用指向元素的指针变量p间接访问二维数组元素\n"); for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) { printf("%d", *ptr1); if ((i + 1) % 4 == 0) printf("\n"); } printf("\n输出3:使用指向一维数组的指针变量q间接访问二维数组元素\n"); for (ptr2 = x; ptr2 < x + 2; ++ptr2) { for (j = 0; j < 4; ++j) printf("%d", *(*ptr2 + j)); printf("\n"); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #define N 80 void replace(char* str, char old_char, char new_char); int main() { char text[N] = "C programme is difficult or not, it is a question."; printf("原始文本:\n"); printf("%s\n", text); replace(text, 'i', '*'); printf("处理后的文本:\n"); printf("%s\n", text); return 0; } void replace(char* str, char old_char, char new_char) { int i; while (/**str*/*str !='\0') { if (*str == old_char) *str = new_char; str++; } } /*replace功能:将原始文本中的'i'全部替换为‘*’*/ /*当然可以啊!*/
replace功能:将原始文本中的'i'全部替换为‘*’
当然可以啊!
task4_2
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define N 80 void str_trunc(char* str, char x); int main() { char str[N]; char ch; printf("输入字符串: "); fgets(str,80,stdin); printf("输入一个字符: "); ch = getchar(); printf("截断处理...\n"); str_trunc(str, ch); printf("截断处理后的字符串: %s\n", str); } void str_trunc(char* str, char x) { while (*str) { if (*str == x) { *str = '\0'; *(str + 1) = '\0'; } str++; } }
task5_1
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> void sort(const char * name[], int n); int main() { const char* course[4] = { "C Program","C++ Object Oriented Program","Operating System","Data Structure and Algorithms" }; int i; sort(course, 4); for (i = 0; i < 4; i++) printf("%s\n", course[i]); return 0; } void sort(const char* name[], int n) { int i, j; const char* tmp; for(i=0;i<n-1;++i) for(j=0;j<n-1-i;++j) if (strcmp(name[j], name[j + 1]) > 0) { tmp = name[j]; name[j] = name[j + 1]; name[j + 1] = tmp; } }
task5_2
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> void sort(const char* name[], int n); int main() { const char* course[4] = { "C Program","C++ Object Oriented Program","Operating System","Data Structure and Algorithms" }; int i; sort(course, 4); for (i = 0; i < 4; i++) printf("%s\n", course[i]); return 0; } void sort(char* name[], int n) { int i, j, k; char* tmp; for (i = 0; i < n - 1; i++) { k = i; for (j = i + 1; j < n; j++) if (strcmp(name[j], name[k]) < 0) k = j; if (k != i) { tmp = name[i]; name[i] = name[k]; name[k] = tmp; } } }
交换的是指针变量的值
task6
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#define N 5
int check_id(const char* str);
int main()
{
const char* pid[N] = {
"31010120000721656X",
"330106199609203301",
"53010220051126571",
"510104199211197977",
"53010220051126133Y"
};
int i;
for (i = 0; i < N; ++i)
{
if (check_id(pid[i]))
printf("%s\tTrue\n", pid[i]);
else
printf("%s\tFalse\n", pid[i]);
}
return 0;
}
int check_id(const char* str) {
int i = 0;
while (*str)
{
if (!((*str >= '0' && *str <= '9') || *str == 'X'))
return 0;
str++;
i++;
}
if (i != 18)return 0;
return 1;
}
task7
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define N 80 void encoder(char* str); // 函数声明 void decoder(char* str); // 函数声明 int main() { char words[N]; printf("输入英文文本: "); fgets(words,N,stdin); printf("编码后的英文文本: "); encoder(words); // 函数调用 printf("%s\n", words); printf("对编码后的英文文本解码: "); decoder(words); // 函数调用 printf("%s\n", words); return 0; } /*函数定义 功能:对s指向的字符串进行编码处理 编码规则: 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 其它非字母字符,保持不变 */ void encoder(char* str) { while (*str) { if (*str == 'Z') { *str = 'A'; str++; continue; } if (*str == 'z') { *str = 'a'; str++; continue; } if (*str < 'z' && *str >= 'a') *str = *str + 1; if (*str < 'Z' && *str >= 'A') *str = *str + 1; str++; } } /*函数定义 功能:对s指向的字符串进行解码处理 解码规则: 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 其它非字母字符,保持不变 */ void decoder(char* str) { while (*str) { if (*str == 'A') { *str = 'Z'; str++; continue; } if (*str == 'a') { *str = 'z'; str++; continue; } if (*str <= 'z' && *str > 'a') *str = *str - 1; if (*str <= 'Z' && *str > 'A') *str = *str - 1; str++; }
task8
#include <stdio.h> #include<string.h> int main(int argc, char *argv[]) { int i,j,k; char *t; for(i=1;i<argc-1;++i){ k=i; for(j=i+1;j<argc;++j) if(strcmp(argv[j],argv[k])<0) k=j; if(k!=1){ t=argv[i]; argv[i]=argv[k]; argv[k]=t; } } for(i = 1; i < argc; ++i) printf("hello, %s\n", argv[i]); return 0; }