由于自己脑子不灵光,所以自己写写偶尔需要看看
1.请遍历页面上所有的TextBox并给它赋值为string.empty.
foreach(System.Windows.Forms.Control co in this Controls)
{
if(co is System.Windows.Forms.Control)
{
System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)co;
tb.Text=String.empty;
}
}
2 一个字符串string="adadfdfseffserfefsefseetsdg"找出里面出现次数最多的字母和出现的次数。
class Program
{
static void Main(string[] args)
{
string str = "adadfdfseffserfefsefseetsdg";
int maxlength = 0;
string result = "";
while (str != "")
{
string oldstr = str;
string getstr = str.Substring(0, 1);
str = str.Replace(getstr, "");
if (oldstr.Length - str.Length > maxlength)
{
maxlength = oldstr.Length - str.Length;
result = getstr + "=" + maxlength.ToString ();
}
}
Console.Write(result);
}
}
3 随机生成1-100的数组里面不能有重复
class Program
{
static void Main(string[] args)
{
int[] array = new int[100];
ArrayList mylist = new ArrayList();
Random r = new Random();
while (mylist.Count < 100)
{
int num = r.Next(1, 101);
if (!mylist.Contains(num))
mylist.Add(num);
}
for (int i = 0; i < mylist.Count; i++)
{
array[i] = (int)mylist[i];
Console.WriteLine(array[i]);
}
}
}
4递归实现1.1.2.3.5.8.......第30位数字
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Foo(30));
}
public static int Foo(int i)
{
if (i <= 0)
return 0;
else if (i > 0 && i <= 2)
return 1;
else return Foo(i - 1) + Foo(i - 2);
}
5递归计算1+2+3+.....100的值
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Recursive(100));
Console.ReadLine();
}
public static int Recursive(int i)
{
//计算1+2+3+4+...+100的值
if (i == 0) return 0;
return Recursive(i - 1) + i;
}
}
6计算1-2+3-4+5.......m表达式
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Recursive(m) - Recursive(m-1));
Console.ReadLine();
}
public static int Recursive(int i)
{
//计算1 -2 +3 -4+ 5- 6 + 7 - 8 ......m的值
if (i == 0) return 1;
if (i == 1) return 2;
else return Recursive(i - 2) + i;
}
}
7冒泡算法
一种
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 };
for (int i = 0; i < array.Length; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp ;
}
}
}
for (int a = 0; a < array.Length; a++)
{
Console.WriteLine(array[a]);
}
}
}
第二种
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 };
for (int i = 0; i < array.Length; i++)
{
for (int j =array.Length -1; j >i; j--)
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp ;
}
}
}
for (int a = 0; a < array.Length; a++)
{
Console.WriteLine(array[a]);
}
}
}
8<FileSystem>
<Driver>
<Dir DirName="MsDos622">
<File FileName="Command.com"></File>
</Dir>
<File FileName="MsDos.sys"></File>
<File FileName="IO.sys"></File>
</Driver>
</FileSystem>
请遍历所有文件名为(FileName)的流程图
void FindFile( Directory d )
{
FileOrFolders = d.GetFileOrFolders();
foreach( FileOrFolder fof in FileOrFolders )
{
if( fof is File )
You Found a file;
else if ( fof is Directory )
FindFile( fof );
}
}
9猫叫、老鼠跑、主人醒 (事件写)
class Cat
{
private string _name;
public event EventHandler<CatCryEventArgs> CatCryEvent;
public Cat(string name)
{
_name = name;
}
public void CatCry()
{
CatCryEventArgs args = new CatCryEventArgs(_name);
Console.WriteLine(args);
CatCryEvent(this, args);
}
}
public class CatCryEventArgs : EventArgs
{
private string _catname;
public CatCryEventArgs(string catname)
: base()
{
_catname = catname;
}
public override string ToString()
{
return "Cat" + _catname + "Cry";
}
}
class Mouse
{
private string _name;
public Mouse(string name, Cat cat)
{
_name = name;
cat.CatCryEvent += CatCryHandle;
}
private void CatCryHandle(Object sender, CatCryEventArgs args)
{
Run();
}
private void Run()
{
Console.WriteLine("Mouse" + _name + "Run");
}
}
class Master
{
private string _name;
public Master(string name, Cat cat)
{
_name = name;
cat.CatCryEvent += CatCryHandle;
}
private void CatCryHandle(Object sender, CatCryEventArgs args)
{
WakeUp();
}
private void WakeUp()
{
Console.WriteLine("Master" + _name + "WakeUp");
}
}
10猫叫、老鼠跑、主人醒 (观察者模式)
class Program
{
public interface Observer
{
void Response();
}
public interface Subject
{
void Aimat(Observer ob);
}
public class Cat : Subject
{
private List<Observer> obs;
private string _name;
public Cat(string name)
{
_name = name;
this.obs = new List<Observer>();
}
public void Aimat(Observer ob)
{
this.obs.Add(ob);
}
public void Cry()
{
Console.WriteLine("Cat" + _name + "Cryed");
foreach (Observer ob in this.obs)
{
ob.Response();
}
}
}
public class Mouse : Observer
{
private string _name;
public Mouse(string name, Subject sub)
{
_name = name;
sub.Aimat(this);
}
public void Response()
{
Console.WriteLine("Mouse" + _name + "run");
}
}
public class Master : Observer
{
private string _name;
public Master(string name, Subject sub)
{
_name = name;
sub.Aimat(this);
}
public void Response()
{
Console.WriteLine("Master" + _name + "wakeup");
}
}
static void Main(string[] args)
{
Cat cat = new Cat("Tom");
Mouse mouse = new Mouse("Jack", cat);
Master master = new Master("Tony", cat);
cat.Cry();
}
}