What is table-drivenCodeThe essence, high flexibility, high efficiency, simpler, and experience the charm of code! (Suitable for beginners)
I am very grateful for the elegance of the Code.
Let's take a look at the code. Have you ever written any code similar to the following?
Fragment:
1 If (( ' A ' > = Inputchar & inputchar <= ' Z ' ) | ( ' A ' > = Inputchar & inputchar <= ' Z ' )) 2 { 3 ..... 4 } 5 Else If (Inputchar = ' ' ) | (Inputchar = ' , ' ) | (Inputchar = ' ; ' ) | (Inputchar = ' : ' ) | (Inputchar = ' ? ' )) 6 { 7 .... 8 }
Long logic judgment. If you are maintaining the code, isn't it hard for you to endure? You have to spend a lot of time reading the previous author's code.
Okay. Now there is a problem:
Assume that you are writing a calculation of the Medical Insurance RateProgramThese rates change with age, gender, marital status, and smoking. Will the code you wrote be like this?
Example:
1 If (Gender = Gender. Female) 2 { 3 If (Maritalstatus = maritalstatus. Single) // Single? 4 { 5 If (Smokingstatus = Smokingstatus. nonsmoking) 6 { 7 If (Age < 18 ) 8 { 9 Rate = 200.00 ; 10 } 11 Else If (Age < 19 ) 12 { 13 Rate = 250.00 ; 14 } 15 ... 16 Else If (Age < 65 ){ 17 Rate = 450.00 18 } 19 } 20 Else 21 { 22 If (Age < 18 ) 23 { 24 Rate = 250.00 ; 25 } 26 Else If (Age < 19 ) 27 { 28 Rate = 300.00 ; 29 } 30 ... 31 Else If (Age < 65 ){ 32 Rate = 575.00 33 } 34 } // Complete single smokingstatus 35 } 36 Else If (Maritalstatus = Maritalstatus. Married) 37 { 38 If (Smokingstatus = Smokingstatus. nonsmoking) 39 { 40 ... 41 } 42 Else 43 { 44 ... 45 } // Finished married smokingstatus 46 } 47 } 48 .... 49 }
It's easy, isn't it? Is it complicated? You can imagine the amount of code required to finish this question.
Well, now let's look at the table-driven solution to this type of logical control structure.
First, we need to save these rates to the array of all element indexes. A simple description is that you need to define a type, such:
1 // Define smoking status 2 3 Enum Smokingstatus 4 { 5 Smoking_firs = 0 , 6 Smoking = 0 , 7 Nonsmoking = 1 , 8 Smoking_last =1 9 } 10 11 // Define a married state 12 13 Enum Maritalstatus 14 { 15 Marital_first = 0 , 16 Single =0 , 17 Maritied = 1 , 18 Marital_last = 1 19 20 }
Yes, it is only an enumeration type. The above only defines two types. We also need to define a gender type, so we will not repeat it here.
Now that the data type has been defined, the difference is data. You can find the data from the database or read it from the file. It depends on how you choose it,
The following defines a method:
1 //Return Rate2 3PrivateDouble rate (smokingstatus smoking, maritalstatus making, gender, age)4 {5 Double rate;6//...7ReturnRate;8}
What we need to do now is assume that the insurer's information is as follows: 18 years old, single, smoking, and female.
Like this, elegant code display:
1 //Returns the corresponding rate.2 3 DoubleRate = Rate (smokingstatus. Smoking, maritalstatus. Single, Gender. Female,18);
Do you think this sentence is very simple? You can understand what it means. It's just literally, right, that's that simple.
In addition to tedious judgment, all we need to do is to add the corresponding data of the rate. What else can we do through rate processing? How can I increase the rate? Since it is a table-driven method, let's look at what the table is like?
This is only one of the table-driven methods. Thank you for your correction. Learn and improve together!
Sharing is a virtue ~ Goodbye ~~~