C#-Visual-Studio-2008实验报告附源代码
C# Visual Studio 2008实验报告附源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
1、创建C#控制台应用程序。设计一个简单的密码验证程序,若密码正确,则显示“欢迎进入本系统!”,否则显示“密码输入错误,请重新输入!”。若连续三次密码输入错误,则显示“对不起,超过最多输入次数,取消服务!”,程序退出。
namespace ConsoleApplication1
{ class Program
{  static void Main(string[] args)
{  int i = 0;
string mima = "123321";
bool k = true;
Console.WriteLine("    ");
Console.WriteLine("                        》》》》欢迎使用本系统《《《《\n\n");
Console.WriteLine(" 请输入您的服务密码: ");
while (k)
{  string get = Console.ReadLine();
if (get != mima)
{  i++;
if (i == 3)
{ Console.WriteLine("对不起,输入的密码错误次数超过三次,\n\n已取消服务,请按任意键结束!!");
Console.ReadLine();
break;
}
else Console.WriteLine("对不起,密码有误,已经输入{0}次,请重新输入!",i );
}
else
{ Console.WriteLine("欢迎进入本系统!!");
Console.ReadLine(); break;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
4.定义一个shape抽象类,利用它作为基类派生出Rectangle、Circle等具体形状类,已知具体形状类均具有两个方法GetArea和GetPerim,分别用来求形状的面积和周长。最后编写一个测试程序对产生的类的功能进行验证。
namespace shiyan14
{  public abstract class Shape
{  public double GetArea()visual studio代码大全
{  return 0;
}
public double GetPerim()
{  return 0;
}
}
public class Circle:Shape
{  private double r;
public Circle(double a)
{  r=a;
}
}
}
}
创建一个点Point类,属性包括横坐标、纵坐标。要求能够完成点的移动操作、求两点距离操作,并利用运算符重载,对两个点进行比较(相等和不等)依据是两坐标点相等指它们横坐标和纵坐标分别相等。编写一个测试程序对产生的类的功能进行验证。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1_2
{  class point
{  public double x, y;
public point(double a, double b)
{  x = a; y = b;
}
public void move(double a, double b)
{  x = x + a;
y = y + b;
}
public static bool operator ==(point a, point b)
{  if ((a.x == b.x) && (a.y == b.y))
return true;
else return false;
}
public static bool operator !=(point a, point b)
{  if ((a.x != b.x) || (a.y != b.y))
return true;
else
return false;
}
public double distance(point a, point b)
{return Math.Sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
}
class Program
{  static void Main()
{  point a = new point(1, 1);
point b = new point(2, 2);
Console.WriteLine("a点的坐标:({0},{1})", a.x, a.y);
Console.WriteLine("b点的坐标:({0},{1})", b.x, b.y);
Console.WriteLine("对a坐标移动2和3,按enter确认!");
Console.ReadLine();
Console.WriteLine("移动后a点得坐标是:({0},{1})", a.x, a.y);
Console.WriteLine("a坐标移动后与b坐标距离是:{0}", a.distance(a, b));
if (a == b)
Console.WriteLine("a点和b点相等\n");
else
Console.WriteLine("a点和b点不相等\n");
Console.WriteLine("对b坐标移动3和4,按enter确认!");
Console.ReadLine();
Console.WriteLine("移后b点坐标:({0},{1})", b.x, b.y);
if (a == b)
Console.WriteLine("a点和b点相等");
else
Console.WriteLine("a点和b点不相等");
Console.ReadLine();
}
}
}