伊莉討論區
標題:
求救~~讀檔、排序、寫檔輸出
[打印本頁]
作者:
都如
時間:
2014-1-14 08:21 PM
標題:
求救~~讀檔、排序、寫檔輸出
題目要求要讀Strawberries.txt檔(放在C槽根目錄)
做heap sort從大排到小
再輸出到Sort.txt
下面是我寫的,但是結果沒辦法寫進Sort.txt裡,請幫我看看
謝謝~
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#define MAX 16777216 // 設定數量上限
using namespace std;
class heaptree {
private:
int last_index; // 最後一筆資料的INDEX
public:
heaptree();
int *heap_tree; // 堆積樹陣列
void create(int id_temp); // 建立資料於堆積樹
void show(); // 印出資料於螢幕
void adjust_u(int temp[], int index); // 從下而上調整資料
void adjust_ud(int temp[], int index); // 從下而上調整資料
void adjust_d(int temp[], int index1, int index2); // 從上而下調整資料
void exchange(int *id1, int *id2); // 交換資料
};
heaptree::heaptree()
{
last_index = 0;
heap_tree=new int[MAX];
}
void heaptree::create(int id_temp) // ID_TEMP為新增資料
{
heap_tree[++last_index] = id_temp; // 將資料新增於最後
////adjust_u(heap_tree, last_index); // 調整新增資料
adjust_ud(heap_tree, last_index); // 調整新增資料
}
void heaptree::show()
{
int *heap_temp=new int[MAX+1];
int c_index;
// 將堆積樹資料複製到另一個陣列作排序工作
for(c_index = 1; c_index <= last_index; c_index++)
heap_temp[c_index] = heap_tree[c_index];
// 將陣列調整為由小到大排列
for(c_index = last_index-1; c_index > 0; c_index--) {
exchange(&heap_temp[1], &heap_temp[c_index+1]);
adjust_d(heap_temp, 1, c_index);
}
for(c_index = last_index; c_index > 0; c_index--) {
cout.width(14);
cout << heap_temp[c_index] << "\n";
}
delete heap_tree,heap_temp;
}
void heaptree::adjust_u(int temp[], int index) // INDEX為目前資料在陣列之INDEX
{
while(index > 1) { // 將資料往上調整至根為止
if(temp[index] <= temp[index/2]) // 資料調整完畢就跳出,否則交換資料
break;
else
exchange(&temp[index], &temp[index/2]);
index /= 2;
}
}
void heaptree::adjust_ud(int temp[], int index) // INDEX為目前資料在陣列之INDEX
{
while(index > 1) { // 將資料往上調整至根為止
if(temp[index] > temp[index/2]) // 資料調整完畢就跳出,否則交換資料
break;
else
exchange(&temp[index], &temp[index/2]);
index /= 2;
}
}
// INDEX1為目前資料在陣列之INDEX,INDEX2為最後一筆資料在陣列之INDEX
void heaptree::adjust_d(int temp[], int index1, int index2)
{
// ID_TEMP記錄目前資料,INDEX_TEMP則是目前資料之CHILDEN NODE的INDEX
int id_temp, index_temp;
id_temp = temp[index1];
index_temp = index1 * 2;
// 當比較資料之INDEX不大於最後一筆資料之INDEX,則繼續比較
while(index_temp <= index2) {
if((index_temp < index2) && (temp[index_temp] < temp[index_temp+1]))
index_temp++; // INDEX_TEMP記錄目前資料之CHILDEN NODE中較大者
if(id_temp >= temp[index_temp]) // 比較完畢則跳出,否則交換資料
break;
else {
temp[index_temp/2] = temp[index_temp];
index_temp *= 2;
}
}
temp[index_temp/2] = id_temp;
}
void heaptree::exchange(int *id1, int *id2) // 交換傳來之ID1及ID2儲存之資料
{
int id_temp;
id_temp = *id1;
*id1 = *id2;
*id2 = id_temp;
}
int main()
{
heaptree obj;
ifstream in("C:\\Strawberries.txt");
char num[6];
if(!in){//如果開啟檔案失敗,file為0;成功,為非0
cout<<"Fail to open file!"<<endl;
}
in.getline(num,32769,' ');
in.close();
obj.show();
cout<<"OK";
cout << "\n";
ofstream out("C:\\Sort.txt");
out << "heap_temp[c_index] "; //用法與cout相同
if(!out){//如果開啟檔案失敗,file為0;成功,為非0
cout<<"Fail to write file!"<<endl;
out.close();
system("pause");
return 0;
}
}
作者:
rex1031
時間:
2014-1-20 01:07 AM
你說的無法寫入是指寫入的內容錯誤嗎?
因為只有大略看了一下,如果你是指最後的結果不是想要的結果的話( sort.txt內的資料 )
應該是因為你的寫入的部份只有寫入一個字串 "heap_temp[c_index]" 而已
並沒有真正的將所有資料寫入,這個部份可能要先檢查一下
然後 function 內的邏輯我沒有仔細檢查
不過你似乎沒有做把資料丟入及 sort 的動作
function 感覺分工還蠻細的,要注意一下彼此間呼叫及參數傳遞的部份 ( 可以多用 cout 確認資料讀入及傳遞無誤 )
然後最後 main 的部份,等 sort function 都有確實呼叫到後
先試著把 sort 後的結果用 cout 印到 console 視窗
確認 function 的功能及 sort 的部份沒問題
再把印出的部份改成 ofstream 就可以了
目前看起來要先解決的問題應該不是檔案寫入哦
參考一下吧~~
歡迎光臨 伊莉討論區 (http://www72.eyny.com/)
Powered by Discuz!