?? word.cpp
字號:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<malloc.h>
#include<conio.h>
#define true 1
#define false 0
typedef struct kinds{
int code;
int value;
};
char word[80]; //put word
int base=0;
int search=0;
char* reserveWord[]={ "if","then","else", "while", "do" };
char getOneChar(){
char ch;
ch=word[base];
base++;
return ch;
}
void retract(){
base--;
}
int reserve(char *token){
int i=0;
int j=0;
for(i=0;i<4;i++){
j=strcmp(token,reserveWord[i]);
if(j==0) return true;
}
return false;
}
kinds scan(void){
kinds kind;
int i=0;
int number;
char ch;
char *token;
token=(char *)malloc(20);
//token="";
// while( (ch=getOneChar())==' ')
ch=getOneChar();
if(isalpha(ch)){ //letter
//printf("\nttt %c",ch);
while(isalpha(ch)||isdigit(ch) ){
//strcat(token,ch);
token[i]=ch;
token[i+1]='\0';
i++;
ch=getOneChar();
}
retract();
kind.code=reserve(token); //reserveWord, code=1
if(kind.code==0){ //not reserveWord
printf("\nIDN %8s",token); return kind;
}
else{
printf("\n%s _",token); //?????????
return kind;
}
}
/* shu zi */
else if(isdigit(ch)){
if(ch=='0'){
ch=getOneChar();
if(ch=='x'||ch=='X'){
ch=getOneChar();
if(ch>='0'&&ch<='9'||ch>='a'&&ch<='f'){
while(ch>='0'&&ch<='9'||ch>='a'&&ch<='f'){
token[i]=ch;
token[i+1]='\0';
i++;
ch=getOneChar();
}
retract();
sscanf(token,"%x",&number);
printf("\nINT16 %d",number);
return kind; //????????????
}
else{
printf("???????? 0x ??????");
}
}
else if(ch>='0'&&ch<='7'){
while(ch>='0'&&ch<='7'){
token[i]=ch;
token[i+1]='\0';
i++;
ch=getOneChar();
}
retract();
sscanf(token,"%o",&number);
printf("\nINT8 %d",number);
return kind; //????????????
}
else{
retract();
//printf("\nINT10 %s",token);
printf("\nINT10 0");
return kind; //????????????
}
}
else{ //ch== 1--9
while(ch>='0'&&ch<='9'){
token[i]=ch;
token[i+1]='\0';
i++;
ch=getOneChar();
}
retract();
printf("\nINT10 %s",token);
return kind; //????????????
}
}
else if(ch=='+'){
printf("\n+ _ ");
return kind;
}
else if(ch=='-'){
printf("\n- _ ");
return kind;
}
else if(ch=='*'){
printf("\n* _ ");
return kind;
}
else if(ch=='/'){
printf("\n/ _ ");
return kind;
}
else if(ch=='='){
printf("\n= _ ");
return kind;
}
else if(ch=='<'){
printf("\n< _ ");
return kind;
}
else if(ch=='>'){
printf("\n> _ ");
return kind;
}
else if(ch==' '){return kind;}
else{ //wrong!
printf("\nI don't know! ");
}
//kind.code=1;
kind.value=0;
free(token);
return kind;
}
void main(){
kinds kind;
int i=0;
clrscr();
gets(word);
//printf("%s",word);
while(base<strlen(word)){
kind=scan();
// printf("\n kind: %d , %d; base:%d ",kind.code,kind.value,base);
}
getch();
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -