關於讀取*.txt內x.y座標分類問題 |
|
rabbit78585
一般會員 發表:11 回覆:22 積分:6 註冊:2006-08-22 發送簡訊給我 |
請問各位大大~
我有把座標存在1.txt檔裡如: X:218 Y: 17 X:422 Y: 17 X:210 Y: 18 X:423 Y: 18 X:211 Y: 19 X:423 Y: 19 X:213 Y: 20 X:424 Y: 20 X:217 Y: 21 X:425 Y: 21 X:426 Y: 21 X:217 Y: 22 X:427 Y: 22 X:428 Y: 22 X:429 Y: 22 X:219 Y: 23 X:430 Y: 23 X:431 Y: 23 X:220 Y: 24 X:431 Y: 24 X:432 Y: 24 X:433 Y: 24 X:434 Y: 24 如怎麼抓出紅色字座標(看Y最前而選取=>17.18.19.20.....)分為一類存2.txt 而藍色字座標看(Y最後而選取)分為第二類存3.txt 黑色座標字則捨去不要 //---------------------------------------------------------------------------------------------------------- 在由第2.txt 檔中把座標算向量求角度 1.X:210 Y: 18 3.X:213 Y: 20 5. |
暗黑破壞神
版主 發表:9 回覆:2301 積分:1627 註冊:2004-10-04 發送簡訊給我 |
|
legolego0003
一般會員 發表:0 回覆:1 積分:0 註冊:2007-01-24 發送簡訊給我 |
你試試這個程式~因該可以達到你想要的
#include #pragma hdrstop #include "Unit1.h" #include "vector.h" #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { AnsiString msg; if(OpenDialog1->Execute()); { if (FileExists(OpenDialog1->FileName)) { Memo1->Lines->LoadFromFile(OpenDialog1->FileName); Form1->Caption = ExtractFileName(OpenDialog1->FileName); } else { msg = ""; msg = OpenDialog1->FileName; msg = "\n沒有找到檔案\n請確認已給了正確的檔案\n"; MessageBox(GetActiveWindow(),msg.c_str(),"開啟文件",MB_OK|MB_ICONWARNING); } } } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn1Click(TObject *Sender) { Memo2->Clear(); //先清除內容 Memo3->Clear(); Memo4->Clear(); int i , j, k = 0, P, Q, y1, y2, y3; String a; char *q = "XY: ";//分離數值的字元,若有多個字元可以寫成這樣 char *tok; vector< vector< String > > A; //以vector動態配置矩陣 P = StrToInt(Memo1->Lines->Count); Q = 2; A.resize(P); for(i=0;i { for(j=0;j { A[i].resize(Q); } } for (i = 0; i < Memo1->Lines->Count; i ) { a = Memo1->Lines->Strings[i]; tok = strtok(a.c_str(), q);//第一次分離出值 while (tok != NULL) //當strtok第一次拆出值後,tok不為空字元再開始動作 { //直到拆到Edit1的字串沒了,tok變為空字元,即結束動作 A[i][k] = tok; k ; tok = strtok(NULL,q);//繼續分離之後的字語 } k = 0; if (i >= 1) //避免 A[-1][1]發生 { y1 = StrToInt(A[i-1][1]); y2 = StrToInt(A[i][1]); if (y1 == y2) //現在這一筆資料與上一筆資料比對一樣 { Memo4->Lines->Add(Memo1->Lines->Strings[i-1]); //輸出至Memo4 } else if (y1 < y2) //不一樣表示 Y 值已抓完同一區間 { if ( i >= 2) //避免 A[-1][1]發生 { y3 = StrToInt(A[i-2][1]); if (y1 == y3) //再檢查上兩筆資料是否為一樣, 一樣就還是存上一筆資料 { //若不一樣則表示說這個區間為單一值, 就不抓取 Memo4->Lines->Add(Memo1->Lines->Strings[i-1]); if (Memo4->Lines->Text != "") //區間值都抓完, 把頭尾值取出 { Memo2->Lines->Add(Memo4->Lines->Strings[0]); Memo3->Lines->Add(Memo4->Lines->Strings[Memo4->Lines->Count - 1]); Memo4->Clear(); //清空 } } } } } if (i == Memo1->Lines->Count - 1) //最後一筆資料沒有再下一筆資料能比對 { //因此做特例判斷 if (y1 == y2) { Memo4->Lines->Add(Memo1->Lines->Strings[i]); Memo2->Lines->Add(Memo4->Lines->Strings[0]); Memo3->Lines->Add(Memo4->Lines->Strings[Memo4->Lines->Count - 1]); Memo4->Clear(); } } } BitBtn3->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn2Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn3Click(TObject *Sender) { ofstream fileout1, fileout2; String PutOut; PutOut = Memo2->Lines->Text; fileout1.open("2.txt",ios::out); fileout1< fileout2.open("3.txt",ios::out); fileout2< } //--------------------------------------------------------------------------- |
rabbit78585
一般會員 發表:11 回覆:22 積分:6 註冊:2006-08-22 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |