C language programming

Source: Internet
Author: User
Code


# Include "stdafx. H"
Typedef int (* pfun) (int A, int B );
Int isnumberequal (INT number );
Void ntos (INT number, int C []);
Void sort (int A [], pfun PF );
Int getmaxn (int A []);
Int getminn (int B []);

Int ASC (int A, int B) {return (A> B? 1:0 );}
Int DESC (int A, int B) {return (A <B? 1:0 );}

Int main (INT argc, char * argv [])
{
Int num = 0;
Printf ("Please enter a number :");
Scanf ("% d", & num );
// When isnumberequal is called, if the number is not 4 bits, or there is danger, put it at the end and return a short circuit.
While (Num <1000 | num> 9999 | isnumberequal (Num )){
Printf ("invalid number: % d, try again: \ n", num );
Scanf ("% d", & num );
}
Int arr [4], Max, min;
Do {
Ntos (Num, arr );
Max = getmaxn (ARR );
Printf ("max = % d,", max );
Min = getminn (ARR );
Printf ("min = % d \ n", min );
Num = max-min;
} While (num! = 6174 );

Printf ("\ n max (% d)-min (% d) = % d \ n", Max, Min, max-min );
Printf ("\ ndone ");
Return 0;
}
// Use the int isnumberequal (INT number) function to check whether the numbers of the input integer numbers are not equal,
// Returns an equal value of 1. Otherwise, the return value is 0;
Int isnumberequal (INT number)
{
Int A [4] = {0 ,};
Ntos (number, );
Int IRES = 0;
For (INT I = 0; I <4; ++ I ){
For (Int J = I + 1; j <4; ++ J ){
If (A [I] = A [J]) {
IRES = 1;
Break;
}
}
If (IRES = 1) break;
}
Return IRES;
}

// Use a function (void ntos (INT number, int C []) to store the four-digit integer number into array C
Void ntos (INT number, int C [])
{
For (INT I = 0; number> 0; ++ I ){
C [I] = Number % 10;
Number/= 10;
}
}
// Use the function (void sort (int A []) to sort array a of four elements (both in ascending or descending order );
Void sort (int A [], pfun PF)
{
// Select Method
For (INT I = 0; I <4; ++ I ){
For (Int J = I + 1; j <4; ++ J ){
If (PF (A [I], a [J]) {
Int T = A [I];
A [I] = A [J];
A [J] = T;
}
}
}
}

// The returned value is the maximum value.
Int getmaxn (int A [])
{
Int sum = 0, POW = 1;
Sort (A, DESC );
For (INT I = 3; I> = 0; -- I, Pow * = 10 ){
Sum + = A [I] * POW;
}
Return sum;
}
// The return value is the minimum value.
Int getminn (int A [])
{
Int sum = 0, POW = 1;
Sort (A, ASC );
For (INT I = 3; I> = 0; -- I, Pow * = 10 ){
Sum + = A [I] * POW;
}
Return sum;
}

 

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.