c#作業題


                                                                          第三章 語法基礎

上機練習

1. 編寫一個控制台程序,要求將字符串中的每個字符顛倒輸出。

 

string str = "ABC";
            Console.WriteLine(str);
            for (int i = str.Length - 1; i >= 0; i--)
            {
                Console.Write(str[i]);
            }
            Console.WriteLine();

 

 

 

2. 編寫一個控制台程序,要求去掉字符串中的所有空格。

 

        //2. 編寫一個控制台程序,要求去掉字符串中的所有空格。
            string str = "  ABC  Dehg JJ";
            Console.WriteLine(str);
            //替換
            string strss=str.Replace(" ", "");
            Console.WriteLine(strss);
            //分割
            string[] strs = str.Split(new char[] {' '});
            foreach (string item in strs)
            {
                Console.Write(item);
            }
            Console.WriteLine();

 

 

 

3. 編寫一個控制台程序,實現從字符串中分離文件路徑、文件名及擴展名的功能。

 

  //3. 編寫一個控制台程序,實現從字符串中分離文件路徑、文件名及擴展名的功能。
            string path = @"F:\c\作業\3班作業\book.exe";
            string[] paths = path.Split('.');
            Console.WriteLine("擴展名:"+paths[1]);
            string[] pathss=paths[0].Split('\\');
            Console.WriteLine(paths[0]);
            Console.WriteLine("文件名:"+pathss[pathss.Length-1]);
            Console.WriteLine("路徑:" + paths[0].Remove(8));

 

 

 

4.  輸入一個字符,判定它是什么類型的字符(大寫字母,小寫字母,數字或者其它字符)

//4.  輸入一個字符,判定它是什么類型的字符(大寫字母,小寫字母,數字或者其它字符)
            Console.WriteLine("請輸入一個字符:");
            char ch = (char)Console.Read();
            if (char.IsUpper(ch))
            {
                Console.WriteLine("大寫字母");
            }
            else if (char.IsLower(ch))
            {
                Console.WriteLine("小寫字母");
            }
            else if (char.IsDigit(ch))
            {
                Console.WriteLine("數字");
            }
            else
            {
                Console.WriteLine("其他");
            }

 

5. 輸入一個字符串,將其中小寫字母改成大寫字母,把大寫字母改成小寫字母,其余字符不變,輸出該字符串。

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("aaddssFF");
            for (int i = 0; i < sb.Length;i++ )
            {
                if (char.IsUpper(sb[i]))
                {
                    sb.Replace(sb[i], char.ToLower(sb[i]),i,1);
                }
                else if (char.IsLower(sb[i]))
                {
                    sb.Replace(sb[i], char.ToUpper(sb[i]), i, 1);
                }
                     
            }
            Console.WriteLine(sb);
            

        }
    }
}

 

 

 

擴展統計下列字符串中每個單詞重復出現的次數(不區分大小寫)。

Sample code may fit more than one of these areas. In those cases, place the sample so it matches the topics you are covering in your documents. Ask yourself what readers will learn from reading your topic. What will they learn from building and running your sample?

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Sample Sample code may fit more than one of these areas. In those cases, place the sample so it matches the topics you are covering in your documents. Ask yourself what readers will learn from reading your topic. What will they learn from building and running your sample?";
            str = str.ToUpper();
            char[] ch = new char[] { ' ', ',', '.', '?' };
            string[] strs = str.Split(ch);
            Console.WriteLine(strs.Length);
            string[] words = new string[strs.Length];
            int num = 0;
            for (int i = 0; i < strs.Length; i++)
            {
                if (!(words.Contains((strs[i]))))
                {

                    words[num] = strs[i];
                    num++;
                    string temp = strs[i];
                    int count = 0;
                    for (int j = 0; j < strs.Length; j++)
                    {
                        if (strs[j] == temp)
                        {
                            count++;
                        }
                    }
                    Console.WriteLine(temp + "出現了" + count + "次!");
                }

            }
            Console.Read();
        }
    }
}

 

 

 

05 流程控制

