StringRecursion。
Recursion
Recursion is an abstraction that is defined in terms of itself. Examples include mathematical abstractions such as Fibonacci series, Factorials, and Exponentials, and other abstractions such as the Towers of Hanoi puzzle, the Eight Queens problem and Fractal Geometry. Recursion is actually an alternate form of repetition. So far, we’ve employed repetition using loops (while, do-while, and for), but many interesting and challenging computational problems are difficult to solve in an iterative fashion. Often these problems have very simple and elegant recursive solutions.
After working though the following exercises using the methods discussed in class, you should begin to get a feel for “thinking recursively” as a problem solving technique.
Strings
Representing textual information using sequences of characters is common throughout computing. Names, sentences, text, prompts, etc. all need a proper representation. We’ve been using string literals since the first week of the course when we discovered how to write “Hello World” to the computer display.
In addition to representing non-numeric and qualitative information, string objects are frequently used in engineering and scientific applications to input and process large text files containing measurements, experimental test data, and so forth.
Comma Separated Value files (CSV)
One common format for representing large data files is the “comma separated value” (CSV) format. For example, if you have data in an Excel spreadsheet, it is a simple matter to output it to a file in CSV format.
The CSV format is simplicity itself: each data element is stored as a text string separated from the succeeding value by a single comma (‘ , ‘). If the file data is tabular (rows and columns), the rows are separated using a single newline character (‘\n’). This makes it possible to use a standard text-editor to view the contents of any CSV formatted file in order to determine its organization.
Getline()
Processing the data in a CSV formatted file requires that you identify and separate the individual values in each row. Individual rows are read using the getline(stream, string) function which returns the entire comma-separated row as a C++ string object. The row string must then be parsed by your program, separating values from the comma separators. This requires some fluency with manipulating strings which we will explore in this Lab Exercise.
Converting Strings to Values
The “values” that are obtained from each CSV string (row) are character strings. Before they can be used in a computation, they must be converted to numeric values (floating-point or integer). There are many clever ways to do this in C++, but the simplest method is to use the functions atof and/or atoi, found in the standard C++ library: lt;cstdlibgt;
. These functions both take a c_string
as an argument and return a double or int respectively. Recall that c_strings
and C++ string objects are not the same thing! If the character string you wish to convert to a value is stored in a C++ string object, you first need to convert it to a c_string
using the string class method c_str
:
1
|
double foo = atof(somestring.c_str())
|
Mystery-Box Challenge
Here is your next mystery-box challenge. Determine what the following function does and explain it to one of your TAs.