New Lab Manual
User Manual:
Open the PDF directly: View PDF .
Page Count: 4
New Style Lab Manual
Prabodh
September 18, 2017
1
1 Program 1
Question goes here
1#i n c l u d e <s t d i o . h>
2#i n c l u d e <s t d l i b . h>
3#i n c l u d e <string .h>
4t y p e d e f s t r u c t {
5unsigned emp id ;
6char emp name [ 2 5 ] ;
7char emp dept [ 2 5 ] ;
8unsigned emp sa l a ry , emp age ;
9}employee t ;
10
11 void fnAddRecord ( void ) ;
12 void fnSearchEmpID( i n t ) ;
13 void fnSearchEmpSal( i n t ) ;
14 void fnSearchEmpDept( char [ ] ) ;
15 void fnSearchEmpAge( i n t ) ;
16 void fnDisplayAllRecords(v o i d ) ;
17
18 i n t main ( )
19 {
20 i n t i d , s a l , age , i C h o i c e ;
21 ch a r dept [ 1 0 ] ;
22
23 f o r ( ; ; )
24 {
25 printf(”\n1 . Add Record \n2 . D is pl ay R ecords \n3 . Sear c h Employee by ID\n” ) ;
26 printf(” 4 . S ea rch Employee by Dept\n5 . Se arch Employee by s a l a r y \n” ) ;
27 printf(” 6 . S ea rch Employee by Age\n7 . Ex it ” ) ;
28 printf(”\nEnt er y ou r c h o i c e : ” ) ;
29 s c a n f ( ”%d ” ,& i C h o i c e ) ;
30
31 switch(iChoice)
32 {
33 case 1 :
34 fnAddRecord ( ) ;
35 break ;
36
37 case 2 :
38 printf(”\n Employee D e t a i l s \n” ) ;
39 f n D i s p l a y A l l R e c o r d s ( ) ;
40 break ;
41
42 case 3 :
43 printf(”\nEn te r t he em p id t h a t you want t o s e a r c h \n” ) ;
44 s c a n f ( ”%d” ,& i d ) ;
45 fnSearchEmpID( id ) ;
46 break ;
47
48 case 4 :
49 printf(”\nEn te r t he d ept t h a t you want to s e a r c h \n” ) ;
50 s c a n f ( ”%s ” , dept ) ;
51 fnSearchEmpDept ( dept ) ;
52 break ;
53
54 case 5 :
55 printf(”\nEnter the s a l a r y t ha t you want to s ea rc h \n” ) ;
56 s c a n f ( ”%d” ,& s a l ) ;
57 fnSearchEmpSal ( s a l ) ;
58 break ;
59
60 case 6 :
61 printf(”\nEn te r t he a ge t h at you want t o s e a r c h \n” ) ;
62 s c a n f ( ”%d” ,& a ge ) ;
63 fnSearchEmpAge( age ) ;
64 break ;
65 case 7 : e x i t ( 0 ) ;
66 }
67 }
68 return 0 ;
69 }
70
71 void fnDisplayAllRecords ()
72 {
73 i n t iCount = 0;
2
74 emp l o y e e t ep ;
75 FILE ∗f p ;
76
77 fp = f open ( ”emp . d at ” ,”r”) ;
78 i f ( fp==NULL)
79 {
80 printf(”\n F i l e do es n ot e x i s t \n” ) ;
81 return ;
82 }
83 while( f s c a n f ( fp , ”%d%s%s%d%d ” ,&ep . emp id , ep . emp name , ep . emp dept , &ep . em p sa lary , &ep .
emp age ) !=EOF)
84 {
85 printf(”%d\t%s \t%s \t%d\t%d\n” , ep . emp id , ep . emp name , ep . emp dept , ep . emp s alary , ep .
emp age ) ;
86 iCount++;
87 }
88 i f (0 == iCount )
89 printf(”\nNo Rec ord s found \n” ) ;
90 f c l o s e ( f p ) ;
91 }
92
93 void fnAddRecord ( )
94 {
95 FILE ∗f p ;
96 emp l oy e e t emp ;
97
98 printf(”\nEnter Employee d e t a i l s \n” ) ;
99 printf(”\nID : ” ) ;
100 s c a n f ( ”%d” ,&emp . emp id ) ; g e t c h a r ( ) ;
101 printf(”\nName : ” ) ;
102 f g e t s (emp . emp name , 2 5 , s t d i n ) ;
103 printf(”\nDept : ” ) ;
104 f g e t s (emp . emp dept , 2 5 , s td i n ) ;
105 printf(”\nSal a ry : ” ) ;
106 s c a n f ( ”%d” ,&emp . em p s al ar y ) ;
107 printf(”\nAge : ” ) ;
108 s c a n f ( ”%d” ,&emp . emp age ) ;
109
110 fp = f open ( ”emp . d at ” ,” a” ) ;
111 f p r i n t f ( fp , ”%d\t%s \t%s \t%d\t%d\n” ,emp . emp id , emp . emp name , emp . emp dept , emp . emp s alary ,
emp . emp age ) ;
112 f c l o s e ( f p ) ;
113 }
114
115 void fnSearchEmpID( i n t i d )
116 {
117 i n t iCount = 0;
118 emp l o y e e t ep ;
119 FILE ∗f p ;
120
121 fp = f open ( ”emp . d at ” ,”r”) ;
122 i f ( fp==NULL)
123 {
124 printf(”\n F i l e do es n ot e x i s t \n” ) ;
125 return ;
126 }
127 while( f s c a n f ( fp , ”%d%s%s%d%d ” ,&ep . emp id , ep . emp name , ep . emp dept , &ep . em p sa lary , &ep .
emp age ) !=EOF)
128 {
129 i f ( ep . emp id == i d )
130 {
131 printf(”%d\t%s \t%s \t%d\t%d\n” , ep . emp id , ep . emp name , ep . emp dept , ep . emp s alary ,
ep . emp age ) ;
132 iCount++;
133 }
134 }
135 i f (0 == iCount )
136 printf(”\nNo Rec ord s found \n” ) ;
137 f c l o s e ( f p ) ;
138 }
139
140 void fnSearchEmpSal( i n t s a l )
141 {
142 i n t iCount = 0;
143 emp l o y e e t ep ;
144 FILE ∗f p ;
3
145
146 fp = f open ( ”emp . d at ” ,”r”) ;
147 i f ( fp==NULL)
148 {
149 printf(”\n F i l e do es n ot e x i s t \n” ) ;
150 return ;
151 }
152 while( f s c a n f ( fp , ”%d%s%s%d%d ” ,&ep . emp id , ep . emp name , ep . emp dept , &ep . em p sa lary , &ep .
emp age ) !=EOF)
153 {
154 i f ( ep . e m p s a l ar y == s a l )
155 {
156 printf(”%d\t%s \t%s \t%d\t%d\n” , ep . emp id , ep . emp name , ep . emp dept , ep . emp s alary ,
ep . emp age ) ;
157 iCount++;
158 }
159 }
160 i f (0 == iCount )
161 printf(”\nNo Rec ord s found \n” ) ;
162 f c l o s e ( f p ) ;
163 }
164
165 void fnSearchEmpDept( char d ep t [ ] )
166 {
167 i n t iCount = 0;
168 emp l o y e e t ep ;
169 FILE ∗f p ;
170
171
172 fp = f open ( ”emp . d at ” ,”r”) ;
173 i f ( fp==NULL)
174 {
175 printf(”\n F i l e do es n ot e x i s t \n” ) ;
176 return ;
177 }
178 while( f s c a n f ( fp , ”%d%s%s%d%d ” ,&ep . emp id , ep . emp name , ep . emp dept , &ep . em p sa lary , &ep .
emp age ) !=EOF)
179 {
180 i f ( ! strcmp ( ep . emp dept , de pt ) )
181 {
182 printf(”%d\t%s \t%s \t%d\t%d\n” , ep . emp id , ep . emp name , ep . emp dept , ep . emp s alary ,
ep . emp age ) ;
183 iCount++;
184 }
185 }
186 i f (0 == iCount )
187 printf(”\nNo Rec ord s found \n” ) ;
188 }
189
190 void fnSearchEmpAge( i n t age )
191 {
192 i n t iCount = 0;
193 emp l o y e e t ep ;
194 FILE ∗f p ;
195
196 fp = f open ( ”emp . d at ” ,”r”) ;
197 i f ( fp==NULL)
198 {
199 printf(”\n F i l e do es n ot e x i s t \n” ) ;
200 return ;
201 }
202 while( f s c a n f ( fp , ”%d%s%s%d%d ” ,&ep . emp id , ep . emp name , ep . emp dept , &ep . em p sa lary , &ep .
emp age ) !=EOF)
203 {
204 i f ( ep . emp age == age )
205 {
206 printf(”%d\t%s \t%s \t%d\t%d\n” , ep . emp id , ep . emp name , ep . emp dept , ep . emp s alary ,
ep . emp age ) ;
207 iCount++;
208 }
209 }
210 i f (0 == iCount )
211 printf(”\nNo Rec ord s found \n” ) ;
212 }
Programs/01EmployeeDB.c
4