实验4

#include<stdio.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:%d\n",&a[i],a[i]);
    }
    
    printf("\n");
    
    for(i=0;i<N;++i)
    {
        printf("%p:%c\n",&b[i],b[i]);
    }
    
    printf("\n");
    
    printf("a=%p\n",a);
    printf("b=%p\n",b);
    
    return 0;
} 

#include<stdio.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:%c\n",&b[i][j],b[i][j]);
    
    return 0;
} 

#include<stdio.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);
    }
    
    return 0;
}

int days_of_year(int year,int month,int day)
{
    int i,days=0;
    int mon[12]={0,31,28,31,30,31,30,31,31,30,31,30};
    for(i=1;i<month;i++)
    {
        if((year%4==0)&&(year%100!=0)||year%400==0)
        {
            mon[2]++;
        }
        days+=mon[i];
    }
    days+=day;
    return days;
}

#include<stdio.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);
    
    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 sum=0,i;double s;
    for(i=0;i<n;++i)
    {
        sum+=x[i];
    }
    s=1.0*sum/n;
    return s;
}

void sort(int x[],int n)
{
    int i,j,k;
    for(i=0;i<n;++i)
    {
        for(j=0;j<i;j++)
        {
                    if(x[i]>x[j])
        {
            k=x[i];
            x[i]=x[j];
            x[j]=k;
        }
        }
    }
}

#include<stdio.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);
    printf("\n");
    
    return 0;
} 

void dec2n(int x,int n)
{
    int a,b;char m[16]={"0123456789ABCDEF"};
    a=x/n;
    b=x%n;
    if(a==0)
    {
        printf("%c",m[b]);
    }
    else
    {
        dec2n(a,n);
        printf("%c",m[b]);
    }
}

#include<stdio.h>

void ulmatrix(int n);

int main()
{
    int n;int a[n][n];
    printf("Enter n: ");
    while(scanf("%d",&n)!=EOF)
    {
        ulmatrix(n);
        printf("\nEnter n: ");
    }
    
    return 0;
} 

void ulmatrix(int n)
{
    int i,j,a[n][n];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i<=j)
            {
                a[i][j]=i+1;
            }
            else
            {
                a[i][j]=j+1;
            }
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}

#include<stdio.h>
#define N 80

int main()
{
    char views1[N]="hey,c,i hate u.";
    char views2[N]="hey,c,i love u.";
    char view[N];int i=0;
    
    printf("original views:\n");
    printf("views1: %s\n",views1);
    printf("views2: %s\n",views2);
    
    for(i=0;i<N;i++)
    {
        view[i]=views1[i];
        views1[i]=views2[i];
        views2[i]=view[i];
    }
    
    printf("\nswapping...\n\n");
    printf("views1: %s\n",views1);
    printf("views2: %s\n",views2);
    
    return 0;
} 

#include<stdio.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]);
        
    return 0;
}

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

 

posted @ 2022-05-08 13:10  施乐乐  阅读(7)  评论(3编辑  收藏  举报