Saturday, 27 September 2014

PL Assignment 6 : Searching and Sorting of students information


1:  /*  
2:   ==============================================================  
3:   Name    : Student Information  
4:   Author   : Mujahid, Shivkumar  
5:   Version   :  
6:   Copyright  : Your copyright notice  
7:   Description : Hello World in C, Ansi-style  
8:   ===========================================================  
9:   */  
10:  #include<stdlib.h>  
11:  #include<string.h>  
12:  #include<stdio.h>  
13:  #include<ctype.h>  
14:  #include<math.h>  
15:  typedef struct student  
16:  {     char name[30];  
17:       int roll_no;  
18:       long long int mob;  
19:       char address[100];  
20:  }stud;  
21:  void ip(stud *,int);  
22:  void sort_asc(stud *,int);  
23:  void sort_ascn(stud *,int);  
24:  void sort_des(stud *,int);  
25:  void print(stud *,int);  
26:  void swap(stud *,stud *);  
27:  void search(stud *,int,int);  
28:  int main(void)  
29:  {     int n,i,j;  
30:       printf("How many records will you enter?");  
31:       scanf("%d",&n);  
32:       if(n<=0)  
33:       {     printf("\nenter valid numbers!");  
34:            exit(0);  
35:       }  
36:       stud *s=(stud *)malloc(n*sizeof(stud)),*s1,*s2;  
37:       ip(s,n);  
38:       do  
39:       {     printf("\n1.Sort in ascending order\n2.Sort in descending order\n3.Search\nEnter your choice");  
40:            scanf("%d",&j);  
41:            switch(j)  
42:            {     case 1:  
43:                 {     s1=s;  
44:                      sort_asc(s1,n);  
45:                      //print(s1,n);  
46:                 }break;  
47:                 case 2:  
48:                 {     s2=s;  
49:                      sort_des(s2,n);  
50:                      //print(s2,n);  
51:                 }break;  
52:                 case 3:  
53:                 {     printf("\nenter roll number to be searched");  
54:                      scanf("%d",&i);  
55:                      search(s,i,n);  
56:                 }break;  
57:                 default:  
58:                      printf("\nEnter valid option");  
59:            }  
60:            printf("\nContinue\t1.yes\t0.No");  
61:            scanf("%d",&j);  
62:       }while(j);  
63:       printf("\nThats all!");  
64:       return 0;  
65:  }  
66:  void ip(stud *p,int n)  
67:  {     int i,j,c,l;  
68:       for(i=0;i<n;i++)  
69:       {  
70:       do  
71:       {     c=0;  
72:            printf("\nenter name");  
73:            scanf("%s",((p+i)->name));  
74:            l=strlen((p+i)->name);  
75:            for(j=0;j<l;j++)  
76:            {  
77:                 if (!isalpha((p+i)->name[j]))  
78:                      c=1;  
79:            }  
80:            if(c==1)  
81:                 printf("\nalphabets only");  
82:       }while(c);  
83:       printf("\nenter address");  
84:       scanf("%s",((p+i)->address));  
85:       printf("\nenter roll number");  
86:       scanf("%d",&((p+i)->roll_no));  
87:       long long int c1;  
88:       do  
89:       {     c=0;  
90:            printf("\nenter mobile number");  
91:            scanf("%llu",&((p+i)->mob));  
92:            c1=(p+i)->mob;  
93:            while(c1>0)  
94:            {     c++;  
95:                 c1/=10;  
96:            }  
97:            if(c!=10)  
98:            printf("\n10 digit numbers only");  
99:       }while(c!=10);  
100:       }  
101:  }  
102:  void sort_asc(stud *p,int n)  
103:  {     //const int n1=n*n;  
104:       int i,j,c=0,flg=1;  
105:       for(i=0;i<(n-1) && flg==1 ;i++)  
106:       {     flg=0;  
107:            for(j=0;j<n-1-i;j++)  
108:            {     if(strcmp((p+j)->name,(p+j+1)->name)>0)  
109:                 {     flg=1;  
110:                      swap((p+j),(p+j+1));  
111:                      c++;  
112:                 }  
113:            printf("\n\n\nrecords after pass %d",(i+1));  
114:            print(p,n);  
115:            }  
116:       }  
117:       switch(c)  
118:       {     case 0:  
119:                 printf("\nBest case ");  
120:                 break;  
121:            /*case n1:  
122:                 printf("\nworst case");  
123:                 break;*/  
124:            default:  
125:                 printf("\naverage case");  
126:                 break;  
127:       }  
128:  }  
129:  void sort_ascn(stud *p,int n)  
130:  {  
131:       int i,j;  
132:       for(i=0;i<n-1;i++)  
133:       {     for(j=0;j<n-1-i;j++)  
134:            {     if((p+j)->roll_no >= (p+j+1)->roll_no)  
135:                 {  
136:                      swap((p+j),(p+j+1));  
137:                 }  
138:            }  
139:       }  
140:  }  
141:  void sort_des(stud *p,int n)  
142:  {     int i,j,c=0,m,flg=0;  
143:       //const int n1=n;  
144:       for(i=0;i<n-1;i++)  
145:       {     flg=0;  
146:            for(j=i+1;j<n;j++)  
147:            {  
148:                 if(strcmp((p+j)->name,(p+i)->name)>0)  
149:                 {     m=j;  
150:                      flg=1;  
151:                 }  
152:                 if(flg==1)  
153:                 {     swap((p+i),(p+m));  
154:                      c++;  
155:                 }  
156:            }  
157:            printf("\n\n\nrecords after pass %d",(i+1));  
158:            print(p,n);  
159:       }  
160:       switch(c)  
161:       {     case 0:  
162:                 printf("\nBest case ");  
163:                 break;  
164:            /*case n1:  
165:                 printf("\nworst case");  
166:                 break;*/  
167:            default:  
168:                 printf("\naverage case");  
169:                 break;  
170:       }  
171:  }  
172:  void print(stud *p,int n)  
173:  {  
174:       int i;  
175:       printf("\nName\tAddress\t\t Roll no.\tMobile number");  
176:       for(i=0;i<n;i++)  
177:            printf("\n%s\t%s\t\t%d\t%llu",(p+i)->name,(p+i)->address,(p+i)->roll_no,(p+i)->mob);  
178:  }  
179:  void search(stud *p,int r,int n)  
180:  {     stud *q=p;  
181:       //const int n1=log(n)/log(2);  
182:       sort_ascn(q,n);  
183:       int u=n-1,l=0,m=(u+l)/2,c=0;  
184:       while(u>=l)  
185:       {  
186:            if((q+m)->roll_no<r)  
187:            {     l=m+1;  
188:                 c++;  
189:            }  
190:            if((q+m)->roll_no>r)  
191:            {     u=m-1;  
192:                 c++;  
193:            }  
194:            if((q+m)->roll_no==r)  
195:            {     printf("found\n%s\t%s\t%d\t%llu",(q+m)->name,(q+m)->address,(q+m)->roll_no,(q+m)->mob);  
196:                 switch(c)  
197:                 {     case 0:  
198:                           printf("\nBest case ");  
199:                           break;  
200:                 /*     case log(n)/log(2):  
201:                           printf("\nworst case");  
202:                           break;*/  
203:                      default:  
204:                           printf("\naverage case");  
205:                           break;  
206:                 }  
207:                 return;  
208:            }  
209:            m=(u+l)/2;  
210:       }  
211:       printf("\nroll number not found. please try again");  
212:  }  
213:  void swap(stud *p,stud *q)  
214:  {     char name[30],add[100];  
215:       int r;  
216:       long long int m;  
217:       strcpy(name,p->name);  
218:       strcpy(p->name,q->name);  
219:       strcpy(q->name,name);  
220:       strcpy(add,p->address);  
221:       strcpy(p->address,q->address);  
222:       strcpy(q->address,add);  
223:       r=p->roll_no;  
224:       p->roll_no=q->roll_no;  
225:       q->roll_no=r;  
226:       m=p->mob;  
227:       p->mob=q->mob;  
228:       q->mob=m;  
229:  }  
230:  -------------OUTPUT---------------  
231:  How many records will you enter?3  
232:  enter namemujahid  
233:  enter addressshirdi  
234:  enter roll number2077  
235:  enter mobile number9856245895  
236:  enter nameshivkumar  
237:  enter addressnanded  
238:  enter roll number2079  
239:  enter mobile number8007921033  
240:  enter namepradip  
241:  enter addressbaramati  
242:  enter roll number2078  
243:  enter mobile number8255654854  
244:  1.Sort in ascending order  
245:  2.Sort in descending order  
246:  3.Search  
247:  Enter your choice1  
248:  records after pass 1  
249:  Name     Address           Roll no.     Mobile number  
250:  mujahid     shirdi          2077     9856245895  
251:  shivkumar     nanded          2079     8007921033  
252:  pradip     baramati          2078     8255654854  
253:  records after pass 1  
254:  Name     Address           Roll no.     Mobile number  
255:  mujahid     shirdi          2077     9856245895  
256:  pradip     baramati          2078     8255654854  
257:  shivkumar     nanded          2079     8007921033  
258:  records after pass 2  
259:  Name     Address           Roll no.     Mobile number  
260:  mujahid     shirdi          2077     9856245895  
261:  pradip     baramati          2078     8255654854  
262:  shivkumar     nanded          2079     8007921033  
263:  average case  
264:  Continue     1.yes     0.No1  
265:  1.Sort in ascending order  
266:  2.Sort in descending order  
267:  3.Search  
268:  Enter your choice2  
269:  records after pass 1  
270:  Name     Address           Roll no.     Mobile number  
271:  shivkumar     nanded          2079     8007921033  
272:  mujahid     shirdi          2077     9856245895  
273:  pradip     baramati          2078     8255654854  
274:  records after pass 2  
275:  Name     Address           Roll no.     Mobile number  
276:  shivkumar     nanded          2079     8007921033  
277:  pradip     baramati          2078     8255654854  
278:  mujahid     shirdi          2077     9856245895  
279:  average case  
280:  Continue     1.yes     0.No1  
281:  1.Sort in ascending order  
282:  2.Sort in descending order  
283:  3.Search  
284:  Enter your choice3  
285:  enter roll number to be searched2079  
286:  found  
287:  shivkumar     nanded     2079     8007921033  
288:  average case  
289:  Continue     1.yes     0.No0  
290:  Thats all! 
 
https://drive.google.com/file/d/0BzbuE6BfgoRbTVJCSkxjX00yZ2c/edit?usp=sharing
 
 

No comments:

Post a Comment

Ads Inside Post