1. 简要回答文件和流之间的区别和联系。
【解答】
文件(file)和流(stream)即有区别又有联系。文件是在各种媒质上(可移动磁盘、硬盘、CD 等)永久存储的数据的有序集合。它是一种进行数据读写操作的基本对象。通常情况下,文件按照树状目录进行组织,每个文件都有文件名、文件所在路径、创建时间、访问权限等属性。
流是字节序列的抽象概念,例如文件、输入输出设备、内部进程通信管道或者TCP/IP套接字等均可以看成流。流提供一种向后备存储器写入字节和从后备存储器读取字节的方式。
2. Directory类为我们提供了哪些目录管理的功能,它们是通过哪些方法来实现的?
【解答】
Directory类为我们提供了对磁盘和目录进行管理的功能,如复制、移动、重命名、创建和删除目录,获取和设置与目录的创建、访问及写入操作相关的时间信息。
如:CreateDirectory方法用于创建指定路径中的所有目录;Delete方法用于删除指定的目录;Move方法能够重命名或移动目录;Exists方法用于确定给定路径是否引用磁盘上的现有目录;GetCurrentDirectory方法用于获取应用程序的当前工作目录;GetFiles方法用于返回指定目录中的文件的名称等。
3. 编写程序综合应用Directory类的主要方法。首先确定指定的目录是否存在,如果存在,则删除该目录;如果不存在,则创建该目录。然后,移动此目录,在其中创建一个文件,并对文件进行计数。
【解答】
程序清单如下:
以下是引用片段:
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\MyDir";
string target = @"c:\TestDir";
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (Directory.Exists(target))
{
Directory.Delete(target, true);
}
Directory.Move(path, target);
File.CreateText(target + @"\myfile.txt");
Console.WriteLine("在{0}中的文件数目是{1}",
target, Directory.GetFiles(target).Length);
}
catch (Exception e)
{
Console.WriteLine("操作失败: {0}", e.ToString());
}
finally {}
}
}
4. 编写程序,将文件复制到指定路径,允许改写同名的目标文件。
【解答】
程序清单如下:
以下是引用片段:
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = path + "temp";
try
{
using (FileStream fs = File.Create(path)) {}
File.Delete(path2);
File.Copy(path, path2);
Console.WriteLine("{0}拷贝到:{1}", path, path2);
File.Copy(path, path2, true);
Console.WriteLine("第二次拷贝成功");
}
catch
{
Console.WriteLine("重复拷贝不允许");
}
}
}
5. 编写程序,使用File类实现删除当前目录下的所有文件。
【解答】
程序清单如下:
以下是引用片段:
using System;
using System.IO;
class FileTest
{
public static void Main()
{
Console.WriteLine("确认删除当前目录下的所有文件?");
Console.WriteLine("点击'Y'键继续,其它键取消操作");
int a = Console.Read();
if(a == 'Y' || a == 'y'){
Console.WriteLine("正在删除文件...");
}
else
{
Console.WriteLine("用户取消操作");
return;
}
DirectoryInfo dir = new DirectoryInfo (".");
foreach (FileInfo f in dir.GetFiles())
{
f.Delete();
}
}
}
1. 解释正则表达式 <a\s+href\s*=\s*""?([^"" >]+)""?>(.+)</a>各部分代表的含义。
【解答】 此正则表达式用于匹配源文件中类似于搜狐新闻的字符串,各部分表示的含义为:
\s+ 一个或多个空白字符
href 后接 HTML 定位点中的确切文本
\s* 零个或多个空白字符
= 后接 HTML 定位点中的确切文本
\s* 零个或多个空白字符
""? 零或无引号(转义的)
( 定义子字符串(定位点 URL)的组的起始点。
[^"" >]+ 任意字符的一个或多个匹配项,括号中的字符除外。
) 定义子字符串的第一组的结束
""? 零或无引号(转义的)
> 后接 HTML 定位点中的确切文本
(.+) 与任意字符(定位点文本)匹配的组。
结束 HTML 定位点的确切文本
2. 下面是检查输入字符串是否为有效的电子邮件的正则表达式:
^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$
试解释各部分的含义。
【解答】
[\w-]+
一个或多个任意字符(a-z、A-Z、0-9 以及下划线)或短划线。在@字符两边,确保地址形式为name@domainname。
\.
一个转义点号。(不带反斜杠,一个点号与除换行符外的任意单个字符匹配。)以此确保域名中至少有一个点号。
*?
对前面的表达式,非贪婪(non-greedy,即最小)地查找零次或多次匹配。
([\w-]+\.)*?
以上三个表达式的组合:
对于包含一个或多个任意字符(a-z、A-Z、0-9 以及下划线)或短划线并且后面只跟一个点号的表达式,非贪婪地查找零次或多次匹配。
3. 写出符合下列要求的正则表达式:
1) 要求4-8个英文字母。
2) 不能包含字母,至少1个字符。
3) 至少3个数字。
4) 至少3个字符。
5) 至少3个英文字母。
6) 任意字符。
7) 3个字母或数字,如123,r3a等。
8) 3个点。
9) @前至少有1个字符,@后至少有3个字符。
10) 必须输入左括号。
【解答】
1) [a-zA-Z]{4,8}
2) [^a-zA-Z]{1,}
3) [0-9]{3,}
4) {3,}
5) [a-zA-Z]{3,}
6) .{0,}
7) [A-Za-z0-9]{3}
8) \.{3}
9) .{1,}@ .{3,}
10) \(
1. 使用保持连接的方式编写程序,计算各年级平均成绩,并显示结果。
【解答】
以下是引用片段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 习题8_6_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//添加Button按钮在ListBox中显示结果
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("年级 平均成绩");
string connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
//根据连接字符串创建SqlConnection实例
SqlConnection conn = new SqlConnection(connectionString);
//创建SqlCommand实例,并设置SQL语句和使用的连接实例
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select substring(学号,1,2) as 年级,avg(成绩) as 平均成绩 from MyTable2 group by substring(学号,1,2)";
cmd.Connection = conn;
try
{
conn.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read() == true)
{
listBox1.Items.Add(string.Format("{0}级 {1}", r[0], r[1]));
}
r.Close();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "计算成绩失败");
}
finally
{
conn.Close();
}
}
}
}
2. 使用保持连接的方式编写程序,查询MyTable2中不及格学生的学号,姓名,性别,成绩。并将结果在ListBox中显示出来。
【解答】
以下是引用片段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 习题8_6_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(" 学号 姓名 性别 成绩");
string connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
//根据连接字符串创建SqlConnection实例
SqlConnection conn = new SqlConnection(connectionString);
//创建SqlCommand实例,并设置SQL语句和使用的连接实例
SqlCommand cmd = new SqlCommand();
cmd.CommandText =
"Select 学号,姓名,性别, 成绩 From MyTable2 Where (成绩<60)";
cmd.Connection = conn;
try
{
conn.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read() == true)
{
listBox1.Items.Add( string.Format("{0} {1} {2} {3}", r[0], r[1], r[2], r[3]));
}
r.Close();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "查询成绩失败");
}
finally
{
conn.Close();
}
}
}
}
3. 编写程序,以“[编码]名称”的样式在comboBox1中显示MyTable1的内容。
【解答】
以下是引用片段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 习题8_6_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string connectionString = Properties.Settings.Default.MyDatabaseConnectionString;
//根据连接字符串创建SqlConnection实例
SqlConnection conn = new SqlConnection(connectionString);
//创建SqlCommand实例,并设置SQL语句和使用的连接实例
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select * From MyTable1";
cmd.Connection = conn;
try
{
conn.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read() == true)
{
comboBox1.Items.Add(string.Format("[{0}] {1}", r[0], r[1]));
}
comboBox1.SelectedIndex = 0;
r.Close();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "显示数据失败");
}
finally
{
conn.Close();
}
}
}
}
4. 在画线处填上合适的内容,使程序变得正确完整。
以下是引用片段:
string connString="server=localhost;Integrated Security=SSPI;database=pubs";
SqlConnection conn=____________________________
string strsql="select * from MyTable2";
SqlDataAdapter adapter=new SqlDataAdapter(_____________);
dataset=new DataSet();
adapter.Fill(________________,"MyTable2");
this.dataGridView1.DataSource=dataset.Tables["MyTable2"];
【解答】
以下是引用片段:
string connString="server=localhost;Integrated Security=SSPI;database=pubs";
SqlConnection conn= new SqlConnection(Properties.Settings.Default.MyDatabaseConnectionString);
string strsql="select * from MyTable2";
SqlDataAdapter adapter=new SqlDataAdapter(conn);
dataset=new DataSet();
adapter.Fill(dataset,"MyTable2");
this.dataGridView1.DataSource=dataset.Tables["MyTable2"];
5. 已知数据库中定义了一张person表,表的数据结构如下:
字段名称字段类型字段含义
id数字编号
xm文本姓名
xb文本性别
nl数字年龄
zip文本邮政编码
用编写代码的方法在DataGridView中显示该数据表中年龄大于18的所有纪录,显示时以编号的升序排序,要求禁止用户编辑数据。
【解答】
以下是引用片段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 习题8_6_5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connectionstring = Properties.Settings.Default.MyDatabaseConnectionString ;
SqlConnection conn = new SqlConnection(connectionstring);
try
{
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(
"select id,xm,xb,nl from person where nl > 18 order by id", conn);
DataSet dataset = new DataSet();
//如果不指定表名,则系统自动生成一个默认的表名
adapter.Fill(dataset, "person");
//可以使用索引引用生成的表
dataGridView1.DataSource = dataset.Tables["person"];
adapter.Dispose();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
conn.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
//不允许用户直接在最下面的行添加新行
dataGridView1.AllowUserToAddRows = false;
//不允许用户直接按Delete键删除行
dataGridView1.AllowUserToDeleteRows = false;
}
}
}
6.例8-18的存储过程定义中,将“@surname nvarchar(2),”改为“@surname nchar(2),”,是否仍然能够得到正确结果,为什么?
【解答】
不一定。因为如果传递的参数值为“王”,在存储过程中会自动变为“王 ”。
7. 调用存储过程,设计程序完成下列功能:任意给出一个汉字,统计MyTable2中所有包含该汉字的人数,并显示统计结果。
【解答】
以下是引用片段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 习题8_6_7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn =
new SqlConnection(Properties.Settings.Default.MyDatabaseConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//设置SQL语句为存储过程名,命令类型为存储过程
cmd.CommandText = "SelectFilterStudentsNum";
cmd.CommandType = CommandType.StoredProcedure;
//添加存储过程中参数需要的初始值,注意参数名要和存储过程定义的参数名相同
if( textBox1.Text=="")
{
MessageBox.Show("请输入有效信息","错误");
textBox1.Focus();
return ;
}
cmd.Parameters.AddWithValue("@surname", textBox1.Text);
cmd.Parameters.AddWithValue("@record", 0);
//指定哪些参数需要返回结果
cmd.Parameters["@record"].Direction = ParameterDirection.Output;
try
{
conn.Open();
//执行存储过程
cmd.ExecuteNonQuery();
//显示返回的结果
MessageBox.Show(string.Format("有{0}条含 {1} 的记录",
cmd.Parameters["@record"].Value,textBox1.Text));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
finally
{
conn.Close();
}
}
}
}
1. 简述三类二维坐标系统之间的相同点和区别。
【解答】
GDI+中的二维笛卡儿坐标系统分为三类:全局坐标系统、页面坐标系统和设备坐标系统。
三类坐标系统的相同点:它们都有坐标原点以及向右和向下的x轴和y轴。
三类坐标系统的区别:全局坐标系统可以进行旋转、平移等操作。页面坐标系统与设备坐标系统都是以设备的左上角为坐标原点,X水平向右为正,Y垂直向下为正。页面坐标系统与设备坐标系统的差异在于X,Y的单位不同:页面坐标系中的X,Y单位可以任意设定,如英寸、毫米等;而设备坐标系中,只有一种单位,那就是点(point)或者像素(pixel)。页面坐标系是不能更改的,它是一个参照标准,将全局坐标最终转换为设备坐标。
2. 简述创建Graphics类对象的三种方法。
【解答】
(1) 在窗体或控件的Paint事件中直接引用Graphics对象。在为窗体创建绘制代码时,一般使用此方法获取对图像的引用。
(2) 从当前窗体获取对Graphics对象的引用。注意这种对象只有在处理当前Windows窗体消息的过程中有效。如果想在已经存在的窗体或控件中绘图,可以使用此种方法。
(3) 从继承自图像的任何对象创建Graphics对象。这个方法适用于需要更改已经存在的
图像。
3. 同时创建多个矩形并用红黑相间的颜色进行填充。
【解答】
(1) 新建一个Windows应用程序,命名为“FillMultiRectangle”,调整窗体到适当大小。更改“Form1.cs”为“FormFillMultiRectangle.cs”。
(2) 切换到代码方式,添加名称空间引用:
using System.Drawing.Drawing2D;
(3) 双击窗体的属性面板里事件页中的Paint事件,添加FormFillMultiRectangle_Paint事件代码。
以下是引用片段:
private void FormFillMultiRetangle_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
HatchBrush mybrush=new HatchBrush (HatchStyle.Cross,Color .Red,Color .Black);
Rectangle[] rect=
{
new Rectangle( 0, 0, 50, 100),
new Rectangle(50,100, 100, 50),
new Rectangle(150, 150, 50, 100),
};
g.FillRectangles(mybrush, rect);
g.Dispose();
}
(4) 结果如图所示。

