?? ddddd.cpp
字號:
// 孫揚.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#define MAX_DATA_LEN 100
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';
}
#include<malloc.h>
#include<memory.h>
#define WT_OPERATOR 0 // 操作符
#define WT_UINT 1 // 非負整數(shù)
#define WT_VARIABLE 2 // 變量
#define DFA_STATUS_ERR 255 // 錯誤狀態(tài)
struct WORDNODE
{
unsigned short byType; // 類別
char Value[MAX_DATA_LEN]; // 值
WORDNODE *pNext; // 下一結(jié)點
};
int IsNumChar(char b)
{
int a=(int) b;
if(0<<a<<9) {return 1;}
else return 0;
}
int GetInt(char c[],int d)////////////////////////////////////////////////////
{
return 1;
}
void Clear(WORDNODE *pHeader)
{
WORDNODE *pNode;
while (pHeader != NULL)
{
pNode = pHeader->pNext;
free(pHeader);
pHeader = pNode;
}
}
int IsEnglishChar(char m)
{
if ('a'<<m<<'z') {
return 1;
}
else return 0;
}
int GetVariableStatus(char c, int nStatus)
{
switch (nStatus)
{
case 0:
if (IsEnglishChar(c) || c == '_')
return 1;
else
return DFA_STATUS_ERR;
case 1:
if (IsNumChar(c) || IsEnglishChar(c) || c == '_')
return 1;
else
return 2;
}
return DFA_STATUS_ERR;
}
int GetVariable(char c[], int nCur)
{
int nStatus = 0;
for (; nCur < MAX_DATA_LEN; nCur++)
{
nStatus = GetVariableStatus(c[nCur], nStatus);
switch (nStatus)
{
case 2:
return nCur - 1;
case DFA_STATUS_ERR:
return -1;
}
}
return nCur;
}
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;
}
int IsOperator(char ch)
{
switch(ch){
case '+': return 1;
case '-': return 1;
case '*': return 1;
case '/': return 1;
default: return 0;
}
}
WORDNODE* WordAnalysis(char c[])
{
WORDNODE *pHeader = (WORDNODE *)malloc(sizeof(WORDNODE));
pHeader->pNext = NULL;
WORDNODE *pTail = pHeader, *pNode = NULL;
// 詞法分析
char cCur;
for (int nCur1 = 0, nEnd; nCur1 < MAX_DATA_LEN; nCur1++)
{
cCur = c[nCur1]; // 空間換時間,防止每個if都要計算數(shù)組下標(biāo)
if (IsNumChar(cCur)) // 整數(shù)
{
if ((nEnd = GetInt(c, nCur1)) == -1) // 出錯
{
Clear(pHeader);
return NULL;
}
pTail = AddNode(c, nCur1, nEnd, WT_UINT, pTail);
nCur1 = nEnd;
}else if (IsEnglishChar(cCur) || cCur == '_')
{ // 標(biāo)識符
if ((nEnd = GetVariable(c, nCur1)) == -1)
{// 出錯
Clear(pHeader);
return NULL;
}
pTail = AddNode(c, nCur1, nEnd, WT_VARIABLE, pTail);
nCur1 = nEnd;
}
else if (IsOperator(cCur)) // 算符
pTail = AddNode(c, nCur1, nCur1, WT_OPERATOR, pTail);
else if (cCur == '\0') // 結(jié)束
return pHeader;
else // 出錯
{
Clear(pHeader);
return NULL;
}
}
return pHeader;
}
/***********************************************************************************
* 函數(shù)功能:在鏈表中增加一個結(jié)點。#include <memory.h>
* 入口參數(shù):略。
* 返 回 值:新增結(jié)點指針。
***********************************************************************************/
/*****************************************
* 函數(shù)功能:標(biāo)識符(變量)識別
* 入口參數(shù):c 緩沖區(qū); nCur 掃描器當(dāng)前字符位置
* 返 回 值:標(biāo)識符結(jié)束位置,-1表示出錯
*****************************************/
/*****************************************
* 函數(shù)功能:標(biāo)識符(變量)狀態(tài)跳轉(zhuǎn)。為與DFA相一致,此函數(shù)未優(yōu)化。
* 入口參數(shù):c 當(dāng)前字符;nStatus 當(dāng)前狀態(tài)
* 返 回 值:下一狀態(tài)
*****************************************/
/*bool Save(WORDNODE *pHeader)
{
// 打開文件
FILE *f = fopen("Words.txt", "w");
if (f == NULL)
{
Clear(pHeader);
return false;
}
// 空出第一個結(jié)點
WORDNODE *pNode = pHeader->pNext;
// 保存數(shù)據(jù)
while (pNode != NULL)
{
fprintf(f, "%c,%s\n", pNode->byType + '0', pNode->Value);
pNode = pNode->pNext;
}
// 關(guān)閉文件
fclose(f);
return true;
}*/
// 清空鏈表
int main()//int argc, char* argv[])
{
WORDNODE *h;
char c[]={'x','y','-','(','x','-','1','0','0',')','/','2'};
h=WordAnalysis(c);
while(h->pNext!=NULL){
printf("dddddddddddddd\n");
printf(h->Value);
h=h->pNext;
}
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -