On May 10, 10:06 pm, Chelong <hl.systemthin...@gmail.comwrote:
hey
now i have a problem,for me a c++ beginner.so i need ur help?
here i have a text file from the copy of excel data,like this:
================================================== =====apple====pear===
First XXZHYY3A(Lily) 0 20 8 0 0 7 XXZHYY3A£¨Lily£© 8 0 0 8 8 8
XXZHYY3A£¨huang£© 7 0 0 7 7 5 Senco XXZHYY3B(huang) 22 22 3.652 22
22 4 XXZHYY11A£¨cheng£© 70¸ö 60 9.96 7 70
Third XSJ14B(huang) 20 20 6.6666 20 20 5 XSJ14B(Lily) 23 23 7.66659 23
23 8 XXZHYY6(huang) 26 30 9.9999 26 26 9
......................and it is a big txt file.
================================================== ====================
first,i want to make it like this:
================================================== =====apple====pear===
First XXZHYY3A(Lily) 0 20 8 0 0 7
First XXZHYY3A£¨Lily£© 8 0 0 8 8 8
First XXZHYY3A£¨huang£© 7 0 0 7 7 5 Senco XXZHYY3B(huang) 22 22
3.652 22 22 4
Senco XXZHYY11A£¨cheng£© 70¸ö 60 9.96 7 70
Third XSJ14B(huang) 20 20 6.6666 20 20 5
Third XSJ14B(Lily) 23 23 7.66659 23 23 8
Third XXZHYY6(Lily) 26 30 9.9999 26 26 9
......................and it is a big txt file.
================================================== ====================
how can i implica it usin STL,or another fast way,is it vector can
implic it?
and the second,i want to select the line that include First &&Lily ,
Lily && Third ,and so on ,
and then i want to get ,for example:
Lily first apple = 0 + 8 pear = 7 + 8
Lily Third apple = 23 + 26 pear =8 + 9
Do u know what i mean?
waiting for ur help !
thanks advan!
Start with the basics, assuming a file that looks like this:
string1
string2
string3
How can you read each line and store these in a std::vector<
std::string >?
Hint:
a) #include <fstream>
b) open a std::ifstream to the target file and
c) use std::getline(...) to read one line at a time into a std::string
buffer.
d) push_back() the contents of the buffer onto the std::vector<
std::string >.
Then display each std::string element to the screen to prove it worked
(by iterating through the vector's elements).
That should be doable without expending a huge amount of effort. If
you can read and store one string, you can store 10,000 or more of
them.
The Excel data:
Analyze the fields and decide what types (std::string, int, double,
long, etc) each field might require. In other words, how must each
field be stored in the Record?
Then write a class that reflects the structure of the record involved.
class Record
{
...
};
Prove that you can initilaize and store an instance of that
record( don't worry about reading these from a file now).
Record record("Lily", 0, 20, 8, 0, 0, 7); //or whatever...
and display the record using std::cout (prefereably using a global
op<< overload).
Its all about methodology. One step at a time. Its the same principle
if you are new to a language or if you have 20 years experience. Once
you can store one Record, you can store any number of these:
std::vector< Record records;
The Record class needs to be copyable and assigneable to satisfy the
requirements of a dynamic container like a std::vector. (what does
that mean?)
To parse the Excel data:
Once you get this far, you can start zeroing in on the problem at
hand: reading and storing the tabulated data.
Keep it simple, use a file with only one single Record to parse with:
First XXZHYY3A(Lily) 0 20 8 0 0 7