ESP 调用约定问题 C++

发布于 2024-12-05 04:38:35 字数 2873 浏览 6 评论 0原文

对于家庭作业,我应该读取一个文件并对字符串进行排序。为此,我使用选择排序并且它有效。现在,在调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-12-12 04:38:35
int selSort(TCHAR thArray[][25], int length)
{
    TCHAR tempArray[1][25];

    // ...

    tempArray[1][25] = NULL;   // In the for loop
 }

tempArray 中没有第二行。如果数组有 n 行,那么它的索引从 0 到 n-1 开始。可能你的意思是 -

tempArray[x][y] = NULL;

不要编写循环,而是使用 std::fill 函数驻留在算法头中用唯一元素填充数组元素。

int selSort(TCHAR thArray[][25], int length)
{
    TCHAR tempArray[1][25];

    // ...

    tempArray[1][25] = NULL;   // In the for loop
 }

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 -

tempArray[x][y] = NULL;

Instead of writing a loop, use a std::fill function residing in the algorithm header to fill the array elements with a unique element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文