Question
编写程序完成以下内容
1.创建一个类库项目Mylib,定义一个Calculation类,为该类添加三个求面积的重载方法Area(),分别计算圆,长方体,三角形的面积。定义一个私有静态字段count,用于统计Calculation类中Area()方法调用的次数,并为其定义一个共有的只读属性Count,用于获取Area()方法被调用的次数
2.新建一个控制台项目,引用上面的类库项目。在Main主方法中调用CAculation类中的Area()方法,分别求圆、长方形、三角形的面积,并输入Area()方法被调用的次数。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mylib
{
class Program
{
const double par = 0.5;
static void Main(string[] args)
{
double r, x, y, b, h;
Calculation c1 = new Calculation();
Calculation c2 = new Calculation();
Calculation c3 = new Calculation();
Console.WriteLine("输入圆半径:");
r = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("圆面积:"+c1.Area(r));
Console.WriteLine("输入长方形的长和宽:");
x= Convert.ToDouble(Console.ReadLine());
y= Convert.ToDouble(Console.ReadLine());
Console.WriteLine("长方形面积::" + c2.Area(x,y));
Console.WriteLine("输入三角形的底和高:");
b = Convert.ToDouble(Console.ReadLine());
h = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("三角形面积:" + c2.Area(b, h,par));
Console.WriteLine("调用次数:"+c3.Count);
Console.ReadKey();
}
}
}
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mylib
{
class Program
{
const double par = 0.5;
static void Main(string[] args)
{
double r, x, y, b, h;
Calculation c1 = new Calculation();
Calculation c2 = new Calculation();
Calculation c3 = new Calculation();
Console.WriteLine("输入圆半径:");
r = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("圆面积:"+c1.Area(r));
Console.WriteLine("输入长方形的长和宽:");
x= Convert.ToDouble(Console.ReadLine());
y= Convert.ToDouble(Console.ReadLine());
Console.WriteLine("长方形面积::" + c2.Area(x,y));
Console.WriteLine("输入三角形的底和高:");
b = Convert.ToDouble(Console.ReadLine());
h = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("三角形面积:" + c2.Area(b, h,par));
Console.WriteLine("调用次数:"+c3.Count);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mylib
{
class Calculation
{
const double pi = 3.14159;
private static int count = 0;
public int Count { get => count; private set => count = value; }
public double Area(double r)
{
count++;
return pi * r * r;
}
public double Area(double x,double y)
{
count++;
return x * y;
}
public double Area(double b,double h,double par)
{
count++;
return par * b * h;
}
}
}
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mylib
{
class Calculation
{
const double pi = 3.14159;
private static int count = 0;
public int Count { get => count; private set => count = value; }
public double Area(double r)
{
count++;
return pi * r * r;
}
public double Area(double x,double y)
{
count++;
return x * y;
}
public double Area(double b,double h,double par)
{
count++;
return par * b * h;
}
}
}
Question
编写程序完成以下内容
1.定义学生类Student,为其定义两个共有自动属性Name和CourseAmount,分别表示学生的姓名和该学生锁上的课程数;定义一个私有二维字符串数组字段courseScore【,】,用于保存学生的课程名称以及该课程的成绩,在构造函数中初始化学生姓名及课程数,并根据课程数初始化数组字段coursescore【,】中。定义索引器,返回数组字段courseScore【,】所指定的下标的课程名称及课程成绩字符安川。重载》=和《=运算符,用于比较两个Student类型对象的平均分高度
2.在主程序中创建两个Student类型对象s1和s2.分别调用InitCourseScore()方法完成课程及成绩的初始化,通过Student类的索引器分别输出两个对象的课程名称和成绩,最后听过重载运算符比较并打印输出两个对象的平均分高低。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YJD
{
class Program
{
class Student
{
public string Name { get; set; }
public int CourseAmount { get; set; }
private string[,] courseScore;
public Student(string name, int courseamount)
{
Name = name;
CourseAmount = courseamount;
courseScore = new string[CourseAmount - 1, 2];
}
public void InitCourseScore()
{
int i = 0;
for (; i < CourseAmount; i++)
{
Console.Read().ToString(courseScore[i, 0]);
Console.Read().ToString(courseScore[i, 1]);
}
}
public string this[int i, int j]
{
get { return courseScore[i, j]; }
set { }
}
public static bool operator >=(Student a, Student b)
{
int i = 0;
int sa = 0, sb = 0;
for (; i < a.CourseAmount; i++)
{
sa += Convert.ToInt32(a.courseScore[i, 1]);
}
for (; i < b.CourseAmount; i++)
{
sb += Convert.ToInt32(b.courseScore[i, 1]);
}
if (sa >= sb)
return true;
else
return false;
}
public static bool operator <=(Student a, Student b)
{
int i = 0;
int sa = 0, sb = 0;
for (; i < a.CourseAmount; i++)
{
sa += Convert.ToInt32(a.courseScore[i, 1]);
}
for (; i < b.CourseAmount; i++)
{
sb += Convert.ToInt32(b.courseScore[i, 1]);
}
if (sa <= sb)
return true;
else
return false;
}
}
static void Main(string[] args)
{
Student s1 = new Student("Alpha", 3);
Student s2 = new Student("Beta", 3);
s1.InitCourseScore();
s2.InitCourseScore();
if (s1 <= s2 && s1 >= s2)
Console.WriteLine("The average score of Alpha and Beta is the same");
else if (s1 <= s2)
Console.WriteLine("Beta is better than Alpha");
else
Console.WriteLine("Alpha is better than Beta");
}
}
}
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YJD
{
class Program
{
class Student
{
public string Name { get; set; }
public int CourseAmount { get; set; }
private string[,] courseScore;
public Student(string name, int courseamount)
{
Name = name;
CourseAmount = courseamount;
courseScore = new string[CourseAmount - 1, 2];
}
public void InitCourseScore()
{
int i = 0;
for (; i < CourseAmount; i++)
{
Console.Read().ToString(courseScore[i, 0]);
Console.Read().ToString(courseScore[i, 1]);
}
}
public string this[int i, int j]
{
get { return courseScore[i, j]; }
set { }
}
public static bool operator >=(Student a, Student b)
{
int i = 0;
int sa = 0, sb = 0;
for (; i < a.CourseAmount; i++)
{
sa += Convert.ToInt32(a.courseScore[i, 1]);
}
for (; i < b.CourseAmount; i++)
{
sb += Convert.ToInt32(b.courseScore[i, 1]);
}
if (sa >= sb)
return true;
else
return false;
}
public static bool operator <=(Student a, Student b)
{
int i = 0;
int sa = 0, sb = 0;
for (; i < a.CourseAmount; i++)
{
sa += Convert.ToInt32(a.courseScore[i, 1]);
}
for (; i < b.CourseAmount; i++)
{
sb += Convert.ToInt32(b.courseScore[i, 1]);
}
if (sa <= sb)
return true;
else
return false;
}
}
static void Main(string[] args)
{
Student s1 = new Student("Alpha", 3);
Student s2 = new Student("Beta", 3);
s1.InitCourseScore();
s2.InitCourseScore();
if (s1 <= s2 && s1 >= s2)
Console.WriteLine("The average score of Alpha and Beta is the same");
else if (s1 <= s2)
Console.WriteLine("Beta is better than Alpha");
else
Console.WriteLine("Alpha is better than Beta");
}
}
}
不会的不要私聊我了,代码都有了,配置自己配一下