C#上机实验(三)
来源:互联网 发布:淘宝上注册公司靠谱吗 编辑:程序博客网 时间:2024/04/28 21:27
1、假设有一个字符串strFilename=@"D:\C#程序设计\实验3\MyFile.TXT"。请使用字符串方法,取出路径中的文件名“MyFile.TXT”。
代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string strFileName = @"D:\C#程序设计\实验3设\MyFile.TXT"; string str1 = getFilename1(strFileName); string str2 = getFilename1(strFileName); Console.WriteLine(str1); Console.WriteLine(str2); Console.ReadKey(); } public static string getFilename1(string strFilename) { char[] ch = strFilename.ToCharArray(); int n = strFilename.IndexOf("M"); string s = ""; for (int i = n; i < ch.Length; i++) { s += ch[i]; } return s; } public static string getFilename2(string strFilename) { int n = strFilename.IndexOf('M'); return strFilename.Substring(n, strFilename.Length); } }}
还有一个IndexOf(string str)方法,用法同IndexOf(string str),只是它的意思是返回str在字符串中最后一次出现的位置。
2、定义一个静态成员方法,该方法实现字符串翻转。如Reconvert("6221982")返回值为2891226。
代码:
</pre><pre name="code" class="csharp">using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string s = Console.ReadLine(); string str = Reverse(s); Console.WriteLine(str); Console.ReadKey(); } public static string Reverse(string str) { StringBuilder strReverse = new StringBuilder(); char[] c = str.ToCharArray(); for (int i = c.Length-1; i >=0 ; i--) { strReverse.Append(c[i]); } return strReverse.ToString(); } }}
3、输入学号和姓名,对不存在的学号加到hashtable类的实例中,对存在学号给出提示。结束后,输出学号为技术的所有学生。
代码:
using System;using System.Collections;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Hashtable hst = new Hashtable(); string[] no_name; for (int j = 0; j < 5; j++) { no_name = Console.ReadLine().Split(' '); int no = int.Parse(no_name[0]); if (!hst.ContainsValue(no)) { hst.Add(no_name[1],no); } else { Console.WriteLine("该学号已存在!"); } } foreach (DictionaryEntry item in hst) { int n = (int)item.Value; if (n% 2 == 1) Console.WriteLine("{0} {1}", item.Value,item.Key); } Console.ReadKey(); } }}
0 0
- C#上机实验(三)
- C#上机实验之题目三
- C#上机实验(二)
- c++上机实验(项目三)
- 数据结构课程上机实验题(三)
- DLUT C++上机作业(实验三)
- C++ 上机实验(三)【模板】
- 上机实验三
- 【web】上机实验三
- C#实验上机
- C#上机实验
- C#上机实验一
- 【JAVA】上机实验三作业
- 第二周JAVA上机实验(三)猜数游戏
- 第二周C#上机实验(一)内容的输出
- C#上机实验物体继承(体积密度质量)
- C#第十一三章 上机
- C++上机实验三:运算符重载
- Change Queue Not Work In Unity Shader
- hdu 4003 Find Metal Mineral(树形dp+分组背包)
- swift中实现description
- 关于VC中 error C2871: 'std' : does not exist or is not a namespace
- 2016蓝桥杯假期任务之《泊松汾酒》
- C#上机实验(三)
- EasyUI - datagrid属性idField详解
- Linux中常用操作命令
- 重写 strlen
- Android Light开发(一)
- nyoj--58 最少步数
- 关于Windows下命令行的路径问题
- Android学习之如何在同一个Activity使用多个ListView
- 40个Java多线程问题总结