#define _CRT_SECURE_NO_WARNINGS;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <string>
#include "head.h"
#include <vector>
using namespace std;
char KeyWord[] = "保留字";
char ID[] = "标识符";
char keyWord[50][100] = { "void","main","printf","if","else","return","int" };
int currentNum = 0;
char entry[40];
FILE* cstream, * outstream;
ChainList keyWordList;
ChainList idList;
int move(int s, char ch);
void AutoForId(FILE* cstream, FILE* outstream);
int getLength(char ch[]);
int main()
{
ChainListNode* temp;
while (strcmp("",keyWord[currentNum])!=0)
{
if (keyWordList.Insert(keyWord[currentNum], currentNum, KeyWord))
printf("%s\n", "插入成功!");
else
printf("%s\n","插入失败!");
currentNum++;
}
currentNum = 0;
if ((cstream = fopen("d:/sample.txt", "r")) == NULL)
{
printf("Failed to open sample.txt!");
exit(0);
}
if ((outstream = fopen("d:/text.txt", "w")) == NULL)
{
printf("Failed to open text.txt");
exit(0);
}
AutoForId(cstream, outstream);
fclose(cstream);
fclose(outstream);
system("pause");
}
void AutoForId(FILE* cstream, FILE* outstream)
{
char ch;
int currentNum = 0;
int s0 = 0;
int sEnd = 2;
int s = s0;
memset(entry,'\0',sizeof(entry));
ch = fgetc(cstream);
while (ch != EOF)
{
s = move(s, ch);
if (s == sEnd)
{
//if (keyWordList.Search(entry) && !idList.Search(entry))
{
idList.Insert(entry, currentNum, KeyWord);
printf("%s\n",entry);//fprintf(outstream, "%d %s %s\n", currentNum, KeyWord, entry);
currentNum++;
}
/*else if (!keyWordList.Search(entry) && !idList.Search(entry))
{
idList.Insert(entry, currentNum, ID);
fprintf(outstream, "%d %s %s\n", currentNum, ID, entry);
currentNum++;
}*/
memset(entry, '\0', sizeof(entry));
s = s0;
}
ch = fgetc(cstream);
}
}
int move(int s, char ch)
{
if (((int)ch >= 65 && (int)ch <= 90) || ((int)ch >= 97 && (int)ch <= 122) && s == 0)
{
entry[getLength(entry)] = ch;
return 1;
}
else if (s == 0)
return 0;
else if (((((int)ch >= 65 && (int)ch <= 90) || ((int)ch >= 97 && (int)ch <= 122) || ((int)ch >= 48 && (int)ch <= 57))) && s == 1)
{
entry[getLength(entry)] = ch;
return 1;
}
else if(s==1)
return 2;
}
int getLength(char ch[])
{
int length = 0;
while (ch[length] != '\0')
length++;
return length;
}
head.h
#pragma once
#include <string.h>
#include<stdio.h>
struct Data
{
public:
char* name;
int num;
char* type;
};
class ChainListNode
{
public:
Data data;
ChainListNode* nextNode;
};
class ChainListHeadNode
{
public:
ChainListNode* nextNode;
};
class ChainList
{
public:
ChainListHeadNode* firstNode;
ChainList()
{
length = 0;
firstNode = new ChainListHeadNode;
firstNode->nextNode= nullptr;
}
~ChainList()
{
ChainListNode* current;
current = firstNode->nextNode;
while (firstNode->nextNode)
{
current = current->nextNode;
delete firstNode->nextNode;
firstNode->nextNode = current;
}
delete firstNode;
}
bool Insert(char *newName,int newNum,char* newType)
{
ChainListNode* newNode = new ChainListNode;
newNode->data.name = newName;
newNode->data.num = newNum;
newNode->data.type = newType;
newNode->nextNode = firstNode->nextNode;
firstNode->nextNode = newNode;
length++;
return true;
}
bool Search(char *searchKey)
{
ChainListNode* current = firstNode->nextNode;
while (current && strcmp(searchKey,current->data.name)!=0)
current = current->nextNode;
if (current)
return true;
else
return false;
}
void Display()
{
ChainListNode* current = firstNode->nextNode;
while (current)
{
printf("%s\n",current->data.name);
current = current->nextNode;
}
}
int getListLength()
{
return length;
}
private:
int length ;
};
这是老师布置的一个识别简单代码中标识符自动机的代码。我构造了一个保留字链表,在之后自动机代码段执行的过程中,被注释掉的if语句中的Search函数除了第一个void值,之后对所有entry执行的返回值都是true,也就是说我的第二个链表只有void一个元素。真心求教。
void main()
{
int i,j,sum;
i=j=1;
sum=0;
sum=i+j;
printf("%d\n",sum);
}
你的最大问题出在class ChainList的成员函数bool Insert(char *newName,int newNum,char* newType)
newNode->data.name = newName;
newNode->data.num = newNum;
newNode->data.type = newType;
改为
newNode->data.name = _strdup(newName);
newNode->data.num = newNum;
newNode->data.type = _strdup(newType);
定义在函数体外的变量均为静态变量,地址不变,而你对变量改变时,与其相同地址的变量都会跟着变。
另外你的函数还有其它问题,如文件路径中“\"要用‘\\’
其它问题不一一列举了,将源码给你:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <string>
#include "head.h"
#include <vector>
using namespace std;
char KeyWord[] = "保留字";
char ID[] = "标识符";
char keyWord[50][100] = { "void","main","printf","if","else","return","int" };
int currentNum = 0;
char entry[40];
FILE* cstream, * outstream;
ChainList keyWordList;
ChainList idList;
int move(int s, char ch);
void AutoForId(FILE* cstream, FILE* outstream);
int getLength(char ch[]);
int main()
{
ChainListNode* temp;
while (strcmp("", keyWord[currentNum]) != 0)
{
if (keyWordList.Insert(keyWord[currentNum], currentNum, KeyWord))
printf("%s\n", "插入成功!");
else
printf("%s\n", "插入失败!");
currentNum++;
}
currentNum = 0;
if ((cstream = fopen("d:\\sample.txt", "r")) == NULL)
{
printf("Failed to open sample.txt!");
exit(0);
}
if ((outstream = fopen("d:\\text.txt", "w")) == NULL)
{
printf("Failed to open text.txt");
exit(0);
}
AutoForId(cstream, outstream);
fclose(cstream);
fclose(outstream);
system("pause");
}
void AutoForId(FILE* cstream, FILE* outstream)
{
char ch;
int currentNum = 0;
int s0 = 0;
int sEnd = 2;
int s = s0;
memset(entry, '\0', sizeof(entry));
ch = fgetc(cstream);
while (ch != EOF)
{
s = move(s, ch);
if (s == sEnd)
{
if (keyWordList.Search(entry) && !idList.Search(entry))
{
idList.Insert(entry, currentNum, KeyWord);
printf("%s\n", entry);
fprintf(outstream, "%d %s %s\n", currentNum, KeyWord, entry);
currentNum++;
}
else if (!keyWordList.Search(entry) && !idList.Search(entry))
{
idList.Insert(entry, currentNum, ID);
printf("%s\n", entry);
fprintf(outstream, "%d %s %s\n", currentNum, ID, entry);
currentNum++;
}
memset(entry, '\0', sizeof(entry));
s = s0;
}
ch = fgetc(cstream);
}
}
int move(int s, char ch)
{
if (((int)ch >= 65 && (int)ch <= 90) || ((int)ch >= 97 && (int)ch <= 122) && s == 0)
{
entry[getLength(entry)] = ch;
return 1;
}
if (((int)ch >= 65 && (int)ch <= 90) || ((int)ch >= 97 && (int)ch <= 122) || ((int)ch >= 48 && (int)ch <= 57) && s == 1)
{
entry[getLength(entry)] = ch;
return 1;
}
if (s == 1)
return 2;
return 0;
}
int getLength(char ch[])
{
int length = 0;
while (ch[length] != '\0')
length++;
return length;
}
head.h
#pragma once
#include <string.h>
#include<stdio.h>
struct Data
{
public:
char* name;
int num;
char* type;
};
class ChainListNode
{
public:
Data data;
ChainListNode* nextNode;
};
class ChainListHeadNode
{
public:
ChainListHeadNode() { nextNode = nullptr; }
public:
ChainListNode* nextNode;
};
class ChainList
{
public:
ChainListHeadNode* firstNode;
ChainList()
{
length = 0;
firstNode = new ChainListHeadNode;
firstNode->nextNode = nullptr;
}
~ChainList()
{
ChainListNode* current;
current = firstNode->nextNode;
while (firstNode->nextNode)
{
current = current->nextNode;
delete firstNode->nextNode;
firstNode->nextNode = current;
}
delete firstNode;
}
bool Insert(char* newName, int newNum, char* newType)
{
ChainListNode* newNode = new ChainListNode;
newNode->data.name = _strdup(newName);
newNode->data.num = newNum;
newNode->data.type = _strdup(newType);
newNode->nextNode = firstNode->nextNode;
firstNode->nextNode = newNode;
length++;
return true;
}
bool Search(char* searchKey)
{
ChainListNode* current = firstNode->nextNode;
for (int i = 0; i < length; i++)
{
if (strcmp(searchKey, current->data.name) == 0)
return true;
if (current->nextNode == nullptr)
return false;
current = current->nextNode;
}
return false;
}
void Display()
{
ChainListNode* current = firstNode->nextNode;
while (current)
{
printf("%s\n", current->data.name);
current = current->nextNode;
}
}
int getListLength()
{
return length;
}
private:
int length;
};
在D:盘下自定义一个文本文件sample.txt,内容为“void","main","printf","if","else","return","int" ,可用于测试
插入成功!
插入成功!
插入成功!
插入成功!
插入成功!
插入成功!
插入成功!
void
main
printf
if
else
return
int
请按任意键继续. . .