Welcome to the CSIG, a Special Interest Group of the ACGNJ. The subject for this month is a discussion about the "State Method" of designing or coding programs in C++.
Even the smallest of programs can benefit from the STATE METHOD. It is simply a way of looking at a problem and dividing it into smaller and smaller parts until each part is in its simplest form. Code can then be written for each part with little concern for the other parts. Finally all parts are combined.
The other day a friend asked me to simplify his analysis of a detailed Access2k program. He had used the Access documenter to produce a listing of the Queries in his program. The listing was a simple text file but it was 234,045 lines long! We needed to filter out some very specific information. Using the "State Method" I was able to write a simple C++ console mode program in about an hour. The program reduced and summarized the original data file down to only a few thousand lines.At the meeting, I will present several programs that I have written using this concept. Some programs are only a page long, but they still benefit from this concept. The "bottom line" is that a program can usually be written faster and with fewer errors using this concept. Additionally, debugging becomes easier.
The main presentation will be a more complex program that translates a CSV file into text. To simplify the design task, a "state diagram" is created first. State diagrams represent the logic of an algorithm. Using the diagram the programmer is able to write the program without undue complexity. He uses the logic contained within each "state" to write each small portion of his program. Joining the states within one big loop solves the total problem.
As always, you can download this month's code from the ACGNJ ftp server at [ftp://ftp.acgnj.org/pub/acgnj/cug]. The ftp site has code and programs from past meetings too.
while (infile.ReadString(lbuff)) { if (0==lbuff.GetLength()) continue; switch (state) { case SEARCHING: if (0==lbuff.Find("Query:")) { state = FOUNDHEADER; cout << endl << lbuff.Left(60).GetBuffer(100) << endl; } break; case FOUNDHEADER: if (0==lbuff.Find("Query Parameters")) { state = FOUNDDATA; cout << lbuff.GetBuffer(100) << endl; } break; case FOUNDDATA: if (0==lbuff.Find("Columns")) { state = SEARCHING; } else cout << lbuff.GetBuffer(100) << endl; break; } }