微机接口与原理期末整理Word格式.docx
《微机接口与原理期末整理Word格式.docx》由会员分享,可在线阅读,更多相关《微机接口与原理期末整理Word格式.docx(57页珍藏版)》请在冰豆网上搜索。
fail"
}
catch(Exceptionex)
{MessageBox.Show(ex.Message.ToString());
//捕捉异常}
finally
{conn.Close();
//只要出现异常就关闭连接}
privatevoidbutton2_Click(objectsender,EventArgse)//查找按钮
stringsqlString="
select*fromstwherestu='
"
+textBox1.Text+"
'
;
SqlCommandcmd2=newSqlCommand(sqlString,conn);
conn.Open();
SqlDataReadersdr=cmd2.ExecuteReader();
if(sdr.HasRows)
{sdr.Read();
textBox2.Text=sdr["
Name"
].ToString();
textBox3.Text=sdr["
birthday"
if(bool.Parse(sdr["
Sex"
].ToString()))//转换成布尔类型判断
radioButton1.Checked=true;
elseradioButton2.Checked=true;
//sdr.Close();
没有查询到该学生"
conn.Close();
C#记事本部分代码
namespace_2
{publicpartialclassForm1:
{publicForm1()
{InitializeComponent();
privatevoidntsmenu_Click(objectsender,EventArgse)
{richTextBox1.Clear();
//新建}
privatevoidopsmenu_Click(objectsender,EventArgse)
{if(openFileDialog1.ShowDialog()==DialogResult.OK&
&
openFileDialog1.FileName!
="
)
{richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);
//打开}
privatevoidstsmenu_Click(objectsender,EventArgse)
{if(saveFileDialog1.ShowDialog()==DialogResult.OK&
saveFileDialog1.FileName!
{richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
//保存}
privatevoidatsmennu_Click(objectsender,EventArgse)
{if(saveFileDialog1.ShowDialog()==DialogResult.OK&
//另保存}
privatevoidxtsmenu_Click(objectsender,EventArgse)
{this.Close();
//退出}
privatevoidutsmenu_Click(objectsender,EventArgse)
{//撤销richTextBox1.Undo();
privatevoidrtsmenu_Click(objectsender,EventArgse)
{//重复}
privatevoidttsmenu_Click(objectsender,EventArgse)
{//剪切richTextBox1.Cut();
privatevoidctsmenu_Click(objectsender,EventArgse)
{richTextBox1.Copy();
//复制}
privatevoidptsmitem_Click(objectsender,EventArgse)
{////粘帖richTextBox1.Paste();
privatevoidatsmenu_Click(objectsender,EventArgse)
{//全选richTextBox1.SelectAll();
privatevoidtsmbady_Click(objectsender,EventArgse)
{//字体设置
fontDialog1.ShowColor=true;
if(fontDialog1.ShowDialog()==DialogResult.OK){
richTextBox1.Font=fontDialog1.Font;
richTextBox1.ForeColor=fontDialog1.Color;
}}
privatevoidtsmauto_Click(objectsender,EventArgse)
{//自动换行richTextBox1.WordWrap=false;
privatevoidtime_Click(objectsender,EventArgse)
{richTextBox1.AppendText(DateTime.Now.ToLocalTime().ToString());
//显示日期时间
richTextBox1.AppendText(DateTime.Now.DayOfWeek.ToString());
//显示星期}
教务管理系统
namespace管理系统
{classProgram
{staticvoidMain(string[]args)
{Hashtableht=newHashtable();
stringkey;
stringval;
Console.WriteLine("
欢迎使用2009级学生管理系统"
============================================"
请选择如下操作:
A;
添加B:
查询C:
删除D:
修改E:
退出系统"
********************************************"
stringr=Console.ReadLine();
while(r!
="
e"
{switch(r)
{case"
a"
:
Console.WriteLine("
请输入学生学号:
key=Console.ReadLine();
Console.Write("
请输入学生姓名:
val=Console.ReadLine();
ht.Add(key,val);
break;
case"
b"
请输入要查询的学号:
if(ht.Contains(key))
Console.WriteLine("
学生姓名为:
{0}"
ht[key]);
elseConsole.WriteLine("
您输入的学生学号不存在"
break;
c"
请输入要删除的学号:
ht.Remove(key);
d"
请输入要修改的学号:
请输入修改后学生姓名:
ht[key]=Console.ReadLine();
r=Console.ReadLine();
}}}}
分割连接
namespaceSplitJoinExample
{///<
summary>
///此示例演示使用Split()和Join()方法分割和连接字符串
///<
/summary>
classProgram
{stringinputString;
//输入的字符串
string[]splitStrings;
//分割后的字符串数组
stringjoinString;
//连接后的新字符串
//输入提示
请输入一串字符串,用空格分隔单词:
inputString=Console.ReadLine();
//接收输入
splitStrings=inputString.Split('
'
//以空格作为分隔符分割字符串
//将分割后的字符串输出
\n分割后的字符串为:
foreach(stringsinsplitStrings)
{Console.WriteLine(s);
//将分割后的字符串使用下划线连接在一起
joinString=string.Join("
_"
splitStrings);
//将连接后的字符串输出
\n连接后的新字符串为:
joinString);
Console.ReadLine();
}}}
BinarySerch方法反回其中的元素5的下标
//给定8个数,存在数组中,利用Array类的BinarySerch方法反回其中的元素5的下标
namespaceConsoleApplication5
{classProgram
{staticvoidMain(string[]args)
{int[]myArr={8,7,6,5,4,3,2,1};
Array.Sort(myArr);
inttarget=5;
intresult=Array.BinarySearch(myArr,target);
{0}的下标为{1}"
target,result);
}}}
sort方法排序
//给定8个数,将这些数存在数组中,利用Array类的Sort方法按从小到达顺序输出
namespaceConsoleApplication4
{int[]array=newint[]{8,7,6,5,4,3,2,1};
原始数组:
for(inti=0;
i<
array.Length;
i++)
Console.Write("
{0}->
array[i]);
Console.WriteLine();
//换行
Array.Sort(array);
排序后的数组:
}}}
冒泡排序法升序排列
//给定数组:
,要求用冒泡排序法将其升序排列
namespaceConsoleApplication7
{int[]SortArray=newint[]{78,89,90,56,79,345,217,5,13,88};
待排序序列:
SortArray.Length;
{0}"
SortArray[i]);
for(inti=SortArray.Length-1;
i>
=0;
i--)
for(intj=0;
j<
i;
j++)
{if(SortArray[j]>
SortArray[j+1])
{inttemp=SortArray[j];
SortArray[j]=SortArray[j+1];
SortArray[j+1]=temp;
}}
排序后序列:
SortArray[i]);
排序
//给定8个数:
87654321将这些数存在数组中,按从小到大输出
namespaceConsoleApplication3
{inti,j,temp,m;
int[]array=newint[]{8,7,6,5,4,3,2,1};
for(j=0;
{temp=j;
{if(array[i]<
array[temp])
temp=i;
if(temp!
=j)
{m=array[j];
array[j]=array[temp];
array[temp]=m;
array[j]);
}}}
判断数组中是否含赵六
namespaceConsoleApplication6
{string[]arrSname={"
大宝"
"
张三"
李四"
赵六"
赵二"
麻子"
};
//判断是否含某值
stringtarget="
boolresult=((System.Collections.IList)arrSname).Contains(target);
包含{0}?
{1}"
//true}}}
time类的构造函数及其重载
namespaceConsoleApplication13
{classTime
{publicintnHour,nMinute,nSecond;
publicTime()//构造函数
{nHour=nMinute=nSecond=0;
publicTime(intHour)
{nHour=Hour;
nMinute=nSecond=0;
publicTime(intHour,intMinute)//重载2
nMinute=Minute;
nSecond=0;
publicTime(intHour,intMinute,intSecond)//重载3
nSecond=Second;
classText//程序入口
{staticvoidMain()
{Timet1,t2,t3,t4;
t1=newTime();
t2=newTime(10);
t3=newTime(10,20);
t4=newTime(10,20,30);
t1时间为:
{0}时{1}分钟{2}秒"
t1.nHour,t1.nMinute,t1.nSecond);
t2时间为:
t2.nHour,t2.nMinute,t2.nSecond);
t3时间为:
t3.nHour,t3.nMinute,t3.nSecond);
t4时间为:
t4.nHour,t4.nMinute,t4.nSecond);
}}}
创建类
namespaceConsoleApplication11
{//学生类
publicclassstudent
{publicstringstrName;
publicintnAge;
publicvoidSetAge(intage)
{this.nAge=age;
publicintGetAge()
{returnthis.nAge;
{students=newstudent();
s.SetAge(20);
//赋值
Console.WriteLine(s.GetAge());
//获取
方法重载
namespaceConsoleApplication1
{publicclassStudent
//长了一岁
publicvoidGrow()
{this.nAge++;
//长age岁
publicvoidGrow(intage)
{this.nAge+=age;
{Students=newStudent();
s.strName="
s.nAge=20;
s.Grow();
Console.WriteLine(s.nAge);
s.Grow
(2);
类的继承
namespaceConsoleApplication12
publicclassStudent
publicclassCollegeStudent:
Student
{publicstringxi;
publicclassProgram
{Students=newStudent();
s.strName="
小明"
s.nAge=18;
姓名:
{0},年龄:
s.strName,s.nAge);
CollegeStudentc=newCollegeStudent();
c.strName="
孙立娟"
c.nAge=22;
c.xi="
信息学院"
{1