陈凯迪的实验4

 

一、实验结论

  1_1

    代码:

 1 #include <stdio.h>
 2 #define N 4
 3 int main()
 4 {
 5     int a[N]={2,0,2,2};
 6     char b[N] ={'2','0','2','2'};
 7     int i;
 8     printf("sizeof(int) = %llu\n",sizeof(int));
 9     printf("sizeof(char) = %llu\n",sizeof(char));
10     printf("\n");
11     for(i=0;i<N;i++)
12         printf("%p:%d\n",&a[i],a[i]);
13     printf("\n");
14 
15     for(i=0;i<N;i++)
16         printf("%p:%c\n",&b[i],b[i]);
17     printf("\n");
18     printf("a = %p\n",a);
19     printf("b = %p\n",b);
20     return 0;
21 }
View Code

 

    图片:

 

 

    问题:1:是、占用4个字节

       2:是、占用1个字节

       3:都是一样的


  1_2

    代码:

 1 #include <stdio.h>
 2 #define N 2
 3 #define M 3
 4 int main(){
 5     int a[N][M] ={{1,2,3},{4,5,6}};
 6     char b[N][M] ={{'1','2','3'},{'4','5','6'}};
 7     int i,j;
 8     for(i=0;i<N;i++)
 9         for(j=0;j<M;j++)
10             printf("%p:%d\n",&a[i][j],a[i][j]);
11     printf("\n");
12     for(i=0;i<N;i++)
13         for(j=0;j<M;j++)
14             printf("%p:%c\n",&b[i][j],b[i][j]);
15     printf("\n");
16 }
View Code

 

 

 

    图片:

    

      问题:1、是、占用4个字节

       2:是、占用1个字节

 

  

  2

    代码:

 1 #include<stdio.h>
 2 int days_of_year(int year,int month,int day);
 3 int main(){
 4     int year,month,day;
 5     int days;
 6     while(scanf("%d%d%d",&year,&month,&day)!=EOF)
 7     {
 8         days = days_of_year(year,month,day);
 9         printf("%4d-%02d-%02d是这一年的第%d填。\n\n",year,month,day,days);
10     }
11 
12     return 0;
13 }
14 int days_of_year(int year,int month,int day){
15     int m=1,days=0;
16     for(;m<month;m++)
17         if(m<7)
18             if(m%2==1)
19                 days+=31;
20             else
21                 if(m==2)
22                     if(year%4==0&&year%100!=0||year%400==0)
23                         days+=29;
24                     else
25                         days+=28;
26                 else 
27                     days+=30;
28         else
29             if(m%2==1)
30                 days+=30;
31             else
32                 days+=31;
33     days+=day;
34     return days;
35 }
View Code

 

 

 

    图片:

 

 

  

  3 

    代码:

 1 #include <stdio.h>
 2 #define  N 5
 3 void input(int x[],int n);
 4 void output(int x[],int n);
 5 double average(int x[],int n);
 6 void sort(int x[],int n);
 7 int main(){
 8     int scores[N];
 9     double ave;
10     printf("录入%d个个数",N);
11     input(scores,N);
12     printf("\n输出课程分数:\n");
13     output(scores,N);
14     printf("\n处理\n");
15     ave = average(scores, N);
16     sort(scores,N);
17     printf("\nave:%.2f\n",ave);
18     printf("\n sort:\n");
19     output(scores,N);
20     return 0;
21 }
22 void input(int x[], int n) { 
23     int i; 
24     for(i=0; i<n; ++i) 
25         scanf("%d", &x[i]); 
26 }
27 void output(int x[], int n) { 
28     int i; 
29     for(i=0; i<n; ++i) printf("%d ", x[i]); 
30     printf("\n"); 
31 }
32 double average(int x[],int n){
33     double a=0;
34     int i;
35     for(i=0;i<n;i++)
36         a+=x[i];
37     a/=n;
38     return a;
39 }
40 void sort(int x[],int n){
41     int i,j,t;
42     for(i=0;i<n-1;i++)
43         for(j=n-1;j>0;j--)
44             if(x[j]>x[j-1]){
45                 t = x[j];
46                 x[j]=x[j-1];
47                 x[j-1]=t;
48             }
49 
50 }
View Code

 

 

 

    图片:

 

 

  4

    代码:

 1 #include <stdio.h>
 2 void dec2n(int x, int n);
 3 int main()
 4 { 
 5     int x; 
 6     printf("输入一个十进制整数: "); 
 7     scanf("%d", &x); 
 8     dec2n(x, 2); 
 9     dec2n(x, 8); 
10     dec2n(x, 16);
11     return 0;
12 }
13 void dec2n(int x,int n){
14     int a[8],i;
15     for(i=0;i<8;i++){
16         a[i]=x%n;
17         x/=n;
18     }
19     for(;i>=0;i--)
20         if(a[i]<10)
21             printf("%d",a[i]);
22         else
23             printf("%c",a[i]+55);
24     printf("\n");
25 
26 }
View Code

 

 

 

    图片:

 

 

  5

    代码:

 1 #include <stdio.h>
 2 #define N 10
 3 int main(){
 4     int a[10][10],i,j,n;
 5     int *p=a[0];
 6     printf("enter n :");
 7     scanf("%d",&n);
 8     for(i=0;i<n;i++){
 9         for(j=0;j<n;j++){
10             a[i][j]=i<j?i+1:j+1;
11             printf("%-2d",a[i][j]);
12         }
13         printf("\n");
14     }
15     return 0;
16 }
View Code

 

 

 

    图片:

 

 

  6

    代码: 

 1 #include <stdio.h>
 2 #define N 80
 3 int main(){
 4     char s1[N]="hey,c,i hate u.";
 5     char s2[N]="hey,c,i love u.";
 6     char s[N];
 7     int i;
 8     printf("before:\n%s\n%s\n",s1,s2);
 9     i=0;
10     while((s[i]=s1[i])!='\0')
11         i++;
12     i=0;
13     while((s1[i]=s2[i])!='\0')
14         i++;
15     i=0;
16     while((s2[i]=s[i])!='\0')
17         i++;
18     printf("after:\n%s\n%s\n",s1,s2);
19     return 0;
20 
21 }
View Code

 

 

 

    图片:

 

 

  7

    代码: 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 #define M 20
 5 void bubble_sort(char str[][M], int n);
 6 int main() {
 7   char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"};
 8   int i;
 9   printf("输出初始名单:\n");
10   for (i = 0; i < N; i++)
11     printf("%s\n", name[i]);
12   printf("\n排序中...\n");
13   bubble_sort(name, N);
14   printf("\n按字典序输出名单:\n");
15   for (i = 0; i < N; i++)
16     printf("%s\n", name[i]);
17   return 0;
18 }
19 void bubble_sort(char str[][M], int n) {
20   int i, j, k, f;
21   char temp[N];
22   for (i = 0; i < n - 1; i++)
23     for (j = n - 1; j > i; j--) {
24       k = 0;
25       f = 0;
26       while (str[j][k]==str[j - 1][k] != 0) {
27         k++;
28       }
29       if (str[j][k] < str[j - 1][k])
30             f=1;
31 
32       if (f == 1) {
33         k = 0;
34         while ((temp[k] = str[j][k]) != '\0')
35           k++;
36         k = 0;
37         while ((str[j][k] = str[j - 1][k]) != '\0')
38           k++;
39         k = 0;
40         while ((str[j - 1][k] = temp[k]) != '\0')
41           k++;
42       }
43     }
44 }
View Code

 

 

 

 

    图片:

 

 

 

 二、实验总结

  学习了数组的用法以及指针ヽ(゚∀゚)メ(゚∀゚)ノ

  

 

 

 

posted on 2022-05-10 07:15  CKDDOUBI  阅读(8)  评论(1编辑  收藏  举报

导航