Guest User

Untitled

a guest
Jun 27th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. typedef struct{char names[30];
  6.                int age;
  7.                char position[30];
  8.                char sex;} data;
  9. void age(data *emp; int n;);
  10. void sex(data *emp; int n;);
  11. void name(data *emp; int n;);
  12. void pos(data *emp; int n;);
  13. void show(data *emp; int n;);
  14. data *emp=0;
  15. int n=0,i;
  16. char ch;
  17.  
  18. void input(data *emp);
  19. int main(){
  20.  
  21.  
  22. printf("Input employees' information here:\n");
  23. again:
  24.     printf("\nEmployee[%d]:\n",n+1);
  25.     emp=(data*)realloc(emp,(n+1)*sizeof(data));
  26.     if(emp==0) return 0;
  27. printf("%10s:","Name"); scanf("%30[^\n]",(emp+n)->names); fflush(stdin);
  28. printf("%10s:","age"); scanf("%d",&((emp+n)->age)); fflush(stdin);
  29. printf("%10s:","Position"); scanf("%30[^\n]",(emp+n)->position); fflush(stdin);
  30. printf("%10s:","Sex"); scanf("%c",&((emp+n)->sex)); fflush(stdin);
  31.     n++;
  32. printf("\nMore employee? (ESC to stop): ");
  33. if(getch()!=27) goto again;
  34. printf("Here are all of your employees' information:");
  35. for(i=0;i<n;i++){
  36.     printf("\n[%d]",i+1);
  37.     printf("\t%s",emp[i].names);
  38.     printf("\t%d",emp[i].age);
  39.     printf("\t%s",emp[i].position);
  40.     printf("\t%c",emp[i].sex);}
  41. printf("\nWhat do you want data to be sorted by?\n");
  42. printf("1/ names\n2/ age\n3/ position\n4/ sex\n");
  43. printf("\nYour choice: ");
  44. ch=getche();
  45. switch(ch){
  46.     case 1 : name(emp, n); show(emp, n); break;
  47.     case 2 : age(emp, n); show(emp, n); break;
  48.     case 3 : pos(emp, n); show(emp, n); break;
  49.     case 4 : sex(emp, n); show(emp, n); break;}
  50. return 0;
  51. }
  52.  
  53. void name(data *emp; int n;){
  54. int i,j;
  55. char temp[30];
  56. for(i=0;i<n-1;i++)
  57.     for(j=i+1;j<n;j++)
  58.         if(strcmp(emp[i].names,emp[j].names)>0) {strcpy(temp,emp[i].names);
  59.                                                  strcpy(emp[i].names,emp[j].names);
  60.                                                  strcpy(emp[j].names,temp);}
  61.  
  62. }
  63.  
  64. void pos(data *emp; int n;){
  65. int i,j;
  66. char temp[30];
  67. for(i=0;i<n-1;i++)
  68.     for(j=i+1;j<n;j++)
  69.         if(strcmp(emp[i].position,emp[j].position)>0) {strcpy(temp,emp[i].position);
  70.                                                  strcpy(emp[i].position,emp[j].position);
  71.                                                  strcpy(emp[j].position,temp);}
  72.  
  73. }
  74.  
  75. void age(data *emp; int n;){
  76. int i,j, temp;
  77. for(i=0;i<n-1;i++)
  78.     for(j=i+1;j<n;j++){
  79.         if(emp[i].age>emp[j].age) {temp=emp[i].age;
  80.                                    emp[i].age=emp[j].age;
  81.                                    emp[j].age=temp;}
  82.     }
  83.  
  84. }
  85.  
  86. void sex(data *emp; int n;){
  87. int i,j;
  88. char temp;
  89. for(i=0;i<n-1;i++)
  90.     for(j=i+1;j<n;j++){
  91.         if(emp[i].sex!=emp[j].sex) {temp=emp[i].sex;
  92.                                    emp[i].sex=emp[j].sex;
  93.                                    emp[j].sex=temp;}
  94.     }
  95.  
  96. }
  97.  
  98. void show(data *emp; int n;){
  99. int i;
  100. printf("\nHere are sorted lists:\n\n");
  101. for(i=0;i<n;i++){
  102.     printf("\n[%d]",i+1);
  103.     printf("\t%s",emp[i].names);
  104.     printf("\t%d",emp[i].age);
  105.     printf("\t%s",emp[i].position);
  106.     printf("\t%c",emp[i].sex);}
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment