Enter the Chinese Date and output the date of the corresponding digit format

Source: Internet
Author: User

Case: compile a function to convert the input Chinese date to the Arabic digit date.

For example, from January 1, 2012 to January 21, to December 21. (Process "10"

Question: 1. * day 10; 2. day 13; 3. 23; 4. day 30 ;)
In section 4, the translation of "10" is different. 1 → 10; 2 → 1; 3 → No translation; 4 → 0 [the year part is not
The '10' may appear in the month and day sections .]


Test data: July 6, July 7
July 9, October 24, 2010, and October 20, 2010


Analysis: You can consider using dictionaries to give keys and output corresponding values.

The difficulty lies in dealing with ten
Case handling
A number of 10
The two numbers are in the first 1
The two numbers are in the last 0.
The three numbers are in the middle (certain) and are not translated.

Using system;

Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading. tasks;


Namespace Date Format Conversion
{
Class Program
{

Static void main (string [] ARGs)
{
While (true)
{
Console. writeline ("Enter the date ");
String dates = console. Readline (). Trim ();
Stringbuilder date = new stringbuilder ();
Date = getdateformat (dates); // the specific format conversion is encapsulated into a method
Console. writeline (date );
Console. readkey ();
Console. Clear ();
}

}
/// <Summary>
/// Method: dictionary, and then call format conversion
/// </Summary>
/// <Param name = "date"> </param>
/// <Returns> </returns>
Private Static stringbuilder getdateformat (string date)
{
// Declare the dictionary and store the key values related to the date
Dictionary <char, char> DIC = new dictionary <char, char> ();
// Give a string, put the keys and values that may appear, and perform 10 Special Operations
String TXT = "0 0 a 1 2 3 3 4 5 6 6 7 8 9 9 ";
// Split the TXT file to obtain the key values and store them in the dictionary.
String [] keyValue = TXT. Split (New char [] {''}, stringsplitoptions. removeemptyentries );
// Traverse the array and store the corresponding key-value pairs respectively
For (INT I = 0; I <keyValue. length; I ++)
{
If (! Dic. containskey (keyValue [I] [0])
{
Dic. Add (keyValue [I] [0], keyValue [I] [1]);
}
}
// Call the method to return the date in numeric format
Return getdigitdate (DIC, date );

}


/// <Summary>
/// Method: Format Conversion
/// </Summary>
/// <Param name = "dic"> </param>
/// <Param name = "date"> </param>
/// <Returns> </returns>
Private Static stringbuilder getdigitdate (Dictionary <char, char> DIC, string date)
{
// Convert string to character array
Char [] CHS = date. tochararray ();
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <CHS. length; I ++)
{

Processing 10
Translation:
10
Dozens of items
Dozens of 0
Dozens of non-Translators
If (CHS [I] = '10 ')
{
X indicates the characters not included in the dictionary.
// 10x10x10
If (! Dic. containskey (CHS [I-1]) &! Dic. containskey (CHS [I + 1])
{
SB. append ("10 ");
}
// Dozens of X 1
Else if (! Dic. containskey (CHS [I-1]) & DIC. containskey (CHS [I + 1])
{
SB. append ("1 ");
}
// Dozens of X dozens of x 0
Else if (DIC. containskey (CHS [I-1]) &! Dic. containskey (CHS [I + 1])
{
SB. append ("0 ");
}
// Dozens of rows x null
Else if (! Dic. containskey (CHS [I-1]) &! Dic. containskey (CHS [I + 1])
{
SB. append ("");
}
}
Else if (DIC. containskey (CHS [I])
{
// Output the corresponding value if the dictionary exists.
SB. append (DIC [chs [I]. tostring ());
}
Else if (CHS [I] = 'Year' | CHS [I] = 'month ')
{
// Year and month-replaced
SB. append ("-");
}
}
Return Sb;
}

}

}

Conclusion: the difficulty of this question lies in the processing of ten. It can return 10, 1, 0, ""; four cases; the dictionary is of the <char, char> type, to judge each character of the input string, if you use CHS [I] = 'X'; then

Return new string (CHS); the string method will be tricky (for example, it is difficult to process it when it is translated into 10); at this time, you need to think of the stringbuilder method.


Enter the Chinese Date and output the date of the corresponding digit format

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.