鱼C论坛

 找回密码
 立即注册
查看: 2880|回复: 1

共同体例题,小甲鱼老师课上的例题, 求大神指教, 附上源码

[复制链接]
发表于 2018-1-21 11:15:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#define N 2


struct
{
        int num;
        char name[10];
        char sex;
        char job;
        union
        {
                int banji;
                char position[10];
        }category;
}person[N];

int  main(void)
{
        int i;
        char j;
        for ( i = 0; i < N; i++ )
        {
                printf("Please enter the num, name, sex, job:");
                scanf("%d %s %c %c", &person[i].num, &person[i].name,  &person[i].sex,  &person[i].job);
                if( person[i].job == 's' )
                {
                        printf("Please enter the class:");
                        scanf("%d", &person[i].category.banji);
                }
                else if( person[i].job == 't')
                {
                        printf("Please enter the position:");
                        scanf("%s", &person[i].category.position);
                }
                else
                {
                        printf("The enter is error");
                        printf("\n");
                        return 0;
                }
        }
        printf("Please enter the job you want to search:");
        scanf("%c", &j);

        if( j != 's' || j != 't')
        {
                printf("The enter is error");
        }
        else
        {
                for ( i = 0; i < N; i++ )
                {
                        if( j == person[i].job )
                        {
                                printf("num = %d, name is %s, sex is %c, job is %c, class is %d", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category);
                        }
                }
        }

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-1-21 11:47:30 | 显示全部楼层
#include <stdio.h>
#define N 2


struct
{
        int num;
        char name[10];
        char sex;
        char job;
        union
        {
                int banji;
                char position[10];
        }category;
}person[N];

int  main(void)
{
        int i;
        char j;
        for ( i = 0; i < N; i++ )
        {
                printf("Please enter the num:");
                scanf("%d", &person[i].num);
                printf("Please enter the name:");
                scanf("%s", &person[i].name);
                getchar();
                printf("Please enter the sex<M/F>:");
                scanf("%c", &person[i].sex);
                getchar();
                printf("Please enter the job<s/t>:");
                scanf("%c", &person[i].job);
                if( person[i].job == 's' )
                {
                        printf("Please enter the class:");
                        scanf("%d", &person[i].category);
                }
                else if( person[i].job == 't')
                {
                        printf("Please enter the position:");
                        scanf("%s", &person[i].category);
                }
                else
                {
                        printf("The enter is error");
                        printf("\n");
                        i--;
                        continue;
                }
                printf("\n");
        }
        printf("Please enter the job you want to search:");
        getchar();
        scanf("%c", &j);

        if( j != 's' && j != 't')
        {
                printf("The enter is error");
        }
        else
        {
                for ( i = 0; i < N; i++ )
                {
                        if( j == person[i].job )
                        {
                                printf("num\t name\t sex\t job\t class\t\n");
                                printf("%d\t %s\t %c\t %c\t %d\t\n", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category);
                        }
                }
        }
        printf("\n\n");

}
修正后的程序,有不足的地方希望大家多多指教
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 04:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表