实验5

task1_1:
 1 #include<stdio.h>
 2 #define N 5
 3 void input(int x[],int n);
 4 void output(int x[],int n);
 5 void find_min_max(int x[],int n,int *pmin,int *pmax);
 6 int main(){
 7  int a[N];
 8  int min,max;
 9  
10  printf("录入%d个数据:\n",N);
11  input(a,N);
12  
13  printf("数据是:\n");
14  output(a,N);
15  
16  printf("数据处理。。。\n");
17  find_min_max(a,N,&min,&max);
18  printf("输出结果:\n");
19  printf("min=%d,max=%d\n",min,max);
20  
21  return 0;
22 }
23 void input(int x[],int n){
24  int i;
25  
26  for(i=0;i<n;++i){
27   scanf("%d",&x[i]);
28  }
29 }
30 void output(int x[],int n){
31  int i;
32  for(i=0;i<n;++i){
33   printf("%d",x[i]);
34  printf("\n");
35  }
36 }
37 void find_min_max(int x[],int n,int *pmin,int *pmax){
38  int i;
39   *pmin=*pmax=x[0];
40   for(i=0;i<n;++i){
41   if(x[i]<*pmin)
42    *pmin=x[i];
43   
44   else if(x[i]>*pmax)
45    *pmax=x[i];
46  }
47 }

Q1:寻找最大最小值

Q2:指向x[0]的地址

task1_2:
 1 #include<stdio.h>
 2 #define N 5
 3 void input(int x[],int n);
 4 void output(int x[],int n);
 5 void *find_max(int x[],int n);
 6 int main(){
 7  int a[N];
 8  int *pmax;
 9  
10  printf("录入%d个数据:\n",N);
11  input(a,N);
12  
13  printf("数据是:\n");
14  output(a,N);
15  
16  printf("数据处理。。。\n");
17  pmax=find_max(a,N);
18  
19  printf("输出结果:\n");
20  printf("max=%d\n",*pmax);
21  
22  return 0;
23 }
24 void input(int x[],int n){
25  int i;
26  
27  for(i=0;i<n;++i){
28   scanf("%d",&x[i]);
29  }
30 }
31 void output(int x[],int n){
32  int i;
33  
34  for(i=0;i<n;++i){
35   printf("%d",x[i]);
36  printf("\n");
37  }
38 }
39 void *find_max(int x[],int n){
40  int i;
41  int max_index=0;
42  
43  for(i=0;i<n;++i){
44    
45   if(x[i]>x[max_index])
46    max_index=i;
47  }
48  return &x[max_index];
49 }

Q1:返回数组中最大值的地址
Q2:*ptr的定义不应该是地址
task2_1:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define N 80
 4 int main(){
 5  char s1[N]="Learning makes me happy";
 6  char s2[N]="Learning makes me sleepy";
 7  char tmp[N];
 8  
 9  printf("sizeof(s1)vs.strlen(s1):\n");
10  printf("sizeof(s1)=%d\n",sizeof(s1));
11  printf("strlen(s1)=%d\n",strlen(s1));
12  
13  printf("\nbefore swap:\n");
14  printf("s1:%s\n",s1);
15  printf("s2:%s\n",s2);
16  
17  printf("\nswapping...\n");
18  strcpy(tmp,s1);
19  strcpy(s1,s2);
20  strcpy(s2,tmp);
21  
22  printf("\nafter swap:\n");
23  printf("s1:%s\n",s1);
24  printf("s2:%s\n",s2);
25  
26  return 0;
27 }

Q1:s1大小为80个字节;sizeof 计算占用字节大小,strlen计算数组长度
Q2:数组要用for循环赋值
Q3:交换
tas2_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;
    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;
}

 

Q1:s1存放字符串的地址用的内存空间,strlen计算字用的内存空间,strlen计算字符串长度
Q2:可以;task2_1中是每个字符单独存放,task2_2中是字符串整体存放
Q3:没有;只是交换地址
task3:
 1 #include <stdio.h>
 2 
 3 int main() {
 4     int x[2][4]={{1,9,8,4},{2,0,4,9}};
 5     int i,j; 
 6     int *ptr1;
 7     int (*ptr2)[4];
 8     
 9     printf("输出1:使用数组名、下标直接访问二维数组元素\n");
10     for(i=0;i<2;++i){
11         for(j=0;j<4;++j)
12             printf("%d",x[i][j]);
13         printf("\n");
14     }
15     
16     printf("\n输出2:使用指针变量ptr1(指向元素)间接访问\n");
17     for(ptr1=&x[0][0,i=0];ptr1<&x[0][0]+8;++ptr1,++i){
18         printf("%d",*ptr1);
19         
20         if((i+1)%4==0)
21             printf("\n");
22              
23     }
24     printf("\n输出3:使用指针变量ptr2(指向一维数组)间接访问\n");
25     for(ptr2=x;ptr2<x+2;++ptr2){
26         for(j=0;j<4;++j)
27             printf("%d",*(*ptr2+j));
28         printf("\n");
29     }
30     
31     return 0;
32 }

 

 Q1:*ptr1中ptr表示x[0][0]

