⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test1.cpp

📁 算符优先分析程序的分析、设计与实现的基本技术与一般方法
💻 CPP
字号:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iomanip.h>
#include <stdafx.h>
#include "formula.h"

struct word
{
	char *str;
	int type;
};
typedef char *strings;
int error_num=0;//error_num用来记录错误单词的位置

//void compiler(char *);
int word_analysis(char *s,word *wordtable,int&);//对公式进行词法分析
//char *single_word(strings &s,int&);//从串中取一单词
//int isNumber(char *str,double &result);//
int isCorrectGrammar(word *wordtable,int word_num);//对词法分析结果进行语法分析
void  del_blank(char *s);//删除字符串s头部的空格字符
//int word_analysis(char *s,word *wordtable,int);//对公式进行词法分析
char *single_word(strings &s,int&,int&);//从串中取一单词
//int isNumber(char *str,double &result);//
//int isCorrectGrammar(word *wordtable,int word_num);//对词法分析结果进行语法分析
void  del_blank(char *s)
{    //删除字符串s头部的空格字符
    char *p=s;
	while(*p==' '&&*p) p++;
	if(p!=s)
		strcpy(s,p);
}
int word_analysis(char *s,word *wordtable,int &error_n)
{   //对公式字符串s进行词法分析,单词放入word中,并返回单词总数
	int kk=0;
	//char  *oneword;
	error_n=0;
	while(*s)
	{
		int i=0;
		del_blank(s);
		wordtable[kk].str=single_word(s,wordtable[kk].type,error_n);  
		      //取下一个单词
	          //将单词及其属性写入单词表
		kk++;
	}
	wordtable[kk].str=new char[2];
	wordtable[kk].str[0]='#';
	wordtable[kk].str[1]='\0';
	wordtable[kk].type=4;
	kk++;
	return kk;
}
char *single_word(strings &s,int &type,int &reror_n)
{   //从串中取一单词
	error_num++;
	char *str;
	char *p;
	switch(*s)
	{
	case '+':
	case '-':
	case '*':
	case '/':
	case '(':
	case ')':
	case '#':
		{ 
			str=new char[2];
			str[0]=*s++;
			str[1]='\0';
			type=4;
			return str;
		}
	}
	str=new char[20];
	p=str;
	if(*s>='0'&&*s<='9')//是数值串
	{
		while(*s&&*s>='0'&&*s<='9')
			*p++=*s++;
		if(*s=='.')
		{
			*p++=*s++;
			if(*s<'0'||*s>'9')
			{
				*p='\0';
				cout<<error_num<<":   实数格式错误!\n";
				type=-1;
				reror_n++;
				return str;
			}
			while(*s>='0'&&*s<='9'&&*s)
				*p++=*s++;
		}
		*p='\0';
		type=3;
		return str;
	}
	if(*s>='a'&&*s<='z'||*s>='A'&&*s<='Z')//为一标识符
	{
		while(*s&&(*s>='0'&&*s<='9'||*s>='a'&&*s<='z'||*s>='A'&&*s<='Z'))
			*p++=*s++;
		*p='\0';
		type=1;    //or type=2;即当前单词为自变量或标准函数名
		return str;
	}
	str[0]=*s++;
	str[1]='\0';
	cout<<error_num<<":   数学公式中无此符号!\n";
	type=-1;
	reror_n++;
	return str;//返回错误的子串
}//single_word

char  value[][10]={">><<<<<>>",">><<<<<>>",">>>><<<>>",">>>><<<>>",
				   ">>>>$$=>>",">>>>$$$>>","<<<<<<<=$",">>>>$$$>>",
					"<<<<<<<$="};
char line[10]="+-*/id()#";
int opr_num(char c)
{
	//求运算符c在line中的下标,失败返回-1
	char *ptr=line;
	while(*ptr)
		if(*ptr==c)
			return ptr-line;
		else ptr++;
	return -1;
}

int isCorrectGrammar(word *wordtable,int word_num)
//对词法分析的结果进行语法分析
{
	char s[100],a;
	int top=0,k=0;
	s[0]='#';
	do
	{
		int j;
		if(wordtable[k].type==1||wordtable[k].type==2)
			a='i';
		else if(wordtable[k].type==3)
			a='d';
		else if(wordtable[k].type==4)
			a=wordtable[k].str[0];
		int a_num=opr_num(a);
		if(s[top]>='A'&&s[top]<='Z')//取栈顶第1个运算符
			j=top-1;
		else
			j=top;
//		int a_num;
//		a_num=opr_num(a);
		int top_opr_num=opr_num(s[j]);
		while(value[top_opr_num][a_num]=='>')
		{
			for(int i=0;i<=top;i++)
				cout<<s[i];
			cout<<"               ";
			for( i=k;i<word_num;i++)
				cout<<wordtable[i].str;
			cout<<endl;

			switch(s[j])
			{
			case'+':
			case'-':
				top-=2;
				s[top]='E';
				break;
			case'*':
			case'/':
				top-=2;
				s[top]='T';
				break;
			case'i':
			case'd':
				s[top]='F';
				break;
			case')':
				if(s[j-2]=='('&&s[j-3]=='i')
				{
					top-=3;
					s[top]='F';
					break;
				}
				if(s[j-2]=='(')
				{
					top-=2;
					s[top]='F';
					break;
				}
				cout<<"语法错误!\n";
				return 0;
			default:
				cout<<"语法错误!\n";
				return 0;
			}
			j=top-1;
			top_opr_num=opr_num(s[j]);
		}
			for(int i=0;i<=top;i++)
				cout<<s[i];
			cout<<"               ";
			for( i=k;i<word_num;i++)
				cout<<wordtable[i].str;
			cout<<endl;

		if(value[top_opr_num][a_num]=='<'||value[top_opr_num][a_num]=='=')
		{
			top++;
			s[top]=a;
			k++;
		}
		else return 0;
	}while(a!='#');
	return 1;
}
#ifndef FORMULA
#define FORMULA

//void compiler(char *);
//void  del_blank(char *s);//删除字符串s头部的空格字符


#endif
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iomanip.h>
#include "formula.h"
void main()
{ 
	char  s[100];
 
 	cout<<"请输入函数公式:\n";
	cin.getline(s,99);
	cout<<"函数公式为:"<<s<<endl;
		
	int word_num,error_num;
	word wordtable[100];
	//del_blank(s);  //删除串中所有空格
	word_num=word_analysis(s,wordtable,error_num);//进行词法分析
	cout<<"共有"<<error_num<<"个错误!\n\n";
	for(int i=0;i<word_num;i++)   //调试用
		cout<<setw(2)<<i+1<<":  "<<setw(10)
		<<wordtable[i].str<<"     "<<wordtable[i].type<<endl;

	//下面进行语法分析
	if(isCorrectGrammar(wordtable,word_num))
		cout<<"OK--符合数学表达式语法!"<<endl;   
	else
		cout<<"ERROR--不符合数学表达式的语法规范!"<<endl;

//	return ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -