system("pause");太奇怪了,加上他就出错,去掉就好了;小弟真心迷茫了,求大神指导。
小弟用char数组试着写个二叉搜索树,想法挺简单,就是插入char数字,空白处用‘-’填充;只是被system("pause")搞蒙了。main.cpp第28行加上system("pause")就出错,注释掉就不出错。求大神前辈给点指导呀!!!
(小弟使用codeblock下的gcc编译)
fun.h
#ifndef _FUN_H_
#define _FUN_H_
char* Enlarge(char oldArry[], int &size);
void Traverse(char oldArray[], const int size);
#endif
fun.cpp
#include <cstring>
#include "fun.h"
using namespace std;
/*
Double the size of old array and fill the empty space with '-',
then delete the old array and return the pointer to the new array.
*/
char* Enlarge(char oldArray[], int &size) {
char* newArray = new char[ 2 * size];
size = 2 * size;
for (int i = 0; i < size; ++i) {
newArray[i] = oldArray[i];
}
for (int i = size; i < 2 * size; ++i) {
newArray[i] = '-';
}
delete []oldArray;
return newArray;
}
/*
Traverse the charactor array and output the element(except '-') to the screen.
*/
void Traverse(char oldArray[], const int size) {
for (int i = 0; i < size; ++i) {
if (oldArray[i] == '-') {
continue;
}
cout << oldArray[i] << " ";
}
cout << endl;
return;
}
BST.h
#ifndef _BST_H_
#define _BST_H_
int Search (char oldArray[], const int size, const char value);
char* Insert (char oldArray[], int &size, const char value);
void Delete (char oldArray[], int &size);
#endif
BST.cpp
#include "BST.h"
#include "fun.h"
using namespace std;
int Search(char oldArray[], const int size, const char value) {
int i = 0;
while (i < size) {
if (oldArray[i] == '-') {
break;
}
if (value == oldArray[i]) {
return i;
} else if (value < oldArray[i]) {
i = i * 2 + 1;
continue;
} else if (value > oldArray[i]) {
i = i * 2 + 2;
continue;
}
}
return -1;
}
/*
Insert a new element, if there is no enough space, enlarge the size of oldarray.
*/
char* Insert(char oldArray[], int &size, const char value) {
if (Search(oldArray, size, value) != -1) {
return oldArray;
}
int i = 0;
while (true) {
if (oldArray[i] == '-') {
oldArray[i] = value;
return oldArray;
} else if (value < oldArray[i]) {
i = i * 2 + 1;
if (i >= size) {
char* newArray = Enlarge(oldArray, size);
newArray[i] = value;
return newArray;
}
continue;
} else {
i = i * 2 + 2;
if (i >= size) {
char* newArray = Enlarge(oldArray, size);
newArray[i] = value;
return newArray;
}
continue;
}
}
}
void Delete (char oldArray[], int &size) {
delete []oldArray;
size = 0;
return;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "BST.h"
#include "fun.h"
using namespace std;
int main()
{
char* bst = new char[1];
bst[0] = '-';
int size = strlen(bst);
int index = -1;
bst = Insert(bst, size, '4');
bst = Insert(bst, size, '3');
bst = Insert(bst, size, '7');
bst = Insert(bst, size, '2');
bst = Insert(bst, size, '5');
bst = Insert(bst, size, '8');
Traverse(bst, size);
index = Search(bst, size, '4');
if (index != -1) {
cout << "haha, I find it : " << bst[index] << endl;
} else {
cout << "this one isn't in the BST! " << endl;
}
//system("pause");
Delete(bst, size);
return 0;
}
[解决办法]
感觉问题出在这里:
char* bst = new char[1];
bst[0] = '-';
bst的size应该至少为2才对,因为字符串需要一个结束符'\0',否则你用strlen计算的长度是随机的,至少可以这样改:
char* bst = new char[2];
bst[0] = '-';
bst[1] = '\0';
[解决办法]
代码没瞄一眼,先请问加上pause后是编译出错还是运行出错?
[解决办法]
linux支持system("pause")的命令?