Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace sbd_proj1
- {
- static class Program
- {
- public static int rowsinbuff=20;
- public static int charsinnum = 7 + 1;
- static string tasma1Fname;
- static string tasma2Fname;
- static string tasma3Fname;
- static public FileBufferReader rw1;
- static public FileBufferReader rw2;
- static public FileBufferReader rw3;
- static int iniFib;
- static Form1 form;
- [STAThread]
- static void Main()
- {
- tasma1Fname = @"tasma1.bin";
- tasma2Fname = @"tasma2.bin";
- tasma3Fname = @"tasma3.bin";
- rw1 = new FileBufferReader(tasma1Fname);
- rw2 = new FileBufferReader(tasma2Fname);
- rw3 = new FileBufferReader(tasma3Fname);
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- form = new Form1();
- Application.Run(form);
- iniFib = 0;
- }
- public static void GenButton()
- {
- RowGenerator gen = new RowGenerator();
- Random rand = new Random();
- rw1.clear();
- //gen.robSwoje(rand.Next(2, 50), rw1);
- //gen.robSwoje(rand.Next(2, 200), rw1);
- gen.robSwoje(70, rw1);
- }
- public static bool formTboxToFile(string x1, string x2,string x3,string x4, string x5)
- {
- List<double> rowrow = new List<double>();
- try
- {
- rowrow.Add(Convert.ToDouble(x1.Replace('.', ',')));
- rowrow.Add(Convert.ToDouble(x2.Replace('.', ',')));
- rowrow.Add(Convert.ToDouble(x3.Replace('.', ',')));
- rowrow.Add(Convert.ToDouble(x4.Replace('.', ',')));
- rowrow.Add(Convert.ToDouble(x5.Replace('.', ',')));
- }
- catch
- {
- rowrow.Clear();
- //done = false;
- return false;
- }
- SingleRow newRow = new SingleRow(rowrow);
- rw1.justWrite(newRow);
- rowrow.Clear();
- return true;
- }
- public static bool phase1()
- {
- //zwraca czy skonczone czy nie
- //rw1 to zawsze input
- //distro do 1 i 2
- //bierz plik rw1 i przepisz go seria po serii do plikow rw2 i rw3.
- int filenr = 2;
- SingleRow currRow = new SingleRow(false);
- double prewVal=0.0;
- double currVal=0.0;
- bool order = true;
- //rw1.startOver();
- if (form.genLog())
- showFile(rw1.fname);
- int f1 = 1;
- int f2 = 1;
- int fn = 1;
- int x = 0;
- //int toWriteOn2 = 0;
- //int toWriteOn3 = 1;
- while (true)
- {
- //z tasmy 1 na tasmy 3 i 2
- currRow = rw1.readRow();
- if (currRow.empty())
- {
- break;
- }
- prewVal = currVal;
- currVal = currRow.geoMean();
- if (currVal < prewVal)
- {
- order = false;
- if (filenr == 2)
- {
- if (rw2.runs == fn)
- {
- fn = f1 + f2;
- f1 = f2;
- f2 = fn;
- filenr = 3;
- }
- }
- else
- {
- if (rw3.runs == fn)
- {
- fn = f1 + f2;
- f1 = f2;
- f2 = fn;
- filenr = 2;
- }
- }
- }
- if (filenr == 2)
- {
- rw2.writeRow(currRow);
- }
- else
- {
- rw3.writeRow(currRow);
- }
- }
- if (filenr == 2)
- {
- rw2.dummyRuns = fn - rw2.runs;
- rw3.dummyRuns = 0;
- }
- else
- {
- rw2.dummyRuns = 0;
- rw3.dummyRuns = fn - rw3.runs;
- }
- iniFib = nextFib(rw2.runs) + nextFib(rw3.runs);
- //eh.
- //i nie musimy sie martwic sklejaniem
- //bo tasmy same licza ile maja serii
- //i luz
- return order;
- }
- public static bool SortButton()
- {
- int currFib = iniFib;
- int numOfPhases = 0;
- bool done = false;
- double prewRowK = 0.0;
- double prewRowD = 0.0;
- SingleRow rowK = new SingleRow(false);
- SingleRow rowD = new SingleRow(false);
- double currRowK=0.0;
- double currRowD=0.0;
- rw2.clear();
- rw3.clear();
- //bierz plik rw1 i przepisz go seria po serii do plikow rw2 i rw3.
- if (phase1())
- {
- if (form.genLog())
- {
- showFile(rw1.fname);
- form.publishToTextbox("\n");
- form.publishToTextbox("File initialy sorted");
- }
- }
- rw2.flush();
- rw3.flush();
- if (form.genLog())
- {
- showFile(rw2.fname);
- showFile(rw3.fname);
- }
- if (form.genLog())
- {
- //form.publishToTextbox("runs in file2 = "+rw2.runs.ToString()+"\n");
- //form.publishToTextbox("runs in file3 = " + rw3.runs.ToString()+"\n");
- form.publishToTextbox("dystrybucja zakonczona");
- form.publishToTextbox("\n\n");
- }
- //faza druga
- FileBufferReader pusty;
- FileBufferReader krotszy;
- FileBufferReader dluzszy;
- pusty = rw1;
- if (rw2.runCntTotal() > rw3.runCntTotal())
- {
- krotszy = rw3;
- dluzszy = rw2;
- }
- else
- {
- krotszy = rw2;
- dluzszy = rw3;
- }
- pusty.clear();
- krotszy.startOver();
- dluzszy.startOver();
- //krotszy na pewno ma fib
- //dluzszy ma fib-dummyRuns
- while (!done)
- {
- numOfPhases++;
- prewRowK = 0.0;
- prewRowD = 0.0;
- //DZIALA YEEEEE
- rowK = new SingleRow(false);
- rowD = new SingleRow(false);
- currRowK = 0.0;
- currRowD = 0.0;
- pusty.clear();
- dluzszy.startOver();
- //krotszy.startOver();
- rowD = dluzszy.readRow();
- rowK = krotszy.readRow();
- while (true)
- {
- //tyle ile jest serii w krotszym mergujemy do pustego
- //if (rowK != "" && justWritten == 2)
- // rowK = krotszy.readRow();
- //else if (rowD != "" && justWritten == 3)
- // rowD = dluzszy.readRow();
- if (rowK.empty() && rowD.empty())
- {
- break;
- //to nie powinno sie stac
- }
- else if (rowD.empty())
- {
- //dlozszy okazal sie krotszym?
- //przepisujemy krotszy do konca i tyle
- pusty.writeRow(rowK);
- rowK = krotszy.readRow();
- while (!rowK.empty())
- {
- pusty.writeRow(rowK);
- rowK = krotszy.readRow();
- }
- continue;
- }
- else if (rowK.empty())
- {
- //przepisujemy tyle serii z dluzeszego
- //ile mamy dummy runów
- currRowD =0.0;
- prewRowD =0.0;
- for (int i = 0; i < krotszy.dummyRuns; i++)
- {
- pusty.writeRow(rowD);
- rowD = dluzszy.readRow();
- prewRowD = currRowD;
- currRowD = rowD.geoMean();
- while (!rowD.empty() && currRowD > prewRowD)
- {
- pusty.writeRow(rowD);
- rowD = dluzszy.readRow();
- prewRowD = currRowD;
- currRowD = rowD.geoMean();
- }
- prewRowD = 0.0;
- prewRowK = 0.0;
- //koniec serii
- //justWritten = 0;
- }
- continue;
- }
- //co teraz? tyle ile serii jest w krotszym tyle serii merdżujemy
- //ale jak to osiagnac, zeby zamienic plik z odczytu na zapis
- //kij z tym, przeciez pamietamy gdzie czytamy
- //nic tam nie bedziemy zapisywac zanim nie opustoszeje
- //ile razy robimy merdż?
- //mamy krótszy.runs (hura)
- //oraz którszy.dummy runs
- currRowD = rowD.geoMean();
- currRowK = rowK.geoMean();
- if (currRowK < currRowD)
- {
- if (currRowK >= prewRowK)
- {
- //kontynuacja serii
- pusty.writeRow(rowK);
- //justWritten = 2;
- prewRowK = currRowK;
- rowK = krotszy.readRow();
- }
- else
- {
- //koniec serii krotszej
- //przepisujemy dluzsza
- //skrot myslowy. nie seria jest krotsza, a plik "krótszy" ma mniej serii niż "dłuższy"
- pusty.writeRow(rowD);
- //justWritten = 3;
- //zapisalismy jeden num co jeszcze byl w pamieci
- //ale czy byl koncem serii?
- rowD = dluzszy.readRow();
- prewRowD = currRowD;
- currRowD = rowD.geoMean();
- while (!rowD.empty() && currRowD > prewRowD)
- {
- pusty.writeRow(rowD);
- rowD = dluzszy.readRow();
- prewRowD = currRowD;
- currRowD = rowD.geoMean();
- }
- //justWritten = 0;
- prewRowD = 0.0;
- prewRowK = 0.0;
- }
- }
- else
- {
- if (currRowD >= prewRowD)
- {
- //kontynuacja serii
- pusty.writeRow(rowD);
- //justWritten = 3;
- prewRowD = currRowD;
- rowD = dluzszy.readRow();
- }
- else
- {
- //koniec serii krotszej
- //przepisujemy dluzsza
- //skrot myslowy. nie seria jest krotsza, a plik "krótszy" ma mniej serii niż "dłuższy"
- pusty.writeRow(rowK);
- //justWritten = 2;
- //zapisalismy jeden num co jeszcze byl w pamieci
- //ale czy byl koncem serii?
- rowK = krotszy.readRow();
- prewRowK = currRowK;
- currRowK = rowK.geoMean();
- while (rowK.empty() && currRowK > prewRowK)
- {
- pusty.writeRow(rowK);
- rowK = krotszy.readRow();
- prewRowK = currRowK;
- currRowK = rowK.geoMean();
- }
- //justWritten = 0;
- prewRowD = 0.0;
- prewRowK = 0.0;
- }
- }
- //tu jeszcze przepisywanie dummy
- currRowD = 0.0;
- prewRowD = 0.0;
- for (int i = 0; i < krotszy.dummyRuns; i++)
- {
- pusty.writeRow(rowD);
- rowD = dluzszy.readRow();
- prewRowD = currRowD;
- currRowD = rowD.geoMean();
- while (!rowD.empty() && currRowD > prewRowD)
- {
- pusty.writeRow(rowD);
- rowD = dluzszy.readRow();
- prewRowD = currRowD;
- currRowD = rowD.geoMean();
- }
- prewRowD = 0.0;
- prewRowK = 0.0;
- //koniec serii
- //justWritten = 0;
- }
- if (form.genLog())
- {
- showFile(krotszy.fname);
- showFile(dluzszy.fname);
- }
- //justWritten = 0;
- pusty.flush();
- if (form.genLog())
- {
- showFile(pusty.fname);
- form.publishToTextbox("\n");
- }
- if (pusty.inOrder)
- {
- break;
- }
- FileBufferReader pom = pusty;
- pusty = krotszy;
- krotszy = dluzszy;
- dluzszy = pom;
- pusty.clear();
- }
- //order = true;
- }
- //form.rwCountPublish(rw1.totalRead, rw1.totalWrite, 1);
- //form.rwCountPublish(rw2.totalRead, rw2.totalWrite, 2);
- //form.rwCountPublish(rw3.totalRead, rw3.totalWrite, 3);
- form.numPhasesPublish(numOfPhases);
- return true;
- }
- static void showFile(string fn)
- {
- int rowscnt = 1;
- string myout;
- double prev = 0.0;
- double curr = 0.0;
- form.publishToTextbox(fn+"\n");
- //using (BinaryWriter b = new BinaryWriter(File.Open("file.bin", FileMode.Create)))
- //{
- // // 3. Use foreach and write all 12 integers.
- // foreach (int i in a)
- // {
- // b.Write(i);
- // }
- //}
- using (BinaryReader binR = new BinaryReader(File.OpenRead(fn)))
- {
- List<double> row;
- while (binR.BaseStream.Position != binR.BaseStream.Length)
- {
- row = new List<double>();
- for (int i = 0; i < 5; i++)
- {
- row.Add(binR.ReadDouble());
- }
- SingleRow currRow = new SingleRow(row);
- curr = currRow.geoMean();
- myout = Math.Round(curr, 2).ToString();
- if (curr < prev)
- {
- myout = "|" + myout;
- rowscnt++;
- }
- myout += " ";
- form.publishToTextbox(myout);
- prev = curr;
- }
- }
- form.publishToTextbox("\nilość serii w pliku =" + rowscnt.ToString());
- form.publishToTextbox("\n");
- }
- static int ROZWALONE_NIE_DO_UZYCIAcountRuns(string fn)
- {
- int ret = 1;
- string line;
- double prev = 0.0;
- double curr = 0.0;
- using (System.IO.StreamReader file = new System.IO.StreamReader(fn))
- {
- while ((line = file.ReadLine()) != null)
- {
- //curr = geoMean(line);
- if (curr < prev)
- ret++;
- prev = curr;
- }
- }
- return ret;
- }
- static public int nextFib(int x)
- {
- int f0 = 1;
- int f1 = 1;
- //tak, wiem ze zaczyna sie od 0
- int fn = 0;
- if (x <= 0)
- return 0;
- else if (x == 1)
- return 1;
- else
- {
- while (fn < x)
- {
- fn = f0 + f1;
- f0 = f1;
- f1 = fn;
- }
- return fn;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement