1. Example:
2. Data Definition:
(1) defined within a common body
#include <stdio.h>
struct
{
int num;
char name[10];
char sex;
char job;
union ///此处无类型
{
int clas;
char position[10];
}category;
}person[2];
(2) define a common outer body
#include <stdio.h>
union Categ ///有共用体类型
{
int clas;
char position[10];
}; ///与定义结构体类似
struct
{
int num;
char name[10];
char sex;
char job;
union Categ category;///调用
}person[2];
3. Enter:
for(i=0;i<2;i++)
{
scanf("%d %s %c %c",
&person[i].num, person[i].name, &person[i].sex, &person[i].job);
if(person[i].job == 's')
scanf("%d", &person[i].category.clas);//班级
else if(person[i].job == 't')
scanf("%s", person[i].category.position); //职务
else
printf("Input error!");
}
4. Output
for(i=0;i<2;i++)
{
if (person[i].job == 's')
printf("%-6d%-10s%-4c%-4c%-10d\n",
person[i].num,
person[i].name,
person[i].sex,
person[i].job,
person[i].category.clas );
else
printf("%-6d%-10s%-4c%-4c%-10s\n",
person[i].num,
person[i].name,
person[i].sex,
person[i].job,
person[i].category.position );
}
The enumeration
(1)
that Italy:
pocket red, yellow, blue, white and black balls several five colors.
Each pocket successively extracted three balls;
seeking to obtain three different colored balls emulated, a case where each print array.
Thinking:
definition of an enumerated type Color, wherein the red, yellow, blue, white, black 5 enumeration element.
3 balls need three enumerations; respectively, from "red" to "black", as long as the "color" is not the same results will play out.
Remove the memory number of different colors in combination with a ball counter (integer variable).
(2)
int main()
{
enum Color{red,yellow,blue,white,black};
enum Color i,j,k;
int n,loop;
n=0;
for (i=red;i<=black;i++)
for (j=red;j<=black;j++)
if (i!=j)
{
for (k=red;k<=black;k++)
if ((k!=i) && (k!=j))
{
n=n+1;
printf("%-4d",n);
printf("%d,%d,%d\n",i,j,k);
}
}
printf("\ntotal:%5d\n",n);
return 0;
}