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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
#include <iostream>
#include <string>
struct Data
{
double number{0};
std::string name;
void setNumber(double aNumber){number = aNumber;}
void setName(std::string aName){name = aName;}
Data operator+ (const Data& dat)
{ return { dat.number + this->number, "SUM"}; }
bool operator== (const Data& rhs) const
{ return this -> number == rhs.number; }
bool operator<= (const Data& rhs)
{ return this -> number <= rhs.number; }
bool operator>= (const Data& rhs)
{ return this -> number >= rhs.number; }
friend std::ostream& operator<<(std::ostream& os, const Data& dat)
{ os << '(' << dat.number << ' ' << dat.name << ')'; return os; }
};
class List
{
private:
struct node
{
Data item; // Store the data of each node
node* next; // Will point to the next node in the list
};
typedef struct node* nodePtr;
nodePtr head; // head of node
nodePtr temp; // temprary node
nodePtr curr; // current node
public:
List();
void addNode(Data);
void deleteNode(Data);
void printList();
//void appendNode(Data);
void displayList() const;
Data getHighestRainfall() const;
Data getLowestRainfall() const;
Data getTotal() const;
};
List::List()
{
head = nullptr;
temp = nullptr;
curr = nullptr;
}
void List::addNode(Data dat)
{
nodePtr n = new node; // node pointer equals new node
n->next = nullptr;
n->item = dat;
if (head != nullptr)
{
curr = head;
while (curr->next != nullptr)
{
curr = curr->next;
}
curr->next = n;
}
else
{
head = n;
}
}
void List::deleteNode(Data dat) {
nodePtr delPtr = nullptr;
temp = head;
curr = head;
while (curr != nullptr && !(curr->item == dat) )
{
temp = curr;
curr = curr->next;
}
if (curr == NULL) {
std::cout << dat << "was not in the list\n";
delete delPtr;
}
else {
delPtr = curr;
curr = curr->next;
temp->next = curr;
if (delPtr == head) {
head = head->next;
temp = NULL;
}
delete delPtr;
std::cout << "The value " << dat << "was deleted\n";
}
}
void List::printList() {
curr = head;
while (curr != nullptr) {
std::cout << curr->item << std::endl;
curr = curr->next;
}
}
Data List::getLowestRainfall() const
{
nodePtr np = head;
Data lowest = np->item;
while (np)
{
if (np->item <= lowest)
lowest = np->item;
np = np->next;
}
return lowest;
}
Data List::getHighestRainfall() const
{
nodePtr np = head;
Data highest = np->item;
while (np)
{
if (np->item >= highest)
highest = np->item;
np = np->next;
}
return highest;
}
Data List::getTotal() const
{
nodePtr np = head;
Data total = np->item;
while (np)
{
total = total + np->item;
np = np->next;
}
return total;
}
void List::displayList() const
{
nodePtr np = head;
while (np)
{
std::cout << "Element " << np->item;
np = np->next;
}
}
int main()
{
// SETUP SOME DATA
Data year_2021[]
{
{12, "Jan"}, {34, "Feb"}, {56, "Mar"}, {1,"Apr"}, {7,"May"}, {32,"Jun"},
{7,"Jul"},{3,"Aug"},{1,"Sep"}, {5,"Oct"},{30,"Nov"},{7,"Dec"}
};
size_t SIZE = sizeof(year_2021)/ sizeof(Data);
// CREATE AND POPULATE A LIST
List rainfall_record;
for(int i = 0; i < SIZE; i++){ rainfall_record.addNode( year_2021[i] ); }
// SOME TESTS
rainfall_record.displayList();
std::cout << year_2021[2] + year_2021[4] + year_2021[5];
std::cout << "Minimum: " << rainfall_record.getLowestRainfall();
std::cout << "Maximum: " << rainfall_record.getHighestRainfall();
std::cout << rainfall_record.getTotal();
Data clone = year_2021[3];
std::cout << clone << '\n';
// MENU STUFF
char userAnswer{};
do {
std::cout
<< '\n'
<< "A--Add a month of statistics\n"
<< "E--Edit a month of statistics\n"
<< "P--Print report\n"
<< "Q--Quit\n"
<< "Choice: ";
std::cin >> userAnswer;
userAnswer = toupper(userAnswer); // <--
int m{0}; // <---
Data temp;
switch (toupper(userAnswer))
{
case 'A':
std::cout << "Enter month: ";
std::cin >> temp.name;
std::cout << "Enter rainfall (in Inches): ";
std::cin >> temp.number;
rainfall_record.addNode(temp);
break;
case 'E':
std::cout << "Enter month: 0 or 1(error)";
std::cin >> m;
if (m==0)
{
std::cout << "Enter new rainfall (in Inches): ";
}
else
std::cout << "ERROR: Invalid Month\n";
break;
case 'P':
std::cout << "FULL LIST: ";
rainfall_record.displayList();
std::cout << " Total rainfall: " << rainfall_record.getTotal() << '\n';
std::cout << "Highest rainfall: " << rainfall_record.getHighestRainfall() << '\n';
std::cout << " Lowest rainfall: " << rainfall_record.getLowestRainfall() << '\n';
break;
case 'Q':
std::cout << "Quitter\n";
break;
default:
std::cout << "Bad answer\n";
}
} while ( userAnswer != 'Q');
std::cout << "That's it!\n";
return 0;
}
|