帮我看看哪里的问题啊
我用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;
}
[解决办法]
第一个回帖,呵呵.
quicksort 算法没仔细看,去网上找找吧, 如果不是一定要用quicksort的话,可以直接使用sort()函数.
后面有两个错误:
(1): find 函数应该有第二个参数,string.find( ", ",index), index 指定从第几个位置开始查找.
你的用法永远返回第一次发现的位置.
(2): 当文件有多行的时候,每次读一行nIntegerNumber 数组都会被重新赋值,最后的排序结果只能是最后一行的排序结果.
[解决办法]
排序算法比较程序
作者:D.R.Punk 更新时间: 2005-05-13
功能要求如下:
排序算法比较: shellsort, quicksort, heapsort, mergesort 的算法实现 ,
对同样数据集的排序时间比较。
源代码:
# include <stdio.h>
# include <time.h>
# define MAXSIZE 2000
typedef struct{
int key[MAXSIZE];
int length;
}list;
long int compCount;
long int shiftCount;
void menu(int *m)/*retun m*/
{
int i;
char menu[6][15]={ "1 CREATE ", "2 IMPORT ", "3 SORT ", "4 SHOW RESULT ",
"5 SAVE RESULT ", "6 EXIT "};
clrscr();
printf( "SORT COMPARE SYSTEM\n ");
for (i=0;i <6;i++) printf( "%s\n ",menu[i]);
printf( "\n Please Select (1-6):\n ");
scanf( "%d ",m);
}
void menusort(int *m)/*retun m*/
{
int i;
char menusort[5][15]={ "1 SHELL SORT ", "2 QUICK SORT ", "3 HEAP SORT ",
"4 MERGE SORT ", "5 ALL SORT "};
clrscr();
printf( "SORT\n ");
for(i=0;i <5;i++) printf( "%s\n ",menusort[i]);
printf( "\n Please Select (1-5):\n ");
scanf( "%d ",m);
}
void menushow(int *m)/*retun m*/
{
int i;
char menushow[4][20]={ "1 SHELL SORT RESULT ", "2 QUICK SORT RESULT ",
"3 HEAP SORT RESULT ", "4 MERGE SORT RESULT "};
clrscr();
printf( "SHOW SORT RESULT\n ");
for(i=0;i <4;i++) printf( "%s\n ",menushow[i]);
printf( "\n Please Select (1-4):\n ");
scanf( "%d ",m);
}
void menusave(int *m)
{
int i;
char menusave[4][20]={ "1 SHELL SORT RESULT ", "2 QUICK SORT RESULT ",
"3 HEAP SORT RESULT ", "4 MERGE SORT RESULT "};
clrscr();
printf( "SAVE:\n ");
for (i=0;i <4;i++) printf( "%s\n ",menusave[i]);
printf( "\n Please Select (1-4):\n ");
scanf( "%d ",m);
}
void create(list *L)
{
int i;
printf( "HOW MANY DATA?\n ");
scanf( "%d ",&((*L).length));
for(i=1;i <=(*L).length;i++)
{
printf( "\nPLEASE INPUT THE %dth DATA:\n ",i);
scanf( "%d ",&(*L).key[i]);
}
printf( "\nCREATE COMPLETE !\n ");
}
int listopen(list *L,char *filename)
{
int k=1;
FILE *data;
data=NULL;
data=fopen(filename, "rb ");
while (! feof(data))
{
fscanf(data, "%d ",&(*L).key[k]);
k++;
}
(*L).length=k-1;
}
void import(list *L)/*fix L*/
{
char filename[255];
int i;
printf( "\nPLEASE INPUT THE FILE PATH AND NAME:\n ");
scanf( "%s ",filename);
clrscr();
listopen(L,filename);
for(i=1;i <(*L).length;i++) printf( "%d ",(*L).key[i]);
printf( "\nPRESS ANYKEY RETURN TO MAINMENU...\n ");
getch();
}
void save(list L)
{
FILE *data;
char filename[255];
int r;
printf( "\nPLEASE INPUT THE FILE PATH AND NAME:\n ");
scanf( "%s ",filename);
data=fopen(filename, "wb ");
for(r=1;r <=L.length;r++) fprintf(data, "%d\n ",L.key[r]);
fclose(data);
printf( "SAVE OK! \n PRESS ANY KEY TO RETURN THE MAINMENU... ");
getch();
}
list shellsort(list L)/*retun L_SHELL*/
{
int i,j,gap,x,n;
compCount=shiftCount=0;
n=L.length;
gap=n/2;
while (gap> 0)
{
compCount++;
for(i=gap+1;i <=n;i++)
{
compCount++;
j=i-gap;
while(j> 0)
{
compCount++;
if(L.key[j]> L.key[j+gap])
{
compCount++;
x=L.key[j];shiftCount++;
L.key[j]=L.key[j+gap];shiftCount++;
L.key[j+gap]=x;shiftCount++;
j=j-gap;
}
else j=0;
}
}
gap=gap/2;
}
return L;
}
void shell(list L,list *LS,float *timeshell)/*return LS,timeshell.
MUST add an "getch "!!*/
{
clock_t start,end;
start=clock();
(*LS)=shellsort(L);
end=clock();
*timeshell=(end-start)/CLK_TCK;
printf( "\nSHELLSORT COST TIME :%f SECONDS. ",*timeshell);
printf( "Compare %d times.Shfit %d times.\n ",compCount,shiftCount);
}
int Partition(list * pL,int low,int high)
{
int pivotkey;
pL-> key[0]=pL-> key[low];shiftCount++;
pivotkey=pL-> key[low];shiftCount++;
while(low <high)
{
compCount++;
while(low <high && pivotkey <=(pL-> key[high]))
{compCount++;compCount++; --high;}
pL-> key[low]=pL-> key[high];shiftCount++;
while(low <high && (pL-> key[low]) <=pivotkey)
{compCount++;compCount++; ++low;}
pL-> key[high]=pL-> key[low];shiftCount++;
}
pL-> key[low]=pL-> key[0];shiftCount++;
return low;
}/*Partition*/
void QSort(list * pL,int low,int high)
{
int pivotloc;
if(low <high)
{
compCount++;
pivotloc=Partition(pL,low,high);
QSort(pL,low,pivotloc-1);
QSort(pL,pivotloc+1,high);
}
}/*QSort*/