数据结构复习笔记(6)

1,  最大子序列和问题(四种解法)

 
 
class  Test
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] a = dot.gif{-2,11,-4,13,-5,-2};
InBlock.gif            
int result = MaxSubsequenceSumFour(a,6);
InBlock.gif            Console.WriteLine(result);
InBlock.gif            Console.Read();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static int MaxSubsequenceSumOne(int[] A,int n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif//算法1
InBlock.gif
            int CurSum,MaxSum=0,i,j;
InBlock.gif            
for(i=0;i<n;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CurSum 
= 0;
InBlock.gif                
for(j=i;j<n;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    CurSum 
+= A[j];
InBlock.gif                    
if(CurSum > MaxSum)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MaxSum 
= CurSum;    
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }
    
InBlock.gif            
return MaxSum;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static int MaxSubsequenceSumTwo(int[] A,int n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//算法2
InBlock.gif
            int CurSum,MaxSum=0,i,j,k;
InBlock.gif            
for(i=0;i<n;i++)
InBlock.gif                
for(j=i;j<n;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    CurSum 
= 0;
InBlock.gif                    
for(k=i;k<=j;k++)
InBlock.gif                        CurSum 
+= A[k];
InBlock.gif                    
if(CurSum > MaxSum)
InBlock.gif                        MaxSum 
= CurSum ;
ExpandedSubBlockEnd.gif                }

InBlock.gif            
return MaxSum;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static int MaxSubsequenceSumThree(int[] A,int n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//算法3
InBlock.gif
             return MaxSubSum( A, 0, n - 1 );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static int MaxSubsequenceSumFour(int[] A,int n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//算法4
InBlock.gif
            int ThisSum, MaxSum, j;
InBlock.gif            ThisSum 
= MaxSum = 0;
InBlock.gif            
for( j = 0; j < n; j++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ThisSum 
+= A[ j ];
InBlock.gif                
if( ThisSum > MaxSum )
InBlock.gif                    MaxSum 
= ThisSum;
InBlock.gif                
else if( ThisSum < 0 )
InBlock.gif                    ThisSum 
= 0;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return MaxSum;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static int Max3( int A, int B, int C )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return A > B ? A > C ? A : C : B > C ? B : C;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static int MaxSubSum(int[ ] A, int Left, int Right )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int MaxLeftSum, MaxRightSum;
InBlock.gif            
int MaxLeftBorderSum, MaxRightBorderSum;
InBlock.gif            
int LeftBorderSum, RightBorderSum;
InBlock.gif            
int Center, i;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if( Left == Right )  /**//* Base case */
InBlock.gif                
if( A[ Left ] > 0 )
InBlock.gif                    
return A[ Left ];
InBlock.gif                
else
InBlock.gif                    
return 0;
InBlock.gif            Center 
= ( Left + Right ) / 2;
InBlock.gif            MaxLeftSum 
= MaxSubSum( A, Left, Center );
InBlock.gif            MaxRightSum 
= MaxSubSum( A, Center + 1, Right );
InBlock.gif            MaxLeftBorderSum 
= 0;
InBlock.gif LeftBorderSum 
= 0;
InBlock.gif            
for( i = Center; i >= Left; i-- )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                LeftBorderSum 
+= A[ i ];
InBlock.gif                
if( LeftBorderSum > MaxLeftBorderSum )
InBlock.gif                    MaxLeftBorderSum 
= LeftBorderSum;
ExpandedSubBlockEnd.gif            }

InBlock.gif            MaxRightBorderSum 
= 0;
InBlock.gif RightBorderSum 
= 0;
InBlock.gif            
for( i = Center + 1; i <= Right; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RightBorderSum 
+= A[ i ];
InBlock.gif                
if( RightBorderSum > MaxRightBorderSum )
InBlock.gif                MaxRightBorderSum 
= RightBorderSum;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return Max3( MaxLeftSum, MaxRightSum,MaxLeftBorderSum + MaxRightBorderSum );
ExpandedSubBlockEnd.gif        }

InBlock.gif    
ExpandedBlockEnd.gif    }

None.gif

2,  高效率的取幂运算

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif class  Test
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(Pow( 
34 ));
InBlock.gif
InBlock.gif            Console.Read();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
static long Pow( int X, int N )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( N == 0 )
InBlock.gif                
return 1;
InBlock.gif            
if( N == 1 )
InBlock.gif                
return X;
InBlock.gif
InBlock.gif            
if(N%2==0//偶数
InBlock.gif
                 return Pow( X * X, N / 2 );
InBlock.gif            
else
InBlock.gif                 
return Pow( X * X, N / 2 ) * X;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
static long PowTwo( int X, int N )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( N == 0 )
InBlock.gif                
return 1;
InBlock.gif            
if(N%2==0)
InBlock.gif                 
return Pow( X * X, N / 2 );
InBlock.gif            
else
InBlock.gif                 
return Pow( X , N -1 ) * X;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

None.gif
None.gif

3,一元多项式运算(链表表示)

None.gif #include < stdio.h >
None.gif#include
< malloc.h >
None.gif#include
< stdlib.h >
None.gif
#define   LEN  sizeof(node) 
None.gif
None.giftypedef 
struct  polynode
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
int coef;//系数
InBlock.gif
 int exp;//指数
InBlock.gif
 struct polynode *next;
ExpandedBlockEnd.gif}
node;
None.gif
None.gifnode 
*  create( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif node 
*h,*r,*s;
InBlock.gif 
int c,e;
InBlock.gif h
=(node *)malloc(LEN);//头结点
InBlock.gif
 r=h;
InBlock.gif printf(
"coef:");
InBlock.gif scanf(
"%d",&c);
InBlock.gif printf(
"exp: ");
InBlock.gif scanf(
"%d",&e);
InBlock.gif 
while(c!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  s
=(node *)malloc(LEN);
InBlock.gif  s
->coef=c;
InBlock.gif  s
->exp=e;
InBlock.gif     r
->next=s;
InBlock.gif     r
=s;
InBlock.gif     printf(
"coef:");
InBlock.gif     scanf(
"%d",&c);
InBlock.gif     printf(
"exp: ");
InBlock.gif     scanf(
"%d",&e);
ExpandedSubBlockEnd.gif }

InBlock.gif r
->next=NULL;//表尾
InBlock.gif
 return(h);
ExpandedBlockEnd.gif}

None.gif 
None.gifpolyadd(node 
* polya, node  * polyb)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif node 
*p,*q,*pre,*temp;
InBlock.gif 
int sum;
InBlock.gif p
=polya->next;
InBlock.gif q
=polyb->next;
InBlock.gif pre
=polya;
InBlock.gif 
while(p!=NULL&&q!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
if(p->exp<q->exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   pre
->next=p;pre=pre->next;
InBlock.gif   p
=p->next;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else if(p->exp==q->exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   sum
=p->coef+q->coef;
InBlock.gif   
if(sum!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    p
->coef=sum;
InBlock.gif    pre
->next=p;pre=pre->next;
InBlock.gif    p
=p->next;temp=q;q=q->next;free(temp);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{temp=p->next;free(p);p=temp;
InBlock.gif    temp
=q->next;free(q);q=temp;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif     }

InBlock.gif 
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{pre->next=q;pre=pre->next;
InBlock.gif  q
=q->next;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif    }

InBlock.gif
if(p!=NULL)
InBlock.gifpre
->next=p;
InBlock.gif
else
InBlock.gifpre
->next=q;                                                         
ExpandedBlockEnd.gif}

None.gif
None.gif
void  print(node  *  p)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {                                                                                                      
InBlock.gif    
while(p->next!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{   
InBlock.gif        p
=p->next;
InBlock.gif        printf(
"     %d*x^%d",p->coef,p->exp);   
InBlock.gif           
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
   
None.gifmain()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif node 
* polya,* polyb;
InBlock.gif printf(
"Welcome to use!\n");
InBlock.gif printf(
"\nPlease input the ploya include coef && exp:\n");
InBlock.gif polya
=create();
InBlock.gif print(polya);
InBlock.gif printf(
"\nPlease input the ployb include coef && exp:\n");
InBlock.gif polyb
=create();
InBlock.gif print(polyb);
InBlock.gif    printf(
"\nSum of the poly is:\n");
InBlock.gif    polyadd(polya,polyb);
InBlock.gif    print(polya);
InBlock.gif    printf(
"\n");
ExpandedBlockEnd.gif}
 
None.gifdatastruct.h
None.gif
None.giftypedef 
struct  list
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int c;    //多项式的项数
InBlock.gif
    int e;    //多项式的指数 
InBlock.gif
    struct list *next;    //下一结点
ExpandedBlockEnd.gif
}
;
None.giftypedef 
struct  list  * LinkList;
None.giftypedef 
struct  list Node;
None.gif

另一个示例:

None.gif // File: ListOper.h
None.gif

None.gif#ifndef DATASTRUCT
None.gif
#define  DATASTRUCT
None.gif#include 
" datastruct.h "
None.gif
#endif
None.gif
// some functions declaretioin
None.gif
bool  CreateList(LinkList  & L);
None.gifNode 
* CreateNode( int  e,  int  c);
None.gif
void  FreeList(LinkList  & L);
None.gif
void  SortList(LinkList  & L);
None.gif
void  DeleteNextNode(Node  * d);
None.gif
void  SweepNextNode(Node  * s);
None.gif
void  OutPutList(LinkList  & L);
None.gif
None.gif相关函数的实现,对应文件ListOper.cpp:
None.gif
// File: ListOper.cpp
None.gif
#include  < stdlib.h >
None.gif#include 
< iostream >
None.gif#ifndef DATASTRUCT
None.gif
#define  DATASTRUCT
None.gif#include 
" datastruct.h "
None.gif
#endif
None.gif#include 
" ListOper.h "
None.gif
using   namespace  std;
None.gif
bool  CreateList(LinkList  & L)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//TODO: 创建线性链表
InBlock.gif
    Node *head;
InBlock.gif    head
=(Node *)malloc(sizeof(Node));
InBlock.gif    
if(head==NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< "内存分配错误" << endl;
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    head
->next=NULL;
InBlock.gif    head
->c=0;
InBlock.gif    head
->e=0;
InBlock.gif    L
=head;
InBlock.gif    
return true;
ExpandedBlockEnd.gif}

None.gifNode 
* CreateNode( int  e,  int  c)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//TODO: 创建结点
InBlock.gif
    Node * pos;
InBlock.gif    pos
=(Node *)malloc(sizeof(Node));
InBlock.gif    
if(pos==NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< "内存分配错误" << endl;
InBlock.gif        exit(
1);
ExpandedSubBlockEnd.gif    }

InBlock.gif    pos
->e=e;
InBlock.gif    pos
->c=c;
InBlock.gif    pos
->next=NULL;
InBlock.gif    
return pos;
ExpandedBlockEnd.gif}

None.gif         
None.gif
void  FreeList(LinkList  & L)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//TODO: 释放整个线性表所占用的内存空间
InBlock.gif
    Node *pos;
InBlock.gif    Node 
*next;
InBlock.gif    pos
=L;
InBlock.gif    
while(pos!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        next
=pos->next;
InBlock.gif        free(pos);
InBlock.gif        pos
=next;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
void  SortList(LinkList  & L)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
bool flag=true//是否需要排序标志
InBlock.gif
    Node *head=L->next;
InBlock.gif    Node 
*pos;
InBlock.gif    Node 
*last;
InBlock.gif    Node 
*temp;
InBlock.gif    
if(head->next==NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
while(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        flag
=true;
InBlock.gif        last
=head;
InBlock.gif        pos
=last->next;
InBlock.gif        
if(last==NULL||last->next==NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
break;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
while(last!=NULL && last->next!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            flag
=false;
InBlock.gif            pos
=last->next;
InBlock.gif            
if(last->e<pos->e)    //哈哈哈哈哈,HTML代码
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                SweepNextNode(last);
InBlock.gif                flag
=true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(last->e==pos->e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                last
->c+=pos->c;
InBlock.gif                DeleteNextNode(last);
InBlock.gif                flag
=true;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**//*last=last->next;
ExpandedSubBlockEnd.gif                pos=last->next;
*/

ExpandedSubBlockEnd.gif            }

InBlock.gif            last
=last->next;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
void  DeleteNextNode(Node  * d)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Node 
*temp;
InBlock.gif    temp
=d->next;
InBlock.gif    d
->next=temp->next;
InBlock.gif    free(temp);
ExpandedBlockEnd.gif}

None.gif
void  SweepNextNode(Node  * s)
None.gif
// 一点偷懒的办法,只交换值,不修改指针
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int c,e;
InBlock.gif    c
=s->c;e=s->e;
InBlock.gif    s
->c=s->next->c;s->e=s->next->e;
InBlock.gif    s
->next->c=c;s->next->e=e;
ExpandedBlockEnd.gif}

None.gif
void  OutPutList(LinkList  & L)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Node 
*pos;
InBlock.gif    pos
=L->next;
InBlock.gif    cout 
<< "输出表达式:";
InBlock.gif    
while(pos!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(pos->c>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cout 
<< "+";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(pos->c!=1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cout 
<< pos->c;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(pos->e!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cout 
<< "x^";
InBlock.gif            cout 
<< pos->e;
ExpandedSubBlockEnd.gif        }

InBlock.gif        pos
=pos->next;
ExpandedSubBlockEnd.gif    }

InBlock.gif    cout 
<< endl;
ExpandedBlockEnd.gif}

None.gif
None.gif主单元文件main.cpp:
None.gif#include 
< iostream >
None.gif#include 
< stdlib.h >
None.gif#include 
< ctype.h >
None.gif#include 
" ListOper.h "
None.gif
using   namespace  std;
None.gifLinkList AnayString(
char  aString[],  int  aLength);
None.gif
int  main( int  argc,  char   * argv[])     // -------------------------------
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    LinkList L;
InBlock.gif    
char InStr[1024];
InBlock.gif    
int len;
InBlock.gif    cout 
<< "一元稀疏多项式计算器" << endl;
InBlock.gif  cout 
<< "请输入一个1024个字符以内的稀疏多项式:";
InBlock.gif    cin 
>> InStr;
InBlock.gif    len
=strlen(InStr);
InBlock.gif    L
=AnayString(InStr,len);
InBlock.gif    SortList(L);
InBlock.gif    OutPutList(L);
InBlock.gif    FreeList(L);
InBlock.gif    system(
"PAUSE"); 
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gifLinkList AnayString(
char  aString[],  int  aLength)     // ---------------
None.gif
// TODO: 字符串分析函数 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    LinkList L
=NULL;
InBlock.gif    Node 
*pos=NULL;
InBlock.gif    Node 
*last;
InBlock.gif    Node 
*head;
InBlock.gif    CreateList(L);
InBlock.gif    head
=L;
InBlock.gif    last
=head;
InBlock.gif    
int c=0;
InBlock.gif    
int e=0;
InBlock.gif    
char temp[1];
InBlock.gif    
char tp;
InBlock.gif    
bool plus=true;
InBlock.gif    
char status='n';    //状态指示符,我省略了系数为负的情况
ExpandedSubBlockStart.gifContractedSubBlock.gif
    /**//*
InBlock.gif    n: 非运算状态
InBlock.gif    c: 正在计算系数
InBlock.gif    e: 正在计算指数 
InBlock.gif    p: 指数为0 
InBlock.gif    f: 完成了一个项目的输入
ExpandedSubBlockEnd.gif    
*/

InBlock.gif    
for(int i=0;i<aLength;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        temp[
0]=aString[i];
InBlock.gif        tp
=temp[0];
InBlock.gif        
switch(status)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
case 'n':
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                c
=0;e=0;
InBlock.gif                status
='c';
InBlock.gif                
if(tp=='-')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    plus
=false;
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(isdigit(tp))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    c
=atoi(temp);
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(tp=='x')//多项式以x开头
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    c
=1;
InBlock.gif                    status
='e';
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
case 'c':
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(isdigit(aString[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(plus)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        c
=c*10+atoi(temp);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        c
=c*10-atoi(temp);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(tp=='x')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(c==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        c
=1;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    status
='e';
InBlock.gif                    e
=0;
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//此处考虑了常数项出现在其他位置的可能
InBlock.gif
                if(tp=='+')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    plus
=true;
InBlock.gif                    status
='p';
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(tp=='-')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    plus
=false;
InBlock.gif                    status
='p';
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**//*if(temp[0]=='^')
InBlock.gif                {
InBlock.gif                    status='e';
InBlock.gif                    e=0;
InBlock.gif                    continue;
ExpandedSubBlockEnd.gif                }
*/
 //此种情况不可能出现
InBlock.gif
                continue;
ExpandedSubBlockEnd.gif            }
    //正在解析系数
InBlock.gif
            case 'e':
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(tp=='^')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(isdigit(tp))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    e
=e*10+atoi(temp);
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(tp=='+')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    plus
=true;
InBlock.gif                    status
='f';
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(tp=='-')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    plus
=false;
InBlock.gif                    status
='f';
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
            //正在解析系数
InBlock.gif
            case 'p':
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                e
=0;
InBlock.gif                status
='f';
InBlock.gif                
continue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
case 'f':
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pos
=CreateNode(e,c);
InBlock.gif                last
->next=pos;
InBlock.gif                last
=pos;
InBlock.gif                c
=0;e=0;
InBlock.gif                status
='c';
InBlock.gif                i
--;
InBlock.gif                
continue;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
    
InBlock.gif    pos
=CreateNode(e,c);
InBlock.gif    last
->next=pos;
InBlock.gif    
return L;
ExpandedBlockEnd.gif}

None.gif
None.gif


4,  中缀表达式转换为后缀表达式

None.gif #include < iostream.h >
None.gif
None.gif
const   int  MAX = 40 ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
void  main( void ) dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
char infix[MAX]=dot.gif{'#'};
ExpandedSubBlockStart.gifContractedSubBlock.gif    
char oprator[MAX]=dot.gif{'@','#'};
InBlock.gif    
int opr=1;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
char postfix[12]=dot.gif{'#'};
InBlock.gif    
int post=0;
InBlock.gif    
int i,j,cnt=0,cntl;
InBlock.gif    
char c;
InBlock.gif
InBlock.gif    
//输入表达式,以等号结束
InBlock.gif
    cin.get(c);
ExpandedSubBlockStart.gifContractedSubBlock.gif    
while(c!='=')dot.gif{
InBlock.gif        infix[cnt]
=c;
InBlock.gif        cnt
++;
InBlock.gif        cin.
get(c);
ExpandedSubBlockEnd.gif    }

InBlock.gif    cntl
=cnt;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for(i=0;i<cnt;i++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
switch(infix[i])dot.gif{
InBlock.gif        
//左括号就直接入栈
InBlock.gif
        case '(':
InBlock.gif            cntl
=cntl-2;
InBlock.gif            oprator[opr]
=infix[i];
InBlock.gif            opr
++;
InBlock.gif            
break;
InBlock.gif        
//右括号则先退栈,直到遇见第一个左括号
InBlock.gif
        case ')':
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for(j=opr-1;j>0;j--)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(oprator[j]!='(')dot.gif{
InBlock.gif                    postfix[post]
=oprator[j];
InBlock.gif                    oprator[j]
='#';
InBlock.gif                    post
++;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
elsedot.gif{
InBlock.gif                    oprator[j] 
= '#';
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            opr
=j;
InBlock.gif            
break;
InBlock.gif        
case '*':
InBlock.gif        
case '/':
InBlock.gif            
//如果前一个运算符为*或/,则先退栈,再入栈,否则直接入栈
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if (oprator[opr] == '*' || oprator[opr] == '/'dot.gif{
InBlock.gif                postfix[post] 
= oprator[opr];
InBlock.gif                oprator[opr]
='#';
InBlock.gif                post
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            oprator[opr] 
= infix[i];
InBlock.gif            opr
++;
InBlock.gif            
break;
InBlock.gif        
case '+' :
InBlock.gif        
case '-' :
InBlock.gif            
//如果上一个运算符不是左括号也不是栈顶,则先退栈再入栈
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if (oprator[opr-1!= '(' && oprator[opr-1!= '@'dot.gif{            
InBlock.gif                postfix[post] 
= oprator[opr];
InBlock.gif                oprator[opr]
='#';
ExpandedSubBlockEnd.gif            }

InBlock.gif            oprator[opr] 
= infix[i];
InBlock.gif            opr
++;
InBlock.gif            
break;
InBlock.gif        
default :
InBlock.gif            
//如果是数字则直接进入后缀表达式数组
InBlock.gif
            postfix[post] = infix[i];
InBlock.gif            post
++;
InBlock.gif            
break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//如果扫描完成,则退栈
ExpandedSubBlockStart.gifContractedSubBlock.gif
    for(j=opr-1;j>0;j--)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(oprator[j]!='@')dot.gif{
InBlock.gif            postfix[post]
=oprator[j];
InBlock.gif            oprator[j]
='#';
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
InBlock.gif            
break;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//输出结果
InBlock.gif
    for(i=0;i<cntl;i++)
InBlock.gif        cout 
<< postfix[i];
InBlock.gif    cout 
<< endl;
ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值