public void show2() //输出相关信息 {
double s; base.show1();
Console.WriteLine(\周长:{0}\\n面积:{1}\ }
}
static void Main(string[] args) {
clsRec rec = new clsRec(2,3,4,6); //定义矩形对象 rec.show2(); //输出结果 } }
(复习重点)P106:5-13、设计一个项目,由程序随机产生12个数,并把12 个数按从大到小的顺序进行输出。(考察随机数与冒泡法排序) static void Main(string[] args) {
int[] a = new int[12]; //定义长度为12 的数组 int b; //b为暂存数 Random ran = new Random(); //定义随机数
Console.WriteLine(\未排序的数据:\ for (int j = 0; j < 12; j++) {
a[j] = (int)ran.Next(100) ; //给数组赋值 随机产生在0到99之间的整数 Console.Write(\ }
Console.WriteLine(\排序后的数据:\ for (int j = 0; j < 12; j++) //冒泡法排序 {
for (int i=0; i < 12-j-1; i++) //每次比较length-j-1次 比较范围内一个最大值放在最后,下一轮的长度是此轮长度减一 {
if (a[i] > a[i + 1]) {
b=a[i];
a[i] = a[i + 1];
a[i + 1] = b; } } }
foreach (int n in a) //用foreach 输出数据 {Console.WriteLine(\ }
P116:6-4、定义一个抽象类Cshape,包含抽象方法Area()(用来计算面积)和SetDate()(用来重设形状大小)。然后派生出三角形CTriangle类、矩形CRect类、圆形CCircle类,分别求其面积。最后定义一个CArea类,计算这几个形状的面积之和,各形状的数据通过CArea类构造函数或成员函数来设置。编写一个完整的程序。
public abstract class CShape
{
public abstract double Area();
public abstract void SetDate(params double[] d); }
public class Ctringle : CShape {
double a, b, c;//三角型三条边属性 public Ctringle() { }
public Ctringle(double a, double b, double c)//构造函数 {
this.a = a; this.b = b; this.c = c; }
public override void SetDate(params double[] d) {
f1: if (d[0] + d[1] > d[2] && d[1] + d[2] > d[0] && d[0] + d[2] > d[1])//判断三边是否构成三角形 {
this.a = d[0]; this.b = d[1]; this.c = d[2]; } else {
Console.WriteLine(\边长不能构成三角形,请重新设置!\ Console.WriteLine(\请输入三角形的三边边长\ for (int i = 0; i < 3; i++)
d[i] =double.Parse( Console.ReadLine());
goto f1;//对新设的三边回到判断 } }
public override double Area() {
double s;
double p=(this.a+this.b+this.c)/2.0;
s = System.Math.Sqrt(p*(p-this.a)*(p-this.b)*(p-this.c)); return s; } }
class CRect : CShape {
double a, b;
public CRect() { }
public CRect(double a,double b) {
this.a = a; this.b = b; }
public override void SetDate(params double[] d) {
this.a = d[0]; this.b = d[1]; }
public override double Area() {
return a * b; } }
class CCircle : CShape {
double r;
public CCircle() { } public CCircle(double r) {
this.r = r; }
public override void SetDate(params double[] d) {
this.r = d[0]; }
public override double Area() {
return r * r * 3.141; } }
class CArea {
CShape a, a1, a2;
public CArea() { }
public CArea(double ct1, double ct2, double ct3, double cr1, double cr2, double cc1)//构造函数 {
a = new Ctringle(ct1,ct2,ct3); a1 = new CRect(cr1,cr2); a2 = new CCircle(cc1); }
public double Alarea()//计算三种形状的面积和 {
return a.Area() + a1.Area() + a2.Area(); }
}
class Program {
static void Main(string[] args) {
double als=0;//计算总面积 CShape a;
a = new Ctringle(3,4,5);
als +=a.Area();//加上三角形面积 Console.WriteLine(\三角形面积:{0:f3}\输出地结果均保留3位小数 a.SetDate(3,4,5);
Console.WriteLine(\三角形面积:{0:f3}\
a = new CRect(4,5);
als+=a.Area();//加上矩形面积
Console.WriteLine(\矩形面积:{0:f3}\ a.SetDate(4, 6);
Console.WriteLine(\矩形面积:{0:f3}\
a = new CCircle(3);
als +=a.Area();//加上圆形面积
Console.WriteLine(\圆面积:{0:f3}\ a.SetDate(4);
Console.WriteLine(\圆面积:{0:f3}\
Console.WriteLine(\三种形状总面积:{0:f3}\
CArea c1=new CArea(3,4,5,4,5,3);
Console.WriteLine(\三种形状总面积:{0:f3}\
} }
(复习重点)P148:7-1、 输入一个字符串,统计其中有多少个单词?单词之间用空格分隔开。(加大难度 对单词进行排序与除重复) static void Main() {
string str;
string[] words = new string[100]; //用来存放分隔出来的数组 string[] newwords = new string[100];//用来存放无重复的数组 str = Console.ReadLine(); //输入字符串 string temb; //寄存字符串,用于排序 int n=0; //计数
words = str.Split(' '); //分隔字符串,用空格分离
for (int i = 0; i < words.Length; i++)//排序 for(int j=0;j if((string.Compare(words[j],words[j+1]))==1) { temb=words[j]; words[j]=words[j+1]; words[j+1]=temb; } } Console.WriteLine(\排序后字符串:\ for (int i=0; i < words.Length; i++) { Console.WriteLine(\ } newwords[0] = words[0];//将排序后字符串首个赋给不重复 for (int i = 0; i < words.Length-1; i++) //除重复 { if ((newwords[n].CompareTo(words[i])) != 0) //如果不相同,字符串赋值给下个单元 { n++; newwords[n] = words[i]; } } Console.WriteLine(\除去重复的字符串:\ for (int i = 0; i < n+1; i++) { Console.WriteLine(\ } }