Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.0 kB
2
Indexable
Never
#pragma warning (disable: 4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct A
{
    short a;
    double b;
    char c[16];
};

int demSoLuongChuSoPhanNguyen(int a)
{
    if (a > 0)
        return demSoLuongChuSoPhanNguyen(a / 10) + 1;
    else
        return 0;
}

// void updateArray(A*& arr, int& N, A x)
// {
//     A* temp = new A[N+1];
//     int i;
//     for (i = 0; i < N; i++)
//         temp[i] = arr[i];
//     temp[i] = x;
//     delete[] arr;
//     arr = temp;
//     N++;
// }

int findMaxLengthA(A* arr, int N)
{
    int Max = demSoLuongChuSoPhanNguyen(arr[0].a);
    for (int i = 1; i < N; i++)
    {
        if (demSoLuongChuSoPhanNguyen(arr[i].a) > Max)
            Max = demSoLuongChuSoPhanNguyen(arr[i].a);
    }
    return Max;
}

double findMaxLengthB(A* arr, int N) //3 chu so sau dau thap phan
{
    double Max = demSoLuongChuSoPhanNguyen(arr[0].b);
    for (int i = 1; i < N; i++)
    {
        if (demSoLuongChuSoPhanNguyen(arr[i].b) > Max)
            Max = demSoLuongChuSoPhanNguyen(arr[i].b);
    }
    return Max + 3;
}

int findMaxLengthC(A* arr, int N)
{
    int Max = strlen(arr[0].c);
    for (int i = 1; i < N; i++)
    {
        if (strlen(arr[i].c) > Max)
            Max = strlen(arr[i].c);
    }
    return Max;
}




void nhap(A& x)
{
    printf("Nhap a: ");
    scanf("%hd", &x.a);
    printf("Nhap b: ");
    scanf("%lf", &x.b);
    printf("Nhap c: ");
    scanf("%s", x.c);
}

void xuat(A x, short d1, double d2, int d3)
{
    int s1 = demSoLuongChuSoPhanNguyen(x.a);
    int s2 = demSoLuongChuSoPhanNguyen(x.b);
    int s3 = strlen(x.c);

    int t = 0;
    if (s1 < d1)
        t = d1 - s1;

    printf("| %hd", x.a);
    for (int i = 0; i < t; i++)
        printf(" ");



    if (s2 < d2)
        t = d2 - s2;
    printf("| %lf", x.b);
    for (int i = 0; i < t; i++)
        printf(" ");


    if (s3 < d3)
        t = d3 - s3;
    printf("| %s", x.c);
    for (int i = 0; i < t; i++)
        printf(" ");

    printf("|\n");
}

int main()
{
    //Nhap du lieu
    int N;
    printf("Nhap kich thuoc mang:  ");
    scanf("%d", &N);
    A* arr = new A[N];
    for (int i = 0; i < N; i++)
    {
        nhap(arr[i]);
    }


    //tinh toan de can
    int d1 = findMaxLengthA(arr, N);
    int d2 = findMaxLengthB(arr, N);
    int d3 = findMaxLengthC(arr, N);

    //xuat du lieu dong dau
    printf("*");
    for (int i = 0; i < (d1 + d2 + d3 + 18); i++)
        printf("-");
    printf("*\n");

    //cac dong du lieu
    for (int i = 0; i < N; i++)
    {
        xuat(arr[i], d1, d2, d3);
        if (i == N - 1)
        {
            printf("*");
            for (int i = 0; i < (d1 + d2 + d3 + 18); i++)
                printf("-");
            printf("*\n");
            break;
        }
        for (int i = 0; i < (d1 + d2 + d3 + 20); i++)
            printf("-");
        printf("\n");
    }

    return 0;
}