标签:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node{ char zifuji[20]; struct node *next; }NODE; typedef struct stack{ char num[20]; int size; }STACK; void cmp(char *ch); //字转词 void zizhuanci(NODE *node){ NODE *p; p=node->next; while(p!=NULL){ cmp(p->zifuji); p=p->next; } }
//判断词义 void cmp(char *ch){ int key; if(strcmp(ch,"begin")==0){ printf("<%s \t 1 >\n",ch); } else if(strcmp(ch,"if")==0){ printf("<%s \t 2 >\n",ch); } else if(strcmp(ch,"then")==0){ printf("<%s \t 3 >\n",ch); } else if(strcmp(ch,"while")==0){ printf("<%s \t 4 >\n",ch); } else if(strcmp(ch,"do")==0){ printf("<%s \t 5 >\n",ch); } else if(strcmp(ch,"end")==0){ printf("<%s \t 6 >\n",ch); } else if(strcmp(ch,"+")==0){ printf("<%s \t 13>\n",ch); } else if(strcmp(ch,"-")==0){ printf("<%s \t 14>\n",ch); } else if(strcmp(ch,"*")==0){ printf("<%s \t 15>\n",ch); } else if(strcmp(ch,"/")==0){ printf("<%s \t 16>\n",ch); } else if(strcmp(ch,":")==0){ printf("<%s \t 17>\n",ch); } else if(strcmp(ch,":=")==0){ printf("<%s \t 18>\n",ch); } else if(strcmp(ch,"<")==0){ printf("<%s \t 20>\n",ch); } else if(strcmp(ch,"<=")==0){ printf("<%s \t 21>\n",ch); } else if(strcmp(ch,"<>")==0){ printf("<%s \t 22>\n",ch); } else if(strcmp(ch,">")==0){ printf("<%s \t 23>\n",ch); } else if(strcmp(ch,">=")==0){ printf("<%s \t 24>\n",ch); } else if(strcmp(ch,"=")==0){ printf("<%s \t 25>\n",ch); } else if(strcmp(ch,";")==0){ printf("<%s \t 26>\n",ch); } else if(strcmp(ch,"(")==0){ printf("<%s \t 27>\n",ch); } else if(strcmp(ch,")")==0){ printf("<%s \t 28>\n",ch); } else if(strcmp(ch,"#")==0){ printf("<%s \t 0>\n",ch); } else{ while(*ch!=NULL){ if(*ch>=‘0‘&&*ch<=‘9‘) key=0; else if(*ch>=‘0‘&&*ch<=‘9‘&&*ch>=‘a‘&&*ch<=‘z‘) key=1; ch++; } if(key==0) printf("<%s \t 11>\n",ch); else if(key ==1) printf("<%s \t 12>\n",ch); else printf("<%s \t 错误>\n",ch); } } //转成词 int word_splitter(char* str,NODE *node){ NODE *p,*q; p=node; int length = strlen(str); char aword[20]; int number = 0; int start = 0; memset(aword, 0, 20); for(int i = start; i < length; ++i){ if(str[i] == ‘ ‘){ memcpy(aword, str + start, i - start); ++number; //printf("%d\t%s\n", number, aword); q=(NODE *)malloc(sizeof(NODE)); q->next=NULL; strcpy(q->zifuji,aword); p->next=q; p=q; memset(aword, 0, 20); start = i + 1; } } if(start < length) { memcpy(aword, str + start, length - start); ++number; //printf("%d\t%s\n", number, aword); q=(NODE *)malloc(sizeof(NODE)); q->next=NULL; strcpy(q->zifuji,aword); p->next=q; p=q; } return number; } int main(){ NODE *node,*p,*q; int i; STACK *stack; int size=-1; node = p =(NODE *)malloc(sizeof(NODE)); stack=(STACK *)malloc(sizeof(STACK)); printf("请输入:\n"); for(i=0;i<1;i++){ q=(NODE *)malloc(sizeof(NODE)); gets(node->zifuji); q=p->next; puts(node->zifuji); //printf("%d",sizeof(node->zifuji)); } i=word_splitter(node->zifuji,node); /* p=node->next; while(p){
puts(p->zifuji); printf("\n"); p=p->next; } */ zizhuanci(node); return 0; }
标签:
原文地址:http://www.cnblogs.com/caishun/p/4826934.html