-
Notifications
You must be signed in to change notification settings - Fork 0
/
candyJar.cpp
216 lines (142 loc) · 4.18 KB
/
candyJar.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "candyJar.hpp"
using namespace std;
candyJar::candyJar(int Capacity){
capacity=Capacity;
width=40;
height=50;
AllJars.push_back(candyJar(capacity));
}
candyJar::candyJar(double Width, double Height, int Capacity){
width=Width;
height=Height;
capacity=Capacity;
}
candyJar::candyJar(vector<candy> allcandy){
AllCandy=allcandy;
}
void candyJar::addCandy(candy& Candy,const candyJar& Jar){
capacity-=1;
if (Jar.capacity>Jar.AllCandy.size()){
if (AllCandy.size()==0){
Candy.x=Candy.length;
Candy.y=Candy.width;
}
else if (AllCandy.size()>=1){
if ((Jar.width-AllCandy.back().x) > Candy.length ){
Candy.x=AllCandy.back().x+Candy.length;
Candy.y=AllCandy.back().y;
}
else{
Candy.x=Candy.length;
Candy.y=AllCandy.back().y+Candy.width;
}
}
AllCandy.push_back(Candy);
}
}
/*
Can you write a couple functions to save the current program state to emory.
Basically, the information to store is this: each jar objects, which there
are any number of needs to be stored. Each one has the following things stored
with it: title(string), times worked on(int), descpition(string), status(I/C/O)
(ICO is Incomplete/Complete/Ongoing). I think the best way to do this is two functions.
One to read/one to write. So basically, if the read function can decode each thing in a
loop (jar1,jar2, jar3 in any order) then I can just add some constructors to your code.
For the write function, it should write a single jar to the file, overwriting it if it
is already stored in the file. Other than that, its coming along smoothly.
*/
/*
void write(candyJar& Jar){
ofstream myfile;
myfile.open("jar.txt");
for (vector<candy>::iterator it = Jar.AllCandy.begin();it!= Jar.AllCandy.end();++it)
{
myfile<<*it<<" ";
}
myfile.close();
}
void read(){
ifstream myfile;
myfile.open("jar.txt");
string candy;
vector<string> Allcandy;
while (not myfile.eof()){ // read a line
myfile >> candy;
Allcandy.push_back(candy);
}
myfile.close();
// print out the contents of the file.
for (auto candy: Allcandy){
cout << candy << endl;
}
}
*/
void write(){
ofstream myfile;
myfile.open("jar.txt");
for (vector<candy>::iterator it = Jars.begin();it!= Jars.end();++it)
{
myfile<<*it<<" "<<"\n";
}
myfile.close();
}
void read(){
ifstream thisfile; // note: this is an IFSTREAM, "I" stands for INPUT
string theline; // we will store the text using these variables
vector<string> contents;
//read a file one line at a time with getline()
thisfile.open("jar.txt");
while (getline(thisfile,theline)) // read a line
contents.push_back(theline); // put it in the vector
thisfile.close();
// print out the contents of the file.
for (auto line: contents){
title=line.substr(0, findFirstof("|"));
line.erase(title.size()+1,0);
descpition=line.substr(0, findFirstof("|"));
line.erase(descpition.size()+1,0);
timeWorkedOn=line.substr(0, findFirstof("|"));
line.erase(timeWorkedOn.size()+1,0);
status=line.substr(0,findFirstof("|"));
line.erase(status.size()+1,0)
ICO=line.substr(0,findFirstof("|"));
}
}
void write(){}
void reloadJars(){
}
int main(){
candy Candy1 = { .width=10, .length=20};
candyJar Jar(40, 50, 40);
Jar.addCandy(Candy1, Jar);
candy Candy2 = { .width=10, .length=20};
Jar.addCandy(Candy2, Jar);
write(Jar);
read();
cout<<"There is "<<Jar.AllCandy.size()<<" pieces of candy"<<endl;
cout<<"They are at:";
cout<<"("<<Candy1.x<<","<<Candy1.y<<")";
cout<<"("<<Candy2.x<<","<<Candy2.y<<")"<<endl;
for (vector<candy>::iterator it = Jar.AllCandy.begin();it!= Jar.AllCandy.end();++it)
{
cout<<*it<<endl;
}
return 0;
}
/* get line from file
string theline; // we will store the text using these variables
vector<string> contents;
//read a file one line at a time with getline()
thisfile.open("reading_a_file_by_lines.cpp");
while (getline(thisfile,theline)) // read a line
contents.push_back(theline); // put it in the vector
thisfile.close();
// print out the contents of the file.
for (auto line: contents){
cout << line << endl;
}
*/