C# 第3次实验报告:面向对象编程基础

时间:2025-02-17 07:19:27
  • using System;
    
    namespace _3_1
    {
    	class Student
    	{
    		public string Name;
    		public double Age;
    		public string Hobby;
    
    		public void hello()
    		{
    			Console.WriteLine("我叫{0},我今年{1}岁了,我的爱好是{2}。", this.Name, this.Age, Hobby);
    		}
    	}
    }
    
    using System;
    
    namespace _3_1
    {
    	class Teacher
    	{
    		public string Name;
    		public double Age;
    		public double WorkingAge;
    
    		public void hello()
    		{
    			Console.WriteLine("我叫{0},我今年{1}岁了,我的工龄是{2}。", this.Name, this.Age, WorkingAge);
    		}
    	}
    }
    
    using System;
    
    namespace _3_1
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Student stu = new Student()
    			{
    				Name = "Coco",
    				Age = 22,
    				Hobby = "coding"
    			};
    			stu.hello();
    
    			Teacher t = new Teacher();
    			t.Name = "Mr. Smith";
    			t.Age = 42;
    			t.WorkingAge = 15;
    			t.hello();
    
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    		}
    	}
    }
    
  • using System;
    
    namespace _3_2
    {
    	class Person
    	{
    		public string Name;
    		public string Blood;
    
    		public Person(string name, string blood)
    		{
    			Name = name;
    			Blood = blood;
    		}
    
    		public void printName()
    		{
    			Console.WriteLine("我叫{0}。", this.Name);
    		}
    
    		public void printBlood()
    		{
    			Console.WriteLine("我的血型是{0}。", this.Blood);
    		}
    	}
    }
    
    using System;
    
    namespace _3_2
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Person p = new Person("江涛", "AB");
    			p.printName();
    			p.printBlood();
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    		}
    	}
    }
    
  • using System;
    
    namespace _3_3
    {
    	class Circle
    	{
    		public double R;
    		public Circle(double r)
    		{
    			R = r;
    		}
    
    		public void c()
    		{
    			Console.WriteLine("周长{0}。", 2*Math.PI* R);
    		}
    
    		public void s()
    		{
    			Console.WriteLine("面积{0}。", Math.PI * R * R);
    		}
    	}
    }
    
    using System;
    
    namespace _3_3
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Circle c = new Circle(1);
    			c.c();
    			c.s();
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    		}
    	}
    }
    
  • using System;
    
    namespace _3_4
    {
    	class Test
    	{
    		public double[] Nums;
    		public Test(double[] numbers)
    		{
    			Nums = numbers;
    		}
    		public void average()
    		{
    			double s=0;
    			foreach (var item in Nums)
    			{
    				s += item;
    			}
    			Console.WriteLine("均值为:{0}。", s/Nums.Length);
    		}
    	}
    }
    
    using System;
    
    namespace _3_4
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			double[] n = { 1, 1, 2 };
    			Test t=new Test(n);
    			t.average();
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    		}
    	}
    }
    
  • using System;
    
    namespace _3_5
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			string str = "2008-08-08";
    			DateTime date = DateTime.Parse(str);
    			Console.WriteLine(date.ToLongDateString());
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    		}
    	}
    }
    
  • using System;
    using System.Linq;
    using System.Text;
    
    namespace _3_6
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			string a = "abc";
    
    			var query = a.Reverse();
    
    			StringBuilder sbStr = new StringBuilder();
    			foreach (var item in query)
    			{
    				sbStr.Append(item);
    			}
    
    			Console.WriteLine(sbStr.ToString());
    
    			string s = "abc";
    			char[] ss = s.ToCharArray();
    			StringBuilder sb = new StringBuilder();
    			for (int i = ss.Length - 1; i >= 0; i--)
    			{
    				sb.Append(ss[i]);
    			}
    			Console.WriteLine(sb.ToString());
    
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    		}
    	}
    }
    
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _3_7
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			string[] arr = "hello c sharp".Split(' ');
    			string result = string.Empty;
    			for (int i = arr.Count() - 1; i >= 0; i--)
    			{
    				result += arr[i] + " ";
    			}
    			Console.WriteLine("反转后的英文句子是:\n方法一:" + result);
    
    			Console.Write("方法二:");
    			string[] arr2 = "hello c sharp2".Split(' ');
    			Array.Reverse(arr2);
    			foreach (var item in arr2)
    			{
    				Console.Write(item + " ");
    			}
    			Console.WriteLine();
    
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadLine();
    		}
    	}
    }
    
  • using System;
    
    namespace _3_8
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			//练习:从email中提取用户名和域名 abc@
    			string str = "abc@";
    			int index = str.IndexOf('@');
    			string strUser = str.Substring(0, index);
    			string strDNS = str.Substring(index + 1);
    			Console.WriteLine(strDNS);
    			Console.WriteLine(strUser);
    
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    		}
    	}
    }
    
  • using System;
    
    namespace _3_9
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Random random = new Random();
    			//for (int i = 0; i < 10; i++)
    			//	("The latest verification code is: " + getVerificationCode(random));
    			inputCode(getVerificationCode(random));
    
    			Console.WriteLine("\nPress any key to quit.");
    			Console.ReadKey();
    
    		}
    
    		public static void inputCode(string code)
    		{
    			Console.WriteLine("The latest verification code is: " + code);
    			Console.WriteLine("Please input the latest verification code.");
    			while (true)
    			{
    				string s = Console.ReadLine();
    				if(s==code)
    				{
    					Console.WriteLine("Congratulation, you passed the validation.");
    					break;
    				}
    				Console.WriteLine("Sorry, your input is wrong, please try again.");
    			}
    		}
    
    		public static string getVerificationCode(Random random)
    		{
    			string str = "";
    			for (int i = 0; i < 4; i++)
    			{
    				int key = random.Next(3);
    				switch (key)
    				{
    					case 0:
    						int code1 = random.Next(10);
    						str += code1;
    						break;
    					case 1:
    						char code2 = (char)(random.Next(26) + 65);
    						str += code2;
    						break;
    					case 2:
    						char code3 = (char)(random.Next(26) + 97);
    						str += code3;
    						break;
    				}
    			}
    			return str;
    		}
    	}
    }