实验四 数组

#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));
    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;
}
//问题一:是;每个元素占4个
//问题二:是;每个元素占1个

#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) !=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 yuefen[13], i,j,s=0;
    for (i = 1; i < 13; i++)
    {
        if (i == 2)
        {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                yuefen[i] = 29;
            else
                yuefen[i] = 28;
        }
        if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
            yuefen[i] = 31;
        if (i == 4 || i == 6 || i == 9 || i == 11)
            yuefen[i] = 30;
    }
    for (j = 1; j < month; j++)
    {
        s = s + yuefen[j];
    }
    s = s + day;
    return s;
}

 

#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)
{
    int i,s=0;
    double ave;
    for (i = 0; i < n;i++)
        s = s + x[i];
    ave = (double)s / n;
    return ave;
}
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 + 1];
                x[j + 1] = x[j];
                x[j] = t;
            }
        }
    }
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define N 80
void dec2n(int x, int n);
int main()
{
    int x;
    printf("输入一个十进制整数: ");
    scanf("%d", &x);
    dec2n(x, 2);
    dec2n(x, 8);
    dec2n(x, 16);
    return 0;
}
void dec2n(int x, int n)
{
    char a[N];
    int    i,j=0,b;
    for (i = 0; x != 0; i++,j++)
    {
        b = x % n;
        if (b < 10)
        {
            a[i] = '0' + b;
        }
        else
        {
            a[i] = 'A' + b - 10;
        }
        x = x / n;
    }
    for (i = j-1; i >=0; i--)
    {
        printf("%c", a[i]);
    }
    printf("\n");

}

#include<stdio.h>
int main()
{
    int n, i, j;
    while (scanf_s("%d", &n) != 0)
    {
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                if (i == 1)
                {
                    printf("%d ", 1);
                }
                else
                {
                    if (j <= i)
                    {
                        printf("%d ", j);
                    }
                    else
                    {
                        printf("%d ", i);
                    }
                }
            }
            printf("\n");
        }
    }
    return 0;
}

#include<stdio.h>
#define N 80
int main()
{
    char views1[N] = "hey,c,i hate u.";
    char views2[N] = "hey,c,i love you.";
    char views3[N];
    int i;
    for (i = 0; views1[i] != '\0'; i++)
    {
        views3[i] = views1[i];
    }
    views3[i] = '\0';
    for (i = 0; views2[i] != '\0'; i++)
    {
        views1[i] = views2[i];
    }
    views1[i] = '\0';
    for (i = 0; views3[i] != '\0'; i++)
    {
        views2[i] = views3[i];
    }
    views2[i] = '\0';
    puts(views1);
    printf("\n");
    puts(views2);
    return 0;
}

#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,k;
    char t[M];
    for (i = 0; i < N - 1; i++)
    {
        for (j = 0; j < N - 1 - i; j++)
        {
            k = strcmp(str[j], str[j + 1]);
            if (k > 0)
            {
                strcpy(t, str[j]);
                strcpy(str[j], str[j + 1]);
                strcpy(str[j + 1], t);
            }
        }
    }
}

 

posted @ 2022-05-03 20:19  唐卉宁  阅读(10)  评论(1编辑  收藏  举报