-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEntry_Array.cpp
More file actions
98 lines (78 loc) · 1.79 KB
/
GameEntry_Array.cpp
File metadata and controls
98 lines (78 loc) · 1.79 KB
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
/*
* GameEntry_Array.cpp
*
* Created on: Dec 8, 2015
* Author: astroy
*/
#include <iostream>
#include "HelpFunc.hpp"
using namespace std;
class GameEntry{
public:
GameEntry(const string& n="", int s=0);
string getName() const; // accessor
int getScore() const;
private:
string name;
int score;
};
GameEntry::GameEntry(const string& n, int s): name(n),score(s) {}
string GameEntry::getName() const { return name;}
int GameEntry::getScore() const { return score;}
class Scores{
public:
Scores(int m=10, int n=0);
~Scores();
void add (const GameEntry& e);
GameEntry remove(int i)
throw(IndexOutOfBounds);
private:
int maxEntries; // max entries
int numEntries; // actual total of entries
GameEntry* entries; // array of entries
};
Scores::Scores(int m, int n): maxEntries(m), numEntries(n){
entries = new GameEntry[maxEntries];
}
Scores::~Scores(){
delete[] entries;
}
void Scores::add(const GameEntry& e){
int newScore = e.getScore();
if((numEntries < maxEntries)){
entries[numEntries] = e;
numEntries++;
}else if(newScore > entries[maxEntries-1].getScore()){
entries[maxEntries-1] = e;
}else return;
int i=numEntries-2;
while((i>=0)&&(entries[i].getScore()<entries[i+1].getScore())){
swap(entries[i],entries[i+1]);
i--;
}
}
GameEntry Scores::remove(int i) throw(IndexOutOfBounds){
if((i<0)or(i>maxEntries-1))
throw IndexOutOfBounds("Invalid Index");
GameEntry e = entries[i];
while(i<numEntries-1){
entries[i] = entries[i+1];
i++;
}
numEntries--;
return e;
}
int main(){
Scores s;
GameEntry q("A",3), r("B",100), e("C",2);
s.add(q);
s.add(r);
s.add(e);
GameEntry t=s.remove(0);
cout<<t.getScore()<<t.getName()<<endl;
t=s.remove(0);
cout<<t.getScore()<<t.getName()<<endl;
t=s.remove(0);
cout<<t.getScore()<<t.getName()<<endl;
return 0;
}