结构体

来源:互联网 发布:java开发笔试题及答案 编辑:程序博客网 时间:2024/06/05 10:50
#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;}
原创粉丝点击