-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arrays and sorting.cpp
179 lines (167 loc) · 3.19 KB
/
Arrays and sorting.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
#include <iostream>
#include <string>
using namespace std;
struct node
{
string name;
int id;
};
class Data
{
public:
void SortByName();
void SortByID();
void DataEntry();
Data()
{
size = 5;
employee = new node[size];
counter = 0;
}
private:
int size;
int counter;
node* employee;
void SizeIncrease();
int AsciiTotal(string&);
};
void Data::DataEntry()
{
cout << "Leave the name space empty to terminate adding to the list\n";
string name;
for (int i = 0; i < size; i++)
{
cin.ignore();
cout << "Name: ";
getline(cin, name);
if (name == "")
break;
else
{
(employee + counter)->name = name;
cout << "ID: ";
cin >> (employee + counter)->id;
counter++;
if (counter >= size)
SizeIncrease();
}
}
}
void Data::SortByID()
{
int small,place;
string smalln;
if (counter > 1)
{
for (int j = 0; j < counter - 1; j++)
{
place = j;
for (int i = j + 1; i < counter; i++)
{
if ((employee + i)->id < (employee + place)->id)
{
small = (employee + i)->id;
smalln = (employee + i)->name;
place = i;
}
}
if (place == j);
else
{
(employee + place)->id = (employee + j)->id;
(employee + place)->name = (employee + j)->name;
(employee + j)->id = small;
(employee + j)->name = smalln;
}
}
}
else cout << "There is only one data entry. It is already sorted\n";
for (int i = 0; i < counter; i++)
{
cout << "Name: " << (employee + i)->name << endl;
cout << "ID: " << (employee + i)->id << endl;
}
system("pause");
}
void Data::SortByName()
{
int small, place;
string smalln;
if (counter > 1)
{
for (int j = 0; j < counter - 1; j++)
{
place = j;
for (int i = j + 1; i < counter; i++)
{
if ((employee + i)->name.at(0) < (employee + place)->name.at(0))
{
small = (employee + i)->id;
smalln = (employee + i)->name;
place = i;
}
}
if (place == j);
else
{
(employee + place)->id = (employee + j)->id;
(employee + place)->name = (employee + j)->name;
(employee + j)->id = small;
(employee + j)->name = smalln;
}
}
}
else cout << "There is only one data entry. It is already sorted\n";
for (int i = 0; i < counter; i++)
{
cout << "Name: " << (employee + i)->name << endl;
cout << "ID: " << (employee + i)->id << endl;
}
system("pause");
}
int Data::AsciiTotal(string& name)
{
int total = 0;
for (int i = 0; i < name.length(); i++)
total += name.at(i);
return total;
}
void Data::SizeIncrease()
{
node* temp = new node[size * 2];
for (int i = 0; i < size; i++)
*(temp + i) = *(employee + i);
employee = temp;
size *= 2;
}
int main()
{
Data d;
char op;
do
{
system("cls");
cout << "1. Enter List\n";
cout << "\n2. Sort by ID\n";
cout << "\n3. Sort by Name\n";
cout << "\nChoose: ";
cin >> op;
switch (op)
{
case '1':
d.DataEntry();
break;
case '2':
d.SortByID();
break;
case '3':
d.SortByName();
break;
default:
cout << "Choose from the following\n";
system("pause");
system("cls");
break;
}
} while (true);
}