Let's continue to learn the grammar of C. The structure in struct and C is the same as the UDT (structure) established in our PLC. It stores different types of data.
There is a saying that I think is very important: methods are dependent on the existence of structures and objects. After that, we will study more deeply.
Struct ure:
It can help us declare different types of variables at once.
Syntax:
[public] struct structure name
{
Members;
}
The following example states:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace draft 8 { 9 class Program 10 { 11 public struct Person 12 { 13 public string name; 14 public int age; 15 public char gender; 16 } 17 static void Main(string[] args) 18 { 19 Person zsPerson; 20 zsPerson.name = "Zhang San"; 21 zsPerson.age = 18; 22 zsPerson.gender = 'male'; 23 24 Console.ReadKey(); 25 } 26 } 27 }
It's worth noting that when declaring a structure, if we don't add public, we can't create a value for the structure. If we don't add public, the default is private. And the variables we create above Main under the namespace are not actually variables, but fields.
The difference between a variable and a field is that a variable can store a value and then be overwritten all the time, while a field is similar to our PLC background data and can store several values.
And I'm going to raise a question here. I've seen several videos and data. There are different ways to name fields. The summary is as follows
(1) Fields and variables should be named differently, for example: _Name
(2) There are also objections to this naming method, because in complex programming tasks, it may affect the role of cross reference with other languages, such as VB. net.
In the process of further study in the future, we are slowly experiencing this, and welcome the gods to solve my doubts.
array
Stores multiple variables of the same type at once.
Syntax:
Array type [] array name = new array type [array length];
Once the length of the array is fixed, it cannot be changed.
For arrays of type int [], the initial value is 0, the initial value of string [] array is null, and the initial value of bool [] array is false.
Here are some ways to declare arrays
int[] nums = new int[10]; / / array elements are not declared, recommended
int[] nums = {1,2,3,4,5,6}; / / implicitly declares element and length, recommended
int[] nums = new int[3]{1,2,3}; / / not recommended, cumbersome and consistent length and number of elements.
int[] nums = new int[]{1,2,3,4,5}; / / similar to type 2
Let's look at exercise 1: get the maximum, minimum, sum and average values from an array of integers.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace draft 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 1,2,3,4,5,6,7,8,9,0}; 14 int max = nums[0]; 15 int min = nums[0]; 16 int sum = 0; 17 18 for (int i = 0; i < nums.Length; i++) 19 { 20 if (nums[i] > max) 21 { 22 max = nums[i]; 23 } 24 25 if (nums[i] < min) 26 { 27 min = nums[i]; 28 } 29 sum += nums[i]; 30 } 31 Console.WriteLine($"The maximum value of this array is{max},The minimum value is{min},The sum is{sum},The average value is{sum/nums.Length}"); 32 Console.ReadKey(); 33 } 34 } 35 }
Exercise 2:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace draft 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] names = { "Lao Yang","Lao Su","Lao Zou","Tiger","Old ox","Old horse"}; 14 string str = null; 15 16 for (int i = 0; i < names.Length-1; i++) 17 { 18 str += names[i] + "|"; 19 } 20 Console.WriteLine(str+names[names.Length-1]); 21 Console.ReadKey(); 22 } 23 } 24 }
Exercise 3: do the following for an integer array: if the element is a positive number, add the element + 1; if the element is a negative number, add the element - 1, and the element is 0, unchanged.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace draft 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 1,-2,3,-4,5,6,0}; 14 for (int i = 0; i < nums.Length; i++) 15 { 16 if (nums[i] > 0) 17 { 18 nums[i] += 1; 19 } 20 else if (nums[i] < 0) 21 { 22 nums[i] -= 1; 23 } 24 else 25 { 26 27 } 28 } 29 30 for (int i = 0; i < nums.Length; i++) 31 { 32 Console.WriteLine(nums[i]); 33 } 34 Console.ReadKey(); 35 } 36 } 37 }
Exercise 4:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace draft 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] names = { "I","yes","Good person"}; 14 for (int i = 0; i < names.Length/2; i++) 15 { 16 string temp = names[i]; 17 names[i] = names[names.Length - 1 - i]; 18 names[names.Length - 1 - i] = temp; 19 } 20 for (int i = 0; i < names.Length; i++) 21 { 22 Console.Write(names[i]); 23 } 24 Console.ReadKey(); 25 } 26 } 27 }
Exercise 5: bubble sorting: it is to arrange the elements in an array from large to small and from small to large.
Analysis: need two cycles, outer cycle, control comparison times, inner cycle, control exchange times.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace draft 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 9,8,7,6,5,4,3,2,1,0}; 14 for (int i = 0; i < nums.Length-1; i++) 15 { 16 for (int j = 0; j < nums.Length-1-i; j++) 17 { 18 if (nums[j] > nums[j+1]) 19 { 20 int temp = nums[j]; 21 nums[j] = nums[j + 1]; 22 nums[j + 1] = temp; 23 } 24 } 25 } 26 for (int i = 0; i < nums.Length; i++) 27 { 28 Console.WriteLine(nums[i]); 29 } 30 Console.ReadKey(); 31 } 32 } 33 }
It is worth noting that the array subscript in C ා is the opposite of that in PLC. The 0 of array subscript in C ා is calculated from the left element.
In fact, this bubbling method is also used in the interview. In our C, we can directly use one method to solve Array.Sort(); (only ascending)
Array.Reverse(); (reverse order) if you want to descend: call Array.Sort() first; then call Array.Reverse().