Simplification of C program algorithms

Source: Internet
Author: User
For more information about C program algorithm simplification-Linux general technology-Linux technology and application, see the following. I have read some algorithms in recent days and found that the class-based algorithms written in C are relatively good:
There are thousands of programs with high complexity, but those with low complexity are limited and often unique. There are several ways to turn high-complexity algorithms into low-complexity algorithms:
1. Changing the nesting of multiple condition sentences into a loop sentence:

Example: Income Tax
("Y" indicates income tax, and "x" indicates monthly income ).
[Table = 390] [tr] [td]
[/Td] [td] 0; x <= 2000; [/td] [/tr] [tr] [td] [/td] [td] (x-2000) * 3%; 2000 100000; [/td] [/tr] [/table]
General idea:
Use a nested multi-Condition Statement:
# Include
Mian ()
{
Flaot x, y;
Scanf ("% f", & x );
If (x <= 2000)
Y = 0;
Else
If (x <= 5000)
Y = (x-2000) * 0.03;
Else
If (x <= 10000)
Y = (X-5000) * 0.05 + 90;
Else
If (x <= 100000)
Y = (x-10000) * 01 + 340;
Else
Y = (x-100000) * 0.3 + 9340;
Printf ("% f", y );
}
This kind of program is really nice, but not universal (the so-called general, that is, the program that only changes data without changing the program after the situation changes ).
The following is a common algorithm for comparison:
# Include "stdio. h"
# Define N 4
Void main ()
{
Float a [N + 1] = {100000 },
B [N + 1] = {0.0, 0.01, 0.05, 0.1, 0.3 },
C [N] = {0, 90, 340,9340 };
Float x, y;
Int I;
Printf ("Please input your Wages :");
Scanf ("% f", & x );

/* This section is added to me to prevent input format errors.
While (1)
{
If (x> = 0)
Break;
Else
{
Printf ("ERROR! \ NPlease input your Wages again :");
Scanf ("% f", & x );
}
}
*/
I = N;
While (x <= )
I = I-1;
Printf ("% f % d", y, I );
Y = (x-) * B+ C [I-1];

/* I prefer to use the for Loop
For (I = N; x; I --);
Y = (x-) * B+ C [I-1];
*/
Printf ("tax payable: % f", y );
}
In this way, you do not need to modify the program even if you need to modify the data, and the complexity of the program is greatly reduced.
Related Article

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.