您的位置:程序门 -> vc/mfc -> 非技术类



帮我改个程序吧,谢谢了


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


帮我改个程序吧,谢谢了
发表于:2007-06-19 19:45:41 楼主
#include   <stdio.h>
#include   <stdlib.h>

#include   <iostream>
#include   <strstream>
#include   <fstream>
#include   <istream>
#include   <iomanip>
#include   <string.h>

#define   invalid_char_tail   0  
#define   no_left   1
#define   no_right   2
#define   invalid_char_in   3
#define   id         4
#define   num       5
#define   plus       6
#define   minus     7
#define   timers   8
#define   over       9
#define   error     255

using   std::string;
using   namespace   std;

const   string   errcodestr[]=
{
    "表达式后跟有非法字符! ",
          "缺少左操作数! ",
          "缺少右操作数! ",
          "表达式中含有非法字符! "
};


char   characteristic[20];   //用于保存单词

//定义token的内容  
  struct   token                                
      {char   character[20];       //存放单词
char   outkind[20];                     //属性信息
      }token1[10];
     
      char   *nextchar=null;  
      char   token[10]= " ";
      char   expression[100]= " ";
      struct   token   *ptoken=token1;  

     
bool   isletter(char   ch)  
{  
if(ch   > = 'a '   &&   ch   <= 'z ')  
return   1;        
return   0;  
}  
   
bool   isdigit(char   ch)  
{  
if(ch   > =   '0 '   &&   ch   <= '9 ')  
return   1;  
return   0;  
}  

void   empty()       //用于清空保存的单词
{
for(int   i=0;i <20;i++)
{
characteristic[i]= '\0 ';
}
}

void   e();                     //   e-> t*e ¦t/e ¦t
void   t();                     //   t-> fg
void   g();                     //   g-> +fg ¦-fg ¦
void   f();                     //   f-> id ¦num
void   error(int   errcode);

void   scan()
{    
  empty();
if(isletter(*nextchar))  
    {             int   i=0;
                  while(isletter(*nextchar)   ¦ ¦   isdigit(*nextchar))  
                  {  
                                    characteristic[i]=*nextchar;  
                                    nextchar++;  
                                    i++;  
                    }                
                   
              strcpy(ptoken-> character,characteristic);
      strcpy(ptoken-> outkind, "id ");
      empty();    
      ptoken++;
      }  
else
{     if(isdigit(*nextchar))  
        {   int   i=0;
                while(isdigit(*nextchar))  
                {  
                                characteristic[i]=*nextchar;  
                                nextchar++;  
                                i++;              
                }  
                strcpy(ptoken-> character,characteristic);
        strcpy(ptoken-> outkind, "num ");
      empty();    
      ptoken++;
        }  
    else
    {   switch(*nextchar)
            {
                case   '+ ':   {nextchar++   ;  
              strcpy(ptoken-> character, "+ ");
                                      strcpy(ptoken-> outkind, "plus ");
        ptoken++;
    }break;
                case   '- ':   {nextchar++   ;  
    strcpy(ptoken-> character, "- ");
    strcpy(ptoken-> outkind, "minus ");
      ptoken++;
    }break;  
                case   '* ':   {nextchar++   ;  
              strcpy(ptoken-> character, "* ");
      strcpy(ptoken-> outkind, "timers ");
        ptoken++;
    }break;
                case   '/ ':   {nextchar++   ;  
              strcpy(ptoken-> character, "/ ");
                                      strcpy(ptoken-> outkind, "over ");
        ptoken++;
    }break;
                default   :   {
      strcpy(ptoken-> character,(char*)nextchar);
                                      strcpy(ptoken-> outkind, "error ");
        ptoken++;
    }
      break;
            }            
    }    
}
}

void   main()
{      
      printf( "请输入表达式: ");
      scanf( "%s ",   expression   );    
      nextchar=expression;
          scan();
  e();

  if((ptoken-> character== "* "
          ¦ ¦ptoken-> character== "/ "
          ¦ ¦ptoken-> character== "+ "
          ¦ ¦ptoken-> character== "- ")
          &&((ptoken++)-> outkind!= "id " ¦ ¦
                (ptoken++)-> outkind!= "num "))
    {           scan();
        error(no_right);
    }
   
  else              
    {   if((ptoken-> character== "* "
          ¦ ¦ptoken-> character== "/ "
          ¦ ¦ptoken-> character== "+ "
          ¦ ¦ptoken-> character== "- ")
          &&((ptoken--)-> outkind!= "id " ¦ ¦
                (ptoken--)-> outkind!= "num "))
            {   scan();
                error(no_left);
            }
           
                else
                      printf( "语法正确!!!\n ");
    }
   
}
           


  void   e()
        {    
    t();
  while(ptoken-> character== "+ "
              ¦ ¦ptoken-> character== "- ")
            scan();
              e();
}

void   t()
{
  f();
  g();
}

void   g()
{

    if(ptoken-> character== "+ "
                  ¦ ¦ptoken-> character== "- ")
                  {       scan();
                    f();
                    g();
                  }
                 
        else   if(ptoken-> character!= "* "
                        &&ptoken-> character!= "/ ")  
                        {
                            scan();
                            error(invalid_char_in);
                        }
                       
                    else      
                            error(invalid_char_in);        
}

void   f()
{  
  if((ptoken++)-> outkind!= "id "&&
                (ptoken++)-> outkind!= "num ")
                scan();
                  error(invalid_char_in);
}


//出错处理函数,输入错误代码,可以指出错误位置,出错信息。
void   error(int   errcode)
{
      printf( "\r ");                                                 //换行
      printf( "语法错误!!!\n ");
      printf( "%s\n ",errcodestr[errcode]);       //输出错误信息
}

               
发表于:2007-06-19 19:49:451楼 得分:0
什么问题
发表于:2007-06-19 23:38:172楼 得分:0
记法分析的程序??
发表于:2007-06-20 10:09:223楼 得分:0
要搞成什么?
发表于:2007-06-22 00:37:044楼 得分:0
不知楼主要达到什么效果呢?
发表于:2007-06-26 15:56:355楼 得分:0
up下   关注


快速检索

最新资讯
热门点击