实验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;
}
1、是连续存放,每个元素占4个
2、是连续存放,每个元素占1个
3、一样


#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; }
1、是连续,4个
2、是连续,1个
3,一样

#define _CRT_SECURE_NO_WARNINGS
#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) != 0)
    {
        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 sum =0,leapyear;
    leapyear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    switch (month-1)
    {
    case 0:sum = day; break;
    case 1:sum = day + 30 + leapyear; break;
    case 2:sum = day + 59 + leapyear; break;
    case 3: sum = day + 90 + leapyear; break;
    case 4: sum = day + 120 + leapyear; break;
    case 5: sum = day + 151 + leapyear; break;
    case 6: sum = day + 181 + leapyear; break;
    case 7: sum = day + 212 + leapyear; break;
    case 8: sum = day + 243 + leapyear; break;
    case 9: sum = day + 273 + leapyear; break;
    case 10: sum = day + 304 + leapyear; break;
    case 11: sum = day + 334 + leapyear; break;
    }
    return sum;
}

#define _CRT_SECURE_NO_WARNINGS
#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)
{
    double sum = 0;
    int i;
    for (i = 0; i < n; ++i)
        sum += x[i];
    return sum / n;
}
void sort(int x[], int n)
{
    int i,j, t;
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - 1 - i; j++)
        {
            if (x[j] < x[j + 1])
            {
                t = x[j];
                x[j] = x[j + 1];
                x[j + 1] = t;
            }
        }
    }
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.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);
    return 0;
}
void dec2n(int x, int n)
{
    int i, j = 0, t = 0;
    char p[100];
    switch (n)
    {
    case 16:
        for (i = 0; x != 0; i++,j++)
        {
            if (x % n <= 9)
                p[i] = x % n + '0';
            else
                p[i] = x % n + 'A' - 10;
            x = x / 16;
        }
        for(t=j-1;t>=0;t--)
                printf("%c", p[t]);
        
        break;
    case 8:printf("%o", x); break;
    case 2:
        for (i = 0;x/2>0; i++)
        {
            t+=pow(10,i)*( x % 2);
            x = x / 2;
        }
        printf("%d", t);

    }
}

#define _CRT_SECURE_NO_WARNINGS
#define N 100
void squ(int x[N][N], int m);
#include<stdio.h>
int main()
{
    int n,a[N][N],i,j;
    printf("Enter n: ");
    while(scanf("%d", &n)!=0)
    {squ(a, n);
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
            printf(" %d", a[i][j]);
        printf("\n");
    }
    printf("Enter n: ");
    }
    return 0;
}
void squ(int x[N][N], int m)
{
    int i,j;
    for (i = 0; i < m; i++)
        x[i][i] = i+1;
    for (i = 0; i < 5; i++)
    {
        for (j=i+1; j < m; j++)
            x[i][j] = x[i][j - 1];
    }
    for(j=0;j<5;j++)
    {
        for (i = j+1; i < m; i++)
            x[i][j] = x[i - 1][j];
    }

}

#include<stdio.h>
#define N 80
void exc(char x[], char y[]);
int main()
{
    char view1[N] = "hey,c,i hate u.";
    char view2[N] = "hey,c,i love u.";
    int b = strlen(view1),i;
    printf("Orginal views:\nViews1:hey,c,i hate u.\n View2:hey,c,i love u.\n");
    exc(view1, view2);
    printf("swapping....\n");
    printf("Views1:");
        printf("%s\n", view1);
    printf("View2:");
        printf("%s", view2);

    return 0;
}
void exc(char x[],char y[])
    {
    char t;
    int a = strlen(x),i;
    for (i = 0; i < a; i++)
    {
        t = y[i];
        y[i] = x[i];
        x[i] = t;

#define _CRT_SECURE_NO_WARNINGS
#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 t[20];
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - i - 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-06 16:51  车马萧萧  阅读(36)  评论(3)    收藏  举报