clm888

导航

实验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个

 

 

#include <stdio.h>
int days_of_year(int year, int month, int day); 
int main()
{
    int year, month, day;
    int days;
    while (scanf_s("%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 m[13], i,s=0;
    for (i = 1; i <= 12; i++)
    {
        if (i == 4 || i == 6 || i == 9 || i == 11)
            m[i] = 30;
        if (i == 2)
        {
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                m[i] = 29;
            else
                m[i] = 28;
        }
        else
            m[i] = 31;
    }
    for (i = 1; i < month; i++)
    {
        s += m[i];
    }
    return (s + day);
}

 

 

#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_s("%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,t,j;
    for (j = 1; j <= n * (n - 1) / 2; j++)
    {
        for (i = 0; i < n - 1; i++)
        {
            if (x[i] < x[i + 1])
            {
                t = x[i + 1];
                x[i + 1] = x[i];
                x[i] = t;
            }
        }
    }
}

 

 

#include <stdio.h>
void dec2n(int x, int n); // 函数声明
int main()
{
    int x;
    printf("输入一个十进制整数: ");
    scanf_s("%d", &x);
    dec2n(x, 2); 
    dec2n(x, 8); 
    dec2n(x, 16); 
    return 0;
}

void dec2n(int x, int n)
{
    int i, j;
    int a[20];
    for (i = 0; i < 20; i++)
    {
        if (x / n != 0)
        {
            a[i] = x % n;
            x = x / n;
        }
        else  
        {
            a[i] = x;
            break;
        }
        
    }
    for (j = i; j >= 0; j--)
    {
        if (n == 16 && a[j] >= 10)
        {
    
            switch (a[j] % 10)
            {
            case 0:printf("A"); break;
            case 1:printf("B"); break;
            case 2:printf("C"); break;
            case 3:printf("D"); break;
            case 4:printf("E"); break;
            case 5:printf("F"); break;
            }
            
        }
    
        else
            printf("%d", a[j]);
    }
    printf("\n");
}

 

 

#include<stdio.h>
#include<stdlib.h>
#define N 50
void juzhen(int x[N][N], int n);
int main()
{
    int n, l,c,a[N][N];
    printf("Enter n:");
    while (scanf_s("%d", &n) != 0)
    {
        juzhen(a, n);
        for (l = 1; l <= n; l++)
        {
            for (c = 1; c <= n; c++)
            {
                printf("%d ", a[l][c]);
            }
            printf("\n");
        }
        printf("\nEnter n:");
    }
    return 0;
}
void juzhen(int x[N][N], int n)
{
    int i, j;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            if (j <= i)
                x[i][j] = j;
            else
                x[i][j] = i;
        }
    }
}

 

 

#include <stdio.h>
#define N 80
int main()
{
    int i, j;
    char t;
    char views1[N] = "hey, c, i hate u.";
    char views2[N] = "hey, c, i love u.";
    printf("original views:\nviews1: ");
    for (i = 0; i < N; i++)
        printf("%c", views1[i]);
    printf("\nviews2: ");
    for (i = 0; i < N; i++)
        printf("%c", views2[i]);
    printf("\n\nswapping...\n\nviews1: ");
    for (i = 0; i < N; i++)
    {
        if (views1[i] == views2[i])
            continue;
        else
        {
            t = views1[i];
            views1[i] = views2[i];
            views2[i] = t;
        }
        
    }
    for (i = 0; i < N; i++)
        printf("%c", views1[i]);
    printf("\nviews2: ");
    for (i = 0; i < N; i++)
        printf("%c", views2[i]);

    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 a[10];
    for (i = 1; i < n * (n - 1) / 2; i++)
    {
        
        for (j = 0; j < N - 1; j++)
        {
            if (str[j][0] == str[j + 1][0])
            {
                for (k = 1; k < M; k++)
                {
                    if (str[j][1] > str[j + 1][1])
                    {
                        strcpy(a, str[j + 1]);
                        strcpy(str[j + 1], str[j]);
                        strcpy(str[j], a);
                    }
                }
                
            }
            else if (str[j][0] > str[j + 1][0])
            {
                strcpy(a, str[j + 1]);
                strcpy(str[j + 1], str[j]);
                strcpy(str[j], a);
            }
        }
        
    }


}

 

posted on 2022-05-03 18:05  clmmmmm  阅读(18)  评论(0编辑  收藏  举报