C#第三次上机

 题目一:

     假设有一个字符串strFileName=@"D:\C#程序设计\实验3\MyFile.TXT".使用字符串方法,取出路径中的文件名"MyFile.TXT".(要求至少想出三种方法实现)。

(1)

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

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            string strFileName = @"D:\C#程序设计\实验3\MyFile.TXT";
            //string [] ss=strFileName.Split('\\');
            Console.WriteLine(getFilename(strFileName));
            Console.ReadKey();
        }

        public static string getFilename(string strFileNme)
        {
            int n = strFileNme.LastIndexOf('M');
            string str = strFileNme.Substring(n);
            return str;
        }
    }
}


(2)

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

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            string strFileName = @"D:\C#程序设计\实验3\MyFile.TXT";
            //string [] ss=strFileName.Split('\\');
            Console.WriteLine(getFilename(strFileName));
            Console.ReadKey();
        }

        public static string getFilename(string strFileNme)
        {
            int n = strFileNme.IndexOf('M');
            string str = strFileNme.Substring(n);
            return str;
        }
    }
}

(3)

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

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            string strFileName = @"D:\C#程序设计\实验3\MyFile.TXT";;
            Console.WriteLine(getFilename(strFileName));
            Console.ReadKey();
        }

        public static string getFilename(string strFileNme)
        {
            string str = "";
            char []cc = strFileNme.ToCharArray();
            int n = strFileNme.IndexOf('M');
            for (int i = n; i < cc.Length; i++)
            {
                str += cc[i];
            }
            return str;
        }
    }
}


题目二:

    实验StringBuilder类。定义一个静态成员方法,该方法实现字符串反转。如Reconvert("6221982")返回值为2891226.自行设计程序验证上述方法正确性。


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

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();;
            Console.WriteLine(Reconvert(str));
            Console.ReadKey();
        }
        public static string Reconvert(string str)
        {
            StringBuilder strR = new StringBuilder();
            char[] cc = str.ToCharArray();
            for (int i = cc.Length - 1; i >= 0; i--)
            {
                strR.Append(cc[i]);
            }
            return strR.ToString();
        }
    }
}


题目三:

    输入学号和姓名,对不存在的学号加到hashtable类的实例中,对存在学号给出提示,结束输入后,输出学号为奇数的所有学生。


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

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable hst= new Hashtable();
            Console.WriteLine("请输入你要存贮的学生人数 ");
            string str=Console.ReadLine();
            int n = int.Parse(str);
            int []num=new int [10];
            string []ss=new string  [10];
            for (int i = 0; i < n; i++)
            {
                string[] str1 = Console.ReadLine().Split(' ');
                num[i] = int.Parse(str1[1]);
                ss[i] = str1[0];
                if(hst.ContainsValue(num[i]))
                {
                    Console.WriteLine("已经存在该学号,请重新设置");
                    continue;
                }
                else
                {
                    hst.Add(ss[i], num[i]);
                }
            }
            Console.WriteLine("输出学号为奇数的所有学生");
            
            foreach(DictionaryEntry item in hst)
            {
                int m=(int) item.Value;
                if(m%2!=0)
                {
                    Console.WriteLine("{0},{1}",item.Key,item.Value);
                }
            }
            Console.ReadKey();
        }
        
    }
}


题目四:

  输入某人出生日期(以字符串方式输入,如1987-4-1)使用DateTime和TimeSpan类

(1)计算此人的年龄

(2)计算从现在到其60周岁期间,总共多少天。



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

namespace app
{
    class Program
    {
        static void Main(string[] args)
        {
            string []str = Console.ReadLine().Split('-');
            int year = int.Parse(str[0]);
            int mouth = int.Parse(str[1]);
            int day = int.Parse(str[2]);
            int age = DateTime.Now.Year - year;
            Console.WriteLine("今年此人{0}岁", age);
            DateTime dt = new DateTime(year, mouth, day);
            year += 60;
            DateTime dt2 = new DateTime(year, mouth, day);
            TimeSpan ts = dt2 - DateTime.Now;
            Console.WriteLine("计算从现在到其60岁期间,总共有{0}天", ts.Days.ToString());
            Console.ReadKey();
        }
     
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值