⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complication.cpp

📁 #include <stdio.h> #define MAX 10
💻 CPP
字号:
  #include     <stdio.h>   
  #include   <stdlib.h>   
  #include   <string.h>   
  #include   <ctype.h>   
  #define   LEN   sizeof(struct  Node)   
  #define   NULL   0   
    
  struct   Node   
  { char   data;   
    struct   Node  *next;   // 定义动态链表数据结构     
  };   
    
  void   output(struct   Node*);//扫描输出函数   
  void   scaner();   //词法分析函数
  void   getbc();   
  int    getch();   
  void   concat();   
  int    letter(char   ch);   
  int    degit(char   ch);   
  int    reserve();   
  void   retract();   
  void   back(int a,char *b);   
    
  struct Node *head,*p;   
  char   ch;   //全局变量   
  char   *rwtab[6]={"begin","if","then","while","do","end"};   //关键字表 
  char   token[20];   //字符数组,存放构成单词的符号串  
  int   Two[20];
    
  int   main(void)   
  {   
      head=(struct Node *)malloc(LEN);   //分配头节点存储空间   
            if(!head)   {printf("error");exit(1);}   
      head->next=NULL;   
      head->data=' ';   
      p=head;   
      printf("Please input string\n");
	  //printf("and end it with '#' in a new line:\n")
    
	  while(1)   
	  {   int   i=0;   
		  char   temp[256];//每行长度不超过256个字符
		  gets(temp);   //输入源程序,以行为单位   
		  if(temp[0]=='#')   break;//当输入的第一个字符为#时表示输入源代码结束 
    
		  p->next=(struct Node *)malloc(LEN);   
		  if(!(head->next))   {printf("error");exit(1);}   
		  p=p->next;   
    
		  while(temp[i]!='\0' && i<256)             /*将输入的代码以行为单位存入缓冲区*/   
		  {   
			  p->data=temp[i];   
			  p->next=(struct Node *)malloc(LEN);   
		   	if(!(p->next))   {printf("error");exit(1);}   
			  p=p->next;   
			  i++;   
		  }   
		  p->data='\0';   
		  p->next=NULL;             /*尾结点*/   
	  }   
    
	  output(head);           /*扫描缓冲区,输出结果*/   
	  p=head->next;   
	  while(p->next!=NULL)   
	  scaner();           /*词法分析*/       
	  return   0;   
  }   
    
  void   output(struct   Node   *head)                           /*扫描缓冲区函数*/   
  {  
	  if(!head)   {printf("error");exit(1);}   
      p=head->next;   
      while(p->next!=NULL)   
	  {   
		printf("%c",p->data);   
		p=p->next;   
      }   
      printf("\n");   
  }   
    
  void  getbc()   /*若ch中是空白字符,则不停调用getch()直到读入的不是空白字符为止*/   
  {   
	  while   (ch==' ')   
	  getch();
  }   
    
  int   getch()         /*从缓冲区读入一字符*/   
  {   
	  ch=p->data;   
	  p=p->next;
	  return ch;
  }   
    
  void   concat()       /*将ch中的字符连接到token的后面*/   
  {   
	  unsigned int i;   
	  i=strlen(token);   
	  token[i]=ch;   
	  token[i+1]='\0';   
  }   
    
  int   letter(char   ch)   /*判断ch中的是否是字母*/   
  {   
	return   isalpha((int)ch);   
  }   
    
  int   digit(char   ch)     /*判断ch中的是否是数字*/   
  {   
	 return   isdigit((int)ch);   
  }   
    
  int   reserve()   /*判断token中的字符串是否是关键字或是标识符*/   
  {    
	  for(int k=1;k<7;k++)   
	  {   
		if(strcmp(rwtab[k],token)==0)   return (k);   
	  }   
	  return 10;   
    }   
    
  void   retract()   /*指针回退一个字符*/   
  {   
	  struct   Node *Q;   
	  Q=head->next;   
	  while(Q->next!=p)   
	  Q=Q->next;   
	  p=Q;   
  }   
    
  void  back(int a,char *b)         /*返回函数,输出序列*/   
  {   
	 printf("(%d,%s)",a,b);   
  }   
    
    
  void   scaner()   //词法分析函数   
  {   
	int c;   
    token[0]=NULL;   //将token清空   
	  getch();   
	  getbc();   //读入一个单词   
	  if(letter(ch))   //处理字符的情况   
	  {   
		 while(letter(ch))   
		 {   
		  concat();   
		  getch();   
		 }   
		  retract();   
		  c=reserve();   
		  back(c,token);    
	  }   
	  else   
		  if(digit(ch))   /*处理数字的情况*/
		  {   
			 while(digit(ch))   
			 {   
			  concat();   
			  getch();  
			  
			 }   
		     retract();
		     int c=atoi(token);
		     for(int i=0;i<8;i++)
			 {
			  while(c!=0)
			  {Two[i]=c%2;
			  int b=c/2;
			  c=b;}
			  //printf("%d",Two[i]);
			  
			 }
			 int d= getch();
		     if(d=='.')
			{
			  getch();
			  while(digit(ch))   
			  {   
			   concat();   
			   getch();  
			  }    
		      retract();
		      int c=atoi(token);
		      for(int j=i+1;j<8;j++)
			  {
			   while(c!=0)
			   {Two[j]=c%2;
			   int b=c/2;
			   c=b;}
			  }
			  else printf("error!");
			 }
			 else  
		  
	printf("(10,%d)",atoi(token));   
		  }   
		else   
		  switch(ch)     /*处理特殊符号的情况*/   
		  {   
		  case'+':   back(13,"+");break;   
		  case'-':   back(14,"-");break;   
		  case'*':   back(15,"*");break;   
		  case'/':   back(16,"/");break; 
		  case':':   getch();
			  if(ch=='=') 
				  back(18,":=");
			  else
			  { retract();
			  back(17,":");}
			  break;
		  case'<':   getch();   
				if(ch=='=')  
					back(22,"<=");
				else
				{retract();   
				back(20,"<");}   
					break;   
		  case'>':   getch();   
				if(ch=='=')   
					back(24,">=");   
				else
				{retract();   
				back(23,">");}   
				break;   
		  case';':   back(26,";");break;   
		  case'{':   back(30,"{");break;   
		  case'}':   back(31,"}");break;   
		  case'(':   back(27,"(");break;   
		  case')':   back(28,")");break;   
		  case'=':   back(25,"=");break; 
		  case'\n':   break;   
		  default:   printf("error");break;   
		  }   
  }   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -