实验四

#include<stdio.h>
#include<stdlib.h>
#define N 4

int main()
{    int a[N]={2,0,2,2};
    char b[N]={'2','0','2','2'};
    int i;

    printf("sizeof(int)= %d\n",sizeof(int));
    printf("sizeof(char)= %d\n",sizeof(char));
    printf("\n");

    for(i=0;i<N;i++)
    printf("%p: %c\n",&b[i],b[i]);
    printf("\n");

    for(i=0;i<N;i++)
        printf("%p; %c\n",&a[i],a[i]);
    printf("\n");

    printf("a= %p\n",a);
    printf("b=%p\n",b);


    system("pause");
}

1.是连续存放的,每个元素占4个内存字节单元

2.是连续存放的,每个元素占1个内存字节单元

3.是一样的,是一样的

  

#include<stdio.h>
#include<stdlib.h>
#define N 2
#define M 3

int main()
{    int a[N][M]={{1,2,3},{4,5,6}};
    char b[N][M]={{'1','2','3'},{'4','5','6'}};
    int i,j;

    for(i=0;i<N;i++)
        for(j=0;j<M;j++)
            printf("%p: %d\n",&a[i][j],a[i][j]);

    printf("\n");

    for(i=0;i<N;i++)
        for(j=0;j<M;j++)
            printf("%p: %d\n",&b[i][j],b[i][j]);

    system("pause");
}

  是,4个

      是,1个

#include <stdio.h>
#include<stdlib.h>

int days_of_year(int year, int month, int day); 
int main()
{
    int year, month, day;
    int days;
    while(scanf("%d%d%d", &year, &month, &day) != EOF) 
    {
    days = days_of_year(year, month, day);
    printf("%4d-%02d-%02d是这一年的第%d天.\n\n", year, month, day, days);
    }
    system("pause");
}
int days_of_year(int year,int month,int day)
{ int i,s=0;
    int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 for(i=1;i<month;i++)
     s+=days[i];

   s+=day;
   if(year %4==0||year%100==0&&year%400!=0)
    s+=1;

   return s;


}

#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 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);
    sort(scores, N);
    printf("\n输出课程均分: %.2f\n", ave);
    printf("\n输出课程分数(高->低):\n");
    output(scores, N);
    system("pause");
    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");




}
double average (int x[],int n)
{    int i;
    double s=0;
    for(i=0;i<n;i++)
        s+=x[i];

    return s/n;

}
void sort (int x[],int n)
{ int i,j,t;

    for(i=0;i<n;i++)
    {for(j=0;j<n-1;j++)
    {if(x[j]<x[j+1])
        {t=x[j];
        x[j]=x[j+1];
        x[j+1]=t;
        }
    }
    }
}

#include <stdio.h>
#include <stdlib.h>

void dec2n(int x, int n); 
int main()
{
    int x;
    printf("输入一个十进制整数: ");
    scanf("%d", &x);

    dec2n(x, 2); 
    printf("\n");
    dec2n(x, 8); 
    printf("\n");
    dec2n(x, 16); 

    system("pause");
}

void dec2n(int x,int n)
{ int a[80];
  int  i=1,k, j=0;
  while(x!=0)
  {i=x%n;
   a[j++]=i;
   x=x/n;}
for(k=j-1;k>=0;k--)
{if(a[k]>9&&a[k]<16)
    printf("%c",a[k]-10+'A');
else
    printf("%d",a[k]);
}
}

#include<stdio.h>
#include<stdlib.h>
void blank(int k,int m);
int main()
{ int i,j,n;
    while(scanf("%d",&n)!=EOF)
  {for(i=1;i<=n;i++)
  {for(j=1;j<=i;j++)
  {printf("%d  ",j);}
   
  blank(n-i,i);
  printf("\n");

  }



  }
system("pause");
}
void blank(int k,int m)
{int i;
 for(i=1;i<=k;i++)
     printf("%d  ",m);






}

#include <stdio.h>
#include <stdlib.h>

#define N 80
int main()
{
    char views1[N] = "hey, c, i hate u.";
    char views2[N] = "hey, c, i love u.";
    char t[N];
    int i;
    printf("original views:\n");
    printf("views1:");
    for(i=0;views1[i]!='\0';i++)
        printf("%c",views1[i]);
    printf("\n");

    printf("views2:");
    for(i=0;views1[i]!='\0';i++)
        printf("%c",views2[i]);
    printf("\n");

    for(i=0;i<N;i++)
    {t[i]=views1[i];
    views1[i]=views2[i];
    views2[i]=t[i];}
    printf("swapping\n");
    printf("views1:");
    for(i=0;views1[i]!='\0';i++)
        printf("%c",views1[i]);
    printf("\n");

    printf("views2:");
    for(i=0;views1[i]!='\0';i++)
        printf("%c",views2[i]);
    printf("\n");

    system("pause");
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 5
#define M 20

void bubble_sort(char str[][M], int n); 
int main()
{
    char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"};
    int i;
    printf("输出初始名单:\n");
    for (i = 0; i < N; i++)
    printf("%s\n", name[i]);

    printf("\n排序中...\n");
    bubble_sort(name, N); 

    printf("\n按字典序输出名单:\n");
    for (i = 0; i < N; i++)
    printf("%s\n", name[i]);

    system("pause");
}
void bubble_sort(char str[][M],int n)
{ int i,j;
  char t[M];
    for(i=0;i<N;i++)
    {
        for(j=0;j<N-1;j++)
    { if(strcmp(str[j],str[j+1])>0)
        {strcpy(t,str[j]);
        strcpy(str[j],str[j+1]);
        strcpy(str[j+1],t);
        
        }
    
        }
    }

}

 

posted @ 2022-05-07 11:25  !!!!???  阅读(17)  评论(3编辑  收藏  举报