4. 使用Label控件分别以矩形、椭圆和圆形的方式显示图片,并保证图片完全由绘制对象的边框决定。
【解答】
(1)新建一个Windows应用程序,命名为“ShowImageExe”,调整窗体到适当大小。更改“Form1.cs”为“FromShowImageExe.cs”。
(2)切换到代码方式,添加名称空间引用:
using System.Drawing.Drawing2D;
(3) 添加四个Button控件分别命名为“buttonOpenFile”、“buttonRectangle”、“buttonEllipse”、“buttonRound”,以及一个openFileDiolog和label控件。
(4)在Form类下声明两个私有变量filename和flag,分别用来记录打开的文件名和判断哪个按钮的click时间被触发。
private string filename = "";
private int flag = 0;
(5) 添加【打开文件】按钮的click事件
| 以下是引用片段: private void buttonOpenFile_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); filename = openFileDialog1.FileName; label1.Refresh(); } |
(6) 在label1控件的paint事件下添加如下代码:
| 以下是引用片段: private void label1_Paint(object sender, PaintEventArgs e) { if (filename.Trim() == "") return; Bitmap mybitmap = new Bitmap(filename); Graphics g = e.Graphics; TextureBrush mybrush = new TextureBrush(mybitmap,WrapMode.Clamp); //保证图片完全由绘制对象的边框决定 switch (flag) { case 1: g.FillRectangle(mybrush, label1.ClientRectangle); break; case 2: g.FillEllipse(mybrush, label1.ClientRectangle); break; case 3: g.FillEllipse(mybrush, (label1.Width- label1.Height)/2,0, label1.Height, label1.Height); break; } } |
(7) 在其他几个按钮的click事件中分别添加如下代码:
| 以下是引用片段: private void buttonRectangle_Click(object sender, EventArgs e) { flag = 1; label1.Refresh(); } private void buttonEllipse_Click(object sender, EventArgs e) { flag = 2; label1.Refresh(); } private void buttonRound_Click(object sender, EventArgs e) { flag = 3; label1.Refresh(); } |

(8) 结果如图所示。
5. 利用PictureBox控件和Panel控件实现使用滚动条浏览大图片。
【解答】
由于Picturebox控件在显示图片时不能直接使用滚动条,所以必须借助Panel控件实现以滚动条的方式浏览大图片。具体操作步骤如下:
(1)新建一个Windows应用程序,命名为“scrollBar”,调整窗体到适当大小。更改“Form1.cs”为“FormScrollBar.cs”。
(2)切换到代码方式,添加名称空间引用:
using System.Drawing.Drawing2D;
(3) 在窗体上分别添加一个button控件命名为“buttonOpenFile”,一个openFileDiolog控件,Picturebox和Panel控件各一个,将Panel控件的AutoScroll属性设为true。
(4) 在“buttonOpenFile”控件的click事件中添加如下代码:
| 以下是引用片段: private void buttonOpenFile_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); if (openFileDialog1.FileName.Trim() == "") return; try { Bitmap mybitmap = new Bitmap(openFileDialog1.FileName); pictureBox1.Image = mybitmap; } catch (Exception Err) { MessageBox.Show("打开文件错误!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } |
(5) 结果如图所示。

6. 实现对图片按任意角度进行旋转、按任意比例进行缩放、按任意位移进行平移。
【解答】
(1) 在窗体上添加六个label控件(其中label1用来显示图片)、一个button控件(用于打开图片文件)和五个numericUpDown控件(分别用来选择图片缩放的比例、图片旋转的角度、图片位移的大小)。
(2) 在构造函数上方添加代码:
private string strfilename="";
(3) 在button控件的click事件里添加如下代码:
| 以下是引用片段: private void button1_Click(object sender, EventArgs e) { openFileDialog1.ShowDialog(); strfilename=openFileDialog1.FileName; label1.Refresh(); } |
(4) 在每一个numericUpDown控件的ValueChanged事件中添加如下代码:
label1.Refresh();
(5) 在label1控件的paint事件中添加如下代码:
| 以下是引用片段: private void label1_Paint(object sender, PaintEventArgs e) { if (this.strfilename.Trim()=="") return ; try { Bitmap mybitmap = new Bitmap(strfilename); Graphics g = e.Graphics; TextureBrush mybrush = new TextureBrush(mybitmap); float x = (float)(numericUpDownS1.Value / 100); float y = (float)(numericUpDownS2.Value / 100); mybrush.ScaleTransform(x, y); g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height); float r = (float)(numericUpDownR1.Value); mybrush.RotateTransform(r); g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height); float tx = (float)(numericUpDownT1.Value); float ty = (float)(numericUpDownT2.Value); mybrush.TranslateTransform(tx, ty); g.FillRectangle(mybrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height); } catch (Exception Err) { MessageBox.Show("打开文件错误!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } |
8-8-6


