วันจันทร์ที่ 8 กุมภาพันธ์ พ.ศ. 2553

สตรัคเจอร์แบบใช้ typedef

#include
#include

typedef struct //typedef
{
char name[20];
char surname[20];
int salary;
}employee;


void getdata(employee em[])//keep in array[]
{
int i;
for(i=0;i<5;i++){
printf("Name [%d] : ",i+1);
gets(em[i].name);
printf("surname : ");
gets(em[i].surname);
printf("salary : ");
flushall();
scanf("%d",&em[i].salary);
flushall();
}
}

void printmax(employee em[]) // printf
{
int i,max;
int temp;
max=em[0].salary;

for(i=0;i<5;i++){
if(em[i].salary>max){
max=em[i].salary;
temp=i;

}
}
printf("Employee [%d] have max salary",temp+1);
printf("Name : %s \n",em[temp].name);
printf("Surname : %s \n",em[temp].surname);
printf("Salary : %d",max);


}

void main()
{
clrscr();
employee x[5];

getdata(x);
printmax(x);
getch();
}
สตรัคเจอร์ แบบธรรมดาไม่มีการใช้ typedef



#include
#include

struct employee //struct
{
char name[20];
char surname[20];
int salary;
};
struct employee em[5];

void getdata(struct employee em[])//keep in array[]
{
int i;
for(i=0;i<5;i++){
printf("Name [%d] : ",i+1);
gets(em[i].name);
printf("surname : ");
gets(em[i].surname);
printf("salary : ");
flushall();
scanf("%d",&em[i].salary);
flushall();
}
}

void printmax(struct employee em[]) // printf
{
int i,max;
int temp;
max=em[0].salary;

for(i=0;i<5;i++){
if(em[i].salary>max){
max=em[i].salary;
temp=i;

}
}
printf("Employee [%d] have max salary",temp+1);
printf("Name : %s \n",em[temp].name);
printf("Surname : %s \n",em[temp].surname);
printf("Salary : %d",max);


}

void main()
{
clrscr();
struct employee x[5];

getdata(x);
printmax(x);
getch();
}