-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhonoreWork.cpp
197 lines (193 loc) · 3.8 KB
/
honoreWork.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <type_traits>
using namespace std;
struct Student
{
string studentName;
int age;
int rollNumber;
float marks;
};
void merge(vector<Student> &arr, int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
vector<Student> L, R;
for (i = 0; i < n1; i++)
L.push_back(arr[l + i]);
for (j = 0; j < n2; j++)
R.push_back(arr[m + 1 + j]);
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i].marks >= R[j].marks)
{
arr[k] = L[i];
i++;
}
else
{
arr[k].marks = R[j].marks;
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(vector<Student> &arr, int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void getStudent(string fileName)
{
cout << "================Register Students====================" << endl;
cout << endl;
ofstream file1;
file1.open(fileName, ios::out | ios::app);
Student student;
if (file1.is_open())
{
cout << "Enter the student name" << endl;
getline(cin, student.studentName);
file1 << student.studentName << ",";
cout << "Enter the student age" << endl;
cin >> student.age;
if (student.age > 30 || student.age < 10)
{
cout << "Not eligible should be between 10 and 30 years" << endl;
exit(0);
}
file1 << student.age << ",";
cout << "Enter the student roll number" << endl;
cin >> student.rollNumber;
if (!cin.fail())
{
if (student.rollNumber > 0)
{
file1 << student.rollNumber << ",";
cout << "Enter the student marks" << endl;
}
else
{
cout << "RE enter the roll number it's invalid" << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> student.rollNumber;
cin.ignore(256, '\n');
}
}
else
{
cout << "RE enter the roll number it's invalid" << endl;
cin.clear();
cin.ignore(256, '\n');
cin >> student.rollNumber;
cin.ignore(256, '\n');
}
cin >> student.marks;
if (student.marks < 0 || student.marks > 50)
{
cout << "Invalid marks try again" << endl;
cin >> student.marks;
}
file1 << student.marks << endl;
file1.close();
}
else
{
cout << "Couldn't open file" << endl;
}
}
void display(string fileName)
{
cout << endl;
cout << endl;
cout << "================Students====================" << endl;
cout << endl;
ifstream file;
file.open(fileName, ios::in);
string line;
vector<Student> students;
cout << "Studentname"
<< "\t"
<< "age"
<< "\t"
<< "roll"
<< "\t"
<< "marks" << endl;
cout << endl;
if (file.is_open())
{
string field1, field2, field3;
while (getline(file, line))
{
Student student;
stringstream ss(line);
getline(ss, student.studentName, ',');
getline(ss, field1, ',');
getline(ss, field2, ',');
getline(ss, field3, ',');
student.age = stoi(field1);
student.rollNumber = stoi(field2);
student.marks = stof(field3);
students.push_back(student);
}
file.close();
}
cout << "Length: " << students.size() - 1;
mergeSort(students, 0, students.size() - 1);
for (int i = 0; i < students.size(); i++)
{
cout << students[i].studentName + "\t"
<< "\t" << students[i].age << "\t" << students[i].rollNumber << "\t" << students[i].marks << endl;
}
}
int main()
{
ofstream file;
int choice;
cout << "============Welcome==================" << endl;
cout << endl;
cout << "Choose in the following" << endl;
cout << endl;
cout << "1.Register Student" << endl;
cout << "2.View all Students" << endl;
cout << "Enter choice" << endl;
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
getStudent("export.txt");
break;
case 2:
display("export.txt");
break;
default:
cout << "Invalid choice try again" << endl;
}
return 0;
}