Saturday, 27 September 2014

PL Assignment 4 : Array of structure


1:  /*  
2:   ============================================================================  
3:   Name    : Array Of Structure  
4:   Author   : Mujahid, Shivkumar  
5:   Version   :  
6:   Copyright  : Your copyright notice  
7:   Description : Hello World in C, Ansi-style  
8:   ============================================================================  
9:   */  
10:  #include <stdio.h>  
11:  #include <stdlib.h>  
12:  #define max 10  
13:  typedef struct  
14:  {  
15:       char name[30];  
16:       int rollno;  
17:       char mobno[10];  
18:  }student;  
19:  int getdata(student *s)  
20:       {  
21:       int i,n;  
22:       printf("Enter the number of students : \n");  
23:       scanf("%d",&n);  
24:       for(i=0;i<n;i++)  
25:       {  
26:            printf("Enter name : \n");  
27:            scanf("%s",&(s+i)->name);  
28:            (s+i)->rollno=valid_roll(s,i);  
29:            valid_mob(s,i);  
30:       }  
31:       return i;  
32:  }  
33:       int valid_roll(student *s,int n)  
34:       {  
35:            int i,roll;  
36:            printf("Enter roll number\n");  
37:            scanf("%d",&roll);  
38:            if(roll<0)  
39:            {  
40:                 printf("Invalid!!\n");  
41:                 printf("Re-enter roll number\n");  
42:                 scanf("%d",&roll);  
43:                 roll = valid_roll(s,n);  
44:            }  
45:            for(i=0;i<n;i++)  
46:            {  
47:                 if(roll==(s+i)->rollno)  
48:                 {  
49:                      printf("Record already exists!!\n");  
50:                      roll = valid_roll(s,n);  
51:                      break;  
52:                 }  
53:            }  
54:            return roll;  
55:       }  
56:       void valid_mob(student s[],int j)  
57:       {  
58:            int i=0,k=0,cnt=0;  
59:            printf("Enter mobile number : \n");  
60:            scanf("%s",(s+j)->mobno);  
61:            while((s+j)->mobno[i]!='\0')  
62:            {  
63:                 cnt++;  
64:                 i++;  
65:            }  
66:            while((s+j)->mobno[k]!='\0')  
67:            {  
68:                 if(isdigit((s+j)->mobno[k]))  
69:                 {  
70:                      ;  
71:                 }  
72:                 else  
73:                 {  
74:                      printf("Invalid number!!\n");  
75:                           valid_mob(s,j);  
76:                 }  
77:                 k++;  
78:            }  
79:            if(cnt!=10)  
80:            {  
81:                 printf("Invalid number!!\n");  
82:                 valid_mob(s,j);  
83:            }  
84:  }  
85:  void printdata(student *s,int n)  
86:  {  
87:       int j;  
88:       printf("\n \tName\t  Roll Number\t\tMobile Number\n");  
89:       for(j=0;j<n;j++)  
90:            printf("\n\t%s\t\t%d\t\t%s",(s+j)->name,(s+j)->rollno,(s+j)->mobno);  
91:  }  
92:  int search(student *s,int n)  
93:  {  
94:       int i,temp;  
95:       printf("\nEnter Roll Number to Search\n");  
96:       scanf("%d",&temp);  
97:       i=0;  
98:       while(i<n && (s+i)->rollno!=temp)  
99:            i++;  
100:       if((s+i)->rollno==temp)  
101:            return i;  
102:       else  
103:            return -1;  
104:  }  
105:  int insert(student *s,int n)     //Value of s[n] only considered  
106:  {  
107:       printf("Enter name : \n");  
108:       scanf("%s",&(s+n)->name);  
109:       (s+n)->rollno=valid_roll(s,n);  
110:       valid_mob(s,n);  
111:       return n+1;  
112:  }  
113:  int delete(student *s,int n, int temp)  
114:  {  
115:       int i;  
116:       for(i=temp+1;i<n;i++)  
117:            *(s+(i-1))=*(s+i);     // * has to written for accessing value  
118:       return n-1;  
119:  }  
120:  int main()     //No changes in main for with and without pointers  
121:  {  
122:       student s[10];  
123:       int n=0,ch=0,temp=0;  
124:       do  
125:       {  
126:            printf("\n\nEnter Choice:");  
127:            printf("\nMENU\n1.Create\n2.Display\n3.Insert\n4.Search\n5.Modify\n6.Display record\n7.Delete Record\n0.Exit\n");  
128:            scanf("%d",&ch);  
129:            switch(ch)  
130:            {  
131:            case 1:     n=getdata(s);  
132:                      break;  
133:            case 2: printdata(s,n);  
134:                      break;  
135:            case 3: printf("\nInsert Record\n");  
136:                      if(n==max)  
137:                      {  
138:                           printf("\nMaximum Records Reached.");  
139:                           return 0;  
140:                      }  
141:                      n=insert(s,n);     //Calling to Insert  
142:                      break;  
143:            case 4:     temp=search(s,n);  
144:                      if(temp<0)  
145:                      {  
146:                           printf("\nNo such record found.");  
147:                           break;  
148:                      }  
149:                      printf("\nLocation=%d",temp+1);  
150:                      break;  
151:            case 5: temp=search(s,n);  
152:                      if(temp<0)  
153:                      {  
154:                           printf("\nNo such record found.");  
155:                           break;  
156:                      }  
157:                      else  
158:                      {  
159:                           temp=insert(s,temp);     //Calling to Insert  
160:                      }  
161:                      break;  
162:            case 6: temp=search(s,n);  
163:                      if(temp<0)  
164:                      {  
165:                           printf("\nNo such record found.");  
166:                           break;  
167:                      }  
168:                      printf("\n \tName\t  Roll Number\t\tMobile Number\n");  
169:                      printf("\n\t%s\t\t%d\t\t%s",s[temp].name,s[temp].rollno,s[temp].mobno);  
170:                      break;  
171:            case 7: temp=search(s,n);  
172:                      if(temp<0)  
173:                      {  
174:                           printf("\nNo such record found.");  
175:                           break;  
176:                      }  
177:                      n=delete(s,n,temp);  
178:                      break;  
179:            case 0: break;  
180:            default:printf("\nInvalid Case.");  
181:                      break;  
182:            }  
183:       }while(ch!=0);  
184:       return 0;  
185:  }  
186:  -----------------------OUTPUT------------------------  
187:  Enter Choice:  
188:  MENU  
189:  1.Create  
190:  2.Display  
191:  3.Insert  
192:  4.Search  
193:  5.Modify  
194:  6.Display record  
195:  7.Delete Record  
196:  0.Exit  
197:  1  
198:  Enter the number of students :   
199:  3  
200:  Enter name :   
201:  Shivkumar  
202:  Enter roll number  
203:  2079  
204:  Enter mobile number :   
205:  8007921033  
206:  Enter name :   
207:  mujahid  
208:  Enter roll number  
209:  2077  
210:  Enter mobile number :   
211:  9658623552  
212:  Enter name :   
213:  pradip  
214:  Enter roll number  
215:  2078  
216:  Enter mobile number :   
217:  8569853658  
218:  Enter Choice:  
219:  MENU  
220:  1.Create  
221:  2.Display  
222:  3.Insert  
223:  4.Search  
224:  5.Modify  
225:  6.Display record  
226:  7.Delete Record  
227:  0.Exit  
228:  2  
229:        Name       Roll Number          Mobile Number  
230:       Shivkumar          2079          8007921033  
231:       mujahid          2077          9658623552  
232:       pradip          2078          8569853658  
233:  Enter Choice:  
234:  MENU  
235:  1.Create  
236:  2.Display  
237:  3.Insert  
238:  4.Search  
239:  5.Modify  
240:  6.Display record  
241:  7.Delete Record  
242:  0.Exit  
243:  3  
244:  Insert Record  
245:  Enter name :   
246:  Anonymous  
247:  Enter roll number  
248:  2000  
249:  Enter mobile number :   
250:  9800000000  
251:  Enter Choice:  
252:  MENU  
253:  1.Create  
254:  2.Display  
255:  3.Insert  
256:  4.Search  
257:  5.Modify  
258:  6.Display record  
259:  7.Delete Record  
260:  0.Exit  
261:  4  
262:  Enter Roll Number to Search  
263:  2000  
264:  Location=4  
265:  Enter Choice:  
266:  MENU  
267:  1.Create  
268:  2.Display  
269:  3.Insert  
270:  4.Search  
271:  5.Modify  
272:  6.Display record  
273:  7.Delete Record  
274:  0.Exit  
275:  6  
276:  Enter Roll Number to Search  
277:  2000  
278:        Name       Roll Number          Mobile Number  
279:       Anonymous          2000          9800000000  
280:  Enter Choice:  
281:  MENU  
282:  1.Create  
283:  2.Display  
284:  3.Insert  
285:  4.Search  
286:  5.Modify  
287:  6.Display record  
288:  7.Delete Record  
289:  0.Exit  
290:  7  
291:  Enter Roll Number to Search  
292:  2000  
293:  Enter Choice:  
294:  MENU  
295:  1.Create  
296:  2.Display  
297:  3.Insert  
298:  4.Search  
299:  5.Modify  
300:  6.Display record  
301:  7.Delete Record  
302:  0.Exit  
303:  0 
 
https://drive.google.com/file/d/0BzbuE6BfgoRbc0xtSHJRaTN6Nmc/edit?usp=sharing
 
 

No comments:

Post a Comment

Ads Inside Post