C#实验题目

1、定义一个静态成员方法,该方法用于提取文件名。比如,给定一个字符串“c:\program files\Maths\all.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 files\Maths\all.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;
        }

    }
}



转载于:https://www.cnblogs.com/sr1993/p/3697934.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值