Wednesday, 16 March 2016

C# ArrayList, SortedList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace vote
{
    class Program
    {
     

        static void Main(string[] args)
        {
            string[] voting = { "X012033001140", "C075100026080", "A050070060100", "Q040088050090" };

            ArrayList a = new ArrayList();

            int total_1=0, total_2=0, total_3=0, total_4 = 0;

            foreach (string b in voting)
            {
                a.Add(b);
            }

            a.Sort();

            for (int i = 0; i < a.Count; i++)
            {
                Console.WriteLine("{0,-10}{1,-10}","city","# of vote");

                string d = a[i].ToString();

                Console.WriteLine("{0,-10}", d.Substring(0,1));

                int x,y,z,xyz;

                x = Convert.ToInt32(d.Substring(1, 1)) * 100 + Convert.ToInt32(d.Substring(2, 1)) * 10 + Convert.ToInt32(d.Substring(3, 1));
                y = Convert.ToInt32(d.Substring(4, 1)) * 100 + Convert.ToInt32(d.Substring(5, 1)) * 10 + Convert.ToInt32(d.Substring(6, 1));
                z = Convert.ToInt32(d.Substring(7, 1)) * 100 + Convert.ToInt32(d.Substring(8, 1)) * 10 + Convert.ToInt32(d.Substring(9, 1));
                xyz = Convert.ToInt32(d.Substring(10, 1)) * 100 + Convert.ToInt32(d.Substring(11, 1)) * 10 + Convert.ToInt32(d.Substring(12, 1));

                total_1 += x;
                total_2 += y;
                total_3 += z;
                total_4 += xyz;

                Console.WriteLine("{0,-10}{1,-10}", "1", x);
                Console.WriteLine("{0,-10}{1,-10}", "2", y);
                Console.WriteLine("{0,-10}{1,-10}", "3", z);
                Console.WriteLine("{0,-10}{1,-10}", "4", xyz);
            }

            Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}", "total_1","total_2","total_3","total_4" );
            Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}", total_1, total_2,total_3, total_4);

            int total = total_1 + total_2 + total_3 + total_4;

            double percent_1 = (double)total_1 / (double)total;
            double percent_2 = (double)total_2 / (double)total;
            double percent_3 = (double)total_3 / (double)total;
            double percent_4 = (double)total_4 / (double)total;

            Console.WriteLine("{0,-10:f2}{1,-10:f2}{2,-10:f2}{3,-10:f2}", percent_1, percent_2, percent_3, percent_4);

            SortedList tag = new SortedList(); //tag before sorting so as to retrieve info later
            tag[percent_1] = "1";              //tagged info could be a class rather than just string
            tag[percent_2] = "2";
            tag[percent_3] = "3";
            tag[percent_4] = "4";

            ArrayList tag_sort = new ArrayList();

            foreach (double key in tag.Keys)
            {
                tag_sort.Add(key);
            }

            tag_sort.Sort();
            tag_sort.Reverse();

            for (int i = 0; i < tag_sort.Count; i++)
            {
                double key = Convert.ToDouble( tag_sort[i]);    //sorted prameter high to low
                string candidate = tag[key].ToString();         //retrieve tagged info

                print(candidate, key);
            }

            Console.Read();
        }

        static void print(string p, double q)
        {
            Console.Write("candidate " + p + "      " );
            Console.Write("{0,10:f2}", q);
            Console.WriteLine("%");
        }


    }
}

---------------------------------------------------------------------------------------------
city      # of vote
A
1         50
2         70
3         60
4         100
city      # of vote
C
1         75
2         100
3         26
4         80
city      # of vote
Q
1         40
2         88
3         50
4         90
city      # of vote
X
1         12
2         33
3         1
4         140
total_1   total_2   total_3   total_4
177       291       137       410
0.17      0.29      0.13      0.40
candidate 4            0.40%
candidate 2            0.29%
candidate 1            0.17%
candidate 3            0.13%


No comments:

Post a Comment