ESP 调用约定问题 C++
对于家庭作业,我应该读取一个文件并对字符串进行排序。为此,我使用选择排序并且它有效。现在,在调用 selSort 函数后,它立即崩溃了。我已经没有办法解决这个问题了,有人可以帮我吗?
// 2_8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <wchar.h>
#include <fstream>
#include <iostream>
using namespace std;
class convert
{
private:
ifstream myFile;
TCHAR charArray[1000][25];
int size;
public:
convert()
{
myFile.open("rand.txt");
for(int x=0;x<1000;x++)
{
for(int y=0;y<25;y++)
{
charArray[x][y] = NULL;
}
}
if(!myFile)
{
cout << ("File not open") << endl;
}
else
{
cout << ("File opened") << endl;
}
}
void readFile()
{
int x = 0;
int y = 0;
int result =0;
if(myFile.is_open())
{
TCHAR tempChar;
while(!myFile.eof())
{
tempChar = myFile.get();
if(tempChar != 32)
{
charArray[x][y++] = tempChar;
}
else
{
size=x++;
y = 0;
}
}
}
result = selSort(charArray,size);
if(result)
{
cout << "We did it!!!!";
}
}
void printString()
{
for(int x=0;x<1000;x++)
{
for(int y=0;y<25;y++)
{
cout << charArray[x][y];
}
cout << endl;
}
}
int selSort(TCHAR thArray[][25], int length)
{
TCHAR tempArray[1][25];
for(int x=0;x<1;x++)
{
for(int y=0;y<25;y++)
{
tempArray[1][25] = NULL;
}
}
for(int x=0;x<length;x++)
{
int best = 0;
for(int y=1;y<length;y++)
{
int result = _tcscmp(thArray[y],thArray[best]);
if (result == 1)
{
best = y;
}
for (int t=0;t < _tcslen(thArray[best]);t++)
{
tempArray[0][t] = thArray[best][t];
}
for(int w=0;w < _tcslen(thArray[x]);w++)
{
thArray[best][w]=thArray[x][w];
}
for(int e=0;e < _tcslen(thArray[x]);e++)
{
thArray[x][e]=tempArray[0][e];
}
}
}
return 1;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
convert c1;
c1.readFile();
system("pause");
return 0;
}
For homework I'm supposed to read a file and the sort the strings. For this I'm using a selection sort and it works. Now, right after it makes the call to selSort function it crashes. I have ran out of ideas to solve this issue can anyone give me a hand?
// 2_8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <wchar.h>
#include <fstream>
#include <iostream>
using namespace std;
class convert
{
private:
ifstream myFile;
TCHAR charArray[1000][25];
int size;
public:
convert()
{
myFile.open("rand.txt");
for(int x=0;x<1000;x++)
{
for(int y=0;y<25;y++)
{
charArray[x][y] = NULL;
}
}
if(!myFile)
{
cout << ("File not open") << endl;
}
else
{
cout << ("File opened") << endl;
}
}
void readFile()
{
int x = 0;
int y = 0;
int result =0;
if(myFile.is_open())
{
TCHAR tempChar;
while(!myFile.eof())
{
tempChar = myFile.get();
if(tempChar != 32)
{
charArray[x][y++] = tempChar;
}
else
{
size=x++;
y = 0;
}
}
}
result = selSort(charArray,size);
if(result)
{
cout << "We did it!!!!";
}
}
void printString()
{
for(int x=0;x<1000;x++)
{
for(int y=0;y<25;y++)
{
cout << charArray[x][y];
}
cout << endl;
}
}
int selSort(TCHAR thArray[][25], int length)
{
TCHAR tempArray[1][25];
for(int x=0;x<1;x++)
{
for(int y=0;y<25;y++)
{
tempArray[1][25] = NULL;
}
}
for(int x=0;x<length;x++)
{
int best = 0;
for(int y=1;y<length;y++)
{
int result = _tcscmp(thArray[y],thArray[best]);
if (result == 1)
{
best = y;
}
for (int t=0;t < _tcslen(thArray[best]);t++)
{
tempArray[0][t] = thArray[best][t];
}
for(int w=0;w < _tcslen(thArray[x]);w++)
{
thArray[best][w]=thArray[x][w];
}
for(int e=0;e < _tcslen(thArray[x]);e++)
{
thArray[x][e]=tempArray[0][e];
}
}
}
return 1;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
convert c1;
c1.readFile();
system("pause");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tempArray 中没有第二行。如果数组有 n 行,那么它的索引从 0 到 n-1 开始。可能你的意思是 -
不要编写循环,而是使用 std::fill 函数驻留在算法头中用唯一元素填充数组元素。
There is no second row in the tempArray. If an array has n rows then it's index starts from 0 to n-1. Probably you meant -
Instead of writing a loop, use a std::fill function residing in the algorithm header to fill the array elements with a unique element.