The enumeration and structure of c entry USES the Detail of console to receive the string output in the opposite direction

  • 2020-06-19 11:34:56
  • OfStack

Enumeration, structure
Enumerations have limited types (short, byte...) And the same, I found some examples of enumerations on MSDN and found this one good:


enum myWeekDay { Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
int i = 3;
myWeekDay today = (myWeekDay)i;

Enumeration needs to be declared first and then used by creating a new variable (today) for the enumeration type. Enumerating the default base type value starts at 0 and increments by 1, which is called an arithmetic sequence.

When enumerating declarations, it is recommended to place them in a namespace, or, of course, in a class or structure. When assigning other variables to enumerated types, you need to cast, for example: today = (myWeekDay)myByte. There is, of course, the Enum.Parse(typeof(),) command, which I won't go into in detail until we get to it.

The structure (struct) is better, with different underlying data types supported within one structure. It is also necessary to declare the structure first, and then declare the variable as the structure type, thus using:


enum orientation : byte { north = 1, south = 2, east = 3, west = 4};
struct route
{
    public orientation direction;
    public double distance;
}

Use public: Make the members of the structure accessible to the code calling the structure. Specific applications:


route myRoute;
int myDirection = -1;
double myDistance;
Console.WriteLine("1) North\n2) South\n3) East\n4) West");
do
{
    Console.WriteLine(" Please select a 1 Driving directions: ");
    myDirection = Convert.ToInt32(Console.ReadLine());
} while (myDirection < 1 || myDirection > 4);
Console.WriteLine(" Please enter the 1 A distance: ");
myDistance = Convert.ToDouble(Console.ReadLine());
myRoute.direction = (orientation)myDirection;
myRoute.distance = myDistance;
Console.WriteLine(" Specify the direction of  {0}  The distance is  {1}", myRoute.direction, myRoute.distance);

Note that the 1 line of myRoute. direction = (orientation)myDirection should be the application scenario of enumeration: you only need to specify the basic type value i in the enumeration value, and then the corresponding string can be obtained by means of (enumName)i.

Declare a structure: route(the name of the structure created) myRoute, and then access the members of the structure through the myRoute. attribute.

An array of

The array in the impression is always more complex stuff. 1 good example: we need to store the names of 10 students, using an array can be solved simply, first declare the array:


string[] friendNames = new string [arrayCount];
string[] friendNames = {" zhang 3"," li 4"," The king 5"," xie 6"," Chen 7"};

Line 1 only initializes the array size, optional constants or constants, and then assigns the array elements using friendNames[0]. Line 2 declares the array directly and initializes its contents.

You can use the for loop with the size of friendNames.Length to access the array value, noting that the position of the first element is 0. You can also use foreach without worrying about going out of array. foreach differs from for in that foreach is read-only.


foreach (string myStr in friendNames)
{
    Console.WriteLine(myStr);
}

Multidimensional arrays are divided into rectangular arrays (rows with the same number of elements in each row) and jagged arrays (rows with different number of elements in each row). There may be {column 1{row 1{layer 1, layer 2}, row 2}, column 2... }), of course, the same way can be used to extract the contents of all elements, more than 1 foreach can be nested:


int[][] jaggedIntArray = { new int[] { 1, 2, 3 }, new int[] { 4, 5 }, new int[] { 6, 7, 8, 9 }, new int[] {10, 11} };
foreach(int[] topArray in jaggedIntArray)
{
    foreach (int bottomArray in topArray)
    {
        Console.Write("{0} ", bottomArray);
    }
    Console.Write("\n");
}

Note: [] or {} are used here, not in the way of (), don't always put the wrong parentheses, it is very low-level.

String handling
This is a lot more interesting. Each character in a string can be accessed via myString[1], with the first character bit being 0:


String myString = "  HeLlO WoRlD ";
char myChar = myString[1];

Using ToCharArray(), get 1 char array after splitting each character of myString:


char[] myChars = myString.ToCharArray();

You can also use myString.Length to get the number of strings, myString.ToLower () to uppercase, and myString.ToUpper () to lowercase. Note: ToLower(), ToUpper() do not change the case of the value of the variable itself. You also need to use myString = myString.ToLower () to change the value of the variable itself.

myString. Trim() can remove Spaces before and after strings, as well as TrimStart() and TrimEnd(), which remove Spaces before and after strings, respectively. You can also use Trim(myChar[]) to specify that the content before and after removal is not limited to Spaces (char[] myChar = {','s'}) :


myString = "  sfrost/110110200010101100-13090909880 ";
char mykg = ' ';
char[] myxhx = {'-','/'};
String[] myStrings = myString.Trim(mykg).Split(myxhx);
Console.WriteLine("myStrings[0] = {0}", myStrings[0]);
Console.WriteLine("myStrings[1] = {0}", myStrings[1]);
Console.WriteLine("myStrings[2] = {0}", myStrings[2]);

In C#, for example, you can break down the personal information entered by a user into one or more keywords. The Split() method used in the example can also be used to indicate the identity of the decomposition using the char array. Under emphasis 1, the location of the Split decomposition string can be the location of multiple different tags.

conclusion
From the beginning of this chapter, a lot of content can be done immediately 1 small topic, hehe. Whether it's enumerations (of the same type), structures (of different types of members), arrays (dimension 1, rectangle [dimension 2], jagged [irregular multidimensional]), and string handling, how the values between enumerations and ordinary variables are converted, how arrays are declared, initialized, accessed, and so on. String manipulation is fun, especially split, and replace, and the char array is the icing on the cake.

Attached: Exercise cases

Write a console application that receives user input strings and outputs the sequence of strings in the opposite direction of input:


Console.WriteLine(" Please enter the string you want to swap places with: ");
String myString = Console.ReadLine().Trim();
Console.WriteLine("{0}", myString.Length);
String tmpStr = "";
for (int i = myString.Length; i > 0; i--)
{
    tmpStr += myString[i-1];
}
Console.WriteLine(tmpStr);

Write a console application that receives user input strings and replaces all no in the string with yes:


enum orientation : byte { north = 1, south = 2, east = 3, west = 4};
struct route
{
    public orientation direction;
    public double distance;
}
0

Write a console application that quotes each word in a string (I assume there must be Spaces between words) :


Console.WriteLine(" Please enter words with Spaces: ");
String myWord = Console.ReadLine().Trim();
String[] myWords = myWord.Split(' ');
myWord = "";
foreach(String tmpWord in myWords)
{
    myWord += "\"" + tmpWord + "\" ";
}
Console.WriteLine(" Sentences with quotes added: {0}", myWord);


Related articles: