题目一:
假设有一个字符串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();
}
}
}