📄 wyby.cpp
字号:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <string.h>
int isCharacter(char c){ //判断输入的是不是字符
int i=(int)(c);
if ( ( i>=97 && i<=122)||( i>=65 && i<=90) ){
return 1;
}
else{
return 0;
}
}
int isNumber(char c){ //判断输入的是不是数字
if( c>='0' && c<='9' )
return 1;
else
return 0;
}
int isOperator(char c){ //判断输入的是不是运算符
if( c=='+' || c=='-' || c=='*' || c=='/' || c=='=')
return 1;
else
return 0;
}
///////////////////////////////// 分析字符串部分 //////////////////////////////////////////////
int isExpression(char currentLine[],int begin,int end); //分析字符串判断是不是表达式
int analyze(char currentLine[],int begin,int end){ //分析字符串判断其属于哪种类型
/* 返回值:
0 语法错误
1 标识符
2 数字
3 运算符
4 表达式 */
int flag=0;
if( (isCharacter(currentLine[begin])==1) || currentLine[begin]=='_' ){
//如果是标识符,字符必须是字母、数字或者下滑线
for(int m=begin+1;m<end;m++){
if(! (( isCharacter(currentLine[m])==1) ||
currentLine[m]=='_' ||
(isNumber(currentLine[m])==1)) ){
//如果不是这些字符的话,看看是不是表达式
if(isOperator(currentLine[m])){
if(isExpression(currentLine,begin,end)){
return 4;
}
}
return 0;
}
}
return 1;
}
else{
switch(currentLine[begin])
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
{
flag=0;
for(int m=begin+1;m<end;m++){
if (!(isNumber(currentLine[m])==1)){
//看看是不是小数
if(currentLine[m]=='.'){
flag++; //如果是小数点,再给一次机会
if(m==end-1)
flag++; //如果小数点在数字末尾,出错
}
else{
flag=2; //如果不是小数点,那么出错了
}
if(flag>1){
//看看是不是表达式
if(isOperator(currentLine[m])){
if(isExpression(currentLine,begin,end)){
return 4;
}
}
return 0;
}
}
}
return 2;
break;
}
case '*':
case '/':
if(begin+1!=end){
return 0;
}
else{
return 3;
}
break;
case '+':
case '-':
if(begin+1==end){
return 3;
}
else
{
if ((currentLine[begin+1]=='+'||currentLine[begin+1]=='=') &&
(begin+2==end)){
return 3;
}
else{
flag=0;
for(int m=begin+1;m<end;m++){
if (!(isNumber(currentLine[m])==1)){
//看看是不是小数
if(currentLine[m]=='.'){
flag++; //如果是小数点,再给一次机会
}
else{
flag=2; //如果不是小数点,那么出错了
}
if(flag>1){
//看看是不是表达式
if(isOperator(currentLine[m])){
if(isExpression(currentLine,begin,end)){
return 4;
}
}
return 0;
}
}
}
return 2;
break;
}
}
break;
case '=': //只允许出现 = ==
if(begin+1==end){
return 3;
}
else{
if ((currentLine[begin+1]=='=') && (begin+2==end)){
return 3;
}
else{
return 0;
}
}
break;
}
}
return 0;
}
int isExpression(char currentLine[],int begin,int end){
int iTempBegin,iTempEnd;
iTempBegin=iTempEnd=begin;
char sOperator[3];
//出现在字符串首部的操作符,只能是++ --,或者是“+”“-”加上数字开头 +563.0
int flag=0;
if(isOperator(currentLine[iTempBegin])){
if( (currentLine[iTempBegin]=='+' ||
currentLine[iTempBegin]=='-') &&
(isNumber(currentLine[iTempBegin+1])) ){
flag=1;
}
if(flag==0){
if(isOperator(currentLine[iTempBegin+1])){
sOperator[0]=currentLine[iTempBegin];
sOperator[1]=currentLine[iTempBegin+1];
if(!(sOperator[0]=='+' || sOperator[0]=='-')){
return 0;
}
else{
if(sOperator[0]=='+'){
if(sOperator[1]!='+')
return 0;
}
else{
if(sOperator[1]!='-')
return 0;
}
}
}
else{
return 0;
}
}
}
//用标点符号分割字符串为各个子项。如果它是表达式,那么必须是每个子项语法都没错
while(iTempEnd!=end){
iTempBegin=iTempEnd;
while( !isOperator(currentLine[iTempBegin]) && iTempBegin < end){
iTempBegin++;
}
if(iTempBegin <= end){
if(analyze(currentLine,iTempEnd,iTempBegin)==0){
return 0;
}
iTempEnd=iTempBegin;
}
//处理字符串中间的操作符,只能连续2个操作符,为3个就出错。
int iCount=0;
while( isOperator(currentLine[iTempEnd]) && iTempEnd < end){
sOperator[iCount]=currentLine[iTempEnd];
if(iCount==2){
return 0;
}
if(iCount==1){
if(sOperator[0]=='=' && sOperator[1]!='=')
return 0;
if(sOperator[0]=='+' && (!(sOperator[1]=='=' || sOperator[1]=='+')))
return 0;
if(sOperator[0]=='-' && (!(sOperator[1]=='=' || sOperator[1]=='-')))
return 0;
if(sOperator[0]=='*' && (!(sOperator[1]=='=')))
return 0;
if(sOperator[0]=='/' && (!(sOperator[1]=='=')))
return 0;
}
iCount++;
iTempEnd++;
}
}
//出现在字符串尾部的操作符,只能是++ --
if(isOperator(currentLine[end-1])){
if(begin<end-1){
if(isOperator(currentLine[end-2])){
sOperator[0]=currentLine[end-2];
sOperator[1]=currentLine[end-1];
if(!(sOperator[0]=='+' || sOperator[0]=='-')){
return 0;
}
else{
if(sOperator[0]=='+'){
if(sOperator[1]!='+')
return 0;
}
else{
if(sOperator[1]!='-')
return 0;
}
}
}
else{
return 0;
}
}
else{
return 0;
}
}
return 1;
}
////////////////////////////// 用于保存结果部分 /////////////////////////////////////////
class ListNode //链表的单元节点
{
public:
char m_string[100];
ListNode * next;
};
class List //链表
{
public:
ListNode m_oHead,m_oEnd;
int length;
//链表的初试化操作
List(){
m_oHead.next=NULL;
m_oEnd.next=NULL;
length=0;
}
//每次添加节点都添加到链表的末尾
void Add(char currentLine[],int begin,int end){
ListNode * pNode=new ListNode();
copySubString(currentLine,pNode->m_string,begin,end);
pNode->next=NULL;
if(m_oHead.next==NULL){
//当插入的是第一个节点
m_oHead.next=pNode;
m_oEnd.next=pNode;
}
else{
//当插入的不是第一个节点
m_oEnd.next->next=pNode;
m_oEnd.next=pNode;
}
}
//打印整个链表
void toString(){
ListNode * pNode=m_oHead.next;
while(pNode!=NULL){
cout<<pNode->m_string<<" ";
pNode=pNode->next;
}
}
void writeSubString(char string[],int begin,int end){
for(int i=begin;i<end;i++){
cout<<string[i];
}
}
void copySubString(char srcString[],char desString[],int begin,int end){
for(int i=begin;i<end;i++){
desString[i-begin]=srcString[i];
}
desString[end-begin]='\0';
}
};
////////////////////////////// 主函数 /////////////////////////////////////////
void main()
{
int begin=0,end=0;
List lstNumbers,lstOperators,lstIdentifiers,lstErrors,lstExpression;
//输入参数为input.txt
ifstream iFile("input.txt",ios::in);
if(!iFile.is_open()){
cout << " Error ,can't open the file!" << endl;
}
char currentLine[100];
cout << " ------------- input.txt -----------"<< endl;
while(!iFile.eof()){
//一行一行分析输入的文件
iFile.getline(currentLine,100,'\n');
cout<< currentLine <<endl;
begin=0,end=0;
//用空格做为分割付再分析字符串
while(currentLine[end]!='\0'){
begin=end;
while(currentLine[begin] == ' ' && currentLine[begin] != '\0'){
begin++;
}
if(currentLine[begin]=='\0'){
end=begin;
}
else
{
end=begin;
while(currentLine[end] != ' ' && currentLine[end] != '\0'){
end++;
}
//根据分析的结果,插入到不同的链表中
switch(analyze(currentLine,begin,end)){
case 0:
lstErrors.Add(currentLine,begin,end);
break;
case 1:
lstIdentifiers.Add(currentLine,begin,end);
break;
case 2:
lstNumbers.Add(currentLine,begin,end);
break;
case 3:
lstOperators.Add(currentLine,begin,end);
break;
case 4:
lstExpression.Add(currentLine,begin,end);
break;
}
}
}
}
iFile.close();
//打印结果
cout << "\r\n ------------- Result -----------"<<endl;
cout<<"This is all the Errors : " ;
lstErrors.toString();
cout<<"\nThis is all the Identifiers : ";
lstIdentifiers.toString();
cout<<"\nThis is all the Numbers : ";
lstNumbers.toString();
cout<<"\nThis is all the Operators : ";
lstOperators.toString();
cout<<"\nThis is all the Expression : ";
lstExpression.toString();
cout<<"\r\n\r\n";
cin>>begin;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -