//理论作业
//分数转绩点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 分数转绩点
{
class Program
{
static void Main(string[] args)
{
int score;
Console.Write("分数:");
score = int.Parse(Console.ReadLine());
if (score >= 95) Console.WriteLine("绩点为4.5");
else if (score >= 90) Console.WriteLine("绩点为4.0");
else if (score >= 85) Console.WriteLine("绩点为3.5");
else if (score >= 80) Console.WriteLine("绩点为3.0");
else if (score >= 75) Console.WriteLine("绩点为2.5");
else if (score >= 70) Console.WriteLine("绩点为2.0");
else if (score >= 65) Console.WriteLine("绩点为1.5");
else if (score >= 60) Console.WriteLine("绩点为1.0");
else Console.WriteLine("不及格");
}
}
}
//计算圆面积
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CircleArea
{
class Program
{
static void Main(string[] args)
{
const double Pi = Math.PI;
double Radius,Area;
Console.Write("请输入要计算面积的半径:");
Radius = double.Parse(Console.ReadLine());
Area = Pi * Radius * Radius;
Console.WriteLine("半径为{0}的圆形面积为{1}", Radius, Area);
}
}
}
//简单构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 构造函数案例跑一下
{
class Program
{
class Student
{
public string name;
public char gender;
public string class_x;
public uint grade;
//默认构造函数
public Student()
{
}
}
static void Main(string[] args)
{
//调用默认构造函数
Student s = new Student();
Console.WriteLine("班级=" + s.class_x);
Console.WriteLine("性别=" + s.gender);
Console.WriteLine("成绩=" + s.grade);
}
}
}
//计算GPA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GPA
{
class Program
{
public class Course
{
string coursename;
int coursecredit;
double score;
public Course(string cname, int ccredit, double cscore)
{
coursename = cname;
coursecredit = ccredit;
score = cscore;
}
public string Coursename
{
get { return coursename; }
}
public int Coursecredit
{
get { return coursecredit; }
}
public double Score
{
get { return score; }
}
}
public class Student
{
int id;
string name;
Course[] courses;
double GPA;
public Student(int id, string name,Course[] courses)
{
this.id = id;
this.name = name;
this.courses = new Course[courses.Length];
for (int i = 0; i < this.courses.Length; i++)
this.courses[i] = courses[i];
}
public void ComputeGPA()
{
int i;
double sumcredit=0;//sumcredit为课程总学分数
double g, sumgpa=0;//g为某门课成绩换算后的绩点,sumgpa是最终所求绩点
for(i=0;i<courses.Length;i++)
{
if (courses[i].Score >= 95)
g = 4.5;
else if (courses[i].Score >= 90)
g = 4.0;
else if (courses[i].Score >= 85)
g = 3.5;
else if (courses[i].Score >= 80)
g = 3.0;
else if (courses[i].Score >= 75)
g = 2.5;
else if (courses[i].Score >= 70)
g = 2.0;
else if (courses[i].Score >= 65)
g = 1.5;
else if (courses[i].Score >= 60)
g = 1.0;
else
g = 0;
sumgpa += g * courses[i].Coursecredit;
sumcredit += courses[i].Coursecredit;
}
GPA = sumgpa / sumcredit;
}
public void Display()
{
Console.WriteLine("学号{0},姓名{1}", id, name);
Console.WriteLine("已修课程信息如下:");
Console.WriteLine("课程名\t学分\t分数\t");
for(int i=0;i<courses.Length;i++)
{
Console.WriteLine("{0}\t{1}\t{2}", courses[i].Coursename, courses[i].Coursecredit, courses[i].Score);
}
Console.WriteLine("该同学的GPA是{0:N2}\n", GPA);
}
}
static void Main(string[] args)
{
Console.Title="南农计算GPA方法";
Course[] sumcourse = new Course[] { new Course("英语", 2, 92), new Course("数学", 3, 90), new Course("语文", 2, 98), new Course("历史", 2, 70), new Course("政治", 2, 89) };
Student s1 = new Student(101, "李华", sumcourse);
s1.ComputeGPA();
s1.Display();
}
}
}
//实验作业
//加法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace plus
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
Console.Write("a="); //WriteLine就换行了
a = int.Parse(Console.ReadLine());
Console.Write("b=");
b = int.Parse(Console.ReadLine());
c = a + b;
Console.WriteLine("a+b={0}",c);//另一写法Console.WriteLine("{0}+{1}={2}",a,b,c);
}
}
}
结构体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @struct
{
class Program
{
struct student
{
public int num;
public string name;
public string gender;
public int age;
public string class_name;
};
static void Main(string[] args)
{
student s1, s2;
s1.num = 101;
s1.name = "李四";
s1.gender = "男";
s1.age = 20;
s1.class_name = "201";
Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}", s1.num, s1.name, s1.gender, s1.age, s1.class_name);
s2 = s1;
s2.num = 102;
s2.name = "王五";
Console.WriteLine("学号:{0},姓名:{1},性别:{2},年龄:{3},班号:{4}", s2.num, s2.name, s2.gender, s2.age, s2.class_name);
}
}
}
//枚举
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace enumerations
{
class Program
{
enum Color { red=3,green,pink,black=1,blue};
static void Main(string[] args)
{
Color c1, c2, c3;
Console.WriteLine ("red={0},green={1},pink={2},black={3},blue={4}", Color.red, Color.green, Color.pink, Color.black, Color.blue);
Console.WriteLine ("red={0},green={1},pink={2},black={3},blue={4}", (int)Color.red, (int)Color.green, (int)Color.pink, (int)Color.black, (int)Color.blue);
c1 = Color.red;
c2 = c1 + 1;
c3 = c2 + 1;
Console.WriteLine("c1={0},c2={1},c3={2}",c1,c2,c3);
Console.WriteLine("c1={0},c2={1},c3={2}", (int)c1, (int)c2, (int)c3);
}
}
}
//循环语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 循环语句
{
class Program
{
static void Main(string[] args)
{
int n, i,j;
Console.Write("请输入一个整数n:");
n = int.Parse(Console.ReadLine());
int sum = 0;
int item;
for (i = 1; i <= n; i++)
{
item = 0;
for(j=1;j<=i;j++)
{
item = item + j;
}
sum = sum + item;
}
Console.WriteLine("1+(1+2)+(1+2+3)+...+(1+2+3+...+n)的结果:{0}", sum);
}
}
}
//结构体判断是否为矩形
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Struct
{
class Program
{
struct Rectangle
{
public double Height;
public double Width;
}
static void Main(string[] args)
{
Rectangle rectangle1;
Console.Write("请输入矩形的长:" );
rectangle1.Height = double.Parse(Console.ReadLine());
Console.Write("请输入矩形的宽:");
rectangle1.Width = double.Parse(Console.ReadLine());
if (rectangle1.Height == rectangle1.Width)
Console.WriteLine("长为{0},宽为{1}的矩形是正方形!", rectangle1.Height, rectangle1.Width);
else
Console.WriteLine("长为{0},宽为{1}的矩形不是正方形!", rectangle1.Height, rectangle1.Width);
}
}
}
Switch菜谱
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnSpecial
{
class Program
{
enum Week { 红烧肉20元=1,宫保鸡丁10元,糖醋藕片10元,皮蛋豆腐15元,麻婆豆腐10元,酸菜鱼15元,回锅肉20元 }
static void Main(string[] args)
{
//day表示星期数
Week day1,day2,day3,day4,day5,day6,day7;
day1 = Week.红烧肉20元;
day2 = day1 + 1;
day3 = day2 + 1;
day4 = day3 + 1;
day5 = day4 + 1;
day6 = day5 + 1;
day7 = day6 + 1;
//digit为中间变量
int digit;
Console.WriteLine("请输入星期:");
digit = int.Parse(Console.ReadLine());
switch(digit)
{
case 1:
Console.WriteLine("星期一特价菜:{0}", day1);
break;
case 2:
Console.WriteLine("星期二特价菜:{0}", day2);
break;
case 3:
Console.WriteLine("星期三特价菜:{0}", day3);
break;
case 4:
Console.WriteLine("星期四特价菜:{0}", day4);
break;
case 5:
Console.WriteLine("星期五特价菜:{0}", day5);
break;
case 6:
Console.WriteLine("星期六特价菜:{0}", day6);
break;
case 7:
Console.WriteLine("星期日特价菜:{0}", day7);
break;
default:
Console.WriteLine("输入的星期不正确!");
break;
}
}
}
}
//百钱买百鸡(for循环嵌套)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CockHenChicknum
{
class Program
{
static void Main(string[] args)
{
int cock, hen, chick;
for (cock = 0; cock >= 0 && cock <= 20; cock++)
for (hen = 0; hen >= 0 && hen <= 33; hen++)
for (chick = 0; chick >= 0 && chick <= 99; chick++)
{
if (cock + hen + chick == 100 && cock * 5 + hen * 3 + chick / 3 == 100 && chick % 3 == 0)
{
Console.WriteLine("公鸡{0}只,母鸡{1}只,小鸡{2}只", cock, hen, chick);
}
}
}
}
}
//水仙花数(while)
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)
{
//i,j,k分别是三位数的个位、十位、百位
int num=100,i,j,k;
while (num<999)
{
num++;
i = num / 100;
j = (num / 10) % 10;
k = num % 10;
if (num == i * i * i + j * j * j + k * k * k)
{
Console.WriteLine("{0}", num);
}
}
}
}
}
//类和对象
//创建员工信息表——类与对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreateEmployee
{
public class Employee
{
public int number;
public string name;
public string professional_Title;
public void SetInfo(int num,string name1,string title)
{
number = num;
name = name1;
professional_Title = title;
}
public void GetInfo()
{
Console.WriteLine("编号是:{0},姓名是:{1},职称是:{2}", number, name, professional_Title);
}
}
class Program
{
static void Main(string[] args)
{
Employee e1 = new Employee();
e1.SetInfo(101, "Tom", "Boss");
e1.GetInfo();
Employee e2 = new Employee();
e2.SetInfo(102, "Mary","Manager");
e2.GetInfo();
}
}
}
//引用类型和构造函数
1.学生的出生日期——DateTime类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentDateTime
{
class Program
{
public class Student
{
public int number;
public string name;
public DateTime birthday;
public void SetInfo(int num, string name1, DateTime birth)
{
number = num;
name = name1;//name1为形参
birthday = birth;
}
public void GetInfo()
{
Console.WriteLine("学号是{0}的学生的姓名是{1},出生日期是{2}", number, name, birthday);
}
public enum week { 周日,周一,周二,周三,周四,周五,周六}
static void Main(string[] args)
{
Student s1 = new Student();
Student s2 = new Student();
DateTime d1 = new DateTime(1995, 10, 18);
DateTime d2 = new DateTime(1996, 2, 16);
s1.SetInfo(101, "张三", d1);
s2.SetInfo(102, "赵四", d2);
s1.GetInfo();
s2.GetInfo();
Console.WriteLine("{0}出生在{1}", s1.name, (week)(int)d1.DayOfWeek);
Console.WriteLine("{0}出生在{1}", s2.name, (week)(int)d2.DayOfWeek);
Console.WriteLine("{0}和{1}相差{2}天", s1.name,s2.name,d2-d1);
}
}
}
}
//老师的
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);
}
}
}
//2.查找词语——String类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FindWord
{
class Program
{
static void Main(string[] args)
{
string strs = "路透社20日报道称,接受这台手术的患者为脑死亡病人,在其身体出现肾衰竭迹象后,家属才同意参与这项猪肾移植试验。医生没有直接将猪的肾脏放入患者体内,而是将其连接到患者的大腿血管上,这样可以方便研究人员观察患者的情况。手术完成后,移植的肾脏立即开始正常工作,随后3天里均未发生排异现象,病人体内的一些指标也降至正常水平。";
string word = Console.ReadLine();
bool b = strs.Contains(word);
if (b == true)
{
Console.WriteLine("词语“{0}”在句子“{1}”中!", word, strs);
}
else
{
Console.WriteLine("请输入检索的词语:");
Console.WriteLine("在句子中没有找到词语“{0}”,请重新输入检索词!",word);
}
}
}
}
//老师的
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);
}
}
}
}
//复数加法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 复数加法
{
class ComplexNumber
{
int real;
int imaginary;
public void Create()
{
Console.WriteLine("请输入实部: ");
real = int.Parse(Console.ReadLine());
Console.WriteLine("请输入虚部: ");
imaginary = int.Parse(Console.ReadLine());
}
public void PrintResult()
{
Console.WriteLine("两复数相加之和为: ");
Console.WriteLine(real + "+" + imaginary + "i");
}
public ComplexNumber ComplexAdd(ComplexNumber Param1)
{
ComplexNumber sum = new ComplexNumber();
sum.real = Param1.real + real;
//Param1.real += real;
sum.imaginary = Param1.imaginary + imaginary;
//Param1.imaginary += imaginary;
return sum;
//return Param1;
}
}
class Program
{
static void Main(string[] args)
{
ComplexNumber Number1 = new ComplexNumber();
ComplexNumber Number2 = new ComplexNumber();
Number1.Create();
Number2.Create();
ComplexNumber Temp = Number1.ComplexAdd(Number2);
Temp.PrintResult();
Number2.PrintResult();
}
}
}
//3.修改班级的班主任和总人数——引用类型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Referencen
{
class Program
{
public class Information
{
public int sunNum; //总数量
}
struct Class //声明结构类型班级Class
{
public Information stuIfo; //学生信息
public string teacher; //班主任
};
static void Main(string[] args)
{
Class class1, class2;
class1.teacher = "Marry";
Information stuInfo1 = new Information();
stuInfo1.sunNum = 30;
class1.stuIfo = stuInfo1;
Console.WriteLine("一班的班主任是:{0}", class1.teacher);
Console.WriteLine("一班的总人数是:{0}\n", class1.stuIfo.sunNum);
Console.WriteLine("将一班的信息赋值给二班!\n");
class2 = class1;
Console.WriteLine("修改二班的班主任信息!");
class2.teacher = "Tom";
Console.WriteLine("二班的班主任是:{0}", class2.teacher);
Console.WriteLine("修改二班的学生人数!");
stuInfo1.sunNum = 31;
class2.stuIfo = stuInfo1;
Console.WriteLine("二班的总人数是:{0}\n", class2.stuIfo.sunNum);
Console.WriteLine("一班的班主任是:{0},总人数是{1}", class1.teacher,class1.stuIfo.sunNum);
}
}
}
//老师的
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);
}
}
}
//构造函数和方法
//1.初始化员工对象——构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReloadFunction
{
class Program
{
public class Employee
{
int number;
string name;
string professionalTitle;
public Employee(int num,string name1,string profTitle)
{
number = num;
name = name1;
professionalTitle = profTitle;
}
public void Salary(int basicPay)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元",number,name,professionalTitle,basicPay);
}
public void Salary(int basicPay,int housePay)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元+住房补贴{4}元={5}元",number,name,professionalTitle,basicPay,housePay,basicPay+housePay);
}
public void Salary(int basicPay, int housePay,int bonus )
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元+住房补贴{4}元+奖金{5}={6}元", number,name, professionalTitle, basicPay, housePay,bonus,basicPay+housePay+bonus);
}
}
static void Main(string[] args)
{
Employee e1 = new Employee(1, "张三", "研究员");
Employee e2 = new Employee(2, "李四", "工程师");
Employee e3 = new Employee(3, "王五", "高级工程师");
e1.Salary(1000);
e2.Salary(1200, 500);
e3.Salary(2000, 1000, 2000);
}
}
}
//2.学生的信息初始化——构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentInfo
{
class Program
{
public class Student
{
public int number;
public string name;
public string sclass;
public Student(int num, string xingming, string classId)
{
number = num;
name = xingming;//xingming为姓名
sclass = classId;
}
public void GetInfo()
{
Console.WriteLine("类Student中学号是{0}的学生的姓名是{1},来自{2};\n", number, name, sclass);
}
}
public struct SStudent
{
public int number;
public string name;
public string sclass;
public void SetInfo(int num, string xingming, string classId)
{
number = num;
name = xingming;//xingming为姓名
sclass = classId;
}
public void GetInfo()
{
Console.WriteLine("结构类型SStudent中学号是{0}的学生的姓名是{1},来自{2};\n", number, name, sclass);
}
}
static void Main(string[] args)
{
//类
Student s1 = new Student(101, "张三", "信息191");
Console.WriteLine("请输出学生s1的信息:");
s1.GetInfo();
Student s2 = s1;
s2.number = 102;
s2.name = "王小花";
Console.WriteLine("请输出学生s1的信息:");
s1.GetInfo();
Console.WriteLine("请输出学生s2的信息");
s2.GetInfo();
//结构
SStudent ss1 = new SStudent();//使用方法时需要写new,不写报错
SStudent ss2 = new SStudent();
ss1.SetInfo(201, "云朵朵", "信息192");
Console.WriteLine("请输出学生ss1的信息:");
ss1.GetInfo();
ss2 = ss1;
ss2.name = "令狐花";
ss2.number = 202;
Console.WriteLine("请输出学生ss1的信息:");
ss1.GetInfo();
Console.WriteLine("请输出学生ss2的信息:");
ss2.GetInfo();
}
}
}
//3.计算不同级别员工的工资——方法重载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReloadFunction
{
class Program
{
public class Employee
{
int number;
string name;
string professionalTitle;
public Employee(int num,string name1,string profTitle)
{
number = num;
name = name1;
professionalTitle = profTitle;
}
public void Salary(int basicPay)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元",number,name,professionalTitle,basicPay);
}
public void Salary(int basicPay,int housePay)
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元+住房补贴{4}元={5}元",number,name,professionalTitle,basicPay,housePay,basicPay+housePay);
}
public void Salary(int basicPay, int housePay,int bonus )
{
Console.WriteLine("编号{0},姓名{1},职称为{2},薪水为:基本工资{3}元+住房补贴{4}元+奖金{5}={6}元", number,name, professionalTitle, basicPay, housePay,bonus,basicPay+housePay+bonus);
}
}
static void Main(string[] args)
{
Employee e1 = new Employee(1, "张三", "研究员");
Employee e2 = new Employee(2, "李四", "工程师");
Employee e3 = new Employee(3, "王五", "高级工程师");
e1.Salary(1000);
e2.Salary(1200, 500);
e3.Salary(2000, 1000, 2000);
}
}
}
//4.计算平面上两点之间的距离——引用类型参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculateDistance
{
class Program
{
public class Tpoint
{
int x;
int y;
public Tpoint(int a,int b)
{
x = a;
y = b;
}
public int X
{
get
{ return x; }
set
{ x = value; }
}
public int Y
{
get
{ return y; }
set
{ y = value; }
}
public double Euclidean(Tpoint T1,Tpoint T2)
{
double dis=(Math.Sqrt(Math.Pow((T1.x-T2.x),2)+Math.Pow((T1.x-T2.y),2)));
return dis;
}
public double Manhattan(Tpoint T1,Tpoint T2)
{
double dis = Math.Abs(T1.x - T2.x) + Math.Abs(T1.y - T2.y);
return dis;
}
public double Cosine(Tpoint T1,Tpoint T2)
{
double dis = (T1.x * T2.x + T1.y * T2.y) / (Math.Sqrt((Math.Pow(T1.x, 2) + Math.Pow(T1.y, 2))) * Math.Sqrt((Math.Pow(T2.x, 2) + Math.Pow(T2.y, 2))));
return dis;
}
}
static void Main(string[] args)
{
Tpoint T1 = new Tpoint(2, 2);
Tpoint T2 = new Tpoint(1, 1);
Console.WriteLine("点({0},{1})和点({2},{3})之间的欧式距离是:{4}", T1.X, T1.Y, T2.X, T2.Y, T1.Euclidean(T1, T2));
Console.WriteLine("点({0},{1})和点({2},{3})之间的曼哈顿距离是:{4}", T1.X, T1.Y, T2.X, T2.Y, T1.Manhattan(T1, T2));
Console.WriteLine("点({0},{1})和点({2},{3})之间的余弦距离是:{4}", T1.X, T1.Y, T2.X, T2.Y, T1.Cosine(T1, T2));
}
}
}
//属性
//1、显示学生所选课程的信息——对象交互
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InteractionObject
{
class Program
{
public class Student
{
public int number;
public string name;
public int scores;
public Course c;
public void UpdateCredit()//没有参数
{
scores+=c.CourseCredit;
}
}
public class Course
{
string classNum;
string className;
int courseCredit;
string teacher;
public int CourseCredit
{
get
{
return courseCredit;
}
set
{
courseCredit = value;
}
}
public string ClassNum
{
get
{
return classNum;
}
set
{
classNum = value;
}
}
public string ClassName
{
get
{
return className;
}
set
{
className = value;
}
}
public string Teacher
{
get
{
return teacher;
}
set
{
teacher = value;
}
}
}
static void Main(string[] args)
{
Student s1 = new Student();
s1.name = "王明";
s1.number = 101;
s1.scores = 30;
Course c1 = new Course();
s1.c = c1;
c1.ClassName = "《高阶英语课》";
c1.ClassNum = "Sc1178";
c1.CourseCredit = 4;
c1.Teacher = "Marry";
s1.UpdateCredit();
Console.WriteLine("该生的学号是{0},姓名是{1},已选择了{2}课,这门课的课程号是{3},{4}学分,由{5}讲授,现在该生的总学分为{6}", s1.number, s1.name, c1.ClassName, c1.ClassNum, c1.CourseCredit, c1.Teacher, s1.scores);
}
}
}
//继承
//1.输出不同员工的工作内容——继承
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inheritance1
{
class Program
{
public class Student//基类
{
public string id;
public string name;
public Student(string xh, string xm)
{
id = xh;
name = xm;
}
public virtual void GetInfo()
{
Console.WriteLine("学生的学号是:{0}\n学生的姓名是:{1}", id, name);
}
}
public class UnderGraduate : Student//本科生
{
public Teacher classTeacher;//班主任
public UnderGraduate(string xh,string xm,Teacher laoshi):base(xh,xm)
{
classTeacher=laoshi;
}
public override void GetInfo()
{
base.GetInfo();
Console.WriteLine("班主任的名字为:{0}\t班主任的教工号为:{1}", classTeacher.tname, classTeacher.tid);
}
}
public class Graduate : Student//研究生
{
public Advisor advisor;//导师
public string major;//研究方向
public Graduate(string xh,string xm,Advisor daoshi,string m):base(xh,xm)
{
advisor=daoshi;
major=m;
}
public override void GetInfo()
{
base.GetInfo();
Console.WriteLine("导师的名字是:{0}\t导师的教工号为:{1}\t导师的研究方向:{2}", advisor.tname, advisor.tid, advisor.tmajor);
}
}
public class Teacher//基类
{
public string tid;
public string tname;
public Teacher (string Tid,string TName)
{
tid = Tid;
tname = TName;
}
}
public class Advisor : Teacher//导师
{
public string tmajor;
public Advisor(string tid,string tname,string tmajor_in):base(tid,tname)
{
tmajor=tmajor_in;
}
}
static void Main(string[] args)
{
Teacher t1 = new Teacher("1","郑老师");
UnderGraduate s1 = new UnderGraduate("101","王明",t1);
Advisor a1 = new Advisor("2", "王老师", "情报学");
Graduate g1 = new Graduate("301", "李华", a1,a1.tmajor);
s1.GetInfo();
g1.GetInfo();
}
}
}
//2.计算圆柱体的表面积——派生类的构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Inheritance2
{
class Program
{
public class Circle
{
public const double PI = Math.PI;
public double radius;
public Circle(int r)
{
radius = r;
}
public virtual double Area()
{
return PI * radius * radius;
}
}
public class Cylinder:Circle
{
public double high;
public Cylinder(int r,int h):base(r)
{
radius = r;
high=h;
}
public override double Area()
{
return base.Area()*2 + 2 * PI * radius * high;
}
}
static void Main(string[] args)
{
Circle c1 = new Circle(100);
Console.WriteLine("半径为{0}的圆的面积为{1:N2}", c1.radius,c1.Area());
Cylinder cy1 = new Cylinder(100, 100);
Console.WriteLine("半径为{0},高为{1}的圆柱体的表面积为{2:N2}", cy1.radius,cy1.high,cy1.Area());
}
}
}
//多态
//1.计算圆柱体和圆锥体的表面积——多态
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ploymorphism
{
class Program
{
public class Circle
{
public const double PI = Math.PI;
public double r;
public Circle(double radius)
{
r = radius;
}
public virtual double Area()
{
return r * r * PI;
}
}
public class Cylinder:Circle
{
public double h;
public Cylinder(double r, double height) : base(r)
{
h = height;
}
public override double Area()
{
return base.Area()*2+2*PI*r*h;
}
public void GetInfo()
{
Console.WriteLine("半径为{0},高为{1}的圆柱体的表面积为{2:N2}", r, h, Area());
}
}
public class Cone:Circle
{
public double ch;//圆锥的高为ch
public Cone(double r,double cheight):base(r)
{
ch = cheight;
}
public override double Area()
{
return PI*r*(r+Math.Sqrt(ch*ch+r*r));
}
public void GetInfo()
{
Console.WriteLine("半径为{0},高为{1}的圆锥体的表面积为{2:N2}", r, ch, Area());
}
}
static void Main(string[] args)
{
Circle c1 = new Circle(100);
Cylinder cy1 = new Cylinder(100,100);
Cone co1 = new Cone(100,100);
cy1.GetInfo();
co1.GetInfo();
}
}
}
//2.输出不同员工的工资——new或虚方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wage
{
class Program
{
public class Workers
{
public const int grotwth = 30; //工龄工资为growth
public const int basic = 1000; //普通员工的基本工资为basic
public int workyears; //工龄为workyears
public Workers(int years)
{
workyears = years;
}
public virtual double WageResult() //工资的计算结果为WageResult
{
return basic + grotwth * workyears;
}
}
public class UnderGraduate:Workers //本科生员工
{
public UnderGraduate(int years) : base(years) { }
public override double WageResult()
{
return base.WageResult()*2;
}
}
public class Graduate:UnderGraduate //研究生员工
{
public Graduate(int years) : base( years) { }
public override double WageResult()
{
return base.WageResult()*1.5;
}
}
static void Main(string[] args)
{
Workers workers1 = new Workers(10);
Console.WriteLine("一位工龄为{0}年的普通员工的工资为{1}", workers1.workyears, workers1.WageResult());
UnderGraduate u1 = new UnderGraduate(1);
Console.WriteLine("一位工龄为{0}年的本科生的工资是{1}", u1.workyears, u1.WageResult());
Graduate g1 = new Graduate(1);
Console.WriteLine("一位工龄为{0}年的研究生的工资是{1}", g1.workyears, g1.WageResult());
}
}
}
//3.图书馆管理学生借书权限——抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AstractClass
{
class Program
{
public abstract class Student
{
public abstract void Authority();
}
public class Undergraduate:Student
{
public override void Authority()
{
Console.WriteLine("本科生可以借5本书;");
}
}
public class Postgraduate:Student
{
public override void Authority()
{
Console.WriteLine("硕士生可以借8本书;");
}
}
public class Doctor:Student
{
public override void Authority()
{
Console.WriteLine("博士生可以借10本书。");
}
}
static void Main(string[] args)
{
Undergraduate u1 = new Undergraduate();
Postgraduate p1 = new Postgraduate();
Doctor d1 = new Doctor();
u1.Authority();
p1.Authority();
d1.Authority();
}
}
}
//4.教师和学生信息的显示——抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AstractInfo
{
public abstract class Person
{
protected int id;//id为编号
protected string name;
//定义编号姓名的抽象属性
public abstract int Id { get; set; }
public abstract string Name { get; set; }
//定义输出信息的抽象方法
public abstract void GetInfo();
}
public class Student : Person
{
string studentclass; //studentclass为学生班级
int grade; //grade为学生成绩
public Student(string studentclass, int grade)
{
this.grade = grade;
this.studentclass = studentclass;
}
public override int Id
{
get { return id; }
set { id = value; }
}
public override string Name
{
get { return name; }
set { name = value; }
}
public override void GetInfo()
{
Console.WriteLine("学生的姓名是{0},学号是{1},班级是{2},成绩是{3}。", Name, Id,studentclass,grade);
}
public class Teacher : Person
{
string title; //title为教师职称
string department; //department为教师部门
public Teacher(string title, string department)
{
this.title = title;
this.department = department;
}
public override int Id
{
get { return id; }
set { id = value; }
}
public override string Name
{
get { return name; }
set { name = value; }
}
public override void GetInfo()
{
Console.WriteLine("老师的姓名为{0},教工号为{1},职称为{2},部门为{3}。",Name,Id,title,department);
}
}
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("信息201", 98);
s1.Id = 101;
s1.Name = "李华";
Teacher t1 = new Teacher("副教授", "信管院");
t1.Id = 198;
t1.Name = "Tom";
s1.GetInfo();
t1.GetInfo();
}
}
}
}
//Windows窗体程序设计
//老师上课举的例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace @try//是关键字,自动加了@
{
public partial class Form1 : Form//分部类;form是封装的
{
public Form1()//构造函数
{
InitializeComponent();//组件的初始化
}
private void button1_Click(object sender, EventArgs e)
{
Form1_1 myform = new Form1_1();
myform.Show();
}
}
}
//设计3个窗体Form1、Form1_1和Form1_2,在执行Form1窗体时,用户单击其中的命令按钮分别调用模式窗体Form1_1和无模式窗体Form1-2。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form myform = new Form1_1();
myform.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
Form myform = new Form1_2();
myform.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm
{
public partial class Form1_1 : Form
{
public Form1_1()
{
InitializeComponent();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm
{
public partial class Form1_2 : Form
{
public Form1_2()
{
InitializeComponent();
}
}
}
//实现一道单选题的选择。设计一个窗体Form2,在其中放置一个分组框GroupBox和一个命令按钮button。在GroupBox中放置4个单选框Radiobutton。用户答题后返回一个消息框,如果答对返回信息提示,如果错了也要返回信息提示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton3.Checked)
MessageBox.Show("您选对了,这是微软公司开发的操作系统", "信息提示", MessageBoxButtons.OK);
else if (radioButton1.Checked || radioButton4.Checked)
MessageBox.Show("您选错了,这是程序设计语言", "信息提示", MessageBoxButtons.OK);
else
MessageBox.Show("您选错了,这是数据库管理系统", "信息提示", MessageBoxButtons.OK);
}
}
}
//实现一道多选题的选择。设计一个窗体Form3,在其中放置一个分组框GroupBox和一个命令按钮button。在GroupBox中放置4个复选框CheckBox。用户答题后返回一个消息框,如果答对返回信息提示,如果错了也要返回信息提示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked && checkBox3.Checked && !checkBox2.Checked && !checkBox4.Checked)
MessageBox.Show("您答对了,真的很棒!!!", "信息提示", MessageBoxButtons.OK);
else
MessageBox.Show("您答错了,继续努力吧!!!", "信息提示", MessageBoxButtons.OK);
}
}
}
//设计一个窗体Form4,以选择命令按钮方式显示春夏秋冬四季的图片。在窗体中放置一个图片框pictureBox和四个命令按钮,下载春夏秋冬四季的图片,通过按钮选择显示相应的图片。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("春.jpg");
//pictureBox1.Image = Image.FromFile("D:\\桌面\\WinForm\\WinForm\\bin\\Debug\\春.jpg");
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("夏.jpg");
//pictureBox1.Image = Image.FromFile("D:\\桌面\\WinForm\\WinForm\\bin\\Debug\\夏.jpg");
}
private void button3_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("秋.jpg");
//pictureBox1.Image = Image.FromFile("D:\\桌面\\WinForm\\WinForm\\bin\\Debug\\秋.jpg");
}
private void button4_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("冬.jpg");
//pictureBox1.Image = Image.FromFile("D:\\桌面\\WinForm\\WinForm\\bin\\Debug\\冬.jpg");
}
}
}
//设计一个窗体Form5,通过一个文本框向组合框中添加项目。在窗体中放置两个分组框GroupBox,在其中一个分组框中放置一个文本框textBox和一个命令按钮button,在另一个分组框中放置一个组合框comboBox。在textBox填写一个民族,单击按钮,这个民族就添加到了组合框的下拉表中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
if (!comboBox1.Items.Contains(textBox1.Text))
comboBox1.Items.Add(textBox1.Text);//不添加重复项
}
}
}
C#study汇总
最新推荐文章于 2022-07-04 19:25:09 发布