1、圆的面积
namespace CircleArea
{
class Program
{
static void Main(string[] args)
{
const double PI = 3.1415926;
double Radius,Area;
Console.Write("请输入要计算面积的圆形的半径:");
Radius = double.Parse(Console.ReadLine());
//Area = PI * Radius * Radius;
Area = PI * Math.Pow(Radius, 2);
Console.WriteLine("半径为{0}的圆形面积为{1}",Radius,Area);
}
}
}
2、判断是否为正方形
namespace Struct
{
class Program
{
struct Rectangle
{
public double Width;
public double Height;
public bool IsSquare()
{
return Width == Height;
}
};
static void Main(string[] args)
{
Rectangle rect;
string issquare;
Console.WriteLine("请输入矩形的长:");
rect.Height = double.Parse(Console.ReadLine());
Console.WriteLine("请输入矩形的宽:");
rect.Width = double.Parse(Console.ReadLine());
if (rect.IsSquare())
{
issquare = "是";
}
else
{
issquare = "不是";
}
Console.WriteLine("长为{0},宽为{1}的矩形{2}正方形!", rect.Height, rect.Width, issquare);
}
}
}
3、switch
namespace OnSpecial
{
class Program
{
enum Week
{
Sunday,
Monday,
Tuesday,
Wenesday,
Thursday,
Friday,
Saturday
}
static void Main(string[] args)
{
Console.WriteLine("请输入星期:");
Week week = (Week)int.Parse(Console.ReadLine());
switch(week)
{
case Week.Sunday:
Console.WriteLine("星期日特价菜:爆炒牛肉18元");
break;
case Week.Monday:
Console.WriteLine("星期一特价菜:啤酒鸭26元");
break;
case Week.Tuesday:
Console.WriteLine("星期二特价菜:红烧肉20元");
break;
case Week.Wenesday:
Console.WriteLine("星期三特价菜:回锅肉16元");
break;
case Week.Thursday:
Console.WriteLine("星期四特价菜:水煮鱼24元");
break;
case Week.Friday:
Console.WriteLine("星期五特价菜:剁椒鱼头30元");
break;
case Week.Saturday:
Console.WriteLine("星期六特价菜:手撕包菜12元");
break;
default:
break;
}
}
}
}
4、for循环嵌套调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CockHenChicknum
{
class Program
{
static void Main(string[] args)
{
int cock, hen, chick;
for (cock = 0; cock <= 20; cock++)
{
for (chick = 0; chick <= 99; chick += 3)
{
hen = 100 - cock - chick;
if (hen >= 0 && hen <= 33 && cock * 5 + chick / 3 + hen * 3 == 100)
{
Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只", cock, hen, chick);
}
}
}
}
}
}
5、水仙花数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cube
{
class Program
{
static void Main(string[] args)
{
int num = 100;
Console.Write("水仙花数有:");
do
{
int x = num / 100;//百位baidu
int y = num % 100 / 10;//十位
int z = num % 10;//个位
if (x * x * x + y * y * y + z * z * z == num)
{
Console.Write(" {0} ",num);
}
num += 1;
} while (num < 1000);
}
}
}
6、学生出生日期-----DateTime类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StudentDataTime
{
enum WeekDayhz { 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六 };
class Program
{
class Student
{
public string number; //学号
public string name; //姓名
public DateTime birthday; //出生日期
public void SetInfo(string xh, string xm, DateTime sr)
{
number = xh;
name = xm;
birthday = sr;
}
public void GetInfo()
{
Console.WriteLine("学号是{0}的学生的姓名是{1},出生日期为{2};", number, name,birthday);
}
}
static void Main(string[] args)
{
Student s1, s2;
s1 = new Student();
s2 = new Student();
DateTime birthday1 = new DateTime(1995, 10, 18);
DateTime birthday2 = new DateTime(1996, 2, 16);
s1.SetInfo("101","张三",birthday1);
s2.SetInfo("102","李四",birthday2);
s1.GetInfo();
s2.GetInfo();
int i = (int)s1.birthday.DayOfWeek;
Console.WriteLine("{0}出生在{1}", s1.name, (WeekDayhz)i);
i = (int)s2.birthday.DayOfWeek;
Console.WriteLine("{0}出生在{1}", s2.name, (WeekDayhz)i);
Console.WriteLine("{0}和{1}相差{2}天", s1.name, s2.name, s2.birthday - s1.birthday);
}
}
}
7、判断给定的词语是否在句子中------string
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FindWord
{
class Program
{
static void Main(string[] args)
{
string sentence = "我希望有个如你一般的人 如山间清爽的风 如古城温暖的光 从清晨到夜晚 由山野到书房 只要最后是你就好";
Console.Write("请输入要检索的词语:");
string word = Console.ReadLine();
if (sentence.IndexOf(word)>=0)
{
Console.WriteLine("词语“{0}”在句子“{1}”中!",word,sentence);
}
else
{
Console.WriteLine("在句子中没有找到词语“{0}”,请重新输入检索词!", word);
}
}
}
}
8、修改班级的班主任和总人数-----引用类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Reference
{
struct Class //声明结构类型班级Class
{
public Information stuIfo; //学生信息
public string teacher; //班主任
};
class Information
{
public int sumNum; //总数量
}
class Program
{
static void Main(string[] args)
{
Class class1, class2;
Information stuInfo1 = new Information();
stuInfo1.sumNum = 30;
class1.stuIfo = stuInfo1;
class1.teacher = "Marry";
Console.WriteLine("一班的班主任是:{0}", class1.teacher);
Console.WriteLine("一班的总人数是:{0}", class1.stuIfo.sumNum);
Console.WriteLine("\n");
Console.WriteLine("将一班的信息赋值给二班!");
Console.WriteLine("\n");
class2 = class1;
Console.WriteLine("修改二班的班主任信息!");
class2.teacher = "Tom";
Console.WriteLine("二班的班主任是{0}:", class2.teacher);
Console.WriteLine("修改二班的学生人数!");
class2.stuIfo.sumNum = 31;
Console.WriteLine("二班的总人数是:{0}", class2.stuIfo.sumNum);
Console.WriteLine("\n");
Console.WriteLine("一班的班主任是{0},总人数是{1}:", class1.teacher,class1.stuIfo.sumNum);
}
}
}
9、学生信息初始化----构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentInfo
{
class Student
{
public string number; //学号
public string name; //姓名
public string sclass; //班级
public Student(string xh, string xm, string bj)
{
number = xh;
name = xm;
sclass = bj;
}
public void GetInfo()
{
Console.WriteLine("类Student中学号是{0}的学生的姓名是{1},来自{2};", number, name, sclass);
}
}
struct SStudent
{
public string number; //学号
public string name; //姓名
public string sclass; //班级
public void SetInfo(string xh, string xm, string bj)
{
number = xh;
name = xm;
sclass = bj;
}
public void GetInfo()
{
Console.WriteLine("结构类型SStudent中学号是{0}的学生的姓名是{1},来自{2};", number, name, sclass);
}
}
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("101", "张三", "信息191");
Console.WriteLine("请输出学生s1的信息:");
s1.GetInfo();
Console.WriteLine("");
Student s2 = s1;
s2.number = "102";
s2.name = "王小花";
Console.WriteLine("请输出学生s1的信息:");
s1.GetInfo();
Console.WriteLine("");
Console.WriteLine("请输出学生s2的信息:");
s2.GetInfo();
Console.WriteLine("");
SStudent ss1 = new SStudent();
ss1.SetInfo("201", "云朵朵", "信息192");
Console.WriteLine("请输出学生ss1的信息:");
ss1.GetInfo();
Console.WriteLine("");
SStudent ss2 = ss1;
ss2.number = "202";
ss2.name = "令狐花";
Console.WriteLine("请输出学生ss1的信息:");
ss1.GetInfo();
Console.WriteLine("");
Console.WriteLine("请输出学生ss2的信息:");
ss2.GetInfo();
Console.WriteLine("");
}
}
}
10、计算不同员工的工资-----方法重载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReloadFunction
{
class Program
{
class Employee
{
int no;
string name;
string professionalTitle;
public Employee()
{ }
public Employee(int bh, string xm, string jb)
{
no = bh;
name = xm;
professionalTitle = jb;
}
public void GetIfo(int basesalary)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元", no, name, professionalTitle, basesalary);
}
public void GetIfo(int basesalary, int housingAllowance)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元+住房补贴{4}元={5}元", no, name, professionalTitle, basesalary, housingAllowance, basesalary + housingAllowance);
}
public void GetIfo(int basesalary, int housingAllowance, int reward)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元+住房补贴{4}元+奖金{5}元={6}元", no, name, professionalTitle, basesalary, housingAllowance, reward, basesalary + housingAllowance + reward);
}
}
static void Main(string[] args)
{
Employee e1 = new Employee(001, "张三", "研究员");
Employee e2 = new Employee(002, "李四", "工程师");
Employee e3 = new Employee(003, "王五", "高级工程师");
e1.GetIfo(1000);
e2.GetIfo(1200, 500);
e3.GetIfo(2000, 1000, 2000);
}
}
}
11、计算两点之间距离------引用型参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CalculateDistance
{
class Program
{
class Point
{
double x;
double y;
public double X
{
get { return x; }
set { x = value; }
}
public double Y
{
get { return y; }
set { y = value; }
}
public void Eu_distance(Point p1)
{
double result;
result = Math.Sqrt(Math.Pow((x - p1.x), 2) + Math.Pow((y - p1.y), 2));
Console.WriteLine("点({0},{1})和点({2},{3})之间的欧式距离是:{4}",x,y,p1.x,p1.y,result);
}
public void MH_distance(Point p1)
{
double result;
result = Math.Abs(x - p1.x) + Math.Abs(y - p1.y);
Console.WriteLine("点({0},{1})和点({2},{3})之间的曼哈顿距离是:{4}", x, y, p1.x, p1.y, result);
}
public void Cos_distance(Point p1)
{
double result;
result = (x * p1.x + y * p1.y) / (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) * Math.Sqrt(Math.Pow(p1.x, 2) + Math.Pow(p1.y, 2)));
Console.WriteLine("点({0},{1})和点({2},{3})之间的余弦距离是:{4}", x, y, p1.x, p1.y, result);
}
}
static void Main(string[] args)
{
Point p = new Point();
Point p1 = new Point();
p.X = 1;
p.Y = 1;
p1.X = 2;
p1.Y = 2;
p.Eu_distance(p1);
p.MH_distance(p1);
p.Cos_distance(p1);
}
}
}
12、显示学生所选课程的信息-------对象交互
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InteractionObject
{
class Program
{
class Student
{
string id;
string name;
int scredits;
Course course;
public Student(string id, string name, int scredits, Course course)
{
this.id = id;
this.name = name;
this.scredits = scredits;
this.course = course;
}
public void UpdateCredit()
{
scredits += course.Credits;
}
public void display()
{
Console.WriteLine("该生的学号是{0},姓名是{1},已选择了《{2}》课,这门课的课程号是{3},{4}学分,由{5}讲授,现在该生的总学分数是{6}"
,id,name,course.Name,course.Cid,course.Credits,course.Teacher,scredits);
}
}
class Course
{
string cid;
string cname;
int credits;
string teacher;
public Course(string cid,string cname,int credits,string teacher)
{
this.cid = cid;
this.cname = cname;
this.credits = credits;
this.teacher = teacher;
}
public string Cid
{
get { return cid; }
}
public string Name
{
get { return cname; }
}
public int Credits
{
get { return credits; }
}
public string Teacher
{
get { return teacher; }
}
}
static void Main(string[] args)
{
Course c = new Course("Scl178","高阶英语",4,"Marry");
Student s = new Student("101","王明",30,c);
s.UpdateCredit();
s.display();
}
}
}
13、输出学生信息-----继承
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inheritance1
{
class Teacher
{
string tid;
string tname;
public Teacher(string tid,string tname)
{
this.tid = tid;
this.tname = tname;
}
public string Tid
{
get
{
return tid;
}
}
public string Tname
{
get
{
return tname;
}
}
}
class Advisor:Teacher
{
string major;
public Advisor(string tid,string tname,string major):base(tid,tname)
{ this.major = major; }
public string Major
{
get
{
return major;
}
}
}
class Student
{
protected string sid;
protected string sname;
public Student(string sid, string sname)
{
this.sid = sid;
this.sname = sname;
}
}
class UnderGraduate : Student
{
Teacher teacher;
public UnderGraduate(string sid, string sname, Teacher teacher) : base(sid, sname)
{ this.teacher = teacher; }
public void Udisplay()
{
Console.WriteLine("该生学号:{0},姓名:{1};他班主任的工号:{2},姓名:{3}。",sid,sname,teacher.Tid,teacher.Tname);
}
}
class Graduate : Student
{
Advisor advisor;
public Graduate(string sid, string sname, Advisor advisor) : base(sid, sname)
{ this.advisor = advisor; }
public void Gdisplay()
{
Console.WriteLine("该生学号:{0},姓名:{1};他导师的工号:{2},姓名:{3},研究方向:{4}。",sid,sname, advisor.Tid, advisor.Tname, advisor.Major);
}
}
class Program
{
static void Main(string[] args)
{
Teacher t = new Teacher("2020112", "Marry");
UnderGraduate ug = new UnderGraduate("20200101", "Rose",t);
ug.Udisplay();
Advisor a = new Advisor("2018111", "Jim", "信息行为");
Graduate g = new Graduate("20200201", "Jack", a);
g.Gdisplay();
}
}
}
14、计算圆柱体表面积------派生类的构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ploymorphism
{
class Circle
{
protected double radius;
public Circle(double bj)
{
radius = bj;
}
public double CircleArea()
{
return Math.PI * radius * radius;
}
}
class Cylinder : Circle
{
double high;
public Cylinder(double r, double h)
: base(r)
{
high = h;
}
public double CylinderArea()
{
return base.CircleArea() * 2 + 2 * Math.PI * radius * high;
}
}
class Program
{
static void Main(string[] args)
{
Circle circle = new Circle(100);
Console.WriteLine("半径为100的圆的面积为{0:N2}", circle.CircleArea());
Cylinder cylinder = new Cylinder(100, 100);
Console.WriteLine("半径为100,高为100的圆柱体的表面积为{0:N2}", cylinder.CylinderArea());
}
}
}
15、计算圆柱体和圆锥体表面积-----多态
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ploymorphism
{
class Circle
{
protected double radius;
public Circle(double bj)
{
radius = bj;
}
public virtual double Area()
{
return Math.PI * radius * radius;
}
}
class Cylinder : Circle
{
double high;
public Cylinder(double r, double h)
: base(r)
{
high = h;
}
public override double Area()
{
return base.Area() * 2 + 2 * Math.PI * radius * high;
}
}
class Cone : Circle
{
double high;
public Cone(double r, double h)
: base(r)
{
high = h;
}
public override double Area()
{
return base.Area() + Math.PI * radius * Math.Sqrt(radius * radius + high * high);
}
}
class Program
{
static void Main(string[] args)
{
Circle cylinder = new Cylinder(100, 100);
Console.WriteLine("半径为100,高为100的圆柱体的表面积为{0:N2}", cylinder.Area());
Circle cone = new Cone(100, 100);
Console.WriteLine("半径为100,高为100的圆锥体的表面积为{0:N2}", cone.Area());
}
}
}
16、输出不同员工的工资-----new或者虚方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Proj7_1
{
class Program
{
public class Employee
{
int no;
string name;
int lengthofservice;
const double basesalary = 1000;
// double realsalary;
public int No
{
get { return no; }
set { no = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public virtual double SetRealSalary()
{
Console.Write("请输入此人的工龄:");
lengthofservice = int.Parse(Console.ReadLine());
double realsalary = basesalary + 30 * lengthofservice;
return realsalary;
}
}
public class UEmployee:Employee
{
public override double SetRealSalary()
{
return base.SetRealSalary() * 2;
}
}
public class GEmployee : UEmployee
{
public override double SetRealSalary()
{
return base.SetRealSalary() * 1.5;
}
}
static void Main(string[] args)
{
Employee s1 = new Employee();
s1.No = 101;
s1.Name = "张三";
Console.WriteLine("普通职工 工号:{0} 姓名:{1}",s1.No,s1.Name);
Console.WriteLine("此人的实际工资为:{0}", s1.SetRealSalary());
Console.WriteLine();
UEmployee s2 = new UEmployee();
s2.No = 201;
s2.Name = "李四";
Console.WriteLine("本科生职工 工号:{0} 姓名:{1}", s2.No, s2.Name);
Console.WriteLine("此人的实际工资为:{0}", s2.SetRealSalary());
Console.WriteLine();
GEmployee s3 = new GEmployee();
s3.No = 301;
s3.Name = "王五";
Console.WriteLine("研究生职工 工号:{0} 姓名:{1}", s3.No, s3.Name);
Console.WriteLine("此人的实际工资为:{0}", s3.SetRealSalary());
}
}
}
17、图书馆管理学生借书权限----抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstructClass
{
abstract class Student
{
public abstract void Authority();
}
class Undergraduate : Student
{
public override void Authority()
{
Console.WriteLine("本科生可以借5本书");
}
}
class Postgraduate : Student
{
public override void Authority()
{
Console.WriteLine("硕士生可以借8本书");
}
}
class Doctor : Student
{
public override void Authority()
{
Console.WriteLine("博士生可以借10本书");
}
}
class Program
{
static void Main(string[] args)
{
Undergraduate s1 = new Undergraduate();
s1.Authority();
Postgraduate s2 = new Postgraduate();
s2.Authority();
Doctor s3 = new Doctor();
s3.Authority();
}
}
}
18、教师和学生信息的显示----抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace duotai1
{
abstract class Person
{
protected string no;
protected string name;
abstract public string No
{ set;}
abstract public string Name
{ set;}
abstract public void Display();
}
class Student : Person
{
string grade;
public override string No
{ set { no = value; } }
public override string Name
{ set { name = value; } }
public string Grade
{ set { grade = value; } }
public override void Display()
{
Console.WriteLine("学生的学号是{0},姓名是{1},班级是{2}!",no, name,grade);
}
}
class Teacher : Person
{
string professionalTitle;
string department;
public override string No
{ set { no = value; } }
public override string Name
{ set { name = value; } }
public Teacher(string no, string name, string professionalTitle, string department)
{
this.no = no;
this.name = name;
this.professionalTitle = professionalTitle;
this.department = department;
}
public override void Display()
{
Console.WriteLine("教师的学号是{0},姓名是{1},是{2}的{3}!", no, name, department, professionalTitle);
}
}
class Program
{
static void Main(string[] args)
{
Student s = new Student();
s.No = "19111701";
s.Name = "Mary";
s.Grade = "信息161";
s.Display();
Teacher t = new Teacher("2017001","Tom","副教授","信息管理系");
t.Display();
}
}
}