资源描述
第一章
1.编写一个C# Windows应用程序,程序的设计界面如图所示。程序运行时单击【退出】按钮将结束应用程序的执行。
图
namespace D_1_1
static 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("{0}{1}{2}={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;i<10;i++)
Console.Write("{0} ",a[i]);
i=0;j=9;
while(i<j)
{ t=a[i];a[i]=a[j];a[j]=t;
i++;j--;
}
Console.WriteLine("\n反序之后:");
for(i=0;i<10;i++)
Console.Write("{0} ",a[i]);
}
}
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;i<M;i++) /*通过random 函数产生一个数组*/
for(j=0;j<N;j++)
a[i,j]=randObj.Next(1,100);//产生随机数并赋值给数组
for(i=0;i<M;i++) /*按行输出数组*/
{ Console.WriteLine();//换行
for(j=0;j<N;j++)//本循环输出一行元素
Console.Write("{0} ",a[i,j]);
}
for(i=0;i<M;i++)
for(j=0;j<N;j++)
{ if (a[i,j]%2==1) jishu=jishu+a[i,j];
else oushu=oushu+a[i,j];
}
Console.WriteLine("\n奇数和为{0},偶数和为{1}",jishu,oushu);
}
}
第五章
1.编写一个递归方法求下式的和s,n的值由用户输入。
s(n)=1²+2²+3²+……+n²
long 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;i<k;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’ →’c’
‘A’ →’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<SStr.Length ;i++)
{ c=SStr[i];
if (c>='a' && c<='z' ||c>='A' && c<='Z')
{
c=(char)(SStr[i]+3);
if (c>'z' && c<='z'+3|| c>'Z' && 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;i<SStr1.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_1
{
public class vehicle
{
public 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:vehicle
{
public 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_2
{
public class Person
{
public 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 double[5];
}
public Student()
{ Name="";
Age=18;
Sex='M';
Score=new double[5];
}
public Student(string xm,int nl,char xb,double []cj )
{
Name=xm;
Age=nl;
Sex=xb;
Score=new double[5];
for(int i=0;i<5;i++)
Score[i]=cj[i];
}
public double CalAver()
{ double sum=0;
for(int i=0;i<5;i++)
sum=sum+Score[i];
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;i<5;i++)
{
cj[i]=randomObj.Next(0,101);
textBox4.Text =textBox4.Text +cj[i].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;i<N;i++)
a[i]=randObj.Next(10,99);/*产生随机数并赋值给数组元素*/
textBox1.Text="";
for(i=0;i<N;i++)//输出整个数组
textBox1.Text +=a[i].ToString()+",";
}
private void button2_Click(object sender, System.EventArgs e)
{ int i,j,t;//定义循环变量和交换用的临时变量
try
{
for(i=1;i<=N;i++)/*i表示轮次*/
for(j=0;j<=N-i;j++) /*j表示每轮比较的次数*/
if(a[j]>a[j+1])/*如果后面的元素值小,则交换*/
{t=a[j];a[j]=a[j+1];a[j+1]=t;}
textBox2.Text ="";
for(i=0;i<N;i++)//输出整个数组
textBox2.Text +=a[i].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;
try
{
num1=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);
try
{
switch(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)
{
}
展开阅读全文