Advertisement
Guest User

c# io labs 04

a guest
Dec 11th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.26 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////
  2. // PGM.cs
  3. //
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace lab04
  13. {
  14.     class PGM
  15.     {
  16.         private String name;
  17.         private int width;
  18.         private int height;
  19.         private int maxPixelValue;
  20.         private List<String> comments;
  21.         private List<List<int>> pixels;
  22.  
  23.         public PGM()
  24.         {
  25.             this.Name = "";
  26.             this.Width = 0;
  27.             this.Height = 0;
  28.             this.MaxPixelValue = 0;
  29.             this.comments = new List<String>(0);
  30.             this.pixels = new List<List<int>>(0);
  31.         }
  32.  
  33.         public PGM(String name, int width, int height, int maxPixelValue)
  34.         {
  35.             this.Name = name;
  36.             this.Width = width;
  37.             this.Height = height;
  38.             this.MaxPixelValue = maxPixelValue;
  39.             pixels = new List<List<int>>(0);
  40.             comments = new List<String>(0);
  41.         }
  42.  
  43.         public void writeToFile(String fileName)
  44.         {
  45.             using (System.IO.StreamWriter outputFile = new StreamWriter(fileName))
  46.             {
  47.                 outputFile.WriteLine(this.Name);
  48.                 foreach (string comment in comments)
  49.                 {
  50.                     outputFile.WriteLine(comment);
  51.                 }
  52.                 outputFile.WriteLine(this.Width + " " + this.Height);
  53.                 outputFile.WriteLine(this.MaxPixelValue);
  54.                 foreach (List<int> pixRow in pixels)
  55.                 {
  56.                     foreach (int pix in pixRow)
  57.                     {
  58.                         outputFile.Write(pix + "\t");
  59.                     }
  60.                     outputFile.Write(Environment.NewLine);
  61.                 }
  62.             }
  63.         }
  64.  
  65.         public List<String> getInputFromFile(String filePath)
  66.         {
  67.             string line;
  68.             List<String> lines = new List<String>(0);
  69.             System.IO.StreamReader file = new System.IO.StreamReader(filePath);
  70.             while ((line = file.ReadLine()) != null)
  71.             {
  72.                 lines.Add(line);
  73.             }
  74.             file.Close();
  75.             return lines;
  76.         }
  77.  
  78. //        public void generateHorizontalLines(int size, int width)
  79. //        {
  80. //            this.Name = "Horizontal Lines";
  81. //            this.MaxPixelValue = 15;
  82. //            this.Width = size;
  83. //            this.Height = size;
  84. //            bool flag = true;
  85. //            for(int i = 0; i < size; i++)
  86. //            {
  87. //                List<int> pixelRow = new List<int>(0);
  88. //            }
  89. //        }
  90.  
  91.         public bool isComment(String line)
  92.         {
  93.             if (line[0].Equals('#'))
  94.             {
  95.                 this.comments.Add(line);
  96.                 return true;
  97.             }
  98.             return false;
  99.         }
  100.  
  101.         public void parseInput(List<String> lines)
  102.         {
  103.             List<String> pixelStrings = new List<String>(0);
  104.             //String[] podzielone = filePath.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  105.             lines.ForEach((line) =>
  106.             {
  107.                 if (isComment(line)) { }
  108.                 else if (this.Name.Length < 1)
  109.                 {
  110.                     this.Name = line;
  111.                 }
  112.                 else if (this.Height < 1)
  113.                 {
  114.                     String[] podzielone = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  115.                     this.Width = Convert.ToInt32(podzielone[0]);
  116.                     this.Height = Convert.ToInt32(podzielone[1]);
  117.                 }
  118.                 else if (this.MaxPixelValue < 1)
  119.                 {
  120.                     String[] podzielone = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  121.                     this.MaxPixelValue = Convert.ToInt32(podzielone[0]);
  122.                 }
  123.                 else
  124.                 {
  125.                     pixelStrings.Add(line);
  126.                 }
  127.             }
  128.             );
  129.             pixelStrings.ForEach((line) =>
  130.             {
  131.                 String[] podzielone = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  132.                 List<int> pixelRow = new List<int>(0);
  133.                 for(int i = 0; i < podzielone.Length; i++)
  134.                 {
  135.                     pixelRow.Add(Convert.ToInt32(podzielone[i]));
  136.                 }
  137.                 pixels.Add(pixelRow);
  138.             });
  139.         }
  140.  
  141.         public void addComment(String comment)
  142.         {
  143.             this.comments.Add("# " + comment);
  144.         }
  145.  
  146.         public int Width { get => width; set => width = value; }
  147.         public int Height { get => height; set => height = value; }
  148.         public int MaxPixelValue { get => maxPixelValue; set => maxPixelValue = value; }
  149.         public string Name { get => name; set => name = value; }
  150.         public override string ToString()
  151.         {
  152.             StringBuilder stringBuilder = new StringBuilder();
  153.             for(int i = 0; i<height; i++)
  154.             {
  155.                 for(int j = 0; j<width; j++)
  156.                 {
  157.                     stringBuilder.Append(pixels[i][j]);
  158.                     stringBuilder.Append("\t");
  159.                 }
  160.                 stringBuilder.Append(Environment.NewLine);
  161.             }
  162.             stringBuilder.Append("Comments: ");
  163.             foreach(string comment in comments)
  164.             {
  165.  
  166.                 stringBuilder.Append(Environment.NewLine);
  167.                 stringBuilder.Append(comment);
  168.             }
  169.             return stringBuilder.ToString();
  170.         }
  171.     }
  172. }
  173.  
  174. ////////////////////////////////////////////////////////////
  175. // lab04 program class
  176. //
  177.  
  178. using System;
  179. using System.Collections.Generic;
  180. using System.Linq;
  181. using System.Text;
  182. using System.Threading.Tasks;
  183.  
  184. namespace lab04
  185. {
  186.     class Program
  187.     {
  188.         static void Main(string[] args)
  189.         {
  190.             PGM pgm = new PGM();
  191.             pgm.parseInput(pgm.getInputFromFile("pixels.pgm"));
  192.             pgm.addComment("Losowy komentarzyk");
  193.             pgm.writeToFile("pixelsSaved.pgm");
  194.             Console.Out.Write(pgm.ToString());
  195.            
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement