• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            誓言不會融化的那個夏天Using Namespace乖狗狗

            GOTO 乖狗狗的綜合博客『有78篇新文章』

            【乖狗狗※熱狗】原創音樂,聆聽字里行間的溫柔

            【狗眼看股】 獨特的眼光,深刻的分析,精準的預測
            記不住網址?沒關系,在絕大多數搜索引擎中,使用“乖狗狗”作為關鍵詞進行搜索,本站將出現在搜索排行中的第位!

            posts - 3,comments - 21,trackbacks - 0
            這個程序應該是我在2004年寫的
            當時還只是一個讀化工專業的大四學生
            基本上對什么編程規范、什么算法,什么性能優化沒什么了解
            只是簡單地用字符串模擬實現了任意大整數的四則運算
            現在看來當然很幼稚了
            不過為保持原來面目,一直沒改,留做紀念。
            供有需要的朋友參考
            敬請指教




              1//long integer operation , inclue addition,substracttion,multiplicaton
              2//and division
              3#include <stdio.h>
              4#include <conio.h>
              5#include <ctype.h>
              6#include <string.h>
              7#define MAX 1000
              8#define MARK  ('0'-1)
              9
             10//The Prototypes of the functions
             11char* Initialiaze(char [],int);
             12int  Read(char [],char [],char * );
             13int  Print(char [],char [],char [],char []);
             14int  Calculate(char[],char[],char[],char []);
             15char* Addition(char[],char[]);
             16char* Substraction(char[],char[]);
             17char* Multiplication(char[],char[]);
             18char* Division(char[],char[]);
             19char Div_per_bit(char [],int , char []);
             20int Sub_per_bit(char [],char [],int *);
             21int Copy(char [],int ,char []);
             22int Compare(char [],char []);
             23int  Data_Process(char []);
             24int  Value(char);
             25int  Check(char []);
             26int  Compare(char [],char []);
             27int Judge(int);
             28int Convert(char [],char [],int);
             29
             30//The main function,calling  Read , Calculate  and Print function
             31int  main(void)
             32{    char a[MAX],b[MAX],c[2*MAX],action[MAX],ch='\t';
             33     while   (ch!=EOF)
             34     {
             35       clrscr();
             36       Read(a,b,action);
             37       if (Calculate(a,b,c,action))
             38         Print(a,b,c,action);
             39       ch=getchar();
             40     }

             41     return 0;
             42}

             43//The check function check if the input number is legal
             44int  Check(char s[])
             45{    int i;
             46     if (strlen(s)==0)
             47         return 0;
             48     if (strlen(s)>=MAX)
             49         return 0;
             50
             51     for (i=0; s[i]; i++)
             52       if (!isdigit(s[i]))
             53      return 0;
             54     return 1;
             55}

             56//The Iniatilize function ,initialize the result number before calculation
             57char* Initialize(char s[],int length)
             58{    int i;
             59     for (i=0; i<length; i++)
             60       s[i]=MARK;
             61     return s;
             62
             63}

             64//The Read function,Read the operands and the operation
             65int  Read(char a[],char b[],char action[])
             66{
             67     printf("************This Program calculats two long integer action!************\n");
             68     printf("Input long integer A,max length is %d:\n",MAX);
             69     gets(a);
             70     while (!Check(a))
             71     {   printf("Input error , Please input a correct value :\n");
             72     gets(a);
             73     }

             74     printf("Iuput the operation over a & b \n");
             75     printf("addition is '+' ,substraction is '-',multiplication is '*',division is '/'\n");
             76     printf("                            Warning:\n");
             77     printf("If you do the division,A must bigger than B ,and B must not equal to zero !\n");
             78     gets(action);
             79     while ((Compare(action,"+")!=0&& (Compare(action,"-")!=0&& (Compare(action,"*")!=0&& (Compare(action,"/")!=0))
             80     {   printf("Input error , Please input a correct action :\n");
             81         gets(action);
             82     }

             83     printf("Input long integer b,max length is %d:\n",MAX);
             84     gets(b);
             85     while (!Check(b))
             86     {   printf("Input error , Please input a correct value :\n");
             87     gets(b);
             88     }

             89     return 1;
             90}

             91//The Calculate function,calling Addition,Substraction,Multiplication or Division function  in accordance with the action
             92int  Calculate(char a[],char b[],char c[],char action[])
             93{
             94    if (Compare(action,"+")==0)
             95    {     strcpy(c,Addition(a,b));
             96         return 1;
             97    }

             98    if (Compare(action,"-")==0)
             99    {      if  ((Substraction(a,b))!=NULL)
            100              strcpy(c,Substraction(a,b));
            101          else
            102          {      strcpy(c,"-");
            103              strcat(c,Substraction(b,a));
            104          }

            105          return 1;
            106    }

            107    if (Compare(action,"*")==0)
            108    {    strcpy(c,Multiplication(a,b));
            109         return 1;
            110    }

            111
            112    if (Compare(action,"/")==0)
            113    {     if ((Division(a,b))!=NULL)
            114            strcpy(c,Division(a,b));
            115     else
            116     {   printf("Press Ctrl-Z to end, Press Enter to recalculate!\n");
            117         return 0;
            118     }

            119         return 1;
            120     }

            121
            122}

            123//The Print function , print the result
            124int  Print(char a[],char b[],char c[],char action[])
            125{    printf("The result of \n%s \n%s \n%s \nis :\n",a,action,b);
            126     puts(c);
            127     printf("Press Ctrl-Z to end , Press Enter  to recalculate..\n");
            128     return 1;
            129}

            130//The Addition function , add two operands and return the result
            131char* Addition(char a[],char b[])
            132{    char c[2*MAX],d[2*MAX];
            133     int i,j,k,a_length,b_length;
            134     Initialize(c,2*MAX);
            135     a_length=strlen(a);
            136     b_length=strlen(b);
            137     for (i=a_length-1,j=b_length-1,k=2*MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
            138     {  if ( i>=0 && j>=0 )
            139      c[k]=Value(a[i])+Value(b[j])+'0';
            140    else
            141      if (i>=0 && j<0 )
            142         c[k]=Value(a[i])+'0';
            143      else
            144        if ( i<0 && j>=0 )
            145           c[k]=Value(b[j])+'0';
            146     }

            147     Data_Process(c);
            148     Convert(c,d,2*MAX);
            149     return d;
            150}

            151//The Substraction function , substract one operand from another operand , and return the result
            152char* Substraction(char a[],char b[])
            153{    char c[2*MAX],d[2*MAX];
            154     int i,j,k,a_length,b_length,sub_result,symbol,flag[2*MAX]={0};
            155     Initialize(c,2*MAX);
            156     a_length=strlen(a);
            157     b_length=strlen(b);
            158     if (strcmp(a,b)==0)
            159    return ("0");
            160     for (i=a_length-1,j=b_length-1,k=2*MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
            161     {   sub_result=a[i]-b[j];
            162     symbol=Judge(sub_result);
            163     if (i>=1 && j>=0)
            164     {      if (flag[k]==0)
            165        {   if  (a[i]>=b[j])
            166               c[k]=sub_result+'0';
            167            else
            168            {       c[k]=sub_result+10+'0';
            169               flag[k-1]=1;
            170            }

            171        }

            172        else
            173        {   if (a[i]-b[j]>=1)
            174              c[k]=sub_result-1+'0';
            175            else
            176            {     c[k]=sub_result+9+'0';
            177              flag[k-1]=1;
            178            }

            179        }

            180     }

            181     else
            182     if  (i==0 && j<0)
            183        {
            184             if (flag[k]==0)
            185              c[k]=a[i];
            186             else
            187             {   if (a[i]==1)
            188                ;
            189             else
            190                 c[k]=a[i]-1;
            191             }

            192        }

            193     else
            194     {  if ((i==0&& (j==0))
            195           {   if (flag[k]==0)
            196               {  switch (symbol)
            197              {  case 0:   ;
            198                       break;
            199                 case 1:   c[k]=sub_result+'0';
            200                       break;
            201                 case -1:  return NULL;
            202                       break;
            203               }

            204               }

            205               else
            206               {   switch (Judge(sub_result-1))
            207               {  case 0:   ;
            208                        break;
            209                  case 1:   c[k]=sub_result-1+'0';
            210                        break;
            211                  case -1:  return NULL;
            212                        break;
            213               }

            214               }

            215           }

            216         else
            217         if ((i<0&& (j>=0))
            218            return NULL;
            219         else
            220         if ((i>0&& (j<0))
            221         {  if (flag[k]==0)
            222              c[k]=a[i];
            223            else
            224            {   if (a[i]>'0')
            225               c[k]=a[i]-1;
            226            else
            227            {  c[k]='9';
            228               flag[k-1]=1;
            229            }

            230            }

            231         }

            232     }

            233     }

            234     Convert(c,d,2*MAX);
            235     return d;
            236}

            237//The Multiplication function,multipy two operands and return the result
            238char* Multiplication(char a[],char b[])
            239{    char c[2*MAX],d[2*MAX];
            240     int i,j,k,p,a_length,b_length;
            241     Initialize(c,2*MAX);
            242     a_length=strlen(a);
            243     b_length=strlen(b);
            244     for (j=b_length-1;  j>=0 ; j--)
            245     {  k=2*MAX-(b_length-j);
            246    for (i=a_length-1,p=k; i>=0; i--,p--)
            247       if (c[p]==MARK)
            248       {    c[p]=(Value(a[i]))*(Value(b[j]))%10+'0';
            249            if (c[p-1]==MARK)
            250                     c[p-1]=(Value(a[i]))*(Value(b[j]))/10+'0';
            251                 else
            252                     c[p-1]+=(Value(a[i]))*(Value(b[j]))/10;
            253       }

            254       else
            255       {    c[p]+=(Value(a[i]))*(Value(b[j]))%10;
            256            if (c[p-1]==MARK)
            257                 c[p-1]=(Value(a[i]))*(Value(b[j]))/10+'0';
            258            else
            259                 c[p-1]+=(Value(a[i]))*(Value(b[j]))/10;
            260       }

            261    Data_Process(c);
            262     }

            263     Convert(c,d,2*MAX);
            264     return d;
            265
            266}

            267//The Division function,divise one operand from another operand, and return the result
            268char* Division(char a[],char b[])
            269{   char a1[MAX],c[MAX],d[MAX];
            270    strcpy(a1,a);
            271    int i,k,a_length=strlen(a1),b_length=strlen(b);
            272    for (i=0; b[i]; i++)
            273       if (b[i]!='0')
            274      break;
            275    if (i==strlen(b))
            276
            277    {    printf("Error!\nIf you do division,the dividend must not equal to zero!\n");
            278        return 0;
            279    }

            280    if (Compare(a,b)==-1)
            281    {    printf("Error!\nIf you do division, A must bigger than B!\n");
            282    return 0;
            283    }

            284    for ( i=0,k=0; k<a_length-b_length+1; i++,k++)
            285      c[k]=Div_per_bit(a1,b_length+i,b);
            286    c[k]='\0';
            287    Convert(c,d,MAX);
            288    return d;
            289
            290}

            291//The Div_per_bit function , calculate quotient per digit ,and return the result to Division function
            292char Div_per_bit(char a[],int a_l,char b[])
            293{   int i,j;
            294    char c[MAX];
            295    for (i=0,j=0; i<a_l; i++)
            296      if ( a[i]!=MARK)
            297      {   c[j]=a[i];
            298      j++;
            299      }

            300    c[j]='\0';
            301    for (i=0; c[i]; i++)
            302       if (c[i]!='0')
            303      break;
            304    if (i==strlen(c))
            305       return '0';
            306    if (Compare(c,b)<0)
            307       return '0';
            308    if (Compare(c,b)==0)
            309    {  for ( i=0; i<a_l; i++)
            310      a[i]=MARK;
            311       return '1';
            312    }

            313    i=0;
            314    while (Compare(c,b)>=0)
            315       Sub_per_bit(c,b,&i);
            316    Copy(a,a_l,c);
            317    return ('0'+i);
            318
            319}

            320//The Sub_per_bit function, do the division by using substraction time after time
            321int  Sub_per_bit(char a[],char b[],int *count)
            322{    char c[MAX],d[MAX];
            323     int i,j,k,a_length,b_length,sub_result,symbol,flag[MAX]={0};
            324     Initialize(c,MAX);
            325     a_length=strlen(a);
            326     b_length=strlen(b);
            327     if (strcmp(a,b)==0)
            328    strcpy(c,"0");
            329     for (i=a_length-1,j=b_length-1,k=MAX-1; ( i>=0 || j>=0 ) ; i--,j--,k--)
            330     {   sub_result=a[i]-b[j];
            331     symbol=Judge(sub_result);
            332     if (i>=1 && j>=0)
            333     {      if (flag[k]==0)
            334        {   if  (a[i]>=b[j])
            335               c[k]=sub_result+'0';
            336            else
            337            {       c[k]=sub_result+10+'0';
            338               flag[k-1]=1;
            339            }

            340        }

            341        else
            342        {   if (a[i]-b[j]>=1)
            343              c[k]=sub_result-1+'0';
            344            else
            345            {     c[k]=sub_result+9+'0';
            346              flag[k-1]=1;
            347            }

            348        }

            349     }

            350     else
            351     if  (i==0 && j<0)
            352        {
            353             if (flag[k]==0)
            354              c[k]=a[i];
            355             else
            356             {   if (a[i]==1)
            357                ;
            358             else
            359                 c[k]=a[i]-1;
            360             }

            361        }

            362     else
            363     {  if ((i==0&& (j==0))
            364           {   if (flag[k]==0)
            365               {  switch (symbol)
            366              {  case 0:   ;
            367                       break;
            368                 case 1:   c[k]=sub_result+'0';
            369                       break;
            370                 case -1:  return 0;
            371                       break;
            372               }

            373               }

            374               else
            375               {   switch (Judge(sub_result-1))
            376               {  case 0:   ;
            377                    break;
            378                  case 1:   c[k]=sub_result-1+'0';
            379                    break;
            380                  case -1:  return 0;
            381                    break;
            382               }

            383               }

            384           }

            385         else
            386         if ((i<0&& (j>=0))
            387         {  return 0;
            388         }

            389         else
            390         if ((i>0&& (j<0))
            391         {  if (flag[k]==0)
            392              c[k]=a[i];
            393            else
            394            {   if (a[i]>'0')
            395               c[k]=a[i]-1;
            396            else
            397            {  c[k]='9';
            398               flag[k-1]=1;
            399            }

            400            }

            401         }

            402     }

            403     }

            404     Convert(c,d,MAX);
            405     strcpy(a,d);
            406     (*count)++;
            407     return 1;
            408}

            409//The Copy function , copy  a number_string  to another , wipe off the inutility symbol
            410int Copy(char a[],int a_l,char c[])
            411{   int i,j,c_l=strlen(c);
            412    for (i=0; i<a_l-c_l; i++)
            413      a[i]=MARK;
            414    for (i,j=0; j<c_l; i++,j++)
            415      a[i]=c[j];
            416    return 1;
            417}

            418//The Compare function ,compare two numbers and return the result
            419int Compare(char a[],char b[])
            420{   char c[MAX];
            421    if ((strlen(a))>(strlen(b)))
            422       return 1;
            423    if ((strlen(a))==(strlen(b)))
            424       return (strcmp(a,b));
            425    if ((strlen(a))<(strlen(b)))
            426       return -1;
            427}

            428
            429//The Value function , receiver a digit_char, and return the number
            430int Value(char c)
            431{   if (isdigit(c))
            432       return (c-'0');
            433    else return 0;
            434}

            435//The Data_Process function,
            436int Data_Process(char s[])
            437{   int p,head,tail=2*MAX-1;
            438    for (head=0; s[head]==MARK ; head++)
            439      ;
            440    for (p=tail; p>=head  ; p--)
            441      if (!isdigit(s[p]))
            442     if (s[p-1]!=MARK)
            443     {  s[p-1]+=(s[p]-'0')/10;
            444        s[p]=(s[p]-'0')%10+'0';
            445     }

            446     else
            447     {  s[p-1]=(s[p]-'0')/10+'0';
            448        s[p]=(s[p]-'0')%10+'0';
            449     }

            450     return 1;
            451}

            452//The Judeg function , judge the symbol of the number
            453int Judge(int i)
            454{   if (i==0)
            455       return 0;
            456    else
            457       if (i>0)
            458      return 1;
            459       else
            460      return  -1;
            461}

            462//The Convert function , convert the original result to the final result ,wipe off the inuility symbol and number
            463int Convert(char c[],char d[],int length)
            464{   int i,j;
            465    for (i=0; i<length; i++)
            466      if (c[i]==MARK || c[i]=='0')
            467      ;
            468      else
            469     break;
            470    for (i,j=0; i<length; i++,j++)
            471      d[j]=c[i];
            472    d[j]='\0';
            473    return 1;
            474}

            475
            posted on 2005-12-04 22:33 乖狗狗 閱讀(6831) 評論(11)  編輯 收藏 引用

            FeedBack:
            # re: 【原創】大數運算全部源代碼
            2005-12-07 01:29 | Lufer
            值得學習,我到現在(大四)還沒編寫過這樣的程序,準備先自己編出來再和您的比較下,有比較才有學習。

            看了您在博客圓的blog,可以用QQ(5648806)和您交流一下嗎?  回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2005-12-08 21:32 | 乖狗狗
            To Lufer
            我現在上網的機會很少,因為工作很忙
            有事情您可以在我的Blog上留言
            我會盡快給您回復
            相信這樣會比用QQ交流更方便  回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2005-12-09 22:00 | Lufer

            乖狗狗,你好。

            我現在簽了慧通,大學畢業以后去成都的華為研究所工作,慧通的待遇對于一個沒有經驗的應屆生而言算是可以了,但是最近我在網上(也包括您的另一個blog上)了解到一些慧通的信息,概括來說就是“工作時間長,勞動強度大”,甚至有人為工作而獻出寶貴的生命。

            我對我的這份工作有些擔心,因為我的身體不算太好,最重要的是:我不知道慧通那“朝九晚九”的工作會給我留下多少自己的時間去看書,保持自己的競爭力。畢竟工作熟悉了以后,工作就差不多是體力勞動了(個人觀點)……

            您是一個過來人,能給我這個應屆生說一下您認為工作之后怎么在那么少的個人時間里提升自己嗎?剛進入工作之后最應該注意的又是什么呢?

            感謝你工作之余給我的回復,您的回復將會是我不竭工作動力的一部分,謝謝!!!  回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2005-12-10 22:55 | 乖狗狗
            To Lufer
            在華為或者慧通工作會很忙
            所以你基本上沒有自己的時間
            不過,如果能夠完成工作,你的核心競爭力已經很強了
            慧通能學到的東西挺多
            個人以為在這里是能夠磨練一陣子的
            在沒畢業以前
            建議你好好玩一玩,因為工作了以后,基本上都沒有時間玩了

            PS:我只是一個2005年畢業的“過來人”
              回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2005-12-11 18:21 | Lufer
            哦~原來你就是群里的南研啊,我的情況是不是跟你05年的情況差不多啊?

            如果是的話,我想問一下技術方面的本科生在慧通里主要做什么具體工作,畢業前最好學點什么做為準備。面試的時候我應聘的軟件研發工程師,但是面試官跟我說主要做測試工作(或許就是軟件測試工程師吧),在這方面我很迷惑……

            對于您的耐心回復非常感謝,謝謝!!!  回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2005-12-12 22:29 | 乖狗狗
            To Lufer
            你現在學的東西和你以后做的東西可能會完全不一樣
            比如我之前學的C和C++
            現在過來要用vxml和jsp,一切都要從頭學起
            至于崗位,我認為開發比測試要好
            當然,這是我的個人之見
            你在畢業之前。。。。。。。。有時間多玩玩吧


              回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2006-02-10 14:24 | zdm
            自己的特長和好的工作環境,哪個最重要呢?迷惑!  回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2006-02-10 14:29 | fly
            技術博客太單薄了,應該做的有點含金量才行啊。呵呵。。。建議。好的博客應該重點突出一個方面。如是齊頭并進,看起來不太專業啊!  回復  更多評論
              
            # re: 【原創】大數運算全部源代碼
            2006-08-13 21:20 | coolar
            向你學習阿!希望以后的生活會好點!  回復  更多評論
              
            # re: 【原創】大數運算全部源代碼[未登錄]
            2008-05-08 11:04 | a
            This is the final result, the original did not imagine the exciting world   回復  更多評論
              
            # re: 【原創】大數運算全部源代碼[未登錄]
            2012-02-15 11:27 | ww
            你那好多函數結尾return d
            return 的是局部數組阿?  回復  更多評論
              
            亚洲综合伊人久久综合| 一本色综合网久久| 亚洲va国产va天堂va久久| 国产精品久久影院| 久久人妻少妇嫩草AV蜜桃| jizzjizz国产精品久久| 亚洲欧美久久久久9999| 秋霞久久国产精品电影院| 精品伊人久久久| 精品久久人人爽天天玩人人妻| 久久婷婷五月综合色高清| 久久午夜福利电影| 国产精品久久国产精麻豆99网站| 久久国产精品无| 伊人久久大香线蕉综合5g| 久久精品国产69国产精品亚洲| 欧美日韩精品久久久免费观看| 色偷偷888欧美精品久久久| 久久亚洲精品中文字幕| 久久精品人人做人人爽电影| 久久亚洲AV无码西西人体| 996久久国产精品线观看| 久久久久久曰本AV免费免费| 日本欧美国产精品第一页久久| 久久综合九色综合久99 | 久久久久久午夜成人影院| 久久久亚洲欧洲日产国码是AV| 欧洲国产伦久久久久久久| 日本道色综合久久影院| 99久久久国产精品免费无卡顿| 亚洲欧美伊人久久综合一区二区| 久久久久久午夜精品| 亚洲国产精品成人久久蜜臀| 久久精品中文字幕有码| 精品久久久久久久中文字幕| 99久久婷婷国产综合精品草原| 日本一区精品久久久久影院| 久久精品国产免费| 亚洲精品国产成人99久久| 国产精品丝袜久久久久久不卡| 97精品伊人久久久大香线蕉|