2020. február 26., szerda

Szavazat-számláló

Az osztálykirándulás szervezőinek kiválasztására kitöltött Google űrlap kimenetének optimalizálására írt program kódja, csemegézés céljára :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace kerdoiv
{
    class Program
    {
        static string[,] matrix = new string[38, 11];
        public struct tag
        {
            public string nev;
            public int szavazat;
        }

        static List<tag> nevsor = new List<tag>();
        static void initMatrix()
        {
            for (int i = 0; i < 38; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    matrix[i,j] = String.Empty;
                }
            }
        }

        static void beolvas()
        {           
            StreamReader sr = new StreamReader("kerdoiv.txt");
            string[] atmeneti;
            int vesszoHelye = 0;
            int i = 0;
            while (!sr.EndOfStream)
            {
                atmeneti = sr.ReadLine().Split('\t');
                vesszoHelye = atmeneti[0].IndexOf(',');
                matrix[i, 0] = atmeneti[0].Substring(vesszoHelye+2, atmeneti[0].Length - vesszoHelye-2);
                int j = 1;
                while (j < atmeneti.Length && atmeneti[j] != String.Empty)
                {
                    matrix[i, j] = atmeneti[j];
                    j++;
                }
                matrix[i, j - 1] = matrix[i, j - 1].Substring(0, matrix[i, j - 1].Length-1);
                i++;
            }
            sr.Close();         
        }

        static void decode()
        {
            for (int i = 0; i < 38; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    matrix[i, j] = matrix[i, j].Replace("Ɓ","Á");
                    matrix[i, j] = matrix[i, j].Replace("Ćó""á");
                    matrix[i, j] = matrix[i, j].Replace("Ć?""é");
                    matrix[i, j] = matrix[i, j].Replace("‘?""ő");
                    matrix[i, j] = matrix[i, j].Replace("Ć•""ü");
                    matrix[i, j] = matrix[i, j].Replace("Ć""ú");
                }
            }
        }
        static void kiir()
        {
            for (int i = 0; i < 38; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    Console.Write("{0} ",matrix[i,j]);
                }
                Console.WriteLine();
            }
        }

        static void unique()
        {
            int k;
            bool vanE;
            tag t;
            List<string> nevek = new List<string>();
            List<string> nevekUnique = new List<string>();

            for (int i = 0; i < 38; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    k = 0;
                    vanE = false;
                    while (k < nevsor.Count && !vanE)
                    {
                        if (nevsor[k].nev == matrix[i,j])
                        {
                            vanE = true;
                        }
                        else k++;
                    }
                    if (k == nevsor.Count)
                    {  
                        nevek.Add(matrix[i,j]);
                    }
                }
            }

            bool van;
            int l;
            for (int i = 0; i < nevek.Count; i++)
            {
                l = 0;
                van = false;
                while (l < nevekUnique.Count && !van)
                {
                    if (nevek[i] != nevekUnique[l])
                    {
                        l++;
                    }
                    else
                    {
                        van = true;
                    }
                }
                if (!van && nevek[i]!="") nevekUnique.Add(nevek[i]);
            }

            nevekUnique.Sort();

            for (int i = 0; i < nevekUnique.Count; i++)
            {
                Console.WriteLine(nevekUnique[i]);
            }

            int db;
            for (int i = 0; i < nevekUnique.Count; i++)
            {
                db = 0;
                for (int j = 0; j < nevek.Count; j++)
                {
                    if (nevekUnique[i] == nevek[j])
                    {
                        db++;
                    }
                }
                t = new tag();
                t.nev = nevekUnique[i];
                t.szavazat = db;
                nevsor.Add(t);
            }


            Console.WriteLine("Névsor és szavazatok:");
            for (int i = 0; i < nevsor.Count; i++)
            {
                Console.WriteLine("{0,-25} {1, 2}", nevsor[i].nev, nevsor[i].szavazat);
            }

        }

        static void Main(string[] args)
        {
            initMatrix();
            beolvas();
            decode();
            kiir();
            Console.ReadLine();
            Console.Clear();
            unique();           
            Console.ReadLine();
        }
    }
}


Nincsenek megjegyzések:

Megjegyzés küldése