2009年4月21日 星期二

入門C#----習題4~陣列與字串

廢言
又想偷懶了啦
馬上補個這篇來找回感覺
話說昨天8點就睡(睡到明早5點半)我早上上課還在累個屁阿
完全處於當機狀態連回答都懶的說
算了反正今天預計熬夜拼物理不過理想跟現實是兩回事
明天就名正言順的睡一天吧!?明天還是有要上課吧

﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
第一題
算出現過幾次英文字......
IsLetter結束
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
string a;
int i, n;
while (true)
{
n = 0;
Console.Write("請輸入一串英文子母:");
a = Console.ReadLine();
for (i = 0; i <= a.Length - 1; i++) {
if (char.IsLetter(a, i)){n += 1;} }
Console.WriteLine("總共輸入了{0}次字母", n);
}
}
}
}



﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
開陣列算4個明星在3個地區的總得票與得票率
注意C#的二維陣列是這樣開(別跟別的搞混!)
int [,]陣列名稱= new int [4,3]
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int [,]a= new int [4,3];
int[] at = new int[4];
int i, n, tol=0;
for (i = 0; i <= 3; i++) {
for (n = 0; n <= 2; n++) {
Console.Write("輸入第{0}明星在第{1}區所得之票數:", i+1, n+1);
a[i, n] = int.Parse(Console.ReadLine());
tol += a[i, n];
at[i] += a[i, n]; }
Console.WriteLine();
}
for (i = 0; i <= 3; i++) {
Console.WriteLine("第{0}位明星總得票數為{1} 得票率為{2}%", i + 1, at[i], at[i] * 100 / tol); }
Console.ReadLine();
}
}
}



﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
輸入5個數字在輸出最大與最小
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int[] a = new int[5];
int i;
for (i = 0; i <= 4; i++) {
Console.Write("請輸入第{0}個數: ", i + 1);
a[i] = int.Parse (Console.ReadLine()); }
Console.WriteLine("最大數為:{0} 最小數為{1}", a.Max(), a.Min()); Console.ReadLine();
}
}
}



﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
加密!
我活這麼大沒寫過這麼白痴的加密法阿囧


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int i;
string a;
while (true)
{
Console.WriteLine ("請輸入想加密的字串: ");
a = Console.ReadLine();
Console.WriteLine ("加密後: ");
for (i = a.Length - 1; i >= 0; i--)
{
Console.Write(a[i]);
}
Console.WriteLine();
}
}
}
}



﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
輸入十個數字讓他排序
建立ArrayListSort直接排在輸出就好勒
程式碼


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

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int i;
ArrayList a = new ArrayList ();
for (i = 1; i <= 10; i++) {
Console.Write("輸入第{0}個數 :", i);
a.Add(int.Parse (Console.ReadLine()));
}
a.Sort();
Console.WriteLine("排序後:");
for (i = 0; i <= 9; i++) { Console.Write(a[i]+" ");} Console.ReadLine();
}
}
}



﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
弄最久的一題
最剛開始很豪邁的直接用上題的作法來搜尋輸出
卻發現一個小問題!
這樣考同分的只會顯示一個阿囧
所以還是祭出老招
開陣列一個一個比較囉(反正只有6個人嘛)
程式碼


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

namespace ConsoleApplication1
{
class Program
{
struct a
{
public int n;
public string na;


}
static void Main(string[] args)
{
a[]a=new a[6];
int i;
string s = "";
while (true)
{
for (i = 0; i <= 5; i++)
{
Console.Write("請輸入第{0}位的姓名: ", i + 1);
a[i].na = Console.ReadLine(); Console.Write("請輸入他的成績: ");
a[i].n = int.Parse(Console.ReadLine()); Console.WriteLine();
}
bool b = true ;
while (b) {
Console.WriteLine ("請輸入要查找的分數: ");
i = int.Parse (Console.ReadLine());
for (int k = 0; k <= 5; k++) { if (i == a[k].n) s += a[k].na + " "; } if (s == "") { Console.WriteLine("查無此人"); } else { Console.WriteLine("考這種成績有\n" + s); } Console.Write("是否重新輸入(Y/N): ");
s = Console.ReadLine();
if (s.ToUpper() == "Y"){
break;
}
else { s = ""; }
}
}
}
}
}


順代一題
因為不想開兩個陣列就乾脆練習一下之前不熟得struct

﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
話說
化學最近好像要背得東西很多耶
還真的是"靠背"
數學排列組合也搞得昏頭轉向還有人誤以為我很好勒囧
總之要努力想辦法跟電腦結合一下方便練習勒


不知為何今天上化學有種莫名的火氣

沒有留言:

張貼留言