By GokiSoft.com| 21:02 30/12/2021|
Lập Trình C

[Video] Tìm hiểu về struct trong C - Khoá học lập trình C



/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include <string.h>

typedef int SO_NGUYEN;
typedef unsigned int SO_NGUYEN_DUONG;

struct Student_ST {
    char fullname[50], address[200];
    int age;
};
typedef struct Student_ST Student;

typedef struct Animal_ST {
    char name[50];
    int age;
} Animal;

typedef struct Vehicle_ST {
    char name[50], color[20];
} Vehicle;

//Khai bao ben ngoai function -> bien global -> pham vi cua bien -> goi dc o moi ham.
Student stdList[5];

int main()
{
    //Khai niem bien local -> trong 1 function -> pham vi cua vien chi co tac dung trong function dc khai bao
    //Phan 1:
    int x = 5;
    printf("\nx = %d", x);
    
    SO_NGUYEN y = 10;
    printf("\ny = %d", y);
    
    SO_NGUYEN_DUONG k = 12;
    printf("\nk = %d", k);
    
    //Phan 2: Structure
    //Khai bao thong tin sinh vien
    //char fullname[50], address[200];
    //int age;
    struct Student_ST std;
    //Nhap thong tin cho sinh vien bang cach nao
    /**printf("\nTen sinh vien: ");
    fflush(stdin); fflush(stdout);
    gets(std.fullname);
    printf("\nTen tuoi: ");
    scanf("%d", &std.age);
    printf("\nTen dia chi: ");
    getchar();
    fflush(stdin); fflush(stdout);
    gets(std.address);
    
    printf("\nThong tin ten: %s, tuoi: %d, dia chi: %s", std.fullname, std.age, std.address);*/
    
    //Khai bao thong tin dong vat 
    //char name[50];
    //int ageAnimal;
    struct Animal_ST animal;
    Animal animal2;
    
    //Khai bao thong tin xe co 
    //char nameVehicle[50], color[20];
    struct Vehicle_ST vehicle;
    Vehicle vehicle2;
    
    //Nhan xet: Khi khai bao nhu tren -> luong bien rat nhieu -> kho quan ly thong tin.
    //Khai niem -> struct -> nhom nhung thong cua 1 doi tuong vao 1 nhom -> quan ly de dang hon
    
    //Khai bao 1 mang sinh vien
    Student studentList[3];
    
    //Nhap thong tin cho 3 sinh vien tren
    int i;
    /**
    for(i=0;i<3;i++) {
        printf("\nTen sinh vien (%d): ", i + 1);
        fflush(stdin); fflush(stdout);
        gets(studentList[i].fullname);
        printf("\nTen tuoi: ");
        scanf("%d", &studentList[i].age);
        printf("\nTen dia chi: ");
        getchar();
        fflush(stdin); fflush(stdout);
        gets(studentList[i].address);
    }
    
    //Hien thi thong tin danh sach sinh vien
    for(i=0;i<3;i++) {
        printf("\nThong tin ten: %s, tuoi: %d, dia chi: %s", studentList[i].fullname, studentList[i].age, studentList[i].address);
    }*/
    
    Student *p;
    p = studentList;
    //su dung bien p -> nhu su dung bien studentList
    
    //Cap phat bo nho cho pointer -> lam nhu vs bien con tro int, float, char, ...
    Student *q;
    //Cap phat q -> mang gom 3 sinh vien
    q = (Student *) malloc (3 * sizeof(Student));
    for(i=0;i<3;i++) {
        printf("\nTen sinh vien (%d): ", i + 1);
        fflush(stdin); fflush(stdout);
        gets(q[i].fullname);
        printf("\nTen tuoi: ");
        scanf("%d", &q[i].age);
        printf("\nTen dia chi: ");
        getchar();
        fflush(stdin); fflush(stdout);
        gets(q[i].address);
    }
    for(i=0;i<3;i++) {
        printf("\nThong tin ten: %s, tuoi: %d, dia chi: %s", q[i].fullname, q[i].age, q[i].address);
    }
    
    //Sap xep cac phan tu trong mang Struct theo thu tu tang dan ve tuoi
    int j;
    /**for(i=0;i<2;i++) {
        for(j=i+1;j<3;j++) {
            if(q[i].age > q[j].age) {
                //Swap cac phan tu trong mang
                Student tmp = q[i];
                q[i] = q[j];
                q[j] = tmp;
            }
        }
    }*/
    //Sap xep cac phan tu trong mang sinh vien theo thu tu ten A-Z
    for(i=0;i<2;i++) {
        for(j=i+1;j<3;j++) {
            //so sanh ten 
            int cmp = strcmp(q[i].fullname, q[j].fullname);
            if(cmp > 0) {
                //sap xep sai
                Student tmp = q[i];
                q[i] = q[j];
                q[j] = tmp;
            }
        }
    }
    
    //Hien thi thong tin danh sach sinh vien
    for(i=0;i<3;i++) {
        printf("\nThong tin ten: %s, tuoi: %d, dia chi: %s", q[i].fullname, q[i].age, q[i].address);
    }
    
    //Su dung cap phat bo nho bang calloc, realloc -> nhu bien int, float, char, .v.v
    /**Student std2;
    
    printf("\nTen sinh vien: ");
    fflush(stdin); fflush(stdout);
    gets(std2.fullname);
    printf("\nTen tuoi: ");
    scanf("%d", &std2.age);
    printf("\nTen dia chi: ");
    getchar();
    fflush(stdin); fflush(stdout);
    gets(std2.address);
    
    printf("\nThong tin ten: %s, tuoi: %d, dia chi: %s", std2.fullname, std2.age, std2.address);
    
    Student *pq;
    pq = &std2;
    
    printf("\nThong tin ten: %s, tuoi: %d, dia chi: %s", pq->fullname, pq->age, pq->address);
    
    **/

    return 0;
}




Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó