帮我看看这个排序问题吧
我用G++编译能通过但是运行程序出现错误,好象是数组益处,我主要是想实现从TXT文件中读取整数并从小到大排序.文件格式如下(其中整数个数和数本身都是随机的,每个整数用 ", "分隔):
1,9,2,-10,0,-4
输出要求是:
-10,-4,0,1,2,9
#include <iostream>
#incldue <string>
#include <fstream>
using namespace std;
const int INTEGER_NUMBER =10;
const int STRING_LENGHT =127;
const int SINGLE_NUMBER_LENGHT=10;
void QuickSort(int R[],int low,int high);
int Partition(int R[],int low ,int high);
int Partition(int R[],int i,int j)
{
int pivot = R[i];
while(i <j)
{
while(i < j&&R[j]> =pivot)
{
j--;
}
if(i < j)
{
R[i++]=R[j];
}
while(i < j && R[i] <=pivot)
{
i++;
}
if(i < j)
{
R[j--] = R[i];
}
}
R[i] = pivot;
return i;
}
void QuickSort(int R[],int low,int high)
{
int pivotpos;
int nCount = 0;
nCount++;
if(nCount == 1 || nCount == 3 || nCount ==4)
{
for(int i =0;i <INTEGER_NUMBER;i++)
{
cout < < R[i] < < " ";
cout < < endl;
}
}
if(low < high)
{
pivotpos = Partition(R,low,high);
QuickSort(R,low,pivotpos- 1);
QuickSort(R,pivotpos+1,high);
}
}
int main(void)
{
ifstream infile;
int nTempValue = 0;
int nStringLenght = 0;
int nIntegerNumber[INTEGER_NUMBER];
char cString[STRING_LENGHT];
char cTempChar[SINGLE_NUMBER_LENGHT];
string strMyString = " ";
string strTempString = " ";
infile.open( "myfile01.txt ",ios::in);
if(!infile)
{
return 0;
}
while(!infile.eof())
{
infile.getline*(cString,127);
strMyString = cString;
nStringLenght = strMyString.length();
for(int i = 0;i <nStringLenght;i++)
{
//这部分有问题似乎只取出了一个整数
int nCommaPos = strMyString.find ( ", ");
memset(cTempChar, '\0 ',10);
strTempString = strncpy(cTempChar,strMyString.c_str(),nCommaPos);
nTempValue = atoi(cTempChar);
nIntegerNumber[i] = nTempValue;
}
}
infile.close();
QuickSort(nIntegerNumber,0,INTEGER_NUMBER-1);
return 0;
}
[解决办法]
//这部分有问题似乎只取出了一个整数
int nCommaPos = strMyString.find ( ", ");
memset(cTempChar, '\0 ',10);
strTempString = strncpy(cTempChar,strMyString.c_str(),nCommaPos);
nTempValue = atoi(cTempChar);
nIntegerNumber[i] = nTempValue;
这个地方,nCommaPos没动过,strncpy只拷贝第一个数字
[解决办法]
for(int i = 0;i <nStringLenght;i++)
{
//这部分有问题似乎只取出了一个整数
int nCommaPos = strMyString.find ( ", ");
memset(cTempChar, '\0 ',10);
strTempString = strncpy(cTempChar,strMyString.c_str(),nCommaPos);
nTempValue = atoi(cTempChar);
nIntegerNumber[i] = nTempValue;
}
还有这段for语句,循环变量和nIntegerNumber的下标不是指同一个东西.