机试第一题:

S2  深入.NET和C#编程 机试测试错题积累-LMLPHP                             S2  深入.NET和C#编程 机试测试错题积累-LMLPHP

 //创建文件流(路径  模式)
FileStream fa = new FileStream(@"D:\\S2第一次全文机试测试\\S2第一次全文机试测试" + textBox1.Text, FileMode.Create);
//写入器
StreamWriter sw = new StreamWriter(fa,Encoding.GetEncoding("GB2312"));
//写入操作
sw.Write("作者:\t" + textBox1.Text + "\t书名:\t" + textBox1.Text + "\t内容\t" + txtnei.Text);
MessageBox.Show("写入成功");
//关闭写入器
sw.Close();
//关闭文件流
fa.Close();

五步走:

1: 创建文件流

2:创建阅读器或者是写入器

3:执行读写操作

4:关闭阅读器或者是写入器

5:关闭文件流

机试第二题:

抽象方法实现多态

S2  深入.NET和C#编程 机试测试错题积累-LMLPHP

public class Bird:Forest
{
//鸟
public override void Play()
{
Console.WriteLine( "鸟在天上飞!");
}
}
public  class dog:Forest
{
public override void Play()
{
Console.WriteLine("狗在地上跑!");
}
}
 public class Fish:Forest
{
public override void Play()
{
Console.WriteLine("鱼在水里游!");
}
}
 public abstract class Forest
{
//动物类
public abstract void Play(); }

main方法:

    static void Main(string[] args)
{
List<Forest> list = new List<Forest>();
Forest a = new Fish();
Forest b = new dog();
Forest c = new Bird();
list.Add(a);
list.Add(b);
list.Add(c);
Console.WriteLine("森林里的小动物在快乐的生活!");
Console.WriteLine();
foreach (Forest item in list)
{
item.Play();
}
Console.ReadLine(); }
}
}

S2  深入.NET和C#编程 机试测试错题积累-LMLPHP

测试第三题:

解析xml绑定树状图treeview

S2  深入.NET和C#编程 机试测试错题积累-LMLPHP

 public class Address
{
//街道
private string street;
//居委会
private string Jwh; //楼号
private string floornum; public string Street
{
get { return street; }
set { street = value; }
} public string Jwh1
{
get { return Jwh; }
set { Jwh = value; }
} public string Floornum
{
get { return floornum; }
set { floornum = value; }
} }
public class RoomAddress
{
public RoomAddress()
{
address = new Address();
}
private string roomnum;
private Address address; public string Roomnum
{
get { return roomnum; }
set { roomnum = value; }
} public Address Address
{
get { return address; }
set { address = value; }
} }
public class Common
{
//解析XML文件。并且将解析的XML文件加载到集合中
public Dictionary<string, RoomAddress> list = new Dictionary<string, RoomAddress>();
public void ReadDataFromXML()
{
XmlDocument doc = new XmlDocument();
doc.Load("Address.xml");
XmlNode root = doc.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
//创建一个地址对象
//Address address = new Address();
//address.Street = root.Attributes["name"].InnerText;
//address.Jwh1 =item.Attributes["name"].InnerText;
string street =root.Attributes["name"].InnerText;
string jwh =item.Attributes["name"].InnerText;
foreach (XmlNode child in item.ChildNodes)
{
string floornum =child.Attributes["name"].InnerText;
foreach (XmlNode node in child.ChildNodes)
{
Address address = new Address();
address.Street = street;
address.Jwh1 = jwh;
address.Floornum = floornum;
RoomAddress roomAddress = new RoomAddress();
//RoomAddress类中才能反映出当前的具体位置信息
roomAddress.Roomnum = node.InnerText;
roomAddress.Address = address;
list.Add(address.Jwh1+address.Floornum+roomAddress.Roomnum,roomAddress);
}
} }
}
}
05-07 15:55