Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > กระทู้สำหรับภาษาต่าง ๆ > รบกวนพี่ๆช่วยแก้โค๊ดให้ทีนะครับ C++ ขอบคุณล่วงหน้าครับ



รบกวนพี่ๆช่วยแก้โค๊ดให้ทีนะครับ C++ ขอบคุณล่วงหน้าครับ

 
Topic : 055796



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์



อันนี้เป็นโปรแกรมเรียงข้อมูล จากน้อยไปมาก จากเลข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 : - - - -


Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-02-09 22:47:02 By : sepombank View : 915 Reply : 0
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : รบกวนพี่ๆช่วยแก้โค๊ดให้ทีนะครับ C++ ขอบคุณล่วงหน้าครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่