共同体例题,小甲鱼老师课上的例题, 求大神指教, 附上源码
#include <stdio.h>#define N 2
struct
{
int num;
char name;
char sex;
char job;
union
{
int banji;
char position;
}category;
}person;
intmain(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.num, &person.name,&person.sex,&person.job);
if( person.job == 's' )
{
printf("Please enter the class:");
scanf("%d", &person.category.banji);
}
else if( person.job == 't')
{
printf("Please enter the position:");
scanf("%s", &person.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.job )
{
printf("num = %d, name is %s, sex is %c, job is %c, class is %d", person.num, person.name, person.sex, person.job, person.category);
}
}
}
}
#include <stdio.h>
#define N 2
struct
{
int num;
char name;
char sex;
char job;
union
{
int banji;
char position;
}category;
}person;
intmain(void)
{
int i;
char j;
for ( i = 0; i < N; i++ )
{
printf("Please enter the num:");
scanf("%d", &person.num);
printf("Please enter the name:");
scanf("%s", &person.name);
getchar();
printf("Please enter the sex<M/F>:");
scanf("%c", &person.sex);
getchar();
printf("Please enter the job<s/t>:");
scanf("%c", &person.job);
if( person.job == 's' )
{
printf("Please enter the class:");
scanf("%d", &person.category);
}
else if( person.job == 't')
{
printf("Please enter the position:");
scanf("%s", &person.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.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.num, person.name, person.sex, person.job, person.category);
}
}
}
printf("\n\n");
}
修正后的程序,有不足的地方希望大家多多指教
页:
[1]