รบกวนพี่ๆช่วยแก้โค๊ดให้ทีนะครับ C++ ขอบคุณล่วงหน้าครับ
อันนี้เป็นโปรแกรมเรียงข้อมูล จากน้อยไปมาก จากเลข0 ไปจนถึง zเลย แต่มันเป็นการเรียงข้อมูลแบบ Bubble sort แระรันไฟล์ text ใหญ่ๆไม่ได้ ผมอยากจะแปลงเป็น quick sort และรันไฟล์text ใหญ่ๆขนาดหลายๆ mb ได้ รบกวนพี่ๆ ช่วยแก้โค๊ดให้ทีนะครับ
-----------------------------------------------------------------------------------------------------------------------------------------------------
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define MAXWORD 60000
class ArrayList{
private:
char *word[MAXWORD];
int wordCount;
public:
ArrayList(void);
~ArrayList(void);
bool setWord(char *word,int location);
bool getWord(char *word,int location);
bool clearWord(int location);
void setWordCount(int count);
int wordLength(int location);
int getWordCount();
void setWordPtr(char *word,int location);
char *getWordPtr(int location);
};
ArrayList::ArrayList(void){
for(int i=0;i<MAXWORD;i++)
word[i]=NULL;
wordCount = 0;
}
ArrayList::~ArrayList(){
for(int i=0;i<MAXWORD;i++)
if(word[i])
delete word[i];
}
bool ArrayList::setWord(char *word,int location){
if(location>=MAXWORD) return false;
if(this->word[location]!=NULL)
delete this->word[location];
this->word[location]= new char[strlen(word)+1];
strcpy(this->word[location],word);
return true;
}
bool ArrayList::getWord(char *word,int location){
if(location>=MAXWORD) return false;
if(this->word[location]==NULL) return false;
strcpy(word,this->word[location]);
return true;
}
bool ArrayList::clearWord(int location){
if(location>=MAXWORD) return false;
if(word[location]==NULL) return false;
delete word[location];
word[location]=NULL;
return true;
}
int ArrayList::wordLength(int location){
return (int)strlen(word[location]);
}
void ArrayList::setWordCount(int count){
wordCount = count;
}
int ArrayList::getWordCount(void){
return wordCount;
}
void ArrayList::setWordPtr(char *word,int location){
this->word[location]=word;
}
char *ArrayList::getWordPtr(int location){
return this->word[location];
}
class Sort{
public:
int toupper(int ch);
int compare(char *s1,char*s2);
int swap(ArrayList *list,int location1,int location2);
int bubbleSort(ArrayList *list);
};
int Sort::toupper(int ch){
if((ch>='a')&&(ch<='z')) return ch-0x20;
return ch;
}
int Sort::compare(char *s1,char *s2){
int count=0;int ch1,ch2;
while(1){
if((s1[count]==0)||(s2[count]==0)) break;
ch1 = this->toupper(s1[count]);
ch2 = this->toupper(s2[count]);
if(ch1==ch2){ count++; continue; }
if(ch1<ch2) return -1;
if(ch1>ch2) return 1;
count++;
}
if((s1[count]==0)&&(s2[count]==0)) return 0;
if(s1[count]!=0) return 1;
return -1;
}
int Sort::swap(ArrayList *list,int location1,int location2){
if(location1>=MAXWORD)return 0;
if(location2>=MAXWORD)return 0;
char *tmp1 = list->getWordPtr(location1);
char *tmp2 = list->getWordPtr(location2);
list->setWordPtr(tmp2,location1);
list->setWordPtr(tmp1,location2);
return 1;
}
int Sort::bubbleSort(ArrayList *list){
int n=list->getWordCount();
if((n==0)||(n>=MAXWORD)) return 0;
int i,j;
for(i=0;i<(n-1);i++){
for(j=n-1;j>i;j--){
if(this->compare(list->getWordPtr(j),list->getWordPtr(j-1))==-1)
this->swap(list,j,j-1);
}
}
return 1;
}
class CFile{
public:
bool readFile(char *filename,ArrayList *list);
bool writeFile(char *filename,ArrayList *list);
};
bool CFile::readFile(char *filename,ArrayList *list){
FILE *f;
char temp[2048];
int ch,tempcount;
int wordcount=0;
if((f=fopen(filename,"rb"))==NULL) return false;
while(!feof(f)){
tempcount=0;
while(1){
ch=fgetc(f);
if((ch<'0')||((ch>'9')&&(ch<'A'))||((ch>'Z')&&(ch<'a'))||(ch>'z')) break;
temp[tempcount++]= ch;
if(tempcount>2046)break;
}
temp[tempcount]=0;
if((tempcount==1)&&
((temp[0]<'0')||((temp[0]>'9')&&(temp[0]<'A'))||
((temp[0]>'Z')&&(temp[0]<'a'))||(temp[0]>'z')))
continue;
if(tempcount==0) continue;
list->setWord(temp,wordcount++);
}
list->setWordCount(wordcount);
fclose(f);
return true;
}
bool CFile::writeFile(char *filename,ArrayList *list){
FILE *f;
if((f=fopen(filename,"wb"))==NULL) return false;
int count = list->getWordCount();
for(int i=0;i<count;i++)
fprintf(f,"%s\x0d\x0a",list->getWordPtr(i));
fclose(f);
return true;
}
void main(int argc,char *argv[]){
try{
CFile *myFile = new CFile();
ArrayList *myList = new ArrayList();
Sort *mySort = new Sort();
if(!myFile->readFile(argv[1],myList)){
cout << "No file " << argv[1] << endl;
return;
}
mySort->bubbleSort(myList);
if(!myFile->writeFile(argv[2],myList)){
cout << "Can not write to file " << argv[2] << endl;
}
cout << argv[2] << "<<<<sort<<<<" << argv[1] << " wordcount: " << myList->getWordCount() << endl;
}catch(char *s){
cout << "Error Occurred!" << endl;
}
}Tag : - - - -
Date :
2011-02-09 22:47:02
By :
sepombank
View :
965
Reply :
0
Load balance : Server 03