请大家帮忙看看这段程序中出现的错误改怎么改啊?

freedom2028 2008-09-19 01:58:43
程序目的是将线性表lb中出现而在线性表la中没有出现的元素插入到线性表la的末尾

程序的源代码如下:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <malloc.h>

using namespace std;

#define INITSIZE 100
#define LISTINCREMENT 100
#define MIDSTRLEN 30


//typedef char ElemType

struct sqList
{
int length;
char* elem;
int listsize;
};

void InitList(sqList* L)
{
L->elem = (char*)malloc(sizeof(char)*INITSIZE);
L->length = 0;
L->listsize = INITSIZE;
}

void GetLen(sqList* L)
{
for (int i = 0; i < (L->listsize); i++)
{
if (*(L->elem + i) != '\0')
{
++(L->length);
}
}
}

char GetElem(sqList L, int i)
{
char e;
e = *(L.elem + i - 1);
return e;
}

void ListInsert(sqList* L, char e)
{
if (L->length >= L->listsize)
{
char* newbase;
newbase = (char*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(char));
if (newbase == NULL)
{
cout<<"Error when alloting memory!"<<endl;
exit(0);
}
L->elem = newbase;
L->listsize = L->listsize + LISTINCREMENT;
}
char* p;
p = L->elem + L->length;
*p = e;
++(L->length);
}

void main (int argc, char* argv[])
{
sqList la, lb;
char str1[MIDSTRLEN];
char str2[MIDSTRLEN];
InitList(&la);
InitList(&lb);
ifstream infile("input.txt");
if (!infile)
{
cout<<"input.txt can't be opened!"<<endl;
system("pause");
exit(0);
}
infile.getline(str1, sizeof(str1));
infile.getline(str2, sizeof(str2));
for (int i = 0; i <= sizeof(str1); i++)
{
if(str1[i] != '\32' || str1[i] != '\0')
{
*(la.elem) = str1[i];
++(la.elem);
}
}
for (int m = 0; m <= sizeof(str2); m++)
{
if(str2[m] != '\32' || str2[m] != '\0')
{
*(lb.elem) = str2[m];
++(lb.elem);
}
}
infile.close();
GetLen(&la);
GetLen(&lb);
for (int n = 1; n < lb.length; n++)
{
char e;
e = GetElem(lb, n);
for (int j = 0; j < la.length; j++)
{
if (*(la.elem + j) == e)
{
ListInsert(&la, e);
}
}
}
ofstream outfile("output.txt");
for (int k = 0; k < 2*(la.length); k++)
{
outfile<<(*(la.elem + k))<<" "<<endl;
}
outfile.close();
system("pause");
}


程序在c++ 2008下编译通过但运行时出现错误如下:


Windows 已在 rerg.exe 中触发一个断点。

其原因可能是堆被损坏,这说明 rerg.exe 中或它所加载的任何 DLL 中有 Bug。

原因也可能是用户在 rerg.exe 具有焦点时按下了 F12。

输出窗口可能提供了更多诊断信息。

输出窗口中的信息:

“rerg.exe”: 已加载“C:\Users\freedom\Desktop\rerg\Debug\rerg.exe”,已加载符号。
“rerg.exe”: 已加载“C:\Windows\System32\ntdll.dll”
“rerg.exe”: 已加载“C:\Windows\System32\kernel32.dll”
“rerg.exe”: 已加载“C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcp90d.dll”,已加载符号。
“rerg.exe”: 已加载“C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcr90d.dll”,已加载符号。
“rerg.exe”: 已加载“C:\Windows\System32\shimeng.dll”
“rerg.exe”: 已加载“C:\Windows\System32\apphelp.dll”
“rerg.exe”: 已加载“C:\Windows\AppPatch\AcLayers.dll”
“rerg.exe”: 已加载“C:\Windows\System32\user32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\gdi32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\advapi32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\rpcrt4.dll”
“rerg.exe”: 已加载“C:\Windows\System32\shell32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\msvcrt.dll”
“rerg.exe”: 已加载“C:\Windows\System32\shlwapi.dll”
“rerg.exe”: 已加载“C:\Windows\System32\ole32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\oleaut32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\userenv.dll”
“rerg.exe”: 已加载“C:\Windows\System32\secur32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\winspool.drv”
“rerg.exe”: 已加载“C:\Windows\System32\mpr.dll”
“rerg.exe”: 已加载“C:\Windows\System32\imm32.dll”
“rerg.exe”: 已加载“C:\Windows\System32\msctf.dll”
“rerg.exe”: 已加载“C:\Windows\System32\lpk.dll”
“rerg.exe”: 已加载“C:\Windows\System32\usp10.dll”
“rerg.exe”: 已加载“C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.6001.18000_none_5cdbaa5a083979cc\comctl32.dll”
HEAP[rerg.exe]: Invalid address specified to RtlValidateHeap( 00870000, 0087523F )
Windows 已在 rerg.exe 中触发一个断点。

其原因可能是堆被损坏,这说明 rerg.exe 中或它所加载的任何 DLL 中有 Bug。

原因也可能是用户在 rerg.exe 具有焦点时按下了 F12。

输出窗口可能提供了更多诊断信息。
程序“[1432] rerg.exe: 本机”已退出,返回值为 0 (0x0)。


请大家帮忙解答一下。
...全文
229 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
开心爸爸84 2008-09-19
  • 打赏
  • 举报
回复
简单改了下,没调试完, 晚上继续。。。。

#include "stdafx.h"
#include "iostream"
#include "fstream"

using namespace std;

#define INITSIZE 100
#define LISTINCREMENT 100
#define MIDSTRLEN 30


//typedef char ElemType

struct sqList
{
int length;
char* elem;
int listsize;
};

void InitList(sqList* L)
{
L->elem = (char*)malloc(sizeof(char)*INITSIZE);
memset(L->elem, 0, INITSIZE);
L->length = 0;
L->listsize = INITSIZE;
}

void GetLen(sqList* L)
{
for (int i = 0; i < (L->listsize); i++)
{
if (*(L->elem + i) != '\0')
{
++(L->length);
}
}
}

char GetElem(sqList L, int i)
{
char e;
e = *(L.elem + i - 1);
return e;
}

void ListInsert(sqList* L, char e)
{
if (L->length >= L->listsize)
{
char* newbase;
newbase = (char*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(char));
if (newbase == NULL)
{
cout <<"Error when alloting memory!" <<endl;
exit(0);
}
L->elem = newbase;
L->listsize = L->listsize + LISTINCREMENT;
}
char* p;
p = L->elem + L->length;
*p = e;
++(L->length);
}

void main (int argc, char* argv[])
{
sqList la, lb;
char str1[MIDSTRLEN];
char str2[MIDSTRLEN];
InitList(&la);
InitList(&lb);
ifstream infile("input.txt");
if (!infile)
{
cout <<"input.txt can't be opened!" <<endl;
system("pause");
exit(0);
}
infile.getline(str1, sizeof(str1));
infile.getline(str2, sizeof(str2));

char* pElem =la.elem;
for (int i = 0; i <= sizeof(str1); i++)
{
if(str1[i] != ' ' && str1[i] != '\0')
{
// *(la.elem) = str1[i] - 48;
// ++(la.elem);
*pElem = str1[i] - 48;
pElem++;
}
}
char* pLb_elem = lb.elem;
for (int m = 0; m <= sizeof(str2); m++)
{
//if(str2[m] != '\32' || str2[m] != '\0')
if(str1[m] != ' ' && str1[m] != '\0')
{
//*(lb.elem) = str2[m];
//++(lb.elem);
*pLb_elem = str2[m] - 48;
pLb_elem++;
}
}
infile.close();
GetLen(&la);
GetLen(&lb);
for (int n = 1; n < lb.length; n++)
{
char e;
e = GetElem(lb, n);
for (int j = 0; j < la.length; j++)
{
if (*(la.elem + j) == e)
{
ListInsert(&la, e);
}
}
}
ofstream outfile("output.txt");
for (int k = 0; k < 2*(la.length); k++)
{
outfile <<(*(la.elem + k)) << " " << endl;
}
outfile.close();
system("pause");
}

freedom2028 2008-09-19
  • 打赏
  • 举报
回复
其中input.txt 的内容如下:
1 3 5 4 6 7 9
1 8 2 4 5

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