-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass2.cpp
165 lines (159 loc) · 3.63 KB
/
ass2.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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
struct Student
{
string name;
int age;
string rollNumber;
double marks;
};
bool check_no(string rollNumber) {
for (char c : rollNumber) {
if (!isdigit(c)) {
cout << "Roll number should be a positive integer" << endl;
return false;
}
}
return true;
}
void merge(vector<Student> &arr, int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
vector<Student> Left, Right;
for (i = 0; i < n1; i++)
Left.push_back(arr[left + i]);
for (j = 0; j < n2; j++)
Right.push_back(arr[mid + 1 + j]);
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (Left[i].marks >= Right[j].marks)
{
arr[k] = Left[i];
i++;
}
else
{
arr[k] = Right[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = Left[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = Right[j];
j++;
k++;
}
}
void mergeSort(vector<Student> &arr, int l, int r)
{
if (l < r)
{
int m = (r + l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
Student students[100];
ofstream myfile;
ifstream myReadFile;
myReadFile.open("export.txt", ios::in);
myfile.open("export.txt", std::ios_base::app);
string mystring;
int n;
cout << "**********************REGISTER YOUR STUDENTS************************" << endl;
cout << "Enter the number of students : ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter the name of student " << i + 1 << ":" << endl;
cin >> students[i].name;
age:
cout << "Enter the age of student " << i + 1 << ":" << endl;
cin >> students[i].age;
cin.ignore();
if (students[i].age > 30 || students[i].age < 10)
{
cout << "Entered age is invalid!" << endl;
goto age;
}
rollNumber:
cout << "Enter the rollNumber of student " << i + 1 << ":" << endl;
cin >> students[i].rollNumber;
cin.ignore();
if(!check_no(students[i].rollNumber)){
goto rollNumber;
}
marks:
cout << "Enter the marks of student " << i + 1 << ":" << endl;
cin >> students[i].marks;
cin.ignore();
if (students[i].marks > 50 || students[i].marks < 0)
{
cout << "Entered marks are invalid!!" << endl;
goto marks;
}
}
for (int i = 0; i < n; i++)
{
if (myfile.is_open())
{
myfile << students[i].rollNumber << "," << students[i].name << "," << students[i].age << "," << students[i].marks;
myfile << "\n";
}
}
cout << endl << endl << "**********DISPLAY STUDENTS***********" << endl << endl;
vector<Student> data;
int average = 0;
myfile.close();
if (myReadFile.is_open())
{
string field1, field2, field3;
while (getline(myReadFile, mystring))
{
Student student;
stringstream ss(mystring);
getline(ss, field1, ',');
getline(ss, student.name, ',');
getline(ss, student.rollNumber, ',');
getline(ss, field3, ',');
student.age = stoi(field1);
student.marks = stof(field3);
data.push_back(student);
average += student.marks;
}
}
myReadFile.close();
mergeSort(data, 0, data.size() - 1);
for (int j = 0; j < data.size(); j++)
{
cout << j+1 << "." << endl ;
cout << "Roll Number: " << data[j].rollNumber << endl;
cout << "Name: " << data[j].name << endl;
cout << "Age: " << data[j].age << endl;
cout << "Marks: " << data[j].marks << endl;
cout << endl << endl;
}
cout << endl
<< endl;
cout << "*** Average marks = " << average / data.size() << "*****" << endl;
}