Advertisement
desdemona

program.cs backup

Nov 7th, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. using System.IO;
  7.  
  8. namespace sbd_proj1
  9. {
  10.     static class Program
  11.     {
  12.         public static int rowsinbuff=20;
  13.         public static int charsinnum = 7 + 1;
  14.         static string tasma1Fname;
  15.         static string tasma2Fname;
  16.         static string tasma3Fname;
  17.  
  18.         static public FileBufferReader rw1;
  19.         static public FileBufferReader rw2;
  20.         static public FileBufferReader rw3;
  21.  
  22.         static int iniFib;
  23.        
  24.         static Form1 form;
  25.         [STAThread]
  26.         static void Main()
  27.         {
  28.             tasma1Fname = @"tasma1.bin";
  29.             tasma2Fname = @"tasma2.bin";
  30.             tasma3Fname = @"tasma3.bin";
  31.  
  32.             rw1 = new FileBufferReader(tasma1Fname);
  33.             rw2 = new FileBufferReader(tasma2Fname);
  34.             rw3 = new FileBufferReader(tasma3Fname);
  35.  
  36.             Application.EnableVisualStyles();
  37.             Application.SetCompatibleTextRenderingDefault(false);
  38.             form = new Form1();
  39.             Application.Run(form);
  40.  
  41.             iniFib = 0;
  42.         }
  43.  
  44.         public static void GenButton()
  45.         {
  46.             RowGenerator gen = new RowGenerator();
  47.             Random rand = new Random();
  48.             rw1.clear();
  49.             //gen.robSwoje(rand.Next(2, 50), rw1);
  50.             //gen.robSwoje(rand.Next(2, 200), rw1);
  51.             gen.robSwoje(70, rw1);
  52.         }
  53.  
  54.         public static bool formTboxToFile(string x1, string x2,string x3,string x4, string x5)
  55.         {
  56.             List<double> rowrow = new List<double>();
  57.             try
  58.             {
  59.                 rowrow.Add(Convert.ToDouble(x1.Replace('.', ',')));
  60.                 rowrow.Add(Convert.ToDouble(x2.Replace('.', ',')));
  61.                 rowrow.Add(Convert.ToDouble(x3.Replace('.', ',')));
  62.                 rowrow.Add(Convert.ToDouble(x4.Replace('.', ',')));
  63.                 rowrow.Add(Convert.ToDouble(x5.Replace('.', ',')));
  64.             }
  65.             catch
  66.             {
  67.                 rowrow.Clear();
  68.                 //done = false;
  69.                 return false;
  70.             }
  71.             SingleRow newRow = new SingleRow(rowrow);
  72.             rw1.justWrite(newRow);
  73.             rowrow.Clear();
  74.  
  75.             return true;
  76.         }
  77.         public static bool phase1()
  78.         {
  79.             //zwraca czy skonczone czy nie
  80.  
  81.             //rw1 to zawsze input
  82.             //distro do 1 i 2
  83.  
  84.             //bierz plik rw1 i przepisz go seria po serii do plikow rw2 i rw3.
  85.             int filenr = 2;
  86.             SingleRow currRow = new SingleRow(false);
  87.             double prewVal=0.0;
  88.             double currVal=0.0;
  89.             bool order = true;
  90.  
  91.             //rw1.startOver();
  92.  
  93.             if (form.genLog())
  94.                 showFile(rw1.fname);
  95.  
  96.  
  97.             int f1 = 1;
  98.             int f2 = 1;
  99.  
  100.             int fn = 1;
  101.             int x = 0;
  102.  
  103.             //int toWriteOn2 = 0;
  104.             //int toWriteOn3 = 1;
  105.  
  106.  
  107.             while (true)
  108.             {
  109.                 //z tasmy 1 na tasmy 3 i 2
  110.                 currRow = rw1.readRow();
  111.                 if (currRow.empty())
  112.                 {
  113.                     break;
  114.                 }
  115.                 prewVal = currVal;
  116.                 currVal = currRow.geoMean();
  117.  
  118.  
  119.                 if (currVal < prewVal)
  120.                 {
  121.                     order = false;
  122.  
  123.                     if (filenr == 2)
  124.                     {
  125.                         if (rw2.runs == fn)
  126.                         {
  127.                             fn = f1 + f2;
  128.                             f1 = f2;
  129.                             f2 = fn;
  130.  
  131.                             filenr = 3;
  132.                         }
  133.                     }
  134.                     else
  135.                     {
  136.                         if (rw3.runs == fn)
  137.                         {
  138.                             fn = f1 + f2;
  139.                             f1 = f2;
  140.                             f2 = fn;
  141.  
  142.                             filenr = 2;
  143.                         }
  144.                     }
  145.                    
  146.                 }
  147.                 if (filenr == 2)
  148.                 {
  149.                     rw2.writeRow(currRow);
  150.                 }
  151.                 else
  152.                 {
  153.                     rw3.writeRow(currRow);
  154.                 }
  155.             }
  156.             if (filenr == 2)
  157.             {
  158.                 rw2.dummyRuns = fn - rw2.runs;
  159.                 rw3.dummyRuns = 0;
  160.             }
  161.             else
  162.             {
  163.                 rw2.dummyRuns = 0;
  164.                 rw3.dummyRuns = fn - rw3.runs;
  165.             }
  166.             iniFib = nextFib(rw2.runs) + nextFib(rw3.runs);
  167.  
  168.             //eh.
  169.             //i nie musimy sie martwic sklejaniem
  170.             //bo tasmy same licza ile maja serii
  171.             //i luz
  172.             return order;
  173.         }
  174.  
  175.  
  176.         public static bool SortButton()
  177.         {
  178.             int currFib = iniFib;
  179.             int numOfPhases = 0;
  180.  
  181.             bool done = false;
  182.  
  183.             double prewRowK = 0.0;
  184.             double prewRowD = 0.0;
  185.  
  186.             SingleRow rowK = new SingleRow(false);
  187.             SingleRow rowD = new SingleRow(false);
  188.  
  189.             double currRowK=0.0;
  190.             double currRowD=0.0;
  191.  
  192.             rw2.clear();
  193.             rw3.clear();
  194.  
  195.             //bierz plik rw1 i przepisz go seria po serii do plikow rw2 i rw3.
  196.             if (phase1())
  197.             {
  198.                 if (form.genLog())
  199.                 {
  200.                     showFile(rw1.fname);
  201.                     form.publishToTextbox("\n");
  202.                     form.publishToTextbox("File initialy sorted");
  203.                 }
  204.             }
  205.  
  206.             rw2.flush();
  207.             rw3.flush();
  208.  
  209.             if (form.genLog())
  210.             {
  211.                 showFile(rw2.fname);
  212.                 showFile(rw3.fname);
  213.             }
  214.  
  215.             if (form.genLog())
  216.             {
  217.                 //form.publishToTextbox("runs in file2 = "+rw2.runs.ToString()+"\n");
  218.                 //form.publishToTextbox("runs in file3 = " + rw3.runs.ToString()+"\n");
  219.                 form.publishToTextbox("dystrybucja zakonczona");
  220.                 form.publishToTextbox("\n\n");
  221.             }
  222.  
  223.             //faza druga
  224.             FileBufferReader pusty;
  225.             FileBufferReader krotszy;
  226.             FileBufferReader dluzszy;
  227.  
  228.             pusty = rw1;
  229.             if (rw2.runCntTotal() > rw3.runCntTotal())
  230.             {
  231.                 krotszy = rw3;
  232.                 dluzszy = rw2;
  233.             }
  234.             else
  235.             {
  236.                 krotszy = rw2;
  237.                 dluzszy = rw3;
  238.             }
  239.             pusty.clear();
  240.             krotszy.startOver();
  241.             dluzszy.startOver();
  242.             //krotszy na pewno ma fib
  243.             //dluzszy ma fib-dummyRuns
  244.  
  245.             while (!done)
  246.             {
  247.                 numOfPhases++;
  248.  
  249.                 prewRowK = 0.0;
  250.                 prewRowD = 0.0;
  251.                 //DZIALA YEEEEE
  252.  
  253.                 rowK = new SingleRow(false);
  254.                 rowD = new SingleRow(false);
  255.  
  256.                 currRowK = 0.0;
  257.                 currRowD = 0.0;
  258.  
  259.                 pusty.clear();
  260.                 dluzszy.startOver();
  261.                 //krotszy.startOver();
  262.  
  263.                 rowD = dluzszy.readRow();
  264.                 rowK = krotszy.readRow();
  265.                 while (true)
  266.                 {
  267.                     //tyle ile jest serii w krotszym mergujemy do pustego
  268.                     //if (rowK != "" && justWritten == 2)
  269.                     //    rowK = krotszy.readRow();
  270.                     //else if (rowD != "" && justWritten == 3)
  271.                     //    rowD = dluzszy.readRow();
  272.  
  273.                     if (rowK.empty() && rowD.empty())
  274.                     {
  275.                         break;
  276.                         //to nie powinno sie stac
  277.                     }
  278.                     else if (rowD.empty())
  279.                     {
  280.                         //dlozszy okazal sie krotszym?
  281.                         //przepisujemy krotszy do konca i tyle
  282.  
  283.                         pusty.writeRow(rowK);
  284.                         rowK = krotszy.readRow();
  285.                         while (!rowK.empty())
  286.                         {
  287.                             pusty.writeRow(rowK);
  288.                             rowK = krotszy.readRow();
  289.                         }
  290.                         continue;
  291.                     }
  292.                     else if (rowK.empty())
  293.                     {
  294.                         //przepisujemy tyle serii z dluzeszego
  295.                         //ile mamy dummy runów
  296.  
  297.                         currRowD =0.0;
  298.                         prewRowD =0.0;
  299.                        
  300.                         for (int i = 0; i < krotszy.dummyRuns; i++)
  301.                         {
  302.                             pusty.writeRow(rowD);
  303.                             rowD = dluzszy.readRow();
  304.                             prewRowD = currRowD;
  305.                             currRowD = rowD.geoMean();
  306.  
  307.                             while (!rowD.empty() && currRowD > prewRowD)
  308.                             {
  309.                                 pusty.writeRow(rowD);
  310.                                 rowD = dluzszy.readRow();
  311.                                 prewRowD = currRowD;
  312.                                 currRowD = rowD.geoMean();
  313.                             }
  314.  
  315.                             prewRowD = 0.0;
  316.                             prewRowK = 0.0;
  317.                             //koniec serii
  318.                             //justWritten = 0;
  319.                         }
  320.                         continue;
  321.                     }
  322.                    
  323.                     //co teraz? tyle ile serii jest w krotszym tyle serii merdżujemy
  324.                     //ale jak to osiagnac, zeby zamienic plik z odczytu na zapis
  325.                     //kij z tym, przeciez pamietamy gdzie czytamy
  326.                     //nic tam nie bedziemy zapisywac zanim nie opustoszeje
  327.  
  328.                     //ile razy robimy merdż?
  329.                     //mamy krótszy.runs (hura)
  330.                     //oraz którszy.dummy runs
  331.  
  332.                     currRowD = rowD.geoMean();
  333.                     currRowK = rowK.geoMean();
  334.  
  335.                     if (currRowK < currRowD)
  336.                     {
  337.                         if (currRowK >= prewRowK)
  338.                         {
  339.                             //kontynuacja serii
  340.                             pusty.writeRow(rowK);
  341.                             //justWritten = 2;
  342.                             prewRowK = currRowK;
  343.                             rowK = krotszy.readRow();
  344.                         }
  345.                         else
  346.                         {
  347.                             //koniec serii krotszej
  348.                             //przepisujemy dluzsza
  349.  
  350.                             //skrot myslowy. nie seria jest krotsza, a plik "krótszy" ma mniej serii niż "dłuższy"
  351.  
  352.                             pusty.writeRow(rowD);
  353.                             //justWritten = 3;
  354.  
  355.                             //zapisalismy jeden num co jeszcze byl w pamieci
  356.                             //ale czy byl koncem serii?
  357.  
  358.                             rowD = dluzszy.readRow();
  359.                             prewRowD = currRowD;
  360.                             currRowD = rowD.geoMean();
  361.                             while (!rowD.empty() && currRowD > prewRowD)
  362.                             {
  363.                                 pusty.writeRow(rowD);
  364.                                 rowD = dluzszy.readRow();
  365.                                 prewRowD = currRowD;
  366.                                 currRowD = rowD.geoMean();
  367.                             }
  368.                             //justWritten = 0;
  369.                             prewRowD = 0.0;
  370.                             prewRowK = 0.0;
  371.                         }
  372.                     }
  373.                     else
  374.                     {
  375.                         if (currRowD >= prewRowD)
  376.                         {
  377.                             //kontynuacja serii
  378.                             pusty.writeRow(rowD);
  379.                             //justWritten = 3;
  380.                             prewRowD = currRowD;
  381.                             rowD = dluzszy.readRow();
  382.                         }
  383.                         else
  384.                         {
  385.                             //koniec serii krotszej
  386.                             //przepisujemy dluzsza
  387.  
  388.                             //skrot myslowy. nie seria jest krotsza, a plik "krótszy" ma mniej serii niż "dłuższy"
  389.  
  390.                             pusty.writeRow(rowK);
  391.                             //justWritten = 2;
  392.  
  393.                             //zapisalismy jeden num co jeszcze byl w pamieci
  394.                             //ale czy byl koncem serii?
  395.  
  396.                             rowK = krotszy.readRow();
  397.                             prewRowK = currRowK;
  398.                             currRowK = rowK.geoMean();
  399.                             while (rowK.empty() && currRowK > prewRowK)
  400.                             {
  401.                                 pusty.writeRow(rowK);
  402.                                 rowK = krotszy.readRow();
  403.                                 prewRowK = currRowK;
  404.                                 currRowK = rowK.geoMean();
  405.                             }
  406.                             //justWritten = 0;
  407.                             prewRowD = 0.0;
  408.                             prewRowK = 0.0;
  409.                         }
  410.                     }
  411.                    
  412.  
  413.                     //tu jeszcze przepisywanie dummy
  414.                     currRowD = 0.0;
  415.                     prewRowD = 0.0;
  416.  
  417.                     for (int i = 0; i < krotszy.dummyRuns; i++)
  418.                     {
  419.                         pusty.writeRow(rowD);
  420.                         rowD = dluzszy.readRow();
  421.                         prewRowD = currRowD;
  422.                         currRowD = rowD.geoMean();
  423.  
  424.                         while (!rowD.empty() && currRowD > prewRowD)
  425.                         {
  426.                             pusty.writeRow(rowD);
  427.                             rowD = dluzszy.readRow();
  428.                             prewRowD = currRowD;
  429.                             currRowD = rowD.geoMean();
  430.                         }
  431.  
  432.                         prewRowD = 0.0;
  433.                         prewRowK = 0.0;
  434.                         //koniec serii
  435.                         //justWritten = 0;
  436.                     }
  437.  
  438.  
  439.  
  440.                     if (form.genLog())
  441.                     {
  442.                         showFile(krotszy.fname);
  443.                         showFile(dluzszy.fname);
  444.                     }
  445.                     //justWritten = 0;
  446.                     pusty.flush();
  447.                     if (form.genLog())
  448.                     {
  449.                         showFile(pusty.fname);
  450.                         form.publishToTextbox("\n");
  451.                     }
  452.  
  453.                     if (pusty.inOrder)
  454.                     {
  455.                         break;
  456.                     }
  457.  
  458.                     FileBufferReader pom = pusty;
  459.                     pusty = krotszy;
  460.                     krotszy = dluzszy;
  461.                     dluzszy = pom;
  462.                     pusty.clear();
  463.                 }
  464.  
  465.                 //order = true;      
  466.             }
  467.             //form.rwCountPublish(rw1.totalRead, rw1.totalWrite, 1);
  468.             //form.rwCountPublish(rw2.totalRead, rw2.totalWrite, 2);
  469.             //form.rwCountPublish(rw3.totalRead, rw3.totalWrite, 3);
  470.  
  471.             form.numPhasesPublish(numOfPhases);
  472.             return true;          
  473.         }
  474.  
  475.         static void showFile(string fn)
  476.         {
  477.             int rowscnt = 1;
  478.             string myout;
  479.             double prev = 0.0;
  480.             double curr = 0.0;
  481.             form.publishToTextbox(fn+"\n");
  482.             //using (BinaryWriter b = new BinaryWriter(File.Open("file.bin", FileMode.Create)))
  483.             //{
  484.             //    // 3. Use foreach and write all 12 integers.
  485.             //    foreach (int i in a)
  486.             //    {
  487.             //        b.Write(i);
  488.             //    }
  489.             //}
  490.             using (BinaryReader binR = new BinaryReader(File.OpenRead(fn)))
  491.             {
  492.                 List<double> row;
  493.                 while (binR.BaseStream.Position != binR.BaseStream.Length)
  494.                 {
  495.                     row = new List<double>();
  496.                     for (int i = 0; i < 5; i++)
  497.                     {
  498.                         row.Add(binR.ReadDouble());
  499.                     }
  500.                     SingleRow currRow = new SingleRow(row);
  501.                     curr = currRow.geoMean();
  502.  
  503.                     myout = Math.Round(curr, 2).ToString();
  504.                     if (curr < prev)
  505.                     {
  506.                         myout = "|" + myout;
  507.                         rowscnt++;
  508.                     }
  509.                     myout += " ";
  510.  
  511.                     form.publishToTextbox(myout);
  512.                     prev = curr;
  513.                 }
  514.             }
  515.             form.publishToTextbox("\nilość serii w pliku =" + rowscnt.ToString());
  516.             form.publishToTextbox("\n");
  517.         }
  518.  
  519.         static int ROZWALONE_NIE_DO_UZYCIAcountRuns(string fn)
  520.         {
  521.             int ret = 1;
  522.             string line;
  523.  
  524.             double prev = 0.0;
  525.             double curr = 0.0;
  526.             using (System.IO.StreamReader file = new System.IO.StreamReader(fn))
  527.             {
  528.                 while ((line = file.ReadLine()) != null)
  529.                 {
  530.                     //curr = geoMean(line);
  531.                     if (curr < prev)
  532.                         ret++;
  533.  
  534.                     prev = curr;
  535.                 }
  536.             }
  537.             return ret;
  538.         }
  539.  
  540.         static public int nextFib(int x)
  541.         {
  542.             int f0 = 1;
  543.             int f1 = 1;
  544.             //tak, wiem ze zaczyna sie od 0
  545.  
  546.             int fn = 0;
  547.             if (x <= 0)
  548.                 return 0;
  549.             else if (x == 1)
  550.                 return 1;
  551.             else
  552.             {
  553.                 while (fn < x)
  554.                 {
  555.                    
  556.                     fn = f0 + f1;
  557.                     f0 = f1;
  558.                     f1 = fn;
  559.                 }
  560.                 return fn;
  561.             }
  562.         }
  563.     }
  564. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement