Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct{char names[30];
- int age;
- char position[30];
- char sex;} data;
- void age(data *emp; int n;);
- void sex(data *emp; int n;);
- void name(data *emp; int n;);
- void pos(data *emp; int n;);
- void show(data *emp; int n;);
- data *emp=0;
- int n=0,i;
- char ch;
- void input(data *emp);
- int main(){
- printf("Input employees' information here:\n");
- again:
- printf("\nEmployee[%d]:\n",n+1);
- emp=(data*)realloc(emp,(n+1)*sizeof(data));
- if(emp==0) return 0;
- printf("%10s:","Name"); scanf("%30[^\n]",(emp+n)->names); fflush(stdin);
- printf("%10s:","age"); scanf("%d",&((emp+n)->age)); fflush(stdin);
- printf("%10s:","Position"); scanf("%30[^\n]",(emp+n)->position); fflush(stdin);
- printf("%10s:","Sex"); scanf("%c",&((emp+n)->sex)); fflush(stdin);
- n++;
- printf("\nMore employee? (ESC to stop): ");
- if(getch()!=27) goto again;
- printf("Here are all of your employees' information:");
- for(i=0;i<n;i++){
- printf("\n[%d]",i+1);
- printf("\t%s",emp[i].names);
- printf("\t%d",emp[i].age);
- printf("\t%s",emp[i].position);
- printf("\t%c",emp[i].sex);}
- printf("\nWhat do you want data to be sorted by?\n");
- printf("1/ names\n2/ age\n3/ position\n4/ sex\n");
- printf("\nYour choice: ");
- ch=getche();
- switch(ch){
- case 1 : name(emp, n); show(emp, n); break;
- case 2 : age(emp, n); show(emp, n); break;
- case 3 : pos(emp, n); show(emp, n); break;
- case 4 : sex(emp, n); show(emp, n); break;}
- return 0;
- }
- void name(data *emp; int n;){
- int i,j;
- char temp[30];
- for(i=0;i<n-1;i++)
- for(j=i+1;j<n;j++)
- if(strcmp(emp[i].names,emp[j].names)>0) {strcpy(temp,emp[i].names);
- strcpy(emp[i].names,emp[j].names);
- strcpy(emp[j].names,temp);}
- }
- void pos(data *emp; int n;){
- int i,j;
- char temp[30];
- for(i=0;i<n-1;i++)
- for(j=i+1;j<n;j++)
- if(strcmp(emp[i].position,emp[j].position)>0) {strcpy(temp,emp[i].position);
- strcpy(emp[i].position,emp[j].position);
- strcpy(emp[j].position,temp);}
- }
- void age(data *emp; int n;){
- int i,j, temp;
- for(i=0;i<n-1;i++)
- for(j=i+1;j<n;j++){
- if(emp[i].age>emp[j].age) {temp=emp[i].age;
- emp[i].age=emp[j].age;
- emp[j].age=temp;}
- }
- }
- void sex(data *emp; int n;){
- int i,j;
- char temp;
- for(i=0;i<n-1;i++)
- for(j=i+1;j<n;j++){
- if(emp[i].sex!=emp[j].sex) {temp=emp[i].sex;
- emp[i].sex=emp[j].sex;
- emp[j].sex=temp;}
- }
- }
- void show(data *emp; int n;){
- int i;
- printf("\nHere are sorted lists:\n\n");
- for(i=0;i<n;i++){
- printf("\n[%d]",i+1);
- printf("\t%s",emp[i].names);
- printf("\t%d",emp[i].age);
- printf("\t%s",emp[i].position);
- printf("\t%c",emp[i].sex);}
- }
Advertisement
Add Comment
Please, Sign In to add comment