?? unit1.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include<stdio.h>
#include<string.h>
#include "Unit1.h"
int x=1; //輸出表格中的行下標
char s[1000]; //暫存由界面或文件輸入的字符
FILE *fp1,*fp2; //輸入輸出文件存取指針
int ssj=1,E=1,z=0,ssj1=1; //控制界面或文件輸入,和是否演示動畫的參數(shù)
int ad=200; //自定義符號的起始地址
int count=0,y=1; //出錯統(tǒng)計參數(shù)
int haha=1; //出錯時動畫處理參數(shù)
AnsiString add; //自定義符號的地址增量
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) //程序初始化按鈕
{
Animate11->Active=true;
Animate11->Visible=true;
if(RadioButton1->Checked==true) //選擇輸入方式(文件或界面)
ssj=1;
else
ssj=0;
if(CheckBox1->Checked==true) //選擇是否播放動畫
ssj1=1;
else
ssj1=0;
int i;
x=1; //輸出從第一列開始
z=0; //對s數(shù)組的下標清零
count=0; //出錯計數(shù)清零
Edit1->Text=0;
y=0; //出錯統(tǒng)計清零
for(int row=0;row<=5;row++)
for(int col=0;col<200;col++)
StringGrid1->Cells[row][col]="\0"; //表格數(shù)據(jù)清零
if(ssj==1)
{
for(i=0;i<=500;i++)
s[i]='\0';
Edit2->Text=RichEdit1->GetTextLen();
for(i=1;i<=RichEdit1->GetTextLen();i++)
{
s[i-1]=RichEdit1->Text[i]; //界面形式輸入的數(shù)據(jù)存儲
}
}
else
{
fp1=fopen("c:\\in.txt","r");
fp2=fopen("c:\\out.txt","w");
char c;
for(i=0;i<=500;i++)
s[i]='\0';
i=0;
while(!feof(fp1))
{
c=fgetc(fp1);
s[i]=c; //文件形式輸入的數(shù)據(jù)存儲
i++;
}
s[i-1]='\0';
}
//Edit2->Text=s;
StringGrid1->Cells[0][0]="序號"; //對表頭初始化
StringGrid1->Cells[1][0]="£";
StringGrid1->Cells[2][0]="特征";
StringGrid1->Cells[3][0]="類型";
StringGrid1->Cells[4][0]="地址/編碼";
StringGrid1->Cells[5][0]="內容";
}
//---------------------------------------------------------------------------
char a[10]={'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'},b[10]={'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};
int i=0,j=0,k=0,n=0;
char o[2],d[10],c[10]; //全局變量定義并初始化
struct //模擬保留字表
{
char a[10];
char b[9];
}str[]={{"\0","00000000"},{"main","00000001"},{"auto","00000010"},{"break","00000011"},{"cin","00000100"},{"cout","00000101"},{"case","00000110"},{"char","00000111"},{"const","00001000"},{"continue","00001001"},{"default","00001010"},{"do","00001011"},
{"double","00001100"},{"else","00001101"},{"enum","00001110"},{"extern","00001111"},{"float","00010000"},{"for","00010001"},{"goto","00010010"},{"if","00010011"},{"int","00010100"},{"long","00010101"},{"register","00010110"},{"return","00010111"},{"short","00011000"},{"signed","00011001"},{"sizeof","00011010"},{"static","00011011"},{"struct","00011100"},{"switch","00011101"},{"typedef","00011110"},{"union","00011111"},{"unsigned","00100000"},{"void","00100001"},{"volatile","00100010"},{"while","00100011"}};
struct //用戶自定義存儲空間
{
char a[10];
char b[9];
}str1[]={{"\0","00000000"}};
struct //運算符號表
{
char a;
char b[9];
}str2[]={{'\0',"00000000"},{'.',"10000000"},{'!',"10000001"},{'~',"10000010"},{'-',"10000011"},{'*',"10000100"},{'&',"10000101"},{'/',"10000110"},{'%',"10000111"},{'+',"10001000"},{'<',"10001001"},{'>',"10001010"},{'^',"10001011"},{'|',"10001100"},{'=',"10001101"}};
struct //雙字符符號表
{
char a[3];
char b[9];
}str3[]={{"\0","00000000"},{"->","11000000"},{"++","11000001"},{"--","11000010"},{"<<","11000011"},{">>","11000100"},{"<=","11000101"},{">=","11000110"},{"==","11000111"},{"!=","11001000"},{"&&","11001001"},{"||","11001010"},{"+=","11001011"},{"-=","11001100"},{"*=","11001101"},{"/=","11001110"},{"%=","11001111"},{"&=","11010000"},{"^=","11010001"},{"|=","11010010"}};
struct //標識符符號表
{
char a;
char b[9];
}str4[]={{'\0',"01000000"},{'`',"01000001"},{'@',"01000010"},{'#',"01000011"},{'$',"01000100"},{'_',"01000101"},{'[',"01000110"},{']',"01000111"},{':',"01001000"},{'\"',"01001001"},{'\'',"01001010"},{'?',"01001011"}};
int check(char a[10]) //保留字,用戶自定義符號處理函數(shù)
{
int i;
for(i=1;i<=36;i++)
if(strcmp(a,str[i].a)==0) //與保留字表進行匹配,若匹配,則為保留字
{
Form1->StringGrid1->Cells[5][x]=a; //對輸出表格賦值
Form1->StringGrid1->Cells[4][x]=str[i].b;
Form1->StringGrid1->Cells[3][x]="保留字";
Form1->StringGrid1->Cells[2][x]="";
Form1->StringGrid1->Cells[1][x]='0';
Form1->StringGrid1->Cells[0][x]=x;
if(ssj==0)
fprintf(fp2,"%10d, 0, , 保留字,%10s,%10s\n",x,str[i].b,a); //對輸出文件賦值
if(Form1->StringGrid1->Cells[5][x]=="int"||Form1->StringGrid1->Cells[5][x]=="char"||Form1->StringGrid1->Cells[5][x]=="long"||Form1->StringGrid1->Cells[5][x]=="float")
for(i=0;i<10;i++) //只有在int,char等后面的字母字符串為用戶自定義的
c[i]=a[i];
x++; //表格行下標加一
a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
return(1);
}
for(i=1;i<E;i++) //檢驗該字符串是否被定義為用戶自定義
if(strcmp(a,str1[i].a)==0)
{
Form1->StringGrid1->Cells[5][x]=a; //對輸出表格賦值
Form1->StringGrid1->Cells[4][x]=str1[i].b;
Form1->StringGrid1->Cells[3][x]="用戶自定義";
Form1->StringGrid1->Cells[2][x]="v";
Form1->StringGrid1->Cells[1][x]='2';
Form1->StringGrid1->Cells[0][x]=x;
if(ssj==0)
fprintf(fp2,"%10d, 2, v, 用戶自定義,%10s,%10s\n",x,str1[i].b,a); //對輸出文件賦值
x++;
a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
return(0);
}
if(strcmp(c,"int")==0||strcmp(c,"char")==0||strcmp(c,"long")==0||strcmp(c,"float")==0||strcmp(c,"short")==0||strcmp(c,"float")==0)
{
for(i=0;i<10;i++) //當字母字符串前是int,char等時該字符串定義為用戶自定義
str1[E].a[i]=a[i];
add=ad; //分配內存單元
add=add+"H";
ad=ad+2;
for(i=1;i<5;i++)
str1[E].b[i-1]=add[i];
str1[E].b[i-1]='\0';
Form1->StringGrid1->Cells[5][x]=a; //對輸出表格賦值
Form1->StringGrid1->Cells[4][x]=str1[E].b;
Form1->StringGrid1->Cells[3][x]="用戶自定義";
Form1->StringGrid1->Cells[2][x]="v";
Form1->StringGrid1->Cells[1][x]='2';
Form1->StringGrid1->Cells[0][x]=x;
if(ssj==0)
fprintf(fp2,"%10d, 2, v, 用戶自定義,%10s,%10s\n",x,str1[E].b,a); //對輸出文件賦值
x++;
E++;
a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
return(0); //暫存數(shù)組a清空
}
Form1->StringGrid1->Cells[5][x]=a; //若該字符串不屬于前面定義的,則視為出錯
Form1->StringGrid1->Cells[4][x]="error";
Form1->StringGrid1->Cells[3][x]="error";
Form1->StringGrid1->Cells[2][x]="error";
Form1->StringGrid1->Cells[1][x]="error";
Form1->StringGrid1->Cells[0][x]=x;
if(ssj==0)
fprintf(fp2,"%10d, error, error, error, error,%10s\n",x,a);
x++;
a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
return(0);
}
int and(int s,char b[]) //將數(shù)字字符串的內容拼接為一個自然數(shù)
{
int n=0,k=0;
for(k=1;k<=s;k++)
{
n=n*10+b[k-1]-48;
}
b[0]='\0';b[1]='\0';b[2]='\0';b[3]='\0';b[4]='\0';b[5]='\0';b[6]='\0';b[7]='\0';b[8]='\0';b[9]='\0';
return(n); //暫存數(shù)組a清空
}
void fuyu(char o[]) //雙字符符號處理函數(shù)
{
int m;
for(m=0;m<20;m++) //與雙字符符號表進行匹配
{
if(strcmp(o,str3[m].a)==0)
break;
}
Form1->StringGrid1->Cells[5][x]=o; //匹配成功輸出結果
Form1->StringGrid1->Cells[4][x]=str3[m].b;
Form1->StringGrid1->Cells[3][x]="運算符";
Form1->StringGrid1->Cells[2][x]="";
Form1->StringGrid1->Cells[1][x]='0';
Form1->StringGrid1->Cells[0][x]=x;
Form1->Edit6->Text=o;
if(ssj==0)
fprintf(fp2,"%10d, 0, , 運算符,%10s,%10s\n",x,str3[m].b,o);
x++;
}
int find1(int q) //檢驗是否有“ )”與“( ”相匹配
{
while(s[q]!='\0') //從當前字符開始查找,直到結尾
{
q=q+1;
if(s[q]=='(') //發(fā)現(xiàn)“( ”時遞歸調用find1()
{
q=find1(q);
if(q==0)
return 0;
}
if(s[q]==')') //發(fā)現(xiàn)“ )”時返回當前下標繼續(xù)匹配
{
q=q+1;
return q;
}
}
if(s[q]=='\0')
return 0;
}
int find2(int q) //檢驗是否有“( ”與“ )”相匹配
{
while(s[q]!='\0') //同find1()
{
q=q-1;
if(s[q]==')')
{
q=find2(q);
if(q==0)
return 0;
}
if(s[q]=='(')
{
q=q-1;
return q;
}
}
if(s[q]=='\0')
return 0;
}
int find3(int q) // 檢驗是否有“ } ”與“ { ”相匹配
{
while(s[q]!='\0') //同find1()
{
q=q+1;
if(s[q]=='{')
{
q=find3(q);
if(q==0)
return 0;
}
if(s[q]=='}')
{
q=q+1;
return q;
}
}
if(s[q]=='\0')
return 0;
}
int find4(int q) // 檢驗是否有“{ ”與“ }”相匹配
{
while(s[q]!='\0') //同find1()
{
q=q-1;
if(s[q]=='}')
{
q=find4(q);
if(q==0)
return 0;
}
if(s[q]=='{')
{
q=q-1;
return q;
}
}
if(s[q]=='\0')
return 0;
}
ok(char ch,int p) //主程序
{
int m;
if(ch>=65&&ch<=90||ch>=97&&ch<=122) //當CH為字母時送字母處理分程序處理
{
if(ssj1==1)
{
Form1->Edit5->Text="\0";Form1->Edit6->Text="\0";
Form1->Animate2->Play(1,24,1);
}
k=0;
}
else if(ch>=48&&ch<=57) //當CH為數(shù)字時送數(shù)字處理分程序處理
{
if(ssj1==1)
{
Form1->Edit4->Text="\0";Form1->Edit6->Text="\0";
Form1->Animate1->Play(1,24,1);
}
k=1;
}
else //除字母,數(shù)字外的符號送字符處理分程序
{
if(ssj1==1)
{
Form1->Edit4->Text="\0";Form1->Edit5->Text="\0";
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -