C#程序设计习题_3(西安交通大学)
一、week 6
1.在Main函数所在的类,实现两个方法用于计算成绩的平均值,小数点后1位。
public static double AvgGrade(int[] gra) //数值型成绩的平均值。得分1~5之间。课程数无限制。
public static double AvgGrade(string[] sgra) //成绩分为“good”和“ok”两个等级。good等价于4分,ok等价于1分。
在Main方法中,分别调用两种方法,均能输出平均成绩。输入用空格分开。
【注:】
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string[] a = Console.ReadLine().Split(' ');
if (a[0] == "good" || a[0] == "ok")
{
Console.WriteLine(Convert.ToDouble(AvgGrade(a)).ToString("0.0"));
}
else
{
int[] b = new int[a.Length];
for (int i = 0; i <= a.Length - 1; i++)
{
b[i] = Convert.ToInt32(a[i]);
}
Console.WriteLine(Convert.ToDouble(AvgGrade(b)).ToString("0.0"));
}
}
public static double AvgGrade(int[] gra)
{
double oup = 0;
for (int i = 0; i <= gra.Length - 1; i++)
{
oup += gra[i];
}
return Math.Round(oup / gra.Length, 1);
}
public static double AvgGrade(string[] sgra)
{
double oup = 0;
for (int i = 0; i <= sgra.Length - 1; i++)
{
if (sgra[i] == "good")
{
oup += 4;
}
else
{
oup += 1;
}
}
return Math.Round(oup / sgra.Length, 1);
}
}
}
2.定义一个时间类
成员有3个属性(包括对应的私有字段)年、月、日。属性要判断 年>0、12>=月份>0、日>0 并要在所在月份天数之内。
一个构造方法,在构造方法中给属性赋值。
在program类中,定义一个方法,方法的两个参数就是时间类的对象,方法的作用是,比较第一个参数(时间)和第二个参数(时间)的早晚,如果第一个时间早,返回1,相等返回0,小于返回-1。
在主函数main()中调用该方法。
如果时间单位前的数值是0,则不输出该时间单位的数值。
样例:
3610
1小时10秒
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string[] inp = Console.ReadLine().Split(' ');
string[] inp_1 = inp[0].Split('/');
string[] inp_2 = inp[1].Split('/');
int year1 = Convert.ToInt32(inp_1[0]);
int month1 = Convert.ToInt32(inp_1[1]);
int day1 = Convert.ToInt32(inp_1[2]);
int year2 = Convert.ToInt32(inp_2[0]);
int month2 = Convert.ToInt32(inp_2[1]);
int day2 = Convert.ToInt32(inp_2[2]);
Time d1 = new Time(year1, month1, day1);
Time d2 = new Time(year2, month2, day2);
Console.WriteLine(Compa(d1, d2));
}
static int Compa(Time d1, Time d2)
{
int fin = 0;
if (d1.year < d2.year)
{
fin = 1;
}
else if (d1.year > d2.year)
{
fin = -1;
}
else
{
if (d1.month < d2.month)
{
fin = 1;
}
else if (d1.month > d2.month)
{
fin = -1;
}
else
{
if (d1.day < d2.day)
{
fin = 1;
}
else if (d1.day > d2.day)
{
fin = -1;
}
else
{
fin = 0;
}
}
}
return fin;
}
}
public class Time
{
public int year;
public int month;
public int day;
public Time(int year, int month, int day)
{
Year = year;
Month = month;
Day = day;
}
public int Year
{
get { return year; }
set
{
if (value > 0)
{
year = value;
}
}
}
public int Month
{
get { return month; }
set
{
if (value > 0 && value <= 12)
{
month = value;
}
}
}
public int Day
{
get { return day; }
set
{
if (value > 0 & value <= Day_judge(this.year, this.month))
{
day = value;
}
}
}
static int mon(int month)
{
int fin = 0;
switch (month)
{
case 1:
fin = 31; break;
case 2:
fin = 28; break;
case 3:
fin = 31; break;
case 4:
fin = 30; break;
case 5:
fin = 31; break;
case 6:
fin = 30; break;
case 7:
fin = 31; break;
case 8:
fin = 31; break;
case 9:
fin = 30; break;
case 10:
fin = 31; break;
case 11:
fin = 30; break;
case 12:
fin = 31; break;
}
return fin;
}
static int Day_judge(int year, int month)
{
int fin = 0;
switch (year % 4)
{
case 0:
if (year % 100 == 0)
{
if (year % 400 == 0)
{
if (month == 2)
{
fin = 29;
}
else
{
fin = mon(month);
}
}
else
{
fin = mon(month);
}
}
else
{
if (month == 2)
{
fin = 29;
}
else
{
fin = mon(month);
}
}
break;
case 1:
fin = mon(month); break;
case 2:
fin = mon(month); break;
case 3:
fin = mon(month); break;
}
return fin;
}
}
}
二、week 7
1.定义圆类Circle,包含半径r,属性R能判断半径r的合理性(r>0),计算圆面积的方法double Area()。从Circle类派生出圆柱体类Cylinder类。
该类新增圆柱体的高h,属性H能判断高h的合理性(h>0),新增计算圆柱体体积的方法double Volume()。
如果半径、高不合法,就设置其值为0。
在Main方法中,创建一个Cylinder对象,输入半径和高(两行输入),并输出该对象的底面半径,高,底面积以及体积(面积和体积用double Math.round(doulbe b, int digit) digit取1。
(要求:不使用构造方法,并且类中的字段为私有,方法为公有)
样例1
3
3
3 3 28.3 84.8
using System;
namespace week7_hom1
{
class Program
{
static void Main(string[] args)
{
double r = Convert.ToDouble(Console.ReadLine());
double h = Convert.ToDouble(Console.ReadLine());
Cylinder ex_1 = new Cylinder();
ex_1.R = r;
ex_1.H = h;
Console.Write(Math.Round(ex_1.R,1));
Console.Write(" ");
Console.Write(Math.Round(ex_1.H,1));
Console.Write(" ");
Console.Write(Math.Round(ex_1.Area(),1));
Console.Write(" ");
Console.Write(Math.Round(ex_1.volume(),1));
}
}
public class Circle
{
private double r;
public double R
{
get
{
return r;
}
set
{
if (value > 0)
{
this.r = value;
}
else
{
this.r = 0;
}
}
}
public double Area()
{
return Math.PI * Math.Pow(this.r, 2);
}
}
public class Cylinder : Circle
{
private double h;
public double H
{
get
{
return h;
}
set
{
if (value > 0)
{
h = value;
}
else
{
h = 0;
}
}
}
public double volume()
{
return base.Area() * h;
}
}
}
2.定义圆类Circle,包含半径r,属性R能判断半径r的合理性(r>=0),定义带参构造方法初始化半径。计算圆面积的方法double Area()。
从Circle类派生出圆柱体类Cylinder类,新增圆柱体的高h,属性H能判断高h的合理性(h>=0),
定义带参构造方法初始化半径和高。新增计算圆柱体体积的方法double Volume()。
在主方法中,创建一个Cylinder对象,并输出该对象的底面积,体积。
using System;
namespace week7_hom2
{
class Program
{
static void Main(string[] args)
{
double r = Convert.ToDouble(Console.ReadLine());
double h = Convert.ToDouble(Console.ReadLine());
Cylinder ex_1 = new Cylinder(r,h);
Console.Write(Math.Round(ex_1.Area(), 1));
Console.Write(" ");
Console.Write(Math.Round(ex_1.volume(), 1));
}
}
public class Circle
{
private double r;
public Circle(double r_1)
{
this.R = r_1;
}
public double R
{
get
{
return r;
}
set
{
if (value >=0)
{
this.r = value;
}
else
{
this.r = 0;
}
}
}
public double Area()
{
return Math.PI * Math.Pow(this.r, 2);
}
}
public class Cylinder : Circle
{
private double h;
public Cylinder(double r_1,double h_1) : base(r_1)
{
this.H = h_1;
}
public double H
{
get
{
return this.h;
}
set
{
if (value >=0)
{
this.h = value;
}
else
{
this.h = 0;
}
}
}
public double volume()
{
return base.Area() * h;
}
}
}
3.实现两个类。
StuGrade类。该类的作用是成绩相关的操作。包括:
私有字段 int[] gra(学生成绩);
构造函数 StuGrade(params int[] stusgra);用于初始化私有字段gra;
公有方法 public double AvgGrade();返回学生成绩的平均值。
Student类。包含:
2个public 自实现属性string Name(学生姓名)、string StuNum(学号);
1个private字段 string stuClass(学生班级);
1个StuGrade对象 stuG;
1个公有方法public bool IsStuNo(string s,out int res)。检测学号是否合法。其中s是待检查的学号。学号必须是8位数字,21开头。不是8位数字res置1,非21开头res置2。合法res=学号。
1个公有方法public bool AddStu(string name, string sno, StuGrade stug, string stuclass= “数学”)。如果学号合法的对象,将姓名、学号、成绩、班级(默认是数学)赋值,返回true。否则不赋值,返回false。
在Main函数中,新建Student类对象stu,并接收控制台输入的信息.
【样例输入】
姓名,学号
成绩
如果输入学号合法,输出 学号:Success!AverageGrade:平均分
如果输入学号非法,输出 学号:Invalid!Error Code:res的值
例如:
新建对象Student stu1,尝试对stu1设置信息Alex, 20812000, 成绩是:1, 2, 3, 4, 5。将按照如下格式输出
20812000:Invalid!Error Code:2
新建Student类对象stu2 ,尝试对stu2设置信息Bob, 21812000, 成绩是:1, 2, 3。将按照如下格式输出
21812000:Success!AverageGrade:2
using System;
namespace week7_hom4
{
class Program
{
static void Main(string[] args)
{
string[] inp_1 = Console.ReadLine().Split(',');
string[] inp_2 = Console.ReadLine().Split(',');
int useless;
int[] grad=new int[inp_2.Length];
for(int i = 0; i<inp_2.Length; i++)
{
grad[i] = Convert.ToInt32(inp_2[i]);
}
StuGrade i_1 = new StuGrade(grad);
Student i_2 = new Student();
if (i_2.IsStuNo(inp_1[1],out useless))
{
Console.Write(inp_1[1]);
Console.Write(":");
Console.Write("Success!AverageGrade:");
Console.Write(i_1.AvgGrade());
}
else
{
Console.Write(inp_1[1]);
Console.Write(":");
Console.Write("Invalid!Error Code:");
Console.Write(useless);
}
}
}
public class StuGrade
{
private int[] gra;
public StuGrade(int[] stusgra)
{
this.gra = stusgra;
}
public double AvgGrade()
{
double sum_1 = 0;
foreach(double i in this.gra)
{
sum_1 += i;
}
return sum_1 / (gra.Length);
}
}
public class Student
{
public string Name;
public string StuNum;
private string stuClass;
public StuGrade stuG;
public bool IsStuNo(string s, out int res)
{
if (s.Length == 8 )
{
if(s.Substring(0, 2) == "21")
{
res = 1;
return true;
}
else
{
res = 2;
return false;
}
}
else
{
res =Convert.ToInt32(s);
return false;
}
}
public bool AddStu(string name, string sno, StuGrade stug, string stuclass = "数学")
{
int judge;
this.IsStuNo(sno,out judge);
if (Convert.ToString(judge) == sno)
{
this.Name = name;
this.StuNum = sno;
this.stuClass = stuclass;
this.stuG = stug;
return true;
}
else
{
return false;
}
}
}
}
有任何问题欢迎留言。