📄 accidenceanalyzer.cpp
字号:
#include <afx.h>
#include <iostream.h>
#include <string.h>
#include <conio.h>
//显示菜单选项函数
void Display()
{
cout<<endl<<endl<<"Option to choose:"<<endl;
cout<<"**********************************************"<<endl;
cout<<"(1).Display the Num-Word table"<<endl; //显示内部定义单词种别码表
cout<<"(2).Display the Sample and the Analysis result"<<endl; //显示样板代码和分析结果
cout<<"(3).Quit"<<endl; //退出
cout<<"**********************************************"<<endl;
cout<<"Please choose the function(1~3):";
}
//对样板每行字符串进行词法分析函数
void Analyze(CString &LineStr) //形参LineStr接收调用函数传来的每一行的字符串
{
int i,j; //i为循环变量,j为字符串word的字符指针
CString str1,word; //str1记录种别码文件每行字符串,word记录被分解出的单词
char ch; //ch记录每读进的一个字符
bool flag=false; //flag标识关系运算符为双字符(如<=)或单字符(如<)
CStdioFile MyFile1;
MyFile1.Open("key.txt",CFile::modeRead|CFile::typeText); //key.txt为单词种别码文件
for(i=0,j=0;i<=LineStr.GetLength();i++) //循环读每行字符串的字符
{
if(i!=LineStr.GetLength()) ch=LineStr.GetAt(i);
//若刚遇到双字符关系运算符,读入新字符后跳出当前循环
if(flag)
{
word.Insert(j++,ch);
flag=false;
continue;
}
//若读入的是数字,且前面读到的是字母或数字,把数字加入到word中
if(ch>=48&&ch<=57&&(word.Right(1)>=48&&word.Right(1)<=57||word.Right(1)>=97&&word.Right(1)<=122))
{
word.Insert(j++,ch);
continue;
}
//若读入的不是数字,且前面读到的都是数字,把word输出,清空word,归入常量种类中
if(word.Left(1)>=48&&word.Left(1)<=57&&word.Right(1)>=48&&word.Right(1)<=57)
{
cout<<"(22,\""<<word<<"\")"<<"\t";
j=0;
word.Empty();
}
//若读入的是字母,且前面读到的是字母,把字母加入到word中
if(ch>=97&&ch<=122&&word.Right(1)>=97&&word.Right(1)<=122)
{
word.Insert(j++,ch);
continue;
}
//若前面读到<,>,=,读入的字符是=,加入到word中,flag为真
if((word=="<"||word==">"||word=="=")&&ch=='=')
{
word.Insert(j++,ch);
flag=true;
}
/*循环读入种别码文件的每一行,与word比较,若找到相同的字符串,
输出编号和word,清空word*/
while(MyFile1.ReadString(str1))
if(word.Compare(str1.Mid(3))==0)
{
cout<<"("<<str1.Mid(0,2)<<",\""<<word<<"\")"<<"\t";
word.Empty();
j=0;
break;
}
MyFile1.SeekToBegin(); //文件指针返回头部
/*若在种别码文件中找不到相同的字符串,且word以字母开头,
把word归为变量种类输出,清空word*/
if(word!=""&&word.Left(1)>=97&&word.Left(1)<=122)
{
cout<<"(21,\""<<word<<"\")"<<"\t";
j=0;
word.Empty();
}
//若不是以字母开头,把word归为错误字符输出,清空word
else if(word!="")
{
cout<<"Error,\""<<word<<"\""<<"\t";
j=0;
word.Empty();
}
//把新读入字符加入word中
if(!flag&&ch!=' ')
word.Insert(j++,ch);
}
MyFile1.Close();
}
void main()
{
CString str; //str记录文件中每一行字符串
int i=0; //i用于控制输出格式
char ch; //ch记录所选择的功能编号
CStdioFile MyFile;
while(true)
{
Display();
cin>>ch;
switch(ch)
{
case '1': //输出自定义种别码文件
MyFile.Open("key.txt",CFile::modeRead|CFile::typeText);
cout<<endl<<"Num-Word Table:"<<endl;
while(MyFile.ReadString(str))
{
i++;
cout<<str<<"\t\t";
if(i%2==0) cout<<endl;
}
i=0;
MyFile.Close();
break;
case '2': //输出样板文件和词法分析结果
MyFile.Open("sample.txt",CFile::modeRead|CFile::typeText);
cout<<endl<<"The Content of Sample:"<<endl;
cout<<"###########################"<<endl;
while(MyFile.ReadString(str))
cout<<str<<endl;
cout<<"###########################"<<endl;
MyFile.SeekToBegin();
cout<<"Press any key to see the result..."<<endl;
getch();
cout<<endl<<"Accidence Analyze Result:"<<endl;
while(MyFile.ReadString(str))
Analyze(str);
MyFile.Close();
break;
case '3': //退出系统
exit(0);
break;
default:
cout<<"Please type in the right function number!"<<endl;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -