大值的存储和表示

来源:互联网 发布:免费开源cms系统 编辑:程序博客网 时间:2024/05/16 01:18

#include   <stdio.h>  
#include   <iostream>  
using   namespace   std;  

int   main()  
{  
    int   data[51];       /*   存储50位数,为了方便记忆,不适用data[0]   */  
    int   index = 1;      /*   表示数组中的位数   */  
    int   n;           /* 准备计算的数值 */  
   
 /*初始化数组 */  
    for(int i=0;   i<51;   ++i)  
  data[i] = 0;  
    data[1] = 3;       
    cout << "   Enter   a   number   to   be   calculated:   "   <<   endl;  
    cin >> n;  
    for(i=1; i<=n; ++i)  
    {
  //计算3的n次方
  for(int j = 1; j < i; ++j)
   data[j] = data[j] * 3;
  
  for(int   k=1;   k<=index;   ++k)  
  {  
   /*数组中每一位存储0~9的数字,大于等于10,进位   */  
   if( data[k] > 10 )  
   {  
    for(int   m=1;   m<=index;   ++m)  
    {  
     /*如果计算之后数组中最高位大于等于,位数index+1*/  
     if(data[index] > 10)  
      index++;  
     /* 当前位进位*/  
     data[m+1] += data[m]/10;  
     /* 进位之后的值*/  
     data[m] = data[m]%10;  
    }  
   }  
  }  
  cout << "3^" << n << "= ";
  
  for(j=index; j>0; --j)  
   cout << data[j];  
  cout   <<   endl;  
    }  
    return   0;  
}