-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass1_2.c
167 lines (167 loc) · 3.83 KB
/
ass1_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include<stdio.h>
#include<string.h>
void add();
void display();
void search();
void modify();
int search_roll();
int search_name();
int search_in();
struct st
{
int roll;
char name[40];
float percentage;
};
struct st stu[100]; // variable array declared globally.
int top=-1; // top counts the index of the last entry.
void main()
{
int ch;
char c;
do
{
printf("Press 1 to add a new record.\nPress 2 dislpay all the existing record.\nPress 3 to search record of any condition.\nPress 4 to modify a record.\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
modify();
break;
default:
printf("You have entered a wrong input.\n");
break;
}
printf("Do you wanna continue: [y/n] ");
scanf(" %c",&c);
/* code */
} while (c!='n');
}
void add()
{
top++;
printf("Enter the roll no of the student: ");
scanf("%d",&stu[top].roll);
printf("Enter the name of the student: ");
scanf("%s",stu[top].name);
printf("Enter the percentage of marks of %s: ",stu[top].name);
scanf("%f",&stu[top].percentage);
}
void display()
{
int i;
if(top==-1)
printf("No information about the students.\n");
else
{
printf("Records are: \n");
for(i=0;i<=top;i++)
printf("Roll no. %d\nName: %s\nPercentage: %f\n\n",stu[i].roll,stu[i].name,stu[i].percentage);
}
}
void search()
{
int ch,in=-1;//if in remain -1. that means there's no record with given information.
printf("Press 1 to search by index.\nPress 2 to search by roll.\n Press 3 to search by Name.\nEnter your choice: ");
scanf("%d",&ch);
switch (ch)
{
case 1:
in=search_in();
break;
case 2:
in=search_roll();
break;
case 3:
in=search_name();
break;
default:
printf("Wrong Input!!");
break;
}
if(in==-1)
printf("There's no record having your provided information.\n");
else
printf("Roll no. %d\nName: %s\nPercentage: %f\n\n",stu[in].roll,stu[in].name,stu[in].percentage);
}
void modify()
{
int ch,in=-1;//if in remain -1. that means there's no record with given information.
printf("Press 1 to modify by index.\nPress 2 to modify by roll.\n Press 3 to modify by Name.\nEnter your choice: ");
scanf("%d",&ch);
switch (ch)
{
case 1:
in=search_in();
break;
case 2:
in=search_roll();
break;
case 3:
in=search_name();
break;
default:
printf("Wrong Input!!");
break;
}
if(in==-1)
printf("There's no record having your provided information.\n");
else
{
printf("Enter the roll no of the student: ");
scanf("%d",&stu[in].roll);
printf("Enter the name of the student: ");
scanf("%s",stu[in].name);
printf("Enter the percentage of marks of %s: ",stu[in].name);
scanf("%f",&stu[in].percentage);
}
}
int search_in()
{
int in=-1,ind;
printf("Enter the index you wanna search for: ");
scanf("%d",&ind);
if(ind<=top)
in=ind;
return in;
}
int search_name()
{
int i,in=-1;
char name[30];
printf("Enter the name of the student: ");
scanf("%s",name);
for(i=0;i<=top;i++)
{
if(strcmp(name,stu[i].name)==0)
{
in=i;
break;
}
}
return in;
}
int search_roll()
{
int in=-1,i,roll;
printf("Enter the roll of the student to search: ");
scanf("%d",&roll);
for(i=0;i<=top;i++)
{
if(roll==stu[i].roll)
{
in=i;
break;
}
}
return in;
}