




已阅读5页,还剩21页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第一章1编写一个C# Windows应用程序,程序的设计界面如图所示。程序运行时单击【退出】按钮将结束应用程序的执行。图namespace D_1_1static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e) Application.Exit();2.找出下列程序中的错误,并用Visual Studio .NET中的命令行编译器CS.EXE.进行编译。using System;class Example1 Public Static Void main() string a; a=System.Console.ReadLine(); System.Console. Writeline(a=1,a); class Example1 public static void Main() string a; a=System.Console.ReadLine(); System.Console.WriteLine(a=0,a); 第二章1.编写一个程序,从键盘上输入3个数,输出这三个数的积及它们的和。要求编写成控制台应用程序。class Test2_1 public static void Main() int a,b,c,sum,mul; a=Convert.ToInt32(Console.ReadLine(); b=Convert.ToInt32(Console.ReadLine(); c=Convert.ToInt32(Console.ReadLine(); sum=a+b+c; mul=a*b*c; Console.WriteLine(Sum=0,Mul=1,sum,mul); 2.编写一个程序,输入梯形的上底、下底和高,输出梯形的面积。要求编写成Windows应用程序,程序的运行界面如图所示图static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)double a,b,h,s;a=Convert.ToSingle(textBox1.Text );/输入上底b=Convert.ToSingle(textBox2.Text );/输入下底h=Convert.ToSingle(textBox3.Text );/输入下底s=1.0/2.0*(a+b)*h;/求面积;textBox4.Text =Convert.ToString(s);/显示面积第三章1.编写一个进行加、减、乘、除四则运算的程序,要求:输入两个单精度数,然后输入一个运算符号,输出两个单精度数进行该运算后的结果。要求编写为控制台程序。using System;class D_3_1 public static void Main() double op1,op2,result=0; char kind; op1=Convert.ToDouble(Console.ReadLine(); op2=Convert.ToDouble(Console.ReadLine(); kind=(char)Console.Read(); switch(kind) case +:result=op1+op2;break; case -:result=op1-op2;break; case *:result=op1*op2;break; case /:result=op1/op2;break; Console.WriteLine(012=3,op1,kind,op2,result); 2.兔子繁殖问题。设有一对新生的兔子,从第三个月开始它们每个月都生一对兔子,新生的兔子从第三个月开始又每个月生一对兔子。按此规律,并假定兔子没有死亡,20个月后共有多少兔子?要求编写为控制台应用程序。using System;class D_3_2 public static void Main() int a,b,c,i; a=1;b=1; Console.Write(0 1 ,a,b); for(i=3;i=20;i+) c=a+b; Console.Write(0 ,c); a=b; b=c; if(i%5=0)Console.WriteLine(); 第四章1.编写程序,把由10个元素组成的一维数组逆序存放再输出。using System;class D_4_1 public static void Main() int a=new int 1,2,3,4,5,6,7,8,9,10;/定义数组并初始化 int i,j,t; Console.WriteLine(反序之前:); for(i=0;i10;i+) Console.Write(0 ,ai); i=0;j=9; while(ij) t=ai;ai=aj;aj=t; i+;j-; Console.WriteLine(n反序之后:); for(i=0;i10;i+) Console.Write(0 ,ai); 2.编写程序,统计4*5二维数组中奇数的个数和偶数的个数。using System;class D_4_2 public static void Main() const int M=2; const int N=2; int ,a=new int M,N; int i,j,jishu,oushu; jishu=0;oushu=0; Random randObj=new Random();/生成随机数变量 for(i=0;iM;i+) /*通过random 函数产生一个数组*/ for(j=0;jN;j+) ai,j=randObj.Next(1,100);/产生随机数并赋值给数组 for(i=0;iM;i+) /*按行输出数组*/ Console.WriteLine();/换行 for(j=0;jN;j+)/本循环输出一行元素 Console.Write(0 ,ai,j); for(i=0;iM;i+) for(j=0;jN;j+) if (ai,j%2=1) jishu=jishu+ai,j; else oushu=oushu+ai,j; Console.WriteLine(n奇数和为0,偶数和为1,jishu,oushu); 第五章1.编写一个递归方法求下式的和s,n的值由用户输入。 s(n)=1+2+3+nlong f(int n) if (n=1) return(1); else return(f(n-1)+n*n);static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e) int k;long result;k=Convert.ToInt32(textBox1.Text ); result=f(k);textBox2.Text =Convert.ToString(result);private void button2_Click(object sender, System.EventArgs e)Application.Exit();2.编写一个求整数任意位数字的过程,过程的调用形式为:digit(n,k),其功能是取出数n从右边起的第k位数字,例如:digit(1234,3)=2,digit(1234,4)=1,digit(1234,6)=0.protected override void Dispose( bool disposing )if( disposing )if (components != null) components.Dispose();base.Dispose( disposing );int digital(long n,int k ) int i;for(i=1;ik;i+)n=n/10;return(int)n%10);static void Main() Application.Run(new Form1();private void button2_Click(object sender, System.EventArgs e)Application.Exit();private void button1_Click(object sender, System.EventArgs e) long num;int m,w;num=Convert.ToInt64(textBox1.Text );m=Convert.ToInt32(textBox2.Text );w=digital(num,m);textBox3.Text =Convert.ToString (w);第六章1.编写一个应用程序用来对输入的字符串进行加密,对于字母字符加密规则如下:a d b e w z x a y b z cA D B E W Z X A Y B Z C对于其他字符,不进行加密。程序的运行界面如图所示。图static void Main() Application.Run(new Form1();private void Form1_Load(object sender, System.EventArgs e)private void button1_Click(object sender, System.EventArgs e)string SStr,DStr;int i;char c;SStr=textBox1.Text ;DStr=;for(i=0;i=a & c=A & cz & cZ & c=Z+3) c=(char)(c-26); DStr=DStr+c.ToString() ;textBox2.Text =DStr;private void button2_Click(object sender, System.EventArgs e)Application.Exit();2.编写一个应用程序用来从一个字符串中删除一个字符,程序的界面如图所示图static void Main() Application.Run(new Form1();private void label3_Click(object sender, System.EventArgs e)private void button1_Click(object sender, System.EventArgs e)string SStr1,DStr,delstr;int i;SStr1=textBox1.Text ;delstr=textBox2.Text ;DStr=;for(i=0;iSStr1.Length ;i+)if (SStr1.Substring(i,1)!=delstr)DStr=DStr+SStr1.Substring(i,1);textBox3.Text =DStr;private void button2_Click(object sender, System.EventArgs e)Application.Exit();第七章1. 定义一个车辆(Vehicle)基类,具有Run、Stop等方法, 具有Speed(速度)、MaxSpeed(最大速度)、Weight(重量)等域。然后以该类为基类,派生出bicycle、car等类。并编程对派生类的功能进行验证。using System;namespace D_7_1public class vehiclepublic double Speed;public double MaxSpeed;public double Weight;public virtual string Run()return(一辆车在马路上奔跑。);public virtual string Stop()return(一辆车在马路上停了下来。);public class bicycle:vehicle public double Price;public bicycle(double S,double M,double W,double P)Speed=S;MaxSpeed=M;Weight=W;Price=P;public override string Run() return(一辆自行车在马路上奔跑。) ;public override string Stop()return(一辆自行车在马路上停了下来。);public class car:vehiclepublic double Price;public override string Run()return(一辆小汽车在马路上奔跑。); public override string Stop()return(一辆小汽车在马路上停了下来。);public car(double S,double M,double W,double P)Speed=S;MaxSpeed=M;Weight=W;Price=P;static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)vehicle v=new vehicle();label1.Text =v.Run()+ +v.Stop();bicycle bike=new bicycle(50,100,50,285);label2.Text =bike.Run()+ 速度:+bike.Speed.ToString() + +bike.Stop ();car Car=new car(100,250,300,150000.0);label3.Text =Car.Run()+ 速度:+Car.Speed.ToString() + +Car.Stop ();2.编写出一个通用的人员类(Person),该类具有姓名(Name),年龄(Age),性别(Sex)等域。然后通过对Person类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类构造函数进行重载,至少给出三个形式。最后编程对Student类的功能进行验证。using System;namespace D_7_2public class Personpublic string Name;public int Age;public char Sex;public class Student:Person public double Score;public double Aver;public Student(string xm,int nl,char xb) Name=xm;Age=nl;Sex=xb;Score=new double5;public Student() Name=;Age=18;Sex=M;Score=new double5;public Student(string xm,int nl,char xb,double cj )Name=xm;Age=nl;Sex=xb;Score=new double5;for(int i=0;i5;i+)Scorei=cji;public double CalAver() double sum=0;for(int i=0;i5;i+)sum=sum+Scorei;Aver=sum/5;return(Aver);static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e) Random randomObj=new Random();textBox4.Text =;for(int i=0;i5;i+)cji=randomObj.Next(0,101);textBox4.Text =textBox4.Text +cji.ToString()+,;private void button2_Click(object sender, System.EventArgs e)string xm=textBox1.Text ;int nl=Convert.ToInt32(textBox2.Text );char xb=Convert.ToChar(textBox3.Text ); Student S1=new Student(xm,nl,xb,cj);S1.CalAver();textBox5.Text =S1.Aver.ToString(); 第八章1.编写一个冒泡法排序程序。要求在程序中能够捕获到数组下标越界的异常。static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)int i;Random randObj=new Random();/生成随机数变量for(i=0;iN;i+)ai=randObj.Next(10,99);/*产生随机数并赋值给数组元素*/textBox1.Text=;for(i=0;iN;i+)/输出整个数组textBox1.Text +=ai.ToString()+,;private void button2_Click(object sender, System.EventArgs e) int i,j,t;/定义循环变量和交换用的临时变量tryfor(i=1;i=N;i+)/*i表示轮次*/for(j=0;jaj+1)/*如果后面的元素值小,则交换*/t=aj;aj=aj+1;aj+1=t;textBox2.Text =; for(i=0;iN;i+)/输出整个数组textBox2.Text +=ai.ToString()+,;catch(IndexOutOfRangeException ex) textBox2.Text =数组下标越界;2、编写一个计算机应用程序,要求在程序中能够捕获到被0除的异常与算术运算溢出的异常。static void Main() Application.Run(new Form1();private void button4_Click(object sender, System.EventArgs e)op=4;label1.Text=/;private void button1_Click(object sender, System.EventArgs e)op=1;label1.Text=+;private void button2_Click(object sender, System.EventArgs e)op=2;label1.Text=-;private void button3_Click(object sender, System.EventArgs e)op=3;label1.Text=*;private void button5_Click(object sender, System.EventArgs e)int num1,num2,resu=0;trynum1=Convert.ToInt32(textBox1.Text );num2=Convert.ToInt32(textBox2.Text );switch(op)case 1:resu=num1+num2;break;case 2:resu=num1-num2;break;case 3:resu=num1*num2;break;case 4:resu=num1/num2;break;textBox3.Text =resu.ToString();catch (DivideByZeroException)textBox3.Text=除数不能为零!;catch(OverflowException)textBox3.Text =算术运算溢出!;第九章1.制作一个简单计算器程序。程序运行时通过按钮输入运行公式,如图,此时单击【计算】按钮将得到计算结果。static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button2_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button3_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button11_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button4_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button5_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button6_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button7_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button8_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button9_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button10_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button12_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button13_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button14_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button15_Click(object sender, System.EventArgs e)double r;string t=textBox1.Text ;int space=t.IndexOf( );string s1=t.Substring(0,space);char op=Convert.ToChar(t.Substring(space+1,1);string s2=t.Substring(space+3);double arg1=Convert.ToDouble(s1);double arg2=Convert.ToDouble(s2);tryswitch(op)case +:r=arg1+arg2;break;case -:r=arg1-arg2;break;case *:r=arg1*arg2;break;case /:r=arg1/arg2;break;default:throw new ApplicationException();textBox1.Text =r.ToString() ;catch(DivideByZeroException)MessageBox.Show(被零除!,被零除对话框);catch(Exception) MessageBox.Show (运算符不正确!);private void button16_Click(object sender, System.EventArgs e)textBox1.Text =;private void textBox1_TextChanged(object sender, System.EventArgs e)2、编写一个对列表框进行列表项添加,修改和删除操作的应用程序。程序的运行界面如图,列表框(名为lstbooks)中的列表项在Froml load事件中加载,【添加】按钮(cmdadd)的功能是将文本框(txtittem)中的内容添加在列表框中,【删除】按钮(cmddelete)的功能是删除列表框中选定的列表项,如果要修改列表项,可先选定列表项,然后单击【修改】(cmdmodify)按钮,所选的项目显示在文本框中,当在文本框中修改完之后,单击确定修改按钮将更新列表框,初始,确定修改不可使用static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button2_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button3_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button11_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button4_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button5_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button6_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button7_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button8_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button9_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button10_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += btn.Text ;private void button12_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button13_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button14_Click(object sender, System.EventArgs e)Button btn=(Button)sender;textBox1.Text += + btn.Text+ ;private void button15_Click(object sender, System.EventArgs e)double r;string t=textBox1.Text ;int space=t.IndexOf( );string s1=t.Substring(0,space);char op=Convert.ToChar(t.Substring(space+1,1);string s2=t.Substring(space+3);double arg1=Convert.ToDouble(s1);double arg2=Convert.ToDouble(s2);tryswitch(op)case +:r=arg1+arg2;break;case -:r=arg1-arg2;break;case *:r=arg1*arg2;break;case /:r=arg1/arg2;break;default:throw new ApplicationException();textBox1.Text =r.ToString() ;catch(DivideByZeroException)MessageBox.Show(被零除!,被零除对话框);catch(Exception) MessageBox.Show (运算符不正确!);private void button16_Click(object
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全培训的目的及意义
- 家庭教育课件
- 家庭成员介绍课件
- 家庭急救心肺复苏课件
- 家庭安检员课件
- 安全培训的微电影课件
- 2025药品管理法考试试题及参考答案
- 2025年度团险渠道反洗钱及消费者权益保护培训考试题及答案
- 家安全用电培训课件
- 广西专业技术人员继续教育公需科目考试试题及答案
- 邮政行业痛点与解决措施
- 二年级《劳动最光荣》课件
- 帕夫雷什中学
- 2023年人教版美术六年级上册全册教案
- 道路交通安全法知识试题库完整
- 《铁路交通事故调查处理规则》解读
- 研究生学术行为规范讲座
- 水资源论证水土保持防洪评价收费标准
- 年处理12万吨煤焦油加工工艺初步设计
- 烟化炉车间技术操作规程-附一:烟化炉开炉、停炉、故障处理及正常操作原则
- YB 4094-1993炮弹用方钢(坯)超声波探伤方法
评论
0/150
提交评论