C#考试资料.doc_第1页
C#考试资料.doc_第2页
C#考试资料.doc_第3页
C#考试资料.doc_第4页
C#考试资料.doc_第5页
已阅读5页,还剩10页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

enum week sun,mon,tue,wed,thr,fir,sat;private void button1_Click(object sender, System.EventArgs e)DateTime wk=Convert.ToDateTime(textBox1.Text);week d=(week)wk.DayOfWeek;label2.Text=this is the +d;求星期几5给一个数组输入数值,如果输入字符时,需要重新输入,如果输入“End”则结束输入,并统计输入的次数。(try-catch-finally)namespace ConsoleApplication2 class Program static void Main(string args) namespace ConsoleApplication3 class Program static void Main(string args) int a = new int10; string b; int d=0; bool c=true; while(c) try for(int i=0;ia.Length;i+) b = Console.ReadLine(); if (b = END) c = false; break; else ai = Convert.ToInt32(b); d = i; catch (FormatException) Console.WriteLine(请输入的number); Console.WriteLine(输入的次数是:0,d+1); Console.ReadLine(); 1. 自己定义一个异常类。并引用其实例查看异常的情况namespace ConsoleApplication1 public class Exception1111 : Exception public string n; public int sum; public Exception1111() : base() public Exception1111(string a, Exception e) : base(a, e) n = a; sum = 1; public void F() int nn = Convert.ToInt32(n); for (int i = 1; i = nn; +i) sum = checked(sum * i); public void display() Console.WriteLine(n!=0, sum); Console.ReadLine(); class Program static void Main() Console.WriteLine(请输入一个数:); try Exception1111 b = new Exception1111(); Exception ee = new Exception(); string a = Console.ReadLine(); Exception1111 c = new Exception1111(a, ee); c.F(); c.display(); catch (FormatException f) Console.WriteLine(输出格式错误。); Console.ReadLine(); catch (OverflowException e) Console.WriteLine(超出数据的范围了。); Console.ReadLine(); 3假设有20张票,由两个线程来实现一个售票程序,每次线程运行时先检查是否还有票未售出,如果有就按照票号从小到大的顺序售出。把上面的结果用windows窗体实现。using System.Threading;namespace ConsoleApplication1 class SaleTicket public bool flag; public int index; Random r = new Random(); public SaleTicket(int Ticketnums) index = Ticketnums; public void Inital() flag = new bool20; for (int i = 0; i 19) Console.WriteLine(票已卖完.); Console.ReadLine(); return 0; else for (int i = 0; i 20; +i) if (flagi = false) Console.WriteLine(0:售出一张票,票号:1,-6,Thread.CurrentThread.Name,(i+1).ToString().Plaleft(3,0); flagi = true; return 0; public void run() int n = 1; Sale(r.Next(1,n+); class Program static void Main(string args) SaleTicket Sal = new SaleTicket(20); Sal.Inital(); Thread MyThread1 = new Thread(new ThreadStart(Sal.run); Thread MyThread2 = new Thread(new ThreadStart(Sal.run); MyThread1.Name = thread_1; MyThread2.Name = thread_1; MyThread1.Start(); MyThread2.Start(); Console.ReadLine(); SaleTicket.cs代码:using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using System.Threading;namespace WindowsFormsApplication10 class SaleTicket private object obl=new object(); Form1 form1; public bool flag; public int index; Random r = new Random(); public SaleTicket(int Ticketnums,Form1 form1) this.index = Ticketnums; this.form1=form1; public void Inital() flag = new bool20; for (int i = 0; i 19) form1.AddListBoxItme(票已售完.); return 0; else for (int i = 0; i 20; +i) string s; if (flagi = false) s=Thread.CurrentThread.Name+:售出一张票,票号:+Convert.ToString(i+1).ToString().PadLeft(3,0); form1.AddListBoxItme(s); flagi = true; return 0; public void run() int n = 1; Sale(r.Next(1,n+); static class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); Form.cs代码:using System.Threading;namespace WindowsFormsApplication10 public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) delegate void AddListBoxItmeDelegate(string st); public void AddListBoxItme(string st) if (listBox1.InvokeRequired) AddListBoxItmeDelegate d = AddListBoxItme; listBox1.Invoke(d, st); else listBox1.Items.Add(st); private void button1_Click(object sender, EventArgs e) listBox1.Items.Clear(); SaleTicket sal = new SaleTicket(20, this); sal.Inital(); Thread MyThread1 = new Thread(new ThreadStart(sal.run); Thread MyThread2 = new Thread(new ThreadStart(sal.run); MyThread1.Name = thread_1; MyThread2.Name = thread_1; MyThread1.Start(); MyThread2.Start(); 用StreamWriter 和StreamReader实现文本的读写using System.IO;namespace WindowsFormsApplication1 public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) private void button2_Click(object sender, EventArgs e) StreamReader b = new StreamReader(E:1.txt, true); while (b.Peek() -1) label1.Text = b.ReadToEnd();/ textBox4.Text = b.ReadString(); b.Close(); private void button1_Click(object sender, EventArgs e) StreamWriter b = new StreamWriter(E:1.txt, true); string a; a = ; a += textBox1.Text; b.Write(a); b.Close(); 实验(实训)原理、装置、内容、步骤、数据记录及结果分析:1. 建立Windows程序,在窗体中显示课程信息(Course)数据表中的数据。界面设计及代码参考教材P180-181【案例5-2】效果如下图所示:using System.Data.SqlClient;namespace WindowsFormsApplication1 public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) try SqlConnection myConnection = new SqlConnection(); /myConnection.ConnectionString = server=localhost;database=student; myConnection.ConnectionString = initial catalog=pubs;integrated security=true; SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = myConnection; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = SELECT * FROM jobs; myConnection.Open(); SqlDataReader dreader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); string s = ; while (dreader.Read() s += dreaderjob_id.ToString() + + dreaderjob_desc.ToString() + dreadermin_lvl.ToString() + dreadermax_lvl.ToString() + rn; textBox1.Text = s; myConnection.Close(); dreader.Close(); catch (SqlException ee) MessageBox.Show(ee.Message); 2. 建立一个Windows程序, 实现对学生信息(Student)数据表的数据维护功能,即实现对学生信息表的增、删、改、查功能。在窗体上放置5个标签,一个列表视图控件(ListView)、5个文本框和3个按钮。界面如下图所示: myConnection = new SqlConnection(); /myConnection.ConnectionString = server=localhost;database=student; myConnection.ConnectionString = initial catalog=pubs;integrated security=true; sqlCommand = new SqlCommand(); sqlCommand.Connection = myConnection; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = select au_id, au_lname,au_fname,phone,address from authors; myConnection.Open(); SqlDataReader dreader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); while (dreader.Read() ListViewItem newItem = listView1.Items.Add(dreaderau_id.ToString(); newItem.SubItems.Add(dreaderau_lname.ToString(); newItem.SubItems.Add(dreaderau_fname.ToString(); newItem.SubItems.Add(dreaderphone.ToString(); newItem.SubItems.Add(dreaderaddress.ToString(); 实验(实训)原理、装置、内容、步骤、数据记录及结果分析:1. 编写代码,建立一个雇员类,代码参考教材P52-53【案例2-1】,并在报告中指出雇员类中哪些是静态成员,哪些是非静态成员。 class e

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论