2010年6月3日 星期四

C# 如何在Arraylis裡找出相同的值並計算出現次數

有一個 Arraylist 裡面有值為 {1,2,3,4,5,3,10,1,2,4,12,9,4} 等數字
要一個數字只顯示一次,不重複
並計算出每個數字共出現幾次?

顯示gridview 如下

Num Count
------------
1 2
2 2
3 2
4 3
5 1
9 1
10 1
12 1


public static void Main(string[] args)
{
int[] iArr = new int[] { 1, 2, 3, 4, 5, 3, 10, 1, 2, 4, 12, 9, 4 };

SortedDictionary<int, int> sDict = new SortedDictionary<int, int>();

foreach (int i in iArr)
{
if (sDict.ContainsKey(i))
{
int value = sDict[i];
sDict.Remove(i);
sDict.Add(i, value + 1);
}
else
sDict.Add(i, 1);
}

Console.WriteLine(" Num Count");
Console.WriteLine(" -------------- ");
foreach (KeyValuePair<int, int> kvp in sDict)
Console.WriteLine("{0,4}{0,8}", kvp.Key, kvp.Value);

Console.ReadKey();
}


Result:
CSharp_Array

2010年6月2日 星期三

C# : Simple Console Calendar


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleCalendar
{
public class Program
{
public static void Main(string[] args)
{
int year, month, day;
Console.Write("請輸入年份:");
year = Convert.ToInt32(Console.ReadLine());
Console.Write("請輸入月份:");
month = Convert.ToInt32(Console.ReadLine());
Console.Write("請輸入日期:");
day = Convert.ToInt32(Console.ReadLine());

Console.Clear();
DateTime d = new DateTime(year, month, day);
Console.WriteLine("{0}/{1}/{2} {3} {4}", d.Year, d.Month, d.Day, d.DayOfWeek, DateTime.DaysInMonth(d.Year, d.Month));
PrintLine();
PrintRows(3, new string[] { "日", "一", "二", "三", "四", "五", "六" }, 7);
PrintLine();
PrintRows(4, GetDaysArray(d), 7);
PrintLine();
Console.ReadKey();
}

private static string[] GetDaysArray(DateTime dt)
{
List<string> list = new List<string>();

//Fill the spaces of first line
DateTime firstDay = new DateTime(dt.Year, dt.Month, 1);
switch (firstDay.DayOfWeek)
{
case DayOfWeek.Monday:
list.Add("");
break;
case DayOfWeek.Tuesday:
list.AddRange(new string[] { "", "" });
break;
case DayOfWeek.Wednesday:
list.AddRange(new string[] { "", "", "" });
break;
case DayOfWeek.Thursday:
list.AddRange(new string[] { "", "", "", "" });
break;
case DayOfWeek.Friday:
list.AddRange(new string[] { "", "", "", "", "" });
break;
case DayOfWeek.Saturday:
list.AddRange(new string[] { "", "", "", "", "", "" });
break;
}
//the date, 1 ~ 28 or 29 or 30 or 31
//mark the date for special color
for (int i = 1; i <= DateTime.DaysInMonth(dt.Year, dt.Month); i++)
{
if (i == dt.Day)
list.Add("{" + i + "}");
else
list.Add(i + "");
}
return list.ToArray();
}

private static void PrintLine()
{
Console.WriteLine(new string('-', 36));
}

private static void PrintRows(int width, string[] columns, int itemsPerLine)
{
StringBuilder sb = new StringBuilder();
int totalLines = columns.Length % itemsPerLine == 0 ? columns.Length / itemsPerLine : columns.Length / itemsPerLine + 1;
for (int i = 0; i < totalLines; i++)
{
for (int j = 0; j < itemsPerLine; j++)
{
int pos = i * itemsPerLine + j;
if (pos >= columns.Length)
break;
sb.Append("|" + AlignCentre(columns[pos], width));
}
sb.Append("|");
sb.Append("\n");
}

//Start to print
uint STD_OUTPUT_HANDLE = 0xfffffff5;
IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
foreach (char c in sb.ToString().ToCharArray())
{
if (c == '{')
{
//SetConsoleTextAttribute(hConsole, 12);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(' ');
continue;
}
if (c == '}')
{
//SetConsoleTextAttribute(hConsole, 7);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(' ');
continue;
}
Console.Write(c);
}
}

private static string AlignCentre(string text, int width)
{
if (string.IsNullOrEmpty(text))
return new string(' ', width);
else
return text.PadLeft(width - (width - text.Length) / 2).PadRight(width);
}

[DllImport("kernel32.dll")]
private static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, int wAttributes);

[DllImport("kernel32.dll")]
private static extern IntPtr GetStdHandle(uint nStdHandle);

}
}



Result:
C#_Console_Calendar_A

C#_Console_Calendar_B

Source Code: Download