Q2:(*ptr2)[4]中ptr表示x[0]

 task4:

 1 #include <stdio.h>
 2 #define N 50
 3 
 4 void replace(char *str,char old_char,char new_char){
 5     int i;
 6     
 7     while(*str){
 8         if(*str==old_char){
 9             *str=new_char;
10         }
11         str++;
12     }
13 } 
14 
15 int main() {
16     char text[N]="Programming is difficult or not,it is a question.";
17     
18     printf("原始文本:\n");
19     printf("%s\n",text);
20     
21     replace(text,'i','*');
22     
23     printf("处理后文本:\n");
24     printf("%s\n",text);
25     
26     
27     return 0;
28 }

 

 Q1:将原字符串中指定的字符改成指定字符

Q2:可以

task5:

 1 #include <stdio.h>
 2 #define N 50
 3 
 4 char *str_trunc(char *str,char x){
 5     char *p=str;
 6     
 7     while(*p){
 8         if(*p==x){
 9             *p='\0';
10             break;
11         }
12         p++;
13     }
14     return str;
15 }
16 int main() {
17     char str[N];
18     char ch;
19     
20     while(printf("输入字符串:"),gets(str)!=NULL){
21         printf("输入一个字符:");
22         ch=getchar();
23         
24         printf("截断处理。。。\n");
25         str_trunc(str,ch);
26         
27         printf("截断处理后的字符串:%s\n\n",str);
28         getchar();
29          
30     }
31     return 0;
32 }

 

 Q1:无法输入字符串

Q2:存储换行符

task6:

 1 #include <stdio.h>
 2 #include<string.h>
 3 #define N 5
 4 
 5 int check_id(char *str){
 6     int len;
 7     int i;
 8     len=strlen(str);
 9     if(len!=18)
10         return 0;
11     for(i=0;i<len-1;i++){
12         if(str[i]<'0'||str[i]>'9')
13             return 0;
14     }
15     if(str[len-1]<'0'||str[len-1]>'9'){
16         if(str[len-1]!='X')
17             return 0;
18     }
19     return 1;
20     
21 }
22 
23 int main() {
24     char *pid[N]={"31010120000721656X","3301061996X0203301","53010220051126571",
25                   "510104199211197977","53010220051126133Y"};
26     int i;
27     for(i=0;i<N;++i)
28         if(check_id(pid[i]))
29             printf("%s\tTrue\n",pid[i]);
30         else
31             printf("%s\tFalse\n",pid[i]);
32                 
33     return 0;
34 }

 

 

task7:

 1 #include <stdio.h>
 2 #define N 80
 3 
 4 void decoder(char *str,int n){
 5     while (*str!= '\0') {
 6         if ((*str >= 'a' && *str <= 'z')) {
 7             *str = (*str - 'a' - n + 26) % 26 + 'a';
 8         } else if ((*str >= 'A' && *str <= 'Z')) {
 9             *str = (*str - 'A' - n + 26) % 26 + 'A';
10         }
11         str++;
12     }
13     
14 } 
15 
16 void encoder(char *str,int n){
17     while (*str!= '\0') {
18         if ((*str >= 'a' && *str <= 'z')) {
19             *str = (*str - 'a' + n) % 26 + 'a';
20             if (*str < 'a') {
21                 *str += 26;
22             }
23         } else if ((*str >= 'A' && *str <= 'Z')) {
24             *str = (*str - 'A' + n) % 26 + 'A';
25             if (*str < 'A') {
26                 *str += 26;
27             }
28         }
29         str++;
30     }
31     
32 }
33 
34 
35 int main() {
36     char words[N];
37     int n;
38     
39     printf("输入英文文本:");
40     gets(words);
41     
42     
43     printf("输入n:");
44     scanf("%d",&n);
45     
46     printf("编码后的英文文本:");
47     encoder(words,n);
48     printf("%s\n",words);
49     
50     printf("对编码后的英文文本解码:");
51     decoder(words,n);
52     printf("%s\n",words); 
53     
54     return 0;
55 }

 

 

task8:

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 int main(int argc, char *argv[]) {
 4     int i,j;
 5     char* temp;
 6     for(i = 1; i < argc; i++){
 7         for(j=1;j<argc-i;j++){
 8             if(strcmp(argv[j],argv[j+1])>0){
 9                 temp = argv[j];
10                 argv[j] = argv[j+1];
11                 argv[j+1] = temp;
12             }
13 
14         }
15     }
16 
17     for(i = 1; i < argc; ++i)
18         printf("hello, %s\n", argv[i]);
19 
20     return 0;
21 }

 

posted @ 2024-12-08 18:19  jjjrrhhhhh  阅读(5)  评论(0编辑  收藏  举报