divide(nodeptr one,nodeptr two,nodeptr ans){
 int i=0;
 while(1){
  
 if(greater(one,two)==3)
 {
     i=i+1;
     break;
 }
 else{
     if(greater(one,two)==2)
     {
  
         break;
  
     }
     else
     {
         i++;
         subtract(one,two,ans);
         stripLeadingZeros(ans->next);
         one=ans;
     }
  
 }
 }
  
 nodeptr tmp=NULL;
 tmp=malloc(sizeof(nodeptr));
 tmp->next=NULL;
 tmp->data=i;
 ans->next=tmp;
  
  
 }
  /*int traverse(nodeptr s)
  {
  
      while(s!=NULL)
      {
          if(s->data!=0)
          {
              return 0;
          }
          s=s->next;
      }
      return 1;
  }
  zero_trim(nodeptr s)
  {
    while(s->next!=NULL)
    {
        if(s->next->data==0)
        {
            if(traverse(s->next))
            s->next=NULL;
        }
        s=s->next;
    }
  
  } */
 stripLeadingZeros( nodeptr s )
 {
   if(s!=NULL)
   stripLeadingZeros(s->next);
   if((s!=NULL)&&s->data==0&&on)
   flg=1;
   if((s!=NULL)&&(s->data!=0)&&flg)
   on=0,flg=0,s->next=NULL;
   if(flg)
   s->next=NULL;
 }
コード例 #3
0
/**
 * Adjust the number data to be with the correct NUMERIC
 * DIGITS setting.
 *
 * @param numPtr    The pointer to the start of the current numeric data.
 * @param result    Where this should be copied back to after adjustment.
 * @param resultLen The length of the result area.  Data is copied relative
 *                  to the end of the data.
 * @param digits    The digits setting for the adjustment.
 *
 * @return The new number ptr after the copy.
 */
char *NumberStringBase::adjustNumber(char *numPtr, char *result, wholenumber_t resultLen, wholenumber_t digits)
{
    // remove all leading zeros that might have occurred after the operation.
    numPtr = stripLeadingZeros(numPtr);

    // after stripping, is the length of the number larger than the digits setting?
    if (digitsCount > digits)
    {
        // NOTE:  the original version had a bug where it was attempting to adjust the
        // exponent, but because it updated the length first, the net adjustment was zero.
        // I "fixed" this and completely broke the power operation.  I really don't understand
        // why the exponent does not need adjusting here, but it appears it doesn't.

        // adjust the length down to the digits value
        digitsCount = digits;
        // round the number.
        mathRound(numPtr);
    }
    // copy the data into the result area, aligned with the end of the
    // buffer.  We return the pointer to the new start of the number
    return (char *)memcpy(((result + resultLen - 1) - digitsCount), numPtr, digitsCount);
}
 main()
 {
     nodeptr one=NULL;
     nodeptr two=NULL;
     nodeptr ans=NULL;
     one=malloc(sizeof(nodeptr));
     two=malloc(sizeof(nodeptr));
     ans=malloc(sizeof(nodeptr));
     initialize(one,two,ans);
     char a[10000];
     printf("first number =");
     scanf("%s",a);
     int l1=num_list_add_leng(one,a);
  
     fflush(stdin);
     printf("\n second number =");
     scanf("%s",a);
     int l2=num_list_add_leng(two,a);
     list_print(one->next);
     printf("\n");
     list_print(two->next);
     printf("\n  */+/-  ? = ");
     char pr;
     scanf(" %c",&pr);
     printf("\n");
     int i=0,j=0,buffer=0;
     nodeptr add[l2];
     int flow=1;
  if((pr=='+')&&flow){
      addit(one,two,ans);
      list_print(ans->next);
      flow=0;
  }
  if((pr=='-')&&flow)
  {
  
      subtract(one,two,ans);
      stripLeadingZeros(ans->next);
      list_print(ans->next);
      flow=0;
  }
  if((pr=='/')&&flow)
  {
      divide(one,two,ans);
      list_print(ans->next);
      flow=0;
  }
  if((pr=='*')&&flow){                                         //note: ans node is useless for addition from here used as temp mem frm here
 for(j=0;j<l2;j++)
 {
     add[j]=malloc(sizeof(nodeptr));
     add[j]->next=NULL;
     int ts=j;
     while(ts!=0)
     {
        list_add_front(add[j],0);
        ts--;
     }
      ans=one;
     int tmp1=two->next->data;
  for(i=0;i<l1;i++){
       int tmp=one->next->data;
       int tmp3=(tmp1*tmp)+buffer;
       list_add_front(add[j],(tmp3)%10);
       buffer=(tmp3)>9?(tmp3/10):0;
           one=one->next;
                   }
                   one=ans;
 if(buffer>=1){
 list_add_front(add[j],buffer);
  
 }
 two=two->next;
 buffer=0;
 }
 i=0;
 freemem(ans);
 nodeptr ans1;
 ans1=malloc(sizeof(nodeptr));
 ans1->next=NULL;
 j=l2;
 while(j>=2){
   addit(add[i],add[i+1],ans1);
   add[i+1]=ans1;
   i++;
   j--;
 }
  
  
 printf("\n");
 list_print(add[l2-1]->next);
 }
 printf("\n written by Namit Sinha");
 getch();
 }
コード例 #5
0
ファイル: numinput.cpp プロジェクト: BackupTheBerlios/btg-svn
         void numberInputWindow::enterKey(t_uint const _key)
         {
            bool change_sign = false;

            std::string k;
            switch (_key)
               {
               case '0':
                  {
                     k = "0";
                     if ((input_.size() == 1) && isNegative())
                        {
                           return;
                        }
                     break;
                  }
               case '1':
                  k = "1";
                  break;
               case '2':
                  k = "2";
                  break;
               case '3':
                  k = "3";
                  break;
               case '4':
                  k = "4";
                  break;
               case '5':
                  k = "5";
                  break;
               case '6':
                  k = "6";
                  break;
               case '7':
                  k = "7";
                  break;
               case '8':
                  k = "8";
                  break;
               case '9':
                  k = "9";
                  break;
               case '-':
                  change_sign = true;
                  break;
               default:
                  return;
               }

            if (change_sign)
               {
                  if (btg::core::convertStringTo<t_int>(input_) == 0)
                     {
                        return;
                     }

                  std::string temp = input_;

                  std::string::iterator iter = temp.begin();
                  if (iter != temp.end())
                     {
                        if (*iter == '-')
                           {
                              temp.erase(iter);
                           }
                        else
                           {
                              temp.insert(iter, _key);                              
                           }
                     }
               
                  if (!validate(temp))
                     {
                        return;
                     }

                  iter = input_.begin();
                  if (iter != input_.end())
                     {
                        if (*iter == '-')
                           {
                              input_.erase(iter);
                           }
                        else
                           {
                              input_.insert(iter, _key);                              
                           }
                        changed_ = true;
                     }
               }
            else
               {
                  std::string temp = input_ + k;

                  if (!validate(temp))
                     {
                        return;
                     }

                  std::string::iterator iter = input_.end();
                  iter--;
                  if (iter != input_.end())
                     {
                        // Append.
                        input_.push_back(_key);
                        changed_ = true;
                     }
               }

            stripLeadingZeros();
         }