1、從鍵盤輸入一個字符,程序檢查輸入字符是否是小寫字符、大寫字符或數字。在任何一種情況下,都會顯示適當的消息

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            //1、從鍵盤輸入一個字符,程序檢查輸入字符是否是小寫字符、大寫字符或數字
            char ch = Convert.ToChar(Console.ReadLine());
            if (char.IsLower(ch))
            {
                Console.WriteLine("小寫字母");
            }
            else if(char.IsUpper(ch))
            {
                Console.WriteLine("大寫字母");
            }
            else if(char.IsDigit(ch))
            {
                Console.WriteLine("數字");
            }
        }
    }
}

 

 

 

2、設計一個簡單的猜數游戲:隨機產生一個1-100的數,要求輸入的數與隨機產生的數進行比較,如果輸入的數大於隨機產生的數,提示:“對不起,您猜大了!”;如果輸入的數小於隨機產生的數,提示:“對不起,您猜小了!”;如果輸入的數等於隨機產生的數,提示:“恭喜您,您猜對了!”程序結束。

提示:隨機產生一個1-100的整數的方法

Random rnd = new Random();//創建隨機數種子

int rndNumber = rnd.Next(1, 100);//返回一個指定范圍內的整數

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            //2、設計一個簡單的猜數游戲:隨機產生一個1-100的數,
            //要求輸入的數與隨機產生的數進行比較,如果輸入的數大於隨機產生的數,
            //提示:“對不起,您猜大了!”;如果輸入的數小於隨機產生的數,提示:“對不起,您猜小了!”
            //;如果輸入的數等於隨機產生的數,提示:“恭喜您,您猜對了!”程序結束。
            //提示:隨機產生一個1-100的整數的方法
            Random rnd = new Random();//創建隨機數種子
            int rndNumber = rnd.Next(1, 100);//返回一個指定范圍內的整數
            Console.WriteLine("請猜數:");
            while(true)
            {
                int num = Convert.ToInt32(Console.ReadLine());
                if (num < rndNumber)
                {
                    Console.WriteLine("猜小了");
                }
                else if (num > rndNumber)
                {
                    Console.WriteLine("猜大了");
                }
                else
                {
                    Console.WriteLine("恭喜您猜對了!");
                    break;
                }
            }
           
        }
    }
}

 

 

 

3、從鍵盤輸入一個數字作為行數,使用for循環語句,在命令窗口中繪制如圖所示的“金字塔”。

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {       
            //3、從鍵盤輸入一個數字作為行數,使用for循環語句,
            //在命令窗口中繪制如圖所示的“金字塔”。
            Console.WriteLine("請輸出金字塔的行數:");
            int num = Convert.ToInt32(Console.ReadLine());
            for (int i = num-1; i>=0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 0; k < 2*(num - i)-1; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            
        }
    }
}

 

 

4、創建一個控制台程序,計算1+2+3+.....+n!的值並輸出,n從鍵盤輸入。

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            //4、創建一個控制台程序,計算1!+2!+3!+.....+n!的值並輸出,n從鍵盤輸入。
            Console.WriteLine("請輸入整數n:");
            int n = Convert.ToInt32(Console.ReadLine());
            int temp=1;
            int rel = 0;
            for (int i = n; i > 0; i--)
            {
                temp = 1;
                for (int j = 1; j <= i; j++)
                {
                    temp = temp * j;
                }
                rel = rel + temp;
            }
            Console.WriteLine(rel);
 

            
        }
    }
}

 

 

 

5、創建一個控制台程序,從鍵盤輸入一個作為月份的數字,使用switch語句,將月份所在的季節輸出到控制台。

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {       
            //5、創建一個控制台程序,從鍵盤輸入一個作為月份的數字,
            //使用switch語句,將月份所在的季節輸出到控制台。
            Console.WriteLine("輸入月份數字:");
            int mo = Convert.ToInt32(Console.ReadLine());
            switch (mo)
            {
                case 1:
                case 2:
                case 3:
                    Console.WriteLine("春季");
                    break;
                case 5:
                case 6:
                case 4:
                    Console.WriteLine("夏季");
                    break;
                case 7:
                case 8:
                case 9:
                    Console.WriteLine("秋季");
                    break;
                default:
                    Console.WriteLine("冬季");
                    break;
            }
            
        }
    }
}

 

集合與泛型

1、數制轉換問題。數制轉換問題是將任意一個非負的十進制數轉換為其它進制的數,這是計算機實現計算的基本問題。其一般的解決方法的利用輾轉相除法。以將一個十進制數N轉換為八進制數為例進行說明。假設N=5142,示例圖:

N   N/8(整除)  N%8(求余)      低

5142   642 6

642 80 2                                                                                                                                  

80 10 0

10 1 2

1 0 1 高

從圖可知,(5142)10=(12026)8。編寫一個控制台程序,實現十進制數轉換成八進制數

(提示:轉換得到的八進制數各個數位是按從低位到高位的順序產生的,而轉換結果的輸出通常是按照從高位到低位的順序依次輸出。也就是說,輸出的順序與產生的順序正好相反,這與棧的操作原則相符。所以,在轉換過程中可以使用一個棧,每得到一位八進制數將其入棧,轉換完畢之后再依次出棧。)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {
        //十進制轉八進制
        static void Main(string[] args)
        {
            //以將一個十進制數N轉換為八進制數為例進行說明。假設N=5142,示例圖:
            //從圖可知,(5142)10=(12026)8。編寫一個控制台程序,實現十進制數轉換成八進制數
            //(提示:轉換得到的八進制數各個數位是按從低位到高位的順序產生的,而轉換結果的輸出通常是按照從高位到低位的順序依次輸出。也就是說,輸出的順序與產生的順序正好相反,這與棧的操作原則相符。所以,在轉換過程中可以使用一個棧,每得到一位八進制數將其入棧,轉換完畢之后再依次出棧。)
            //2、編寫一個控制台程序,把控制台輸入的數組字符串(如:"123")轉換為中文大寫(如:壹貳叄)。(要求使用Dictonary<T>)
            //3、編寫一個控制台程序,實現List<T>的添加、插入、刪除、查找、排序等功能。
            Console.WriteLine("請輸入一個十進制的數:");
            int num = Convert.ToInt32(Console.ReadLine());
            Stack<int> nums = new Stack<int>();
            nums.Push(num % 8);
            int  n = num / 8;
            while(n!=0)
            {
                nums.Push(n % 8);
                n = n / 8;
            }
            foreach (int item in nums)
            {
                Console.Write(item);
            }
            Console.ReadKey();        
        }

    }
}

 

2、編寫一個控制台程序,控制台輸入的數組字符串(如:"123"轉換為中文大寫(如:壹貳叄)。(要求使用Dictonary<T>)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {        

        static void Main(string[] args)
        {
            //2、編寫一個控制台程序,把控制台輸入的數組字符串(如:"123")轉換為中文大寫(如:壹貳叄)。
            Console.WriteLine("請輸入一個數組字符串:");
            string str = Convert.ToString(Console.ReadLine());
            //(要求使用Dictonary<T>)
            Dictionary<int,string> dic= new Dictionary<int,string>(); 
            dic.Add(1,"");
            dic.Add(2, "");
            dic.Add(3, "");
            dic.Add(4, "");
            dic.Add(5, "");
            dic.Add(6, "");
            dic.Add(7, "");
            dic.Add(8, "");
            dic.Add(9, "");
            dic.Add(0, "");
            Console.WriteLine("轉換");
            foreach (char item in str)
            {
                int n = (int)item -48;
                str=str.Replace(Convert.ToString(n),dic[n]);
            }
            Console.WriteLine(str);
            //3、編寫一個控制台程序,實現List<T>的添加、插入、刪除、查找、排序等功能。
        }
    }
}

 

3、編寫一個控制台程序,實現List<T>的添加、插入、刪除查找、排序等功能。

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {

        static void Main(string[] args)
        {
            
//3、編寫一個控制台程序,實現List<T>的添加、插入、刪除、查找、排序等功能。
            //list實例化
            List<string> li = new List<string>();
            //List添加
            li.Add("list1");
            li.Add("list2");
            li.Add("list3");
            li.AddRange(li);
            //list刪除
            li.Remove("list1");
            li.RemoveAt(1);
            //list查找
            li.IndexOf("list2");
            //list排序
            li.Sort();
            foreach (string item in li)
            {
                Console.WriteLine(item);
            }
            
        }
    }
}

 

 

 

