hxy。。 2021-03-21 11:59 采纳率: 63.6%
浏览 34
已采纳

c++ 链表中有关指针问题的求教

#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);
}
  • 写回答

1条回答 默认 最新

  • 幸福快乐着 2021-03-21 17:02
    关注

    你的最大问题出在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
      请按任意键继续. . .
      本回答被题主选为最佳回答 , 对您是否有帮助呢?
      评论

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效