Level 2013 C + + 15th week (Spring) project--input and output stream and file file operation

Source: Internet
Author: User
Tags readfile wxwidgets

Course home in:http://blog.csdn.net/sxhelijian/article/details/11890759. A complete teaching program and resources Link


This week's program reading and program debugging required documents, please go to http://pan.baidu.com/s/1qW59HTi download.


The first part of the reading program (when executing the program, download the source code from the above link)
1, reading the textbook example 13.1 to example 13.7 of the program, according to the function used in the function of the limitations of their own design input test data, execution procedures.
(1) To understand the use of each function in relation to its expected result;
(2) In-depth understanding of the data flow classes at design time for application developers to provide convenience.
2, read the textbook example 13.11 to Example 13.13 of the program and execute the program. For each example, focus on the following questions:
(1) What does this example do with the file and what type of open file is used?
(2) How to implement input/output from file in this example? What kind of operator or function is used?
3, read the following two paragraphs of the procedure. Say the function of the program and verify it at the machine (please build the a.txt).


Program 1#include <iostream> #include <fstream>using namespace Std;int main () {    ifstream readFile;    Ofstream WriteFile;    char ch;    Readfile.open ("A.txt", ios::in);    Writefile.open ("B.txt", ios::out);    while (Readfile.get (CH))        writefile.put (CH);    Readfile.close ();    Writefile.close ();    cout << "finish!" << Endl;    return 0;}

Program 2#include <iostream> #include <fstream> #include <cstring>using namespace Std;int main () {    Ifstream ReadFile;    Ofstream WriteFile;    Char ch[100];    Readfile.open ("A.txt", ios::in);    Writefile.open ("B.txt", ios::out);    while (!readfile.eof ())    {        readfile.getline (ch,100, ' \ n ');        Writefile.write (Ch,strlen (CH));        Writefile.write ("\ n", 1);    }    Readfile.close ();    Writefile.close ();    cout << "finish!" << Endl;    return 0;}

The second part of the practice project
"Item 1-Input of payroll data"
(1) Enter the wages of a number of employees (1000-10000 of the number), the numbers in descending order after the output. Please work on the basis of the following procedures.
#include <iostream>using namespace Std;int main () {    double salarys[500];    int n=0;    while (Cin>>salarys[n])    {        n++;   Reads the data from the CIN stream    }    //Sorts the wages of N employees and outputs        return 0;}
Input Example:
3736.98 9169.35 5595.57 1006 6631.19 5917.13 6076.89 9692.97 9771.73^z
Output Example:
9771.73 9692.97 9169.35 6631.19 6076.89 5917.13 5595.57 3736.98 1006
Tips: In the form above, you can enter an indeterminate number of data. When typing with the keyboard, you need to use ^z as the end.

(2) In the software development process, in order to debug the program, often need to execute the program many times. The input of multiple executions is often the same, which is problematic, especially when the amount of data is too high. People are doing things with no technical content!
The easy way to use it is. Use input redirection to avoid this repetition: Please build a file a.txt. Include the data from the above input example, in the program (1). While before adding a sentence freopen ("A.txt", "R", stdin), return 0; add a fclose (stdin) before it;. Then execute the program .... Get used to it in such a way.


Tip 1:The practice of OJ is often used. Before you open the test, do a program template such as the following, this can improve efficiency. This technique can be used for exams. Just putting the input in the input example into the file can only be committed by paying attention to the Freopen line.


#include <iostream> #include <cstdio>    //Support Freopen header file using namespace Std;int main () {    freopen (" A.txt "," R ", stdin);    Here is the code to write       return 0;}
(3) in the actual application, the data is not input from the keyboard. The way to enter redirects is also not a "legitimate" channel. The practice in project is to put the data into a dedicated file for processing.
Download the file Salary.txt, which has a salary of no more than 500 employees.

The program reads into the wages of these workers. is sorted in descending order and then displayed and saved to the file ordered_salary.txt.


"Item 2-list of students saved with a file"
The names and C + + classes, high scores, and English grades of several students are saved in file Score.dat.
(1) Define student class, including name, C + + class, high and English scores and total data members, the member function depends on the need to determine.

