// WordAnalysis.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include "Chars.h"
#define MAX_DATA_LEN 256 // 数据缓冲区长度
// word type
#define WT_OPERATOR 0 // 操作符
#define WT_UINT 1 // 非负整数
#define WT_VARIABLE 2 // 变量
struct WORDNODE
{
unsigned short byType; // 类别
char Value[MAX_DATA_LEN]; // 值
WORDNODE *pNext; // 下一结点
};
// 预处理:将多余空格去掉
void Prefix(char c[])
{
for (int i = 0, j = 0; j < MAX_DATA_LEN && c[j] != '\0'; j++)
{
if (c[j] != ' ')
c[i++] = c[j];
}
c[i] = '\0';
}
// 清空链表
void Clear(WORDNODE *pHeader)
{
WORDNODE *pNode;
while (pHeader != NULL)
{
pNode = pHeader->pNext;
free(pHeader);
pHeader = pNode;
}
}
// 增加结点
WORDNODE* AddNode(char c[], int nBegin, int nEnd, unsigned short byType, WORDNODE *pTail)
{
WORDNODE *pNode = (WORDNODE *)malloc(sizeof(WORDNODE));
pNode->byType = byType;
pNode->pNext = NULL;
int nChars = nEnd - nBegin + 1;
memcpy(pNode->Value, &c[nBegin], nChars);
pNode->Value[nChars] = '\0';
pTail->pNext = pNode;
return pNode;
}
// 根据上一状态获取单词类别
unsigned short GetWordType(int nStatus)
{
switch (nStatus)
{
case 1:
return WT_OPERATOR;
case 2:
return WT_UINT;
case 3:
return WT_VARIABLE;
}
return 0xFF;
}
/***************************************
* 函数功能:识别一个单词
* 入口参数:c 扫描缓冲区
* nCur 扫描器指针
* pTail 单词序列尾指针
* 返 回 值:识别出的单词指针,NULL表示出错
*****************************************/
WORDNODE* IdentifyOneWord(char c[], int &nCur, WORDNODE *pTail)
{
int begin,type,end;
unsigned short bytype1,bytype2;
begin=nCur;
if(IsEnglishCharOr_(c[begin]))
{type=3;}
if(IsNumChar(c[begin]))
{type=2;}
if(IsOperator(c[begin]))
{type=1;}
bytype1=GetWordType(type);
do{
nCur++;
if(IsEnglishCharOr_(c[nCur]))
{type=3;}
if(IsNumChar(c[nCur]))
{type=2;}
if(IsOperator(c[nCur]))
{type=1;}
bytype2=GetWordType(type);
}while(bytype1==bytype2);
end=nCur-1;
return(AddNode(c,begin,end,bytype1,pTail));
}
// 词法分析
WORDNODE* WordAnalysis(char c[])
{
// 第一个结点作为头结点,不使用
WORDNODE *pHeader = (WORDNODE *)malloc(sizeof(WORDNODE));
pHeader->pNext = NULL;
WORDNODE *pTail = pHeader, *pNode = NULL;
// 词法分析
for (int nCur = 0; c[nCur] != '\0'; )
{
// 识别一个单词
pNode = IdentifyOneWord(c, nCur, pTail);
if (pNode == NULL) // 出错
{
Clear(pHeader);
return NULL;
}
// 识别下一个单词
pTail = pNode;
}
return pHeader;
}
bool Save(WORDNODE *pHeader)
{
// 文件名
char FileName[256];
printf("单词序列输出文件名(如a.txt):\n");
scanf("%s", FileName);
// 打开文件
FILE *f = fopen(FileName, "w");
if (f == NULL)
{
Clear(pHeader);
return false;
}
// 空出第一个结点
WORDNODE *pNode = pHeader->pNext;
// 保存数据
while (pNode != NULL)
{
fprintf(f, "%c,%s\n", pNode->byType + '0', pNode->Value);
pNode = pNode->pNext;
}
// 关闭文件
fclose(f);
return true;
}
// 主函数
int main(int argc, char* argv[])
{// 输入char c[MAX_DATA_LEN];
printf("请输入表达式:\n");
gets(c);
// 预处理
Prefix(c);
// 词法分析
WORDNODE *pHeader = WordAnalysis(c);
if (pHeader == NULL)
{
printf("\n词法分析错误!\n");
return 0;
}
// 保存
if (!Save(pHeader))
{
printf("\n保存文件失败\n");
return 0;
}
// 清空数据
Clear(pHeader);
// 完成
printf("\n词法分析成功,并已保存到文件\n", c);
printf("按任意键退出\n", c);
getchar();
getchar();
return 0;
}
#if !defined(AFX_CHARS_H__D2786D29_6773_4679_93E5_3FC3AF73385F__INCLUDED_)
#define AFX_CHARS_H__D2786D29_6773_4679_93E5_3FC3AF73385F__INCLUDED_
/***********************************************
* 字符类别判断
************************************************/// c是否为英文字符或下划线
bool IsEnglishCharOr_(char c)
{return ((c >= 'a' && c <= 'z')|| (c >= 'A' && c <= 'Z'))|| (c == '_');
}
// c是否为数字
bool IsNumChar(char c)
{return (c >= '0' && c <= '9');}
// c是否为运算符
bool IsOperator(char c)
{switch (c){case '+': case '-': case '*': case '/': case '(':case')':
--------------------next---------------------
return true;}return false;
}#endif
//这是上面的那个程序的一部分
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__780CBCE9_325B_45B9_ACD6_368471BF18B6__INCLUDED_)
#define AFX_STDAFX_H__780CBCE9_325B_45B9_ACD6_368471BF18B6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__780CBCE9_325B_45B9_ACD6_368471BF18B6__INCLUDED_)
//我调了好多次就是不知道错误在哪,顺便说一下,这是我们老师布置的一个上机题,自己写的函数就只有下面这个函数
/***************************************
* 函数功能:识别一个单词
* 入口参数:c 扫描缓冲区
* nCur 扫描器指针
* pTail 单词序列尾指针
* 返 回 值:识别出的单词指针,NULL表示出错
*****************************************/
WORDNODE* IdentifyOneWord(char c[], int &nCur, WORDNODE *pTail)
--------------------next---------------------
阅读(1366) | 评论(0) | 转发(0) |