读书人

Linux/Dev-C连双重指针也不给修改了…

发布时间: 2012-04-03 12:38:19 作者: rapoo

Linux/Dev-C连双重指针也不给修改了……试验做的郁闷!
主程序 a2.c

C/C++ code
#include"a2.h"main(){      LargeInt powerOf2 , temp ;      int i ;      /* Print table of powers of 2 */      Linit( powerOf2 );      Linit( temp );      LintToLarge( powerOf2 , 1 );      printf("Powers of 2 n 2^\n");      for( i = 1 ; i <= 200 ; i++ )      {           printf("       %i ", i ) ;           Lprint( powerOf2 ) ;           printf("\n");           Ladd( temp, powerOf2, powerOf2 ) ;           Lassign( powerOf2 , temp );      }}


头文件 a2.h

C/C++ code
#include<stdio.h>#include<stdlib.h>#include<math.h>#define MAX_LARGE_DIGITS 1000#define LARGE_INT_MODULE 10typedef int** LargeInt ;void Linit( LargeInt );void LintToLarge( LargeInt , const int ) ;int LtoInt( const LargeInt ) ;void Lassign( LargeInt , const LargeInt ) ;void Ladd( LargeInt , const LargeInt , const LargeInt ) ;void Lscale( LargeInt , const LargeInt , const int ) ;void Lprint( const LargeInt ) ;int Lcompare( const LargeInt  , const LargeInt ) ;void Lcarry(LargeInt) ;void Linit( LargeInt intVar ){    *intVar=(int*)malloc((MAX_LARGE_DIGITS+2)*sizeof(int));    *intVar[0]=1;    *intVar[1]=1;    int i;    for(i=2; i<=MAX_LARGE_DIGITS+1; i++)        *intVar[i]=0;}void LintToLarge( LargeInt target , const int value ){    int tempValue=value;    if(tempValue<0)    {        *target[0]=-1;        tempValue*=-1;    }    else        *target[0]=1;    *target[1]=1;    *target[2]=tempValue;    Lcarry(target);}int LtoInt( const LargeInt source ){    if(*source[1]>5)    {        printf("\nLarge Int too big to fit in int.");        return 0;    }    if(*source[1]==5)    {        if(*source[6]<=3)        {            if(*source[5]<=2)            {                if(*source[4]<=7)                {                    if(*source[3]<=6)                    {                        if(((*source[2]<7)&&(*source[0]==1))||((*source[2]<8)&&(*source[0]==-1)))    ;                        else                        {                            printf("\nLarge Int too big to fit in int.");                            return 0;                        }                    }                }            }        }    }    int tempInt=0;    int i;    for(i=*source[1]+1; i>=2; i--)        tempInt*=LARGE_INT_MODULE+*source[i];    return tempInt;}void Lassign( LargeInt target , const LargeInt source ){     int i;         for(i=0; i<=*source[1]+1; i++)        *target[i]=*source[i];}void Ladd( LargeInt target , const LargeInt leftOp , const LargeInt rightOp ){    *target[0]=1;    int i;    for(i=2; i<=*leftOp[1]+1; i++)        *target[i]=(*leftOp[i])*(*leftOp[0]);    for(i=2; i<=*rightOp[1]+1; i++)        *target[i]+=(*rightOp[i])*(*rightOp[0]);    if(*leftOp[1]>*rightOp[1])        *target[1]=*leftOp[1];    else        *target[1]=*rightOp[1];    Lcarry(target);}void Lscale( LargeInt target , const LargeInt source , const int scaleBy ){    *target[0]=*source[0];    *target[1]=*source[1];    int i;    for(i=2; i<=*source[1]+1; i++)        *target[i]=(*source[i])*scaleBy;    Lcarry(target);}void Lprint( const LargeInt source ){    if(*source[0]==-1)        printf("-");    int i;    for(i=*source[1]+1; i>=2; i--)        printf ("%d", *source[i]);}int Lcompare( const LargeInt leftOp , const LargeInt rightOp ){    if(*leftOp[0]>*rightOp[0])        //if signs are different, the results are obvious        return 1;    else    {        if(*leftOp[0]<*rightOp[0])            return -1;    }//Then, signs are the same, now look at absolute value    int abscompare=0;    if(*leftOp[1]!=*rightOp[1])    {        if(*leftOp[1]>*rightOp[1])        {            abscompare=1;        }        else            abscompare=-1;    }    else    {        int i;                for(i=*leftOp[1]+1; i>=2; i--)        {            if(*leftOp[i]>*rightOp[i])            {                abscompare=1;                break;            }            else            {                if(*leftOp[i]<*rightOp[i])                {                    abscompare=-1;                    break;                }            }        }    }    switch(abscompare)    {        case 0:            return 0;        case 1:            if(*leftOp[0]>0)                return 1;            else                return -1;        case -1:            if(*leftOp[0]>0)                return -1;            else                return 1;    }}void Lcarry (LargeInt source){//Carry forward    int i,j;    if((*source[2]>=LARGE_INT_MODULE)&&(*source[1]==1))    {        *source[3]=*source[2]/LARGE_INT_MODULE;        *source[2]%=LARGE_INT_MODULE;        *source[1]++;    }    for(i=2; i<=*source[1]+1; i++)    {        j=i;        while(j<=*source[1]+1)        {            *source[j+1]+=*source[j]/LARGE_INT_MODULE;            *source[j]%=LARGE_INT_MODULE;            j++;            if(*source[*source[1]+1]>=LARGE_INT_MODULE)            {                *source[*source[1]+2]=*source[*source[1]+1]/LARGE_INT_MODULE;                *source[*source[1]+1]%=LARGE_INT_MODULE;                *source[1]++;                if(*source[1]>1000)                {                    printf("\nCarry forward overflow!");                    return;                }            }        }    }//If top digit minus, flip all digits and sign    if(*source[*source[1]+1]<0)    {        for(i=2; i<=*source[1]+1; i++)            *source[i]*=-1;        *source[0]*=-1;    }//Carry backword    for(i=*source[1]; i>=2; i--)    {        j=i;        while((*source[j]<0)&&(j>=2))        {            *source[j+1]-=((int)log10(0-*source[j])/(int)log10(LARGE_INT_MODULE)+1);            *source[j]+=LARGE_INT_MODULE*(int)log10(0-*source[j])/(int)log10(LARGE_INT_MODULE);        }    }//Clear unused digits    i=*source[1]+1;    while((*source[i]==0)&&(i>=2))        *source[1]--;} 



要求是不能修改主程序。题目的要求在以前的帖子里面有:
http://topic.csdn.net/u/20071004/14/4e74124a-04a4-49cf-b569-dd5f01671b52.html

问题是我在申请内存修改二重指针的那一步立刻报内存违例……报得我莫名其妙……找了一些二重指针的例子也都跟我写的基本相同,现在要疯掉了……还是没有保护的TC3.0好……

谁能帮忙看出关键所在呢?先谢谢了……

[解决办法]
你先看看这个吧

其他的就不管了

C/C++ code
void Linit( LargeInt intVar ){    intVar = (int**)malloc(sizeof(intVar) * (MAX_LARGE_DIGITS+2));        for(int i = 0; i < MAX_LARGE_DIGITS+2; i++)    {        *(intVar +i )=(int*)malloc((MAX_LARGE_DIGITS+2)*sizeof(int));    }    *intVar[0]=1;    *intVar[1]=1;    for(i=2; i<=MAX_LARGE_DIGITS+1; i++)        *intVar[i]=0;}
[解决办法]
'Lcompare' : not all control paths return a value
warning C4700: local variable 'powerOf2' used without having been initialized
warning C4700: local variable 'temp' used without having been initialized

VC6一编译就出这三个警告,都是可能导致严重错误的.现把这三个消除.
[解决办法]
poor coding habit with messy coding style
poor anything~

读书人网 >C语言

热点推荐