## 4364: (2006广东省赛) Problem I Parse 程序解析

[提交][状态][讨论版][命题人:]

## 题目描述

The CSV ("Comma Separated Value") file format is often used to exchange data between disparate applications. The file format, as it is used in Microsoft Excel, has become a pseudo standard throughout the industry, even among non-Microsoft platforms.

The CSV file format in this problem is defined below:

A CSV file consists of zero or more records, and a record consists of zero or more fields.

Each record ends with a line feed character (ASCII/LF=0x0A).However, fields may contain embedded line-breaks (see below) so a record may span more than one line.

Fields are separated with commas. Example: John,Doe,120 any st., "Anytown, WW",08123

Leading and trailing space-characters in a field are ignored, if they are not delimited by double quotes.So John ,   Doe ,... resolves to "John" and "Doe", etc. Space characters can be spaces, or tabs.

Fields with embedded commas must be delimited with double quote characters.In the above example. "Anytown, WW" has to be delimited in double quotes because it has an embedded comma.

Fields that contain double quote characters must be entirely surrounded by double quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.So, John "Da Man" Doe would convert to "John ""Da Man""",Doe, 120 any st.,...

A field that contains embedded line-breaks must be surrounded by double quotes.So:

Field 1: Conference room 1

Field 2:

John,

Please bring the M. Mathers file for review

-J.L.

Field 3: 10/18/2002

...
can be converted to:

Conference room 1, "John,

Please bring the M. Mathers file for review

-J.L. ",10/18/2002,...

Note that this is a single CSV record, even though it takes up more than one line in the CSV file. This works because the line breaks are embedded inside the double quotes of the field.

Fields with leading or trailing spaces must be delimited with double-quote characters.So, to preserve the leading and trailing spaces around, the last name above: John ,"   Doe   ",...

Fields may always be delimited with double quotes.The delimiters will always be discarded.
Your task is to write a program as a CSV parser which can parse the input CSV file correctly.

## 输入

Your program should read the content of input file and parse it according to the CSV format described above. Input is ended by EOF.
Every field contains no more than 50 characters, and there are at most 500 fields in the input file.

## 输出

If the input file isn’t a legal CSV format file, simply output “Wrong Format” in one line, otherwise you should output all fields. Please output one line-break (‘\n’) after each field. E.g. suppose the input file is like:

field11, field12, …, field1n
field21, field22, …, field2n
fieldm1, fieldm2, …, fieldmn

So the output should actually be like:

field11
field12
field1n
field21
field22
field2n
fieldm1
fieldm2
fieldmn
Note that one field may contain embedded line-breaks, so in the output file one field may occupy several lines. (You can refer to the Sample Input 3 and Sample Output 3 for details).

## 样例输入

Sample Input 1:
field11, field12
field21, field22

Sample Input 2:
John,   Doe   , "Anytown, WW" , "John ""Da Mon"" Von"

Sample Input 3:
Conference room 1, "John,
Please bring the M.Mathers file for review
-J.L.
", 3/20/2006

Sample Input 4:
John, "Wrong field" sample", Bob

## 样例输出

Sample Input 1:
field11, field12
field21, field22

Sample Input 2:
John,   Doe   , "Anytown, WW" , "John ""Da Mon"" Von"

Sample Input 3:
Conference room 1, "John,
Please bring the M.Mathers file for review
-J.L.
", 3/20/2006

Sample Input 4:
John, "Wrong field" sample", Bob

[提交][状态]