拓展軟件設計大賽題目

文字祖瑪游戲需求如下:

1).程序通過控制台輸出一個字符串,由ABCDE五個字母組成,例如:ACBEEBBAD

2).用戶輸入一個字符,只能是ABCDE其中之一,然后再輸入一個要插入的位置。

3).程序會將這個字符插入到字符串的指定位置前(第一個字符位置為0,第二個字符位置為1,依此類推),然后消除連續出現的三個相同的字符,直到沒有連續三個相同的字符為止。

例如:

控制台輸出:ACBEEBBAD

用戶輸入:E, 3

控制台輸出:ACAD

以上示例表示:在位置3插入E后,結果是:ACBEEEBBAD,消除連續的三個E,結果是:ACBBBAD再次消除連續三個B,結果是:ACAD

要求如下:

A.為實現此游戲,需要設計一個方法DealString()

/**

* 參數:

* str: 原始字符串

* index: 要插入字符的位置

* letter: 要插入的字符

* 返回結果: 經過處理后的字符串

**/

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test3
{

    class Program
    {
        static void Main(string[] args)
        {
            //1).程序通過控制台輸出一個字符串,由A、B、C、D、E五個字母組成,例如:ACBEEBBAD。
            Console.WriteLine("請輸入一串字符串(ABCDE):");
            string str = Convert.ToString(Console.ReadLine());
            //2).用戶輸入一個字符,只能是A、B、C、D、E其中之一,然后再輸入一個要插入的位置。
            Console.WriteLine("請輸入一個字符(ABCDE):");
            string ch = Convert.ToString(Console.ReadLine());
            Console.WriteLine("請輸入要插入的位置:");
            int pos = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(str);
            str=str.Insert(1,ch);
            Console.WriteLine(str);
            //3).程序會將這個字符插入到字符串的指定位置前(第一個字符位置為0,第二個字符位置為1,依此類推),然后消除連續出現的三個相同的字符,直到沒有連續三個相同的字符為止。
            for (int i = 0; i < str.Length-2; i++)
            {
                if (str[i] == str[i + 1] && str[i + 1] == str[i + 2])
                {                    
                    str=str.Remove(i,1);
                    str=str.Remove(i,1);
                    str=str.Remove(i,1);
                }
            }
            Console.WriteLine(str);
            //例如:
            //控制台輸出:ACBEEBBAD
            //用戶輸入:E, 3
            //控制台輸出:ACAD
            //以上示例表示:在位置3插入E后,結果是:ACBEEEBBAD,消除連續的三個E,結果是:ACBBBAD再次消除連續三個B,結果是:ACAD。
            //要求如下:
            //A.為實現此游戲,需要設計一個方法DealString()
            /**
            * 參數:
            * str: 原始字符串
            * index: 要插入字符的位置
            * letter: 要插入的字符
            * 返回結果: 經過處理后的字符串
            **/
        
        }

    }
}

 

 

 

11 屬性和索引器

上機練習題

1、編寫代碼實現需求: 

編寫一個類Student,代表學員,要求: 

(1)具有屬性:姓名、年齡,其中年齡不能小於16歲,否則輸出錯誤信息 

(2)具有方法:自我介紹,負責輸出該學員的姓名、年齡

2. 編寫一個類Student1,代表學員,要求: 

(1)具有屬性:姓名、年齡、性別、專業 

(2)具有方法:自我介紹,負責輸出該學員的姓名、年齡、性別、以及專業 

(3)具有兩個帶參構造方法:第一個構造方法中,設置學員的性別為男、專業為計算機,其余屬性的值由參數給定;第二個構造方法中,所有屬性的值都由參數給定

(4)編寫測試類

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test3
{
//編寫一個類Student,代表學員,要求: 
    //(1)具有屬性:姓名、年齡,其中年齡不能小於16歲,否則輸出錯誤信息 
    //(2)具有方法:自我介紹,負責輸出該學員的姓名、年齡
    class student
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int age;

        public int Age
        {
            get { return age; }
            set {
                if (value >= 16)
                {
                    age = value;
                }
                else
                {

                    Console.WriteLine("error");
                }
            }
        }
        public void intro()
        {
            Console.WriteLine("姓名:"+name);
            Console.WriteLine("年齡:"+age);

        }
        public student(string name, int age)
        {
            this.age = age;
            this.name = name;
        }


    }
//2. 編寫一個類Student1,代表學員,要求: 
//    (1)具有屬性:姓名、年齡、性別、專業 
//(2)具有方法:自我介紹,負責輸出該學員的姓名、年齡、性別、以及專業 
    class student1
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        string zhuanye;

        public string Zhuanye
        {
            get { return zhuanye; }
            set { zhuanye = value; }
        }
        public void intro()
        {
            Console.WriteLine("我的姓名是:"+name);
            Console.WriteLine("我的年齡是:"+age);
            Console.WriteLine("我得性別是:"+sex);
            Console.WriteLine("我的專業是:"+zhuanye);
           

        }
         public student1(string name,int age,string sex,string zhuanye)
         {
             this.name=name;
             this.age=age;
             this.sex=sex;
             this.zhuanye=zhuanye;

         }
         public student1(string name, int age)
         {
             this.name = name;
             this.age = age;
             this.sex = "nan";
             this.zhuanye = "it";

         }
    
    }
//(4)編寫測試類
//(3)具有兩個帶參構造方法:第一個構造方法中,
//    設置學員的性別為男、專業為計算機,其余屬性的值由參數給定;第二個構造方法中,所有屬性的值都由參數給定
    class Program
    {
        static void Main(string[] args)
        {
            student stu1 = new student("name2",14);
            student1 stu2 = new student1("name3",17,"nv","it");
            student1 stu3 = new student1("name4",15);
            stu1.intro();
            Console.WriteLine();
            stu2.intro();
            Console.WriteLine();
            stu3.intro();

        }

    }
}

 

 

3、屬性和索引練習

(1) 定義一個Student類,其包含屬性:SId(學號), Name(姓名),Score(總分),並重載其構造方法,初始化其屬性。

(2) 定義一個班級類ClassDemo,其包含屬性Count(學生人數,只讀),List<Student>(學生列表);定義其索引器,按照序號獲得學生對象,按照學號和姓名獲得學生對象(索引重載)。

(3) 編寫測試類,創建4個學生對象:

學號

姓名

總分

1

張三

375

2

李四

400

3

王五

425

4

薛六

498

  1. 將以上學生添加到班級,輸出班級人數,並遍歷班級學生的信息;
  2. 使用索引器查找學號是2,姓名是“李四”的總分,並輸出到控制台。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test3
{
    class student
    {
        string sid;

        public string Sid
        {
            get { return sid; }
            set { sid = value; }
        }

        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        int score;

        public int Score
        {
            get { return score; }
            set { score = value; }
        }
        public student(string sid, string name, int score)
        {
            this.sid = sid;
            this.name = name;
            this.score = score;
        }
    }

    class ClassDemo
    {
        int count;

        public int Count
        {
            get
            {
                count = stu.Count;
                return count;
            }
            set { count = value; }
        }
        List<student> stu;

        internal List<student> Stu
        {
            get { return stu; }
            set { stu = value; }
        }
        //定義其索引器,按照學號和姓名獲得學生對象(索引重載)。
        public student this[int index]
        {
            get
            {
                if (index > 0 && index < stu.Count)
                {
                    return stu[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (index >= 0 && index < stu.Count)
                {
                    stu[index] = value;
                }
            }
        }

        public student this[string sid]
        {
            get
            {
                for (int i = 0; i < stu.Count; i++)
                {
                    if (stu[i].Sid == sid)
                    {
                        return stu[i];
                    }
                }
                return null;
            }
            set
            {
                for (int i = 0; i < stu.Count; i++)
                {
                    if (stu[i].Sid == sid)
                    {
                        stu[i] = value;
                    }
                }
            }
        }
        public student this[string sid, string name]
        {
            get
            {
                foreach (student item in stu)
                {
                    if (item.Sid == sid && item.Name == name)
                    {
                        return item;
                    }
                }
                return null;
            }
            set
            {
                for (int i = 0; i < stu.Count; i++)
                {
                    if (stu[i].Name == name && stu[i].Sid == sid)
                    {
                        stu[i] = value;
                    }
                }
            }
        }
        public ClassDemo(int count)
        {
            this.count = count;
            stu = new List<student>(count);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //(3) 編寫測試類,創建4個學生對象:
            student stu1 = new student("001", "name1", 98);
            student stu2 = new student("002", "name2 ", 94);
            student stu3 = new student("003", "name3", 95);
            student stu4 = new student("004", "name4", 92);
            //將以上學生添加到班級,輸出班級人數,並遍歷班級學生的信息;
            ClassDemo cd = new ClassDemo(4);
            cd.Stu.Add(stu2);
            cd.Stu.Add(stu2);
            Console.WriteLine();
            cd.Stu.Add(stu3);
            cd.Stu.Add(stu4);
            foreach (student item in cd.Stu)
            {
                Console.Write(item.Name + " ");
            }
            Console.WriteLine();
            cd[0] = stu2;
            cd[1] = stu2;
            cd[2] = stu2;
            cd[3] = stu4;
            foreach (student item in cd.Stu)
            {
                Console.Write(item.Name + "* ");
            }
            Console.WriteLine();
            Console.WriteLine(cd.Count);
            //使用索引器查找學號是004,姓名是“name4”的總分,並輸出到控制台。
            Console.WriteLine(cd["004", "name4"].Score);
        }

    }
}

 

 

13.抽象類和接口

 

上機練習題

 

1、定義一個Shape類,該類有名為ColorString類型的數據成員,還有一個名為GetColor方法(用於獲取圖形的顏色),另外還有一個名為GetArea的抽象方法。在Shape類的基礎上定義其子類Circle,子類有半徑R數據成員,並提供對GetArea方法的實現。以計算相應圖形的面積。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test3
{
    //1、定義一個Shape類,該類有名為Color的String類型的數據成員,
    class shape
    {
        string color;
        public string Color
        {
            get { return color; }
            set { color = value; }
        }
        //還有一個名為GetColor方法(用於獲取圖形的顏色),
        public string Getcolor()
        {
            return color;
        }
        //另外還有一個名為GetArea的抽象方法。
        public virtual double  GetArea()
        {
            return 0.0;
        }
        public  shape(string color)
        {
            this.color = color;
        }
    }
    //在Shape類的基礎上定義其子類Circle,
    //子類有半徑R數據成員,並提供對GetArea方法的實現。
    //以計算相應圖形的面積。
    class Circle:shape
    {
        int r;
        public override double GetArea()
        {
            return r * Math.PI * Math.PI;
        }
        public Circle(string color,int r):base(color)
        {
            this.r=r;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            shape sh = new Circle("red",1);
            Console.WriteLine("該圓的面積為"+sh.GetArea());
            Console.WriteLine("該圓的顏色是:"+sh.Getcolor());
        }
    }
}

 

 

 

2、定義如下接口,任何類使用該接口都可產生一系列數字。

 

public interface IDataSeries
{
    int  GetNext();    //返回下一個系列數字
    void Reset();      //重新開始
    void SetInit(int i);  //設置系列數字的開始值
}

 

創建一個類FiveSeries,實現上述接口,用於產生一個公差為5的的數列。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test3
{
    public interface IDataSeries
    {
        int GetNext();    //返回下一個系列數字
        void Reset();      //重新開始
        void SetInit(int i);  //設置系列數字的開始值
    }
    public class get
    {
        int num;
        int temp;
        public int GetNext()
        {
            num = num + 5;
                return num;
        }
        public void Reset()
        {
            this.num = temp;
        }
        public void SetInit(int i)
        {
            this.num = i-5;
            this.temp = i;
        }
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            get get = new get();
            get.SetInit(1);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(get.GetNext());
            }
      }
                
    }
}

 

 

 

 

15.文件操作

 

上機練習題

 

創建一個控制台程序,功能要求:

 

1、創建一個名為Letter的文本文件;

 

2、把A-Z的26個英文字母保存到上述文件中;

 

3、把Letter文件中的內容讀取到程序中;

 

4、把Letter文件名修改為New-Letter;

 

5、拷貝New-Letter文件到Copy-Letter;

 

6、刪除New-Letter和Copy-Letter文件。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM