2的x次幂的值
发布网友
发布时间:2024-10-21 07:35
我来回答
共3个回答
热心网友
时间:2024-10-27 23:13
不错的一道题!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* NOSD for printing (see NOSD below) */
#define NOSD_PRINT 32
/* number of significant digit in our HPFP (high-precision floating-point): NOSD + ~20% */
#define NOSD ( NOSD_PRINT + NOSD_PRINT / 5 )
/* the HPFP, initialized to 1 */
char significand[ NOSD ] = { 1 }; /* using "char" to make use of memmove( ) */
int exponent;
/* multiplies our HPFP by the least significant digit of d */
void multiplyByOneDigit( unsigned d ) {
/* do the LSD (least significant digit) first,
then start the loop from the digit on its left */
char *digit = significand + NOSD - 1;
d %= 10;
for ( *digit *= d; significand <= --digit; ) {
*digit *= d;
if ( *( digit + 1 ) > 9 ) { /* carry from lower digit */
*digit += *( digit + 1 ) / 10;
*( digit + 1 ) %= 10;
}
}
if ( *significand > 9 ) { /* MSD overflows: drop LSD */
memmove( significand + 1, significand, NOSD - 1 );
*significand = *( significand + 1 ) / 10;
*( significand + 1 ) %= 10;
++exponent;
}
}
void divideByOneDigit( unsigned d ) {
char *digit = significand;
d %= 10;
for ( ; digit <= significand + NOSD - 1; digit++ ) {
if ( digit != significand + NOSD - 1 )
*( digit + 1 ) += *digit % d * 10;
*digit /= d;
}
if ( *significand == 0 ) {
memmove( significand, significand + 1, NOSD - 1 );
--exponent;
}
}
/* returns the string representation of our HPFP */
char *toString( ) {
char *s = calloc( NOSD_PRINT + 30, 1 ); /* enough room even if int is 64-bit */
int digit_index = 0;
for ( ; digit_index < NOSD_PRINT; digit_index++ )
s[ digit_index ] = significand[ digit_index ] + '0';
/* potential round up of last digit, hence potential carry propagation */
if ( significand[ digit_index ] > 4 )
if ( ++s[ digit_index - 1 ] > '9' ) {
int i = digit_index - 1;
while ( --i && s[ i + 1 ] > '9' ) {
s[ i + 1 ] -= 10;
s[ i ]++;
}
}
/* normalize: discard trailing zeroes */
while ( s[ --digit_index ] == '0' && digit_index - 1 )
s[ digit_index ] = 0;
/* convert to scientific format */
memmove( s + 2, s + 1, strlen( s ) - 1 );
s[ 1 ] = '.';
sprintf( s + digit_index + 2, "e%+04d", exponent );
return s;
}
void main( ) {
int exp, isNegative;
char input[ 99 ];
puts( "Enter the exponent (must be an integer between -1000 and 1000):" );
for ( ; ; ) {
gets( input );
if ( strchr( input, '.' ) != NULL ) {
puts( "Decimal point isn't allowed in integer. Try again:" );
continue;
}
exp = atoi( input );
if ( strlen( input ) > 5 || exp < -1000 || 1000 < exp ) {
puts( "The integer's out of range. Try again:" );
continue;
}
break;
}
isNegative = exp < 0 ? exp *= -1 : 0;
while ( exp-- )
if ( isNegative )
divideByOneDigit( 2 );
else
multiplyByOneDigit( 2 );
fprintf( stdout, "Result:\n\t%s\n", toString( ) );
}
热心网友
时间:2024-10-27 23:16
#include "stdio.h"
#include "string.h"
#define MAX 1001
int num[MAX],n,b;
int isos(int a)/*判断是否是偶数*/
{
switch(a)
{
case 0:
case 2:
case 4:
case 6:
case 8:return 1;
default:return 0;
}
}
void chengfa(void)/*以前写的,没有优化,效率可能不高*/
{
int i;
for(i=1;i<=num[0];i++)
{
num[i]*=2;
}
for(i=1;i<=num[0];i++)
{
if(num[i]>9)
{
num[i+1]+=num[i]/10;
num[i]%=10;
}
}
if(num[num[0]+1]>0)num[0]++;
}
void chufa(void)
{
int i;
for(i=num[0];i>=1;i--)
{
if(isos(num[i])) num[i]/=2;
else
{
num[i]/=2;
num[i+1]+=5;
}
}
if(num[num[0]+1]>0)num[0]++;
}
void zjisuan()
{
while(b<n)
{
chengfa();
b++;
}
}
void fjisuan()
{
while(b>n)
{
chufa();
b--;
}
}
void zprint()
{
int i,r[MAX];
memset(r,0,sizeof(r));
r[0]=num[0];
for(i=1;i<=num[0];i++) r[r[0]+1-i]=num[i];
printf("%d.",r[1]);
for(i=2;i<=64;i++) printf("%d",r[i]);
printf("\be+%d\n",r[0]-1);
}
void fprint()
{
int i=1,j;
while(!num[i]) i++;
printf("%d.",num[i]);
for(j=i+1;j<=i+64;j++)printf("%d",num[j]);
printf("\be-%d\n",i-1);
}
int main()
{
memset(num,0,sizeof(num));
b=0;
num[0]=1;num[1]=1;
printf("请输入指数:");
scanf("%d",&n);
if(n>0){ zjisuan();zprint();}
if(n<0){ fjisuan();fprint();}
system("pause");
return 0;
}
热心网友
时间:2024-10-27 23:11
http://www.qaohoo.com