C#代码 课后习题代码.doc_第1页
C#代码 课后习题代码.doc_第2页
C#代码 课后习题代码.doc_第3页
C#代码 课后习题代码.doc_第4页
C#代码 课后习题代码.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

第1章 :习题一、 习题二 using System; using System.Collections.Generic; namespace WindowsFormsApplication2 using System.Linq;using System.Text; public Form1()InitializeComponent();namespace ConsoleApplication1 private void button1_Click(object sender, EventArgs e)class Program label1.Font =new Font(华文彩云,80);static void Main(string args) label1.ForeColor=System.Drawing.Color.Blue; Console.WriteLine(Hello World!); button1.Enabled=false; Console.Read(); 第二章 习题二 private void button1_Click(object sender, EventArgs e) String s, a; s = Convert.ToString(textBox1.Text); a = s.ToUpper(); textBox1.Text = a.ToString(); private void button2_Click(object sender, EventArgs e) String s, a; s = Convert.ToString(textBox1.Text); a = s.ToLower();textBox1.Text = a.ToString(); 习题六 private void button1_Click(object sender, EventArgs e) string a; a = Convert.ToString(textBox1.Text); MessageBox.Show(Convert.ToString(a)+,欢迎进入C#的世界!, 欢迎进入C#的世界!, MessageBoxButtons.OK, MessageBoxIcon.Information); 习题三 private void button1_Click(object sender, EventArgs e) int a, b, c, d, x; x = Convert.ToInt32(textBox1.Text); a = x/1000; b = x/100-a*10; c = x/10-a*100-b*10; d = x%10; textBox2.Text=Convert.ToString(a); textBox3.Text= Convert.ToString(b); textBox4.Text=Convert.ToString(c); textBox5.Text=Convert.ToString(d); 习题四银行贷款的月还额 private void button1_Click(object sender, EventArgs e) double p, r, a, b; int n; p = Convert.ToDouble(textBox1.Text); r = Convert.ToDouble(textBox2.Text); n = Convert.ToInt32(textBox3.Text); b = Math.Pow(1 + r), n); a = p * r * b / (b - 1); textBox4.Text = a.ToString(.#); 习题五、随机数 private void button1_Click(object sender, EventArgs e) Random intra = new Random(); int a=intra.Next(1, 101); char b = (char)a; textBox1.Text = Convert.ToString(a); textBox2.Text = Convert.ToString(b); 第三章 习题二是否是闰年 private void button1_Click(object sender, EventArgs e) int a; a=Convert.ToInt32(textBox1.Text); if (a % 4 = 0 & a % 100 != 0 | a % 400 = 0) label2.Text = 闰年; else label2.Text = 不是闰年; 习题三一元二次方程组 private void button1_Click(object sender, EventArgs e) int a, b, c;double x; a = Convert.ToInt32(textBox1.Text); b = Convert.ToInt32(textBox2.Text); c = Convert.ToInt32(textBox3.Text); x=Math.Pow(b,2); if (a = 0) abel4.Text = 不是一元二次方程; else if (x - 4 * a * c = 0) label4.Text = 有两个相等的实根; else if (x - 4 * a * c 0) label4.Text = 有两个不等的实根; else label4.Text = 无解; 习题四 private void button1_Click(object sender, EventArgs e) double a, b, c, m, s; a = Convert.ToDouble(textBox1.Text); b = Convert.ToDouble(textBox2.Text); c = Convert.ToDouble(textBox3.Text); m = (a + b + c) / 2; if (a 0 & b 0 & c 0) if (a + b c & a + c b &b + c a) s = Math.Sqrt(m * (m - a) * (m - b) * (m - c); label4.Text = 三角形面积为 + s; else MessageBox.Show(输入错误, 提示, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); label4.Text = ; else MessageBox.Show(输入错误, 提示, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); label4.Text = ; 习题一、 private void button1_Click(object sender, EventArgs e) int a, b, c, max, min; a = Convert.ToInt32(textBox1.Text); b = Convert.ToInt32(textBox2.Text); c = Convert.ToInt32(textBox3.Text); max = (a b ? a : b) c ? (a b ? a : b) : c; min = (a b ? a : b) c ? (a b ? a : b) : c; b = a + b + c - max - min; label5.Text = max+,+b+,+min; 第4章 习题三private void button1_Click(object sender, EventArgs e) int s=0; for(int i=1;i=100;i+) for (int j = 0; j = i; j+) s = s + j; textBox1.Text = Convert.ToString(s); private void button1_Click(object sender, EventArgs e) int s = 0,u=0; for (int i = 1; i = 100; i+) u=u+i-1; s = s+u+i; textBox1.Text = Convert.ToString(s); 习题五、3和7的倍数private void button1_Click(object sender, EventArgs e) for (int i = 1; i = 100; i+) if (i % 3 = 0 & i % 7 = 0) textBox1.Text = textBox1.Text + rn + Convert.ToString(i); 习题一 private void button1_Click(object sender, EventArgs e) int s=0; for (int i = 1; i = 100; i+) s = s + i; textBox1.Text = Convert.ToString(s) 第五章习题六 private void button1_Click(object sender, EventArgs e) int a; int i, n; n = Convert.ToInt32(textBox1.Text); BestPerson f = new BestPerson(); f.getBestPosition(n, out a); for (i = 0; i n; i+) textBox3.Text = textBox3.Text + Convert.ToString(ai); public partial class BestPerson public void getBestPosition(int a, out int s) s = new int50; int i, j = -1, k; for (i = 0; i a; i+) si = 0; for (i = 1; i 0 & a 0) if (n = 1) return (1); else return (n * fac(n - 1); else return 1; private void button1_Click(object sender, EventArgs e) int n, s; n = Convert.ToInt32(textBox1.Text); s = fac(n); textBox2.Text = Convert.ToString(s); 第六章 习题七 private void button1_Click(object sender, EventArgs e) int i; party p = new party3; party p1 = new party(); communistParty p2 = new communistParty(); kuomintang p3 = new kuomintang(); p0 = p1; p1 = p2; p2 = p3; for (i = 0; i 3; i+) pi.sayPolicy(); public class party public virtual void sayPolicy() MessageBox.Show(两岸同属于一个中国。); public class communistParty :party public override void sayPolicy() MessageBox.Show(两岸同属于一个中国,用一国两制统一中国。); public class kuomintang :party public override void sayPolicy() MessageBox.Show(两岸同属于一个中国,用三民主义统一中国。); private void button1_Click(object sender, EventArgs e) Cat c = new Cat(); = textBox1.Text; Mouse m = new Mouse(); = textBox2.Text; c.getUp +=new sayGetUpDelegate(m.sayGetUp); c.crow();习题八 public delegate void sayGetUpDelegate(); public class Cat public event sayGetUpDelegate getUp; public string name; public void crow() MessageBox.Show(name+喵喵喵喵喵喵); getUp(); public class Mouse public string name; public void sayGetUp () MessageBox.Show(name+快跑!); 习题九private void button1_Click(object sender, EventArgs e) train t = new train(); = textBox1.Text; cat c = new cat(); = textBox2.Text; c.h = Convert.ToInt16(textBox3.Text); t.nshow(); c.nshow(); private void button2_Click(object sender, EventArgs e) train t = new train(); cat c = new cat(); t.shout(); c.shout();public interface makeVoice int hget;set; void shout(); public abstract class Vehicle public string name; public abstract void nshow(); public abstract class Anima public string name; public abstract void nshow(); public class train:Vehicle ,makeVoice int H; public int h get return this.H; set this.H = value; public override void nshow()MessageBox.Show(train的名字是+name); public void shout()MessageBox.Show(train +呜呜嗒嗒。呜呜嗒嗒); public class cat:Animal ,makeVoice int H; public int h get return this.H; set this.H = value; public override void nshow() MessageBox.Show(cat的名字是 + name+身高是+h); public void shout() MessageBox.Show(cat +喵喵喵。喵喵喵);第七章 习题一随机产生10个两位整数,找出其中最大值,最小值、高于平均数的数 private void button1_Click(object sender, EventArgs e) int sum = 0; int z; Random ra = new Random(); int a = new int10; for (int i = 0; i a.Length; i+) ai = ra.Next(10, 100); for (int i = 0; i a.Length; i+) textBox1.Text += Convert.ToString(ai+ ) ; for (int i = 0; i a.Length; i+) sum += ai ; z = sum / 10; textBox4.Text = Convert.ToString(z) for (int i = 0; i = z) textBox5.Text += Convert.ToString(ai + ); Array.Sort(a); textBox2.Text = Convert.ToString(a9); textBox3.Text = Convert.ToString(a0); 产生10个学生的成绩,并用冒泡进行排序private void button1_Click(object sender, EventArgs e) Random ra = new Random(); int a = new int10; for (int i = 0; i a.Length; i+) ai = ra.Next(1, 100); textBox1.Text += Convert.ToString(ai + ); for (int i = 0; i a.Length-1; i+) for (int j = i + 1; j a.Length; j+) if (ai aj) int t; t = ai; ai = aj; aj = t; for (int i = 0; i a.Length; i+) textBox2.Text += Convert.ToString(ai + ); 交错数组 private void button1_Click(object sender, EventArgs e) int jcArray = new int4; jcArray0 = new int 1, 32, 89, 6, 8 ; jcArray1 = new int 2,17,8 ; jcArray2 = new int 3,5; jcArray3 = new int 4,9,23,12 ; textBox1.Text = 交错数组的数据: + rn; foreach (int j in jcArray) int x = 0; foreach (int i in j) x += i; textBox1.Text += Convert.ToString(i) + t; textBox1.Text += rn+该行数据总和为:+Convert.ToString(x)+rn; 用二维数组存储学生成绩private void button1_Click(object sender, EventArgs e) int, cj = 1001, 93 , 1004, 78 , 1002, 59 , 1005, 87 , 2001, 55 ; textBox1.Text = 学生成绩; for(int i=0;icj .GetLength(0);i+) for (int j = 0; j cj.GetLength(1); j+) if (j = 1) textBox1.Text = textBox1.Text + t + Convert.ToString(cji, j); else textBox1.Text = textBox1.Text + rn + Convert.ToString(cji, j); textBox1.Text = textBox1.Text + rn + 不及格的学生; for (int i = 0; i cj.GetLength(0); i+) if (cji, 1 60) textBox1.Text = textBox1.Text + rn + Convert.ToString(cji, 0)+ t+Convert .ToString(cji,1) textBox1.Text+=rn+平均成绩; int sum = 0; int z; ; for (int i=0;icj.GetLength(0);i+) sum += cji, 1; z = sum / cj.GetLength(0); textBox1.Text += Convert.ToString(z); 第十章简易文本编辑器private void button1_Click(object sender, EventArgs e) richTextBox1.Text = ; private void button2_Click(object sender, EventArgs e) openFileDialog1.FileName=; openFileDialog1.ShowReadOnly=true; openFileDialog1.ReadOnlyChecked =true; openFileDialog1.Filter = *.text|*.text|RTF文件(*.rtf)|*.rtf; if (openFileDialog1.ShowDialog() = DialogResult.OK) richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText); private void button3_Click(object sender, EventArgs e) saveFileDialog1.InitialDirectory = f:; saveFileDialog1.Filter = *.text|*.text|RTF文件(*.rtf)*.rtf; if (saveFileDialog1.ShowDialog() = DialogResult.OK) richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText); private void button4_Click(object sender, EventArgs e) fontDialog1.ShowApply = true; if (fontDialog1.ShowDialog() = DialogResult.OK) richTextBox1.Font = fontDialog1.Font; private void button5_Click(object sender, EventArgs e) colorDialog1.ShowHelp = true; if (colorDialog1.ShowDialog() = DialogResult.OK) richTextBox1.SelectionColor = colorDialog1.Color; private void Form1_Load(object sender, EventArgs e) 第十一章通讯录(数据库) OleDbConnection mConn = new OleDbConnection(); OleDbDataAdapter mAdapter; OleDbCommandBuilder mBuilder; DataTable mDatatable = new DataTable(); int mRowposition = 0; private void showRecord() if (mDatatable.Rows.Count = 0) textBox1.Text = ; textBox2.Text = ; textBox3.Text = ; textBox4.Text = ; textBox5.Text = ; textBox1.Text = mDatatable.RowsmRowposition编号1.ToString(); textBox2.Text = mDatatable.RowsmRowposition姓名.ToString(); textBox3.Text = mDatatable.RowsmRowposition性别.ToString(); textBox4.Text = mDatatable.RowsmRowposition籍贯.ToString(); textBox5.Text = mDatatable.RowsmRowposition电话.ToString(); public Form1() InitializeComponent(); private void label1_Click(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e) mConn.ConnectionString = Provider =Microsoft.Jet.OLEDB.4.0;Data Source =F:新建文件夹1208150129林相玲(12)通讯录.mdb; mConn.Open(); mAdapter = new OleDbDataAdapter(select *

温馨提示

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

评论

0/150

提交评论