For more information about debugging, see the c program below.

Source: Internet
Author: User

I recently wrote a lot of programs and found that the results were very good. Some Complex programs I understood before, but I could not write them quickly. Everything benefited from debugging, in the past, I never thought that debugging could do much. It was a big mistake !!!

I am deeply touched today. Since I debugged the program, I have not even written the program into warning, not to mention the problem that the program cannot run. It has really made me learn a lot and I am very happy, it also forms a good habit of constantly writing comments for your code and forming a good habit. This is also benefited from the teachings of Teacher Chen zhengchong !! Thank you for choosing ~~~

Take a look at the following program:

/*************************************** *********
* File name: multi_rows.c
* CopyRight: 2011-03-24, All rights Reserved.
* Module name :...
*
* CPU:
* RTOS :....
*
* Create Date: 2011-03-24
* Author/Corporation: hackerling/maple Corporation
*
* Abstract Description:
* The idea of this program is to store two polynomials in an array, and then the results are stored from the bottom.

* Mark the position starting from 0 and then output it. Of course, in the main () function, the starta/finisha, startb/finishb that calls padd), the values of these parameters need to be changed based on your input, I did not continue to improve, because it is very simple, please note !!
* --------------- Revision History --------------
* No Version Date Revised By Item Description
*
*
**************************************** **********************/

# Include <stdio. h>
# Include <stdlib. h>
# Define MAX_TERMS 100

Struct polynomial // The node Structure of a polynomial
{
Float coef; // Coefficient
Int expon; // Index
};
Struct polynomial terms [MAX_TERMS]; // The maximum number of arrays to be MAX_TERMS
Int avail = 0; // avail is the global variable.

Int COMPARE (int coef1, int coef2) // COMPARE the size of two numbers
{
If (coef1 <coef2)
Return-1;
Else if (coef1 = coef2)
Return 0;
Else
Return 1;
}

Void attach (float coefficient, int exponent) // Add a new item to a polynomial
{
 
If (avail> MAX_TERMS)
{
Printf ("Too effecterms in the polynomial ");
Exit (1 );
}
Terms [avail]. coef = coefficient;



/*
**************************************** **************************************** *******************/
Terms [avail]. expon = exponent; // there is a risk here, which is a great risk, because it will change the value of the array and thus affect the subsequent judgment *

/*************************************** **************************************** ********************
*/


Avail ++; // The number of polynomials plus 1
}
Void padd (int starta, int finisha, int startb, int finishb, int * startd, int * finishd)
// Used to calculate a (x) + B (x) = d (x)
// The start and end subscript of the first polynomial represented by starta/finisha, and startb/finishb indicate the subscript of the second Polynomial
{
Float coefficient; // storage coefficient
* Startd = avail; // Add the result to the array starting from subscript 0 to subscript finishd.
While (starta <= finisha & startb <= finishb) // process two possible addition Conditions
{
Switch (COMPARE (terms [starta]. expon, terms [startb]. expon ))
{
Case-1:
Attach (terms [startb]. coef, terms [startb]. expon );
Startb ++;
Break;
Case 0:
Coefficient = terms [starta]. coef + terms [startb]. coef;
If (coefficient)
Attach (coefficient, terms [starta]. expon );
Starta ++;
Startb ++;
Break;
Case 1:
Attach (terms [starta]. coef, terms [starta]. expon );
Starta ++;
Break;
Default:
Break;
}
}
For (; starta <= finisha; starta ++) // process the first polynomial separately.
Attach (terms [starta]. coef, terms [starta]. expon );
For (; startb <= finishb; startb ++) // process the second polynomial separately.
Attach (terms [startb]. coef, terms [startb]. expon );
* Finishd = avail-1; // the final position of the result
}

Void main ()
{
Int startd, finishd;
Int I;
For (I = 0; I <4; I ++) // enter the polynomial value to be calculated and enter the line feed According to: coef space expon
{
Scanf ("% f % d", & terms [I]. coef, & terms [I]. expon );
}
Padd (0, 1, 2, 3, & startd, & finishd );
For (I = 0; I <= finishd; I ++) // output
{
Printf ("% f % d \ n", terms [I]. coef, terms [I]. expon );
}
Return;
}

Compile and run the program, and you will find that all addition operations are possible and correct,

4 * pow (x, 3) + 2 * pow (x, 2)

5 * pow (x, 3) + 3 * pow (x, 2)

 

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I152FG-0.jpg "border =" 0 "/>

The above results are completely correct, that is, 9 * pow (x, 3) + 5 * pow (x, 2)

However,

No, this program is wrong. I may be happy to think that I have compiled a polynomial addition operation. However, please debug the following data:
 

4 * pow (x, 4) + 4

3 * pow (x, 1) + 1

After finding the sum of the two polynomials, you will find the following results:

 

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" error result "src =" http://www.bkjia.com/uploads/allimg/131228/1I1521415-1.jpg "border =" 0 "/>

This result is incorrect !!! Not 4 * pow (x, 4) + 3 * pow (x, 1) + 5

Why ???

In fact, as long as you use the debugger for debugging, you will find that

 

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I1525457-2.jpg "border =" 0 "/>

This is where the judgment of this program is affected, so the result is incorrect! Well, I think program debugging is really important because of this. Also, I use this in general C language case development classic, maybe the author hasn't found this problem yet !! We hope to make corrections as soon as possible !!!

Thank you !!!

This article is from "Wang xiaomao-love you !" Blog, please be sure to keep this source http://lingchuan.blog.51cto.com/933741/529037

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.