Define class Student{public:    //Declare necessary member functions private:    string name;    Double cpp;    Double math;    Double 中文版;    Double total;    static int stu_num;  Number of students, the appropriate static double total_sum for the static members of the class    ;//Student total score and};

(2) Use an array of objects to store the student's scores, read the scores and calculate the total score. The information of the students with the total score above the average score and the Pass_score.dat is saved to the file.
int main () {    Student stud[200],t;//stud[200] is an array of objects that holds the data    string sname;    Double Total_avg;    int i=0;    Reads data from the file Score.dat. Saved to an array of objects    //scores above average score and the information of the students who did not hang the section save to file Pass_score.dat    return 0;}
Discuss: the second solution to the number of students and their total score is to use global variables.

But these two kinds of information are related to students. is the "attribute" of the student. It is appropriate to be a data member of the student class, both of which are determined by the students in general. Appropriate for use as a static data member.

View the relevant sections of the textbook. Review how to deal with it.



"Project 3-oop edition electronic Dictionary"
Make a simple e-dictionary.

In the file dictionary.txt. It is a dictionary of English-Chinese comparison, with a vocabulary of nearly 8,000 words. Use ' \ t ' between English and Chinese interpretation and part of speech.
Programming. The user enters the English word. Display part of speech and Chinese interpretation.
Tip 1: assume that you want to use OOP to complete the dictionary (and of course it can be done with OO methods). The ability to define a word class to represent an entry. Among the data members of string 中文版; Represents the English word, string Chinese; indicates the corresponding Chinese meaning, string word_class; denotes the part of speech of the word. It is also possible to define a dictionary class to represent dictionaries in which word words[8000] members represent entries in the dictionary, int wordsnum, and the number of entries in the dictionary. The entry is read from the file in the constructor, and a member function is specifically added to look up the word.
Tip 2: the words in the file are sorted, so when searching, use the binary search method to improve the efficiency.
Tip 3: Such projects are best organized in multiple files
"Project 3 expand 1 (choose to do)" to make this dictionary. Read an article and output an explanation of the words in it.

For example, for Aboutcpp.txt, the output can be seen in the following left-hand image (see also where improvements are being made).
"Project 3 expansion 2 (optional)" Trial wxwidgets do a form version of the electronic dictionary. For example, the image below looks like this:

  


"Item 4-Procedures for handling C + + source code"

The functionality of code formatting is provided in the IDE, such as Codeblocks. Completion of such a function of the program. The operational data is a source code file written in C + +.

A C + + source file is a text file that can be manipulated by a program.

The integrated development Environment (IDE) compiles the program, and the "data" of the operation is the source program. In the compilation. To do the lexical check and grammar checking of the source program, we might also do some work such as target code generation, code optimization and so on.

The relevant technology will be learned in the principles of compiling lesson.

These technologies can be used in a number of areas and, of course, allow us to have a deeper understanding of programming languages.
This project will use the C + + source program as the object of operation. Completed a series of processing of the source program. Each function can be programmed to be implemented separately (it is recommended to use such a simple scheme). can also be integrated (making the IDE work for yourself).
(1) Read into a C + + program and infer if there is only one main () function. Output "No problem found temporarily". Or "no main () function", or "multiple main () functions cannot be defined".
Tip 1: simple processing can only infer "main ()". Consider the fact that the various possibilities, the parentheses after main has a random number of spaces and void should be counted. It is recommended that the simplest case be handled.
Tip 2: It is recommended to design a function. Compare the code you read to the string "Main ()".

The function is used to infer whether S1 is "included" in the read line of S2, when called, the actual participation at the S1 is "main ()", so that the "abstract" level is improved, easier to implement, and the corresponding higher code quality.
(2) read into a C + + program, so that all the left curly braces "{" and the right curly brace "}" in a separate line, the new program is saved to a. cpp file, and the screen shows the processed program, displayed with the line number.
(3) read into a C + + program. Enter m, n two digits, and n lines of code from line m will be used as gaze (that is, precede these lines with "//"), and the new program is saved in a. cpp file. and displays the processed programs on the screen. Add line numbers when displayed.
(4) (optional) read into a C + + program, all the gaze in the program (including//form and/*...*/form) is deleted, the new program is saved to a. cpp file, and displays the processed program on the screen, plus line number when displayed.


(5) (optional) read into a C + + program. In the program: (1) All the left curly braces "{" and the right curly brace "}" are separate lines; (2) Each statement occupies a single line, (3) each line adopts a uniform reduction of emissions (each encounter a "{", its next line of the program, the first meaningful symbol before the number of spaces added 4 (can also be added a ' \ T '), each encounter a "}", its next line of the program. The number of spaces before the first meaningful symbol is reduced by 4 (can also be a ' \ t ')).
(6) (optional) based on (5). Using the Wxwidgets design interface, select the name of the source file to process in the dialog box and specify the target file name for processing.

If you click button "..." In, the file will be selected using the Open File dialog (Wxfiledialog) Form.

  




================= Helijian csdn Blog column ================= |== It Student Growth Guide Column column category folder (not regularly updated) ==| |== C + + Classroom Online Column The course teaching link (sub-course grade) ==| |== I wrote the book-"The reverse of the university-to the positive energy of IT students" ==| ===== the runway for it novices to take off. University ===== with students to enjoy happiness and passion


Level 2013 C + + 15th week (Spring) project-input and output stream and file file operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.