- 论坛徽章:
- 0
|
贴下我的当时答题的代码
#include <stdio.h>
#include <stdlib.h>
/**************************************************************************
* function : get_element
*
* Discription:
* the function is used to extract elemnts from formatted string
*
* Input parameters:
* char *pstr: the input string
* int *value: record the value
* int *length: record the length of (!)
*
* Output parameters:
* void
*
* Author: Created by huzza.hu May.30th.2007
**************************************************************************/
static void get_element( char *pstr, int *value, int *length)
{
int string_len = strlen( pstr );
int i = 0;
*value = 0;
*length = 0;
while ( i < string_len ){
if ( pstr[i] != '!'){
*value = (*value)*10 + pstr[i] - '0';
}
else{
break;
}
i++;
}
while ( i < string_len ){
(*length) ++;
i++;
}
}
/**************************************************************************
* function : calfac
*
* Discription:
* the function is used to calculate the factorial of base
*
* Input parameters:
* int base: the base value
*
* Output parameters:
* int : the factorial of base
*
* Author: Created by huzza.hu May.30th.2007
**************************************************************************/
static int calfac( int *base, int compval )
{
int i = *base;
int calvalue=1;
for( ; i > 1 ; i-- ){
calvalue = calvalue * i;
if ( calvalue > compval ){
return 1;
}
}
*base = calvalue;
return 0;
}
/**************************************************************************
* function : compare
*
* Discription:
* the function is used to compare the elements
*
* Input parameters:
* char *pa: the first input parameters
* char *pb: the second input paramters
* Output parameters:
* int: if (*pa > *pb) return 1
* esle retrun 0
*
* Author: Created by huzza.hu May.30th.2007
**************************************************************************/
int compare(char *pa, char *pb) //no static here, so can be called by other program
{
int iCompa, iCompb;
int numa, numb;
int min = 0;
int result = 0;
get_element( pa , &iCompa, &numa);
get_element( pb , &iCompb, &numb);
printf("%d,the length of (!) is:%d\n", iCompa, numa);
printf("%d,the length of (!) is:%d\n", iCompb, numb);
min = (( numa > numb ) ? numb:numa);
numa = numa - min;
numb = numb - min;
result = ( iCompa > iCompb );
if ( 0 == numa ){
while( numb > 0 ){
result = calfac( &iCompb, iCompa );
if ( 1 == result ){
result = 0;
break;
}
else{
result = 1;
}
numb--;
}
}
else{
while( numa > 0 ){
result = calfac( &iCompa, iCompb );
if ( 1 == result){
break;
}
else{
result = 0;
}
numa--;
}
}
return result;
}
/**************************************************************************
* function : main
*
* Discription:
* the function is used to get the input of customer and output the result
*
* Input parameters:
* void
*
* Output parameters:
* int: error code
* 0 no error; >0 error
*
* Author: Created by huzza.hu May.30th.2007
**************************************************************************/
int main(void)
{
char acCompa[128];
char acCompb[128];
int flag = 0;
printf("please input two compares:\n");
scanf("%s", acCompa);
scanf("%s", acCompb);
printf("number1:%s\n", acCompa);
printf("number2:%s\n", acCompb);
flag = compare(acCompa, acCompb);
printf("%s is bigger than %s.\n", (flag == 1) ? acCompa:acCompb,(flag == 1) ? acCompb:acCompa);
return 0;
} |
|