123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using CsvHelper;
- using System.Diagnostics;
- namespace Bobarik31P
- {
- public class Reshenie
- {
- {
- Path = path;
- }
- private (double[], double[], double[,]) readingFile()
- {
- Debug.Listeners.Add(new TextWriterTraceListener("debug.txt"));
- Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
- Debug.AutoFlush = true;
- if (!Path.Contains(".csv"))
- {
- double[] spros = new double[0];
- double[] predlozhenie = new double[0];
- double[,] tarif = new double[0,0];
- Console.WriteLine("Неверный формат файла");
- Console.ReadKey();
- return (spros, predlozhenie, tarif);
- }
- else
- {
- try
- {
- var dataStr = File.ReadAllLines(Path)
- .Select(line => line.Split(';').ToArray()
- ).ToArray();
- double[] spros = new double[dataStr[0].Length];
- double[] predlozhenie = new double[dataStr[1].Length];
- double[,] tarif = new double[dataStr.Length - 2, dataStr[1].Length];
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < dataStr[i].GetLength(0); j++)
- {
- if (i == 0)
- {
- spros[j] = Convert.ToDouble(dataStr[i][j]);
- Debug.WriteLine(spros[j]);
- }
- else
- {
- predlozhenie[j] = Convert.ToDouble(dataStr[i][j]);
- Debug.WriteLine(predlozhenie[j]);
- }
- }
- }
- for (int i = 0; i < dataStr.Length - 2; i++)
- {
- for (int j = 0; j < dataStr[i].Length; j++)
- {
- tarif[i, j] = Convert.ToDouble(dataStr[i][j]);
- }
- }
- return (spros, predlozhenie, tarif);
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- private void writingFile(List<List<double?>> itog)
- {
- using (StreamWriter writer = new StreamWriter("result.csv"))
- {
- using (CsvWriter csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture))
- {
- for(int i = 0; i < itog.Count; i++)
- {
- for(int j = 0; j < itog[i].Count; j++)
- {
- csv.WriteRecord(itog[i][j].Value);
- }
- csv.NextRecord();
- }
- }
- }
- }
- public void MinElement()
- {
- Trace.Listeners.Add(new TextWriterTraceListener("debug.txt"));
- Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
- Trace.AutoFlush = true;
- double[] spros = readingFile().Item1;
- double[] predloahenie = readingFile().Item2;
- double[] copy_spros = new double[spros.Length];
- spros.CopyTo(copy_spros, 0);
- double[] copy_predloahenie = new double[predloahenie.Length];
- predloahenie.CopyTo(copy_predloahenie, 0);
- double[,] tarif = readingFile().Item3;
- double[,] copy_tarif = new double[tarif.GetLength(0), tarif.GetLength(1)];
- for(int i = 0; i < copy_tarif.GetLength(0); i++)
- {
- for(int j = 0; j < copy_tarif.GetLength(1); j++)
- {
- copy_tarif[i,j] = tarif[i,j];
- }
- }
- List<List<double?>> itog = new List<List<double?>>();
- for(int i = 0;i<copy_tarif.GetLength(0); i++)
- {
- itog.Add(new List<double?>());
- for (int j = 0; j < copy_tarif.GetLength(1); j++)
- {
- itog[i].Add(null);
- }
- }
- bool flag = true;
- while (flag)
- {
- double minTarif = double.MaxValue;
- int strokaMinTarifa = -1;
- int stolbecMinTarifa = -1;
- for (int i = 0; i < copy_tarif.GetLength(0); i++)
- {
- for(int j = 0; j < copy_tarif.GetLength(1); j++)
- {
- if (copy_tarif[i, j] < minTarif)
- {
- minTarif = copy_tarif[i, j];
- strokaMinTarifa = i;
- stolbecMinTarifa = j;
- }
- }
- }
- double razmerPostavki = Math.Min(copy_spros[strokaMinTarifa], copy_predloahenie[stolbecMinTarifa]);
- Trace.WriteLine(razmerPostavki);
- copy_predloahenie[stolbecMinTarifa] -= razmerPostavki;
- Trace.WriteLine(copy_predloahenie[stolbecMinTarifa]);
- copy_spros[strokaMinTarifa] -= razmerPostavki;
- Trace.WriteLine(copy_spros[strokaMinTarifa]);
- if (copy_spros[strokaMinTarifa] == 0)
- {
- for(int i = 0; i < itog.Count; i++)
- {
- if (itog[strokaMinTarifa][i] == null)
- {
- itog[strokaMinTarifa][ i] = 0;
- }
- copy_tarif[strokaMinTarifa, i] = double.MaxValue;
- }
- }
- if (copy_predloahenie[stolbecMinTarifa] == 0)
- {
- for (int i = 0; i < itog[0].Count; i++)
- {
- if (itog[i][stolbecMinTarifa] == null)
- {
- itog[i][stolbecMinTarifa] = 0;
- }
- copy_tarif[i, stolbecMinTarifa] = double.MaxValue;
- }
- }
- itog[strokaMinTarifa][stolbecMinTarifa] = razmerPostavki;
- bool proverkaOkonchania = true;
- for(int i = 0;i < itog.Count;i++)
- {
- for(int j = 0; j < itog[i].Count; j++)
- {
- if(itog[i][j] == null) { proverkaOkonchania = false; }
- }
- }
- if (proverkaOkonchania)
- {
- break;
-
- }
- }
- writingFile(itog);
- }
- }
- }
|