C#函数拓展(EduCoder实训题目)

2021-01-08 07:28

阅读:451

标签:委托   cti   ice   open   str   重载   div   reading   space   

第1关:结构函数

技术图片技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H1
{

    class Program
    {
        /********** Begin *********/
        struct book
        {
            public string author;
            public string bookName;
            public double price;
            public int yearOfPublication;
            public void setAuthor(string s)
            {
                author = s;
            }
 
            public void setBookName(string s)
            {
                bookName = s;
            }
 
            public void setPrice(double s)
            {
                price = s;
            }
 
            public void setYearOfPublication(int s)
            {
                yearOfPublication = s;
            }
 
            public string getInformation()
            {
                string info = "author:" + author + ",price:" + price + ",yearOfPublication:" + yearOfPublication
                    + ",bookeName:" + bookName;
                return info;
            }
        }
        /********** End *********/

        static void Main(string[] args)
        {
            book orderBook = new book();

            orderBook.setAuthor("Cixin Liu");
            orderBook.setBookName("The three body problem");
            orderBook.setPrice(40);
            orderBook.setYearOfPublication(2006);

            Console.WriteLine(orderBook.getInformation());

        }
    }
}
View Code

第2关:函数的重载

技术图片技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H2
{
    class Program
    {
        struct book
        {
            string author;
            int price;
            int yearOfPublication;
            string bookName;

            public string getAuthor()
            {
                return author;
            }
            public void setAuthor(string _author)
            {
                author = _author;
            }
            public int getPrice()
            {
                return price;
            }
            public void setPrice(int _price)
            {
                price = _price;
            }
            public int getYearOfPublication()
            {
                return yearOfPublication;
            }
            public void setYearOfPublication(int _yearOfPublication)
            {
                yearOfPublication = _yearOfPublication;
            }
            public string getBookName()
            {
                return bookName;
            }
            public void setBookName(string _bookName)
            {
                bookName = _bookName;
            }

            public void showInformation()
            {
                Console.WriteLine("author:" + author);
                Console.WriteLine("book name:" + bookName);
                Console.WriteLine("price:" + price);
                Console.WriteLine("year of publication:" + yearOfPublication);
            }

            public void setParam(book _bk)
            {
                author = _bk.getAuthor();
                price = _bk.price;
                yearOfPublication = _bk.getYearOfPublication();
                bookName = _bk.getBookName();
            }

            /********** Begin *********/
            //your function
            public void setParam(string _author, int _price, int _yearOfPublication, string _bookName)
            {
            author = _author;
            price = _price;
            yearOfPublication = _yearOfPublication;
            bookName = _bookName;
            }
            /********** End *********/
        }

        static void Main(string[] args)
        {

            book yourBook = new book();

            //book myBook = new book();
            //myBook.setAuthor("Hemingway");
            //myBook.setBookName("The Old Man and the Sea");
            //myBook.setPrice(30);
            //myBook.setYearOfPublication(1952);
            //yourBook.setParam(myBook);

            /********** Begin *********/
            //Use your functions instead of comments section
            yourBook.setParam("Hemingway", 30, 1952, "The Old Man and the Sea");
            /********** End *********/
            yourBook.showInformation();

        }
    }
}
View Code

第3关:委托

技术图片技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H3
{
    class Program
    {
        /********** Begin *********/
        //delegate
        delegate string myRead();
        static void Apply(myRead r)
        {
            string str = r();
            Console.WriteLine("my words:" + str);
        }
        //Apply function

        /********** End *********/
        static void Main(string[] args)
        {

            Apply(Console.ReadLine);

        }
    }
}
View Code

C#函数拓展(EduCoder实训题目)

标签:委托   cti   ice   open   str   重载   div   reading   space   

原文地址:https://www.cnblogs.com/QQ1723545340/p/13131153.html


评论


亲,登录后才可以留言!