C#第四次上机

来源:互联网 发布:癌症晚期知乎 编辑:程序博客网 时间:2024/04/30 09:19

 (1)设有一个描述坐标点的CPoint类,其私有变量x和y代表一个点的x,y坐标值。编写程序实现以下功能:利用构造函数传递参数,并设其默认参数值为60和75,利用成员函数display()输出这一默认值;利用公有成员函数setpointQ将坐标值修改为(80,150),并利用成员函数输出修改后的坐标值

 

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyCs{    class Program    {        static void Main(string[] args)        {            CPoint P1 = new CPoint();            Console.Write("调用无参构造函数:");            P1.display();            CPoint P2 = new CPoint(60, 75);            Console.Write("调用有参构造函数:");            P2.display();            P2.setpointQ(80, 150);            Console.Write("坐标值修改后:");            P2.display();            Console.ReadKey();        }    }    class CPoint    {        protected int x;        protected int y;        public CPoint()        {        }        public CPoint(int xx, int yy)        {            x = xx;            y = yy;        }        public void display()        {            Console.WriteLine("({0},{1})", x, y);        }        public void setpointQ(int x1, int y1)        {            x = x1;            y = y1;        }    }}


 

(2)定义一个人员类CPerson,数据成员包括:姓名,编号,性别和用于输入输出的成员函数。在此基础上派生出学生类CStudent(增加成绩)和教师类CTeacher(增加教龄),并实现对学生和教师信息的输入/输出。

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CPP{    class Program    {        static void Main(string[] args)        {            CStudent stu = new CStudent();            stu.Input();            stu.Output();            CTeacher tea = new CTeacher();            tea.Input();            tea.Output();            Console.ReadKey();        }    }    class CPerson    {        public string name;        public int num;        public string sex;        public CPerson()        {        }        public CPerson(string na, int n, string s)        {            name = na; num = n;            sex = s;        }        public void input()        {            Console.WriteLine("name,num,sex");            string[] str = Console.ReadLine().Split(' ');            name = str[0];            num = int.Parse(str[1]);            sex = str[2];        }        public void output()        {            Console.WriteLine("姓名: {0} ", name);            Console.WriteLine("编号: {0} ", num);            Console.WriteLine("性别: {0} ", sex);        }    }    class CStudent : CPerson    {        protected int grade;        public CStudent()        {        }        public CStudent(string na, int n, string s, int gr)            : base(na, n, s)        {            this.grade = gr;        }        public void Input()        {            base.input();            Console.WriteLine("grade");            string ss = Console.ReadLine();            int gr = int.Parse(ss);            grade = gr;        }        public void Output()        {                        base.output();            Console.WriteLine("成绩: {0} ", grade);        }    }    class CTeacher : CPerson    {        protected int year;        public CTeacher()        {        }        public CTeacher(string na, int n, string s, int y)            : base(na, n, s)        {            this.year = y;        }        public void Input()        {            base.input();            Console.WriteLine("year");            string SS = Console.ReadLine();            int ye = int.Parse(SS);            year = ye;        }        public void Output()        {            base.output();            Console.WriteLine("教龄: {0} ", year);        }    }}


 

(3)输入一个由若干字符组成的字符串,写一个静态方法,方法中使用输出参数输出其中的大写字母,小写字母,数字和其他字符的个数。

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CPP{         class Program    {        static void Main(string[] args)        {            int daxie = 0;            int xiaoxie = 0;            int num = 0;            int other = 0;            string str = Console.ReadLine();            char[] ss = str.ToCharArray();            Test.GetNum(out  daxie, out xiaoxie, out num,out other,ss);            Console.WriteLine("该字符串中共有{0}个大写字母,{1}个小写字母,{2}个数字,{3}个其他字符", daxie, xiaoxie, num, other );            Console.ReadKey();        }         }   class Test   {       public static void  GetNum(out int daxie,out int xiaoxie,out int num,out int other ,params char [] ss)       {           daxie = 0;           xiaoxie = 0;           num = 0;           other = 0;           for(int i=0;i<ss.Length;i++)         {          if(ss[i]>='A'&& ss[i]<='Z')          {            daxie++;          }          else if(ss[i]>='a'&& ss[i]<='z')          {              xiaoxie++;          }          else if(ss[i]>='0'&& ss[i]<='9')          {              num++;          }          else          {              other++;          }         }       }   }}    


 

 

 

0 0
原创粉丝点击