实验5
task.1.1
代码:
#include<stdio.h> #define N 5 void input(int a[],int n); void output(int a[],int n); void find_max_min(int a[],int n,int *pmax,int *pmin); int main() { int a[N]; int max,min; printf("输入:\n"); input(a,N); printf("输出:\n"); output(a,N); find_max_min(a,N,&max,&min); printf("结果是:\n"); printf("min=%d,max=%d\n",min,max); return 0; } void input(int a[],int n) { int k; for(k=0;k<n;k++) scanf("%d",&a[k]); } void output(int a[],int n) { int k; for(k=0;k<n;k++) printf("%d",a[k]); } void find_max_min(int a[],int n,int *pmax,int *pmin) { int k; *pmax=*pmin=a[0]; for(k=1;k<n;k++) { if(a[k]>*pmax) *pmax=a[k]; else if(a[k<*pmin]) *pmin=a[k]; } }
问题:1函数功能:将2个地址分别指向数组a的最大最小值
2:pmax指向a[0],pmin指向pmax
task.1.2
代码:
#include<stdio.h> #define N 5 void input(int a[],int n); void output(int a[],int n); int *find_max(int a[],int n); int main() { int a[N]; int *pmax; printf("输入:\n"); input(a,N); printf("输出:\n"); output(a,N); pmax=find_max(a,N); printf("结果是:\n"); printf("%d",*pmax); return 0; } void input(int a[],int n) { int k; for(k=0;k<n;k++) scanf("%d",&a[k]); } void output(int a[],int n) { int k; for(k=0;k<n;k++) printf("%d",a[k]); } int *find_max(int a[],int n) { int *per=&a[0],k; for(k=0;k<n;k++) if(a[k]>*per) per=&a[k]; return per; /*int max=0,k; for(k=1;k<n;k++) { if(a[max]<a[k]) max=k; } return &a[max];*/ }
图片:
问题:find返回:最大a[]的地址.可以
task.2.1
代码:
#include <stdio.h> #include <string.h> #define N 80 int main() { char s1[N] = "Learning makes me happy"; char s2[N] = "Learning makes me sleepy"; /*s1="Learning makes me happy";*/ 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; }
图片:
问题1:80;数组结构大小;字符串长度;
2:不能;字符数组不能直接输入字符串;
3:是;
task2.2
代码:
#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; s1= "Learning makes me happy"; 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; }
图片:
问题1:字符串地址;char型地址变量所占大小;s1所指字符串大小;
2:能;2.2中是将字符串地址传递而2.1中是字符串传递
3:地址;没有
task.3
代码:
#include<stdio.h> int main() { int x[2][4]={{1,2,3,4},{5,6,7,8}}; int j,i; int *ptr1; int (*ptr2)[4]; for(ptr1=x[0]+0,i=1;ptr1<&x[0][0]+8;ptr1++,i++) { printf("%d",*ptr1); if(i%4==0) printf("\n"); } for(ptr2=x;ptr2<x+2;++ptr2) { for(j=0;j<4;++j) printf("%d",*(*ptr2+j)); printf("\n"); } return 0; }
图片:
task.4
代码:
#include <stdio.h> #define N 80 void replace(char *str, char old_char, char new_char); int main() { char text[N] = "Programming 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!='\0') { if(*str == old_char) *str = new_char; str++; } }
图片:
问题1:将输入字符串中old字符换成newd
2:能
task.5
代码:
#include <stdio.h> #define N 80 char *str_trunc(char *str, char x); int main() { char str[N]; char ch; while(printf("输入字符串: "), gets(str) != NULL) { printf("输入一个字符: "); ch = getchar(); printf("截断处理...\n"); str_trunc(str, ch); printf("截断处理后的字符串: %s\n\n", str); getchar(); } return 0; } char *str_trunc(char *str,char x) { char *p=str; while(*str++!=x); for(str--;str<p+80;str++) *str='\0'; return *str; }
图片:
问题:无法第二次输入字符;吃掉多余回车;
task.6
代码:
#include <stdio.h> #include <string.h> #define N 5 int check_id(char *str); int main() { char *pid[N] = {"31010120000721656X", "3301061996X0203301", "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(char* str) { int k; char* p = str; k = strlen(str); if (k != 18){ return 0; } for (; str < p + 17; str++) if (*str <48 || *str>57) return 0; if (*str >= 48 && *str <= 57) return 1; else if (*str == 'X') return 1; else return 0; }
图片:
task7.c
代码:
#include <stdio.h> #define N 80 void encoder(char *str, int n); void decoder(char *str, int n); int main() { char words[N]; int n; printf("输入英文文本: "); gets(words); printf("输入n: "); scanf("%d", &n); printf("编码后的英文文本: "); encoder(words, n); // 函数调用 printf("%s\n", words); printf("对编码后的英文文本解码: "); decoder(words, n); printf("%s\n", words); return 0; } void encoder(char *str, int n) { for(; *str!='\0'; str++) { if((int)*str<=122&&(int)*str>=97) { if(*str+n>122) *str=*str-25+n; else *str+=n; } if((int)*str<=90&&(int)*str>=65) { if(*str+n>90) *str=*str-25+n; else *str+=n; } } } void decoder(char *str, int n) { for(; *str!='\0'; str++) { if((int)*str<=122&&(int)*str>=97) { if(*str-n<97) *str=*str+25-n; else *str-=n; } if((int)*str<=90&&(int)*str>=65) { if(*str-n<65) *str=*str+25-n; else *str-=n; } } }
图片
task8.c
代码:
#include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { int i, j; char* temp; for (i = 1; i < argc - 1; i++) { for (j = 1; j < argc - 1 - i; j++) { if (strcmp(argv[j], argv[j + 1]) > 0) { temp = argv[j]; argv[j] = argv[j + 1]; argv[j + 1] = temp; } } } for (i = 1; i < argc; ++i) printf("hello, %s\n", argv[i]); return 0; }
图片: