zoukankan      html  css  js  c++  java
  • C#实验题目

    1、定义一个静态成员方法,该方法用于提取文件名。比如,给定一个字符串“c:program filesMathsall.dat”,使用该方法即可获取文件名all.dat。自行设计程序验证上述方法正确性。

       public static string getFilename(stringfile)

       {

          //提示:主体中使用string类的indexof方法和substring方法

       }

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace sr
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(getFilename(@"c:program filesMathsall.dat"));
                Console.ReadKey();
            }
            public static string getFilename(string file)
            {
                int p=file.LastIndexOf(@"");
                string name;
                name=file.Substring(p+1);
                return name;
            }
        }
    }
    

    2、定义一个静态成员方法,该方法实现字符串反转。自行设计程序验证上述方法正确性。

       public static string Reverse(string str)

       {

          //方法主体中使用StringBuilder

       }

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace sr
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(Reverse("abcdefghi"));
                Console.ReadKey();
            }
            public static string Reverse(string str)
            {
                StringBuilder sb = new StringBuilder(50);
                for (int i = str.Length - 1; i >= 0; i--)
                {
                    sb.Append(str[i]);
                }
                return sb.ToString();
            }
        }
    }
    
    3.输入学号和姓名,对不存在的学号加到hashtable类的实例中,对存在学号给出提示。结束输入后,输出学号为奇数的所有学生。

    代码:

    using System;
    using System.Collections;
    using System.Linq;
    using System.Text;
    
    namespace sr
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hashtable hst = new Hashtable();
                string name, xuehao;
                Console.WriteLine("输入要添加的个数:");
                int n;
                n = int.Parse(Console.ReadLine());
                for (int i = 1; i <= n; i++)
                {
                    Console.WriteLine("输入学号:");
                    xuehao = Console.ReadLine();
                    bool find = true;
                    foreach (DictionaryEntry item in hst)
                    {
                        if (hst.Contains(xuehao))
                        {
                            find = false;
                            break;
                        }
                    }
                    if (find == false)
                    {
                        Console.WriteLine("学号已存在,请重新输入:");
                        i--;
                    }
                    else
                    {
                        Console.WriteLine("请输入姓名:");
                        name = Console.ReadLine();
                        Console.WriteLine("添加成功!");
                        hst.Add(xuehao, name);
                    }
                }
                foreach (DictionaryEntry item in hst)
                {
                    if (int.Parse ((string )item.Key) % 2 == 1)
                        Console.WriteLine("{0},{1}", item.Key, item.Value);
                }
                Console.ReadKey();
            }
    
        }
    }


     

    4.编写一个控制台程序,创建哈希集合,内含部分国家的名称和首都,提示用户输入一个国家名称,则在哈希集合中查找该国首都名称并输出。

    using System;
    using System.Collections;
    using System.Linq;
    using System.Text;
    
    namespace sr
    {
        class Program
        {
            static void Main(string[] args)
            {
                Hashtable hst = new Hashtable();
                hst.Add("China", "Beijing");
                hst.Add("Japan", "Tokyo");
                hst.Add("Italy","Rome");
                hst.Add("America", "Washington");
                hst.Add("Canada", "Ottawa");
                Console.WriteLine("Please input the name of country:");
                while (true)
                {
                    string str = Console.ReadLine();
                    if (hst.Contains(str))
                    {
                        Console.WriteLine("The capital is :{0}",hst[str]);
                    }
                }
                Console.ReadKey();
            }
    
        }
    }
    

    5.

    3、假定已经获取题库中的试题号,并存放在数组arrayKT中。例如, int [] arrayKT={10,13,18,19,20,22,30,31...}。定义一个静态成员方法,该方法实现从上述数组中随机抽出给定数量(n1<=n<=arrayKT.Length)的考题,并组成一个考题字符串。比如,随机从arrayKT中抽取5题组成考题字符串:“10,18,20,22,30”。要求,组成考题字符串中考题不重复,且一定在数组中存在。自行设计程序验证上述方法正确性。

       public static string getKTH(int n,param int [] arrayKT)

       {

          //提示:主体中使用random

       }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace sr
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] array = { 10, 18, 20, 22, 30 ,60,90,100};
                Console.WriteLine(get(5,array));
                Console.ReadKey();
            }
            public static string get(int n, params int[] arrary)
            {
                int[] temp = new int[n + 1];
                string ti = "";
                int c = 0;
                Random rd = new Random();
                for (int i = 1; i <= n; i++)
                {
                    int t = rd.Next() % arrary.Length;
                    bool find = true;
                    for (int j = 0; j < i; j++)
                    {
                        if (temp[j] == t)
                        {
                            find = false;
                            i--;
                            break;
                        }
                    }
                    if (find == true)
                    {
                        temp[c++] = t;
                        ti = ti + arrary[t].ToString()+",";
                    }
                }
                return ti;
            }
    
        }
    }
    



  • 相关阅读:
    wifi通信过程的研究--(2)Wifi传输认证过程
    wifi通信过程的研究--(1)Wifi基本属性介绍
    wifi通信过程的研究--(3)传输过程概念细分
    网络编程之TCP/IP各层详解
    持续集成CI与自动化构建
    IEEE 802.11标准列表
    IEEE802.11协议基础知识
    IEEE 802.11协议基础知识整理
    beacon帧字段结构最全总结(三)——VHT字段总结
    beacon帧字段结构最全总结(二)——HT字段总结
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697934.html
Copyright © 2011-2022 走看看