結構體

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


//結構體
struct Person
{
    char name[20];
    int count;
}leader[3] = { "Li", 0, "Zhang", 0, "Sun", 0 };

int main1()
{
    int i, j;
    char leader_name[20];
    for (i = 0; i <= 10; i++)
    {
        scanf("%s", leader_name);
        for (int j = 0; j < 3; j++)
            if (strcmp(leader_name, leader[j].name) == 0)//如果輸入名字一直,相應count+1
                leader[j].count++;
    }
    printf("\nResult:\n\n");

    for (i = 0; i < 3; i++)
        printf("%5s:%d\n", leader[i].name, leader[i].count);

}
struct Student
{
    int num;
    char name[20];
    float score;
};

int main2()
{
    struct Student stu[5] = { { 11001, "Zhang", 80 }, { 11003, "Wang", 98.5 }, { 11005, "li", 86 }, { 11008, "Ling", 73.5 }, { 11002, "Sun", 100 } };
    struct Student temp;
    const int n = 5;

    int i, j, k;
    printf("the order is:\n");
    for ( i = 0; i < n-1; i++)
    {
        k = i;
        for (j = i + 1; j < n; j++)
            if (stu[j].score>stu[k].score)
                k = j;
        temp = stu[i];
        stu[i] = stu[k];
        stu[k] = temp;
    }
    for ( i = 0; i < n; i++)
        printf("%6d,%6s,%6.2f\n", stu[i].num, stu[i].name, stu[i].score);

}

struct Student_1
{
    int num;
    char name[20];
    char sex;
    float score;
};

int main3()
{
    struct Student_1 stu1;
    struct Student_1 *p;
    int i;
    p = &stu1;
    stu1.num = 10101;
    strcpy(stu1.name, "Li Ming");
    stu1.sex = 'M';
    stu1.score = 89.5;
    printf("%5d,%10s,%2c,%6.2f\n", stu1.num, stu1.name, stu1.sex, stu1.score);
    printf("%5d,%10s,%2c,%6.2f\n", p->num, p->name, p->sex, p->score);

}
struct Student stu[5] = { { 11001, "Zhang", 80 }, { 11003, "Wang", 98.5 }, { 11005, "li", 86 }, { 11008, "Ling", 73.5 }, { 11002, "Sun", 100 } };//定義結構體數組並初始化
int main()
{

    struct Student *p;//定義指向結構體變量的指針變量


    for (p = stu; p < stu + 5; p++)
        printf("%5d %-20s% 5.2f\n", p->num, p->name, p->score);


    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章