问题描述

已有Racer类和冠军车手数据。

已有程序,把所有的车手信息写入一个个文件中。

请编写程序,读入这些文件,记录年份和当年冠军车手姓名,存入SortedDictionary<int, string>(该类与Dictionary用法类似,但会按键进行排序;Dictionary用法在4月10日讲过),按年代先后顺序输出。

提示:用好string类的IndexOf, Split, SubString等方法,提取信息

保存车手信息的程序如下:

IList<Racer> racers = Racer.GetChampions();for (int i = 0; i < racers.Count; i++){Racer r = racers[i];FileStream fs = File.Create($"D:/Racer - {r:N}.txt");StreamWriter sw = new StreamWriter(fs);sw.WriteLine($"{r:A}");sw.Write("Cars: ");for (int j = 0; j < r.Cars.Length; j++){sw.Write(r.Cars[j] + "\t");}sw.WriteLine();sw.Write("Years: ");for (int k = 0; k < r.Years.Length; k++){sw.Write(r.Years[k] + "\t");}sw.Close();}

解决方案

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Homework9
{public class Racer : IComparable<Racer>, IFormattable{public Racer(string firstName = null, string lastName = null, string country = null, int starts = 0, int wins = 0, IEnumerable<int> years = null, IEnumerable<string> cars = null){this.FirstName = firstName;this.LastName = lastName;this.Country = country;this.Starts = starts;this.Wins = wins;var yearsList = new List<int>();foreach (var year in years){yearsList.Add(year);}this.Years = yearsList.ToArray();var carList = new List<string>();foreach (var car in cars){carList.Add(car);}this.Cars = carList.ToArray();}public string FirstName { get; set; }public string LastName { get; set; }public string Country { get; set; }public int Wins { get; set; }//夺冠场数public int Starts { get; set; }//首发场数public string[] Cars { get; private set; }//赛车手获得冠军那一年使用的所有车型public int[] Years { get; private set; }//赛车手获得冠军的年份public override string ToString(){return String.Format("{0} {1}", FirstName, LastName);}public int CompareTo(Racer other){if (other == null) throw new ArgumentNullException("other");return this.LastName.CompareTo(other.LastName);}public string ToString(string format){return ToString(format, null);}public string ToString(string format,IFormatProvider formatProvider){switch (format){case null:case "N":return ToString();case "F":return FirstName;case "L":return LastName;case "C":return Country;case "S":return Starts.ToString();case "W":return Wins.ToString();case "A":return String.Format("{0} {1}, {2}; starts: {3}, wins: {4}",FirstName, LastName, Country, Starts, Wins);default:throw new FormatException(String.Format("Format {0} not supported", format));}}public static IList<Racer> GetChampions()//1950-2008年一级方程式锦标赛冠军{IList<Racer> racers = new List<Racer>(40);racers.Add(new Racer("Nino", "Farina", "Italy", 33, 5, new int[] { 1950 }, new string[] { "Alfa Romeo" }));racers.Add(new Racer("Alberto", "Ascari", "Italy", 32, 10, new int[] { 1952, 1953 }, new string[] { "Ferrari" }));racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, new int[] { 1951, 1954, 1955, 1956, 1957 }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }));racers.Add(new Racer("Mike", "Hawthorn", "UK", 45, 3, new int[] { 1958 }, new string[] { "Ferrari" }));racers.Add(new Racer("Phil", "Hill", "USA", 48, 3, new int[] { 1961 }, new string[] { "Ferrari" }));racers.Add(new Racer("John", "Surtees", "UK", 111, 6, new int[] { 1964 }, new string[] { "Ferrari" }));racers.Add(new Racer("Jim", "Clark", "UK", 72, 25, new int[] { 1963, 1965 }, new string[] { "Lotus" }));racers.Add(new Racer("Jack", "Brabham", "Australia", 125, 14, new int[] { 1959, 1960, 1966 }, new string[] { "Cooper", "Brabham" }));racers.Add(new Racer("Denny", "Hulme", "New Zealand", 112, 8, new int[] { 1967 }, new string[] { "Brabham" }));racers.Add(new Racer("Graham", "Hill", "UK", 176, 14, new int[] { 1962, 1968 }, new string[] { "BRM", "Lotus" }));racers.Add(new Racer("Jochen", "Rindt", "Austria", 60, 6, new int[] { 1970 }, new string[] { "Lotus" }));racers.Add(new Racer("Jackie", "Stewart", "UK", 99, 27, new int[] { 1969, 1971, 1973 }, new string[] { "Matra", "Tyrrell" }));racers.Add(new Racer("Emerson", "Fittipaldi", "Brazil", 143, 14, new int[] { 1972, 1974 }, new string[] { "Lotus", "McLaren" }));racers.Add(new Racer("James", "Hunt", "UK", 91, 10, new int[] { 1976 }, new string[] { "McLaren" }));racers.Add(new Racer("Mario", "Andretti", "USA", 128, 12, new int[] { 1978 }, new string[] { "Lotus" }));racers.Add(new Racer("Jody", "Scheckter", "South Africa", 112, 10, new int[] { 1979 }, new string[] { "Ferrari" }));racers.Add(new Racer("Alan", "Jones", "Australia", 115, 12, new int[] { 1980 }, new string[] { "Williams" }));racers.Add(new Racer("Keke", "Rosberg", "Finland", 114, 5, new int[] { 1982 }, new string[] { "Williams" }));racers.Add(new Racer("Niki", "Lauda", "Austria", 173, 25, new int[] { 1975, 1977, 1984 }, new string[] { "Ferrari", "McLaren" }));racers.Add(new Racer("Nelson", "Piquet", "Brazil", 204, 23, new int[] { 1981, 1983, 1987 }, new string[] { "Brabham", "Williams" }));racers.Add(new Racer("Ayrton", "Senna", "Brazil", 161, 41, new int[] { 1988, 1990, 1991 }, new string[] { "McLaren" }));racers.Add(new Racer("Nigel", "Mansell", "UK", 187, 31, new int[] { 1992 }, new string[] { "Williams" }));racers.Add(new Racer("Alain", "Prost", "France", 197, 51, new int[] { 1985, 1986, 1989, 1993 }, new string[] { "McLaren", "Williams" }));racers.Add(new Racer("Damon", "Hill", "UK", 114, 22, new int[] { 1996 }, new string[] { "Williams" }));racers.Add(new Racer("Jacques", "Villeneuve", "Canada", 165, 11, new int[] { 1997 }, new string[] { "Williams" }));racers.Add(new Racer("Mika", "Hakkinen", "Finland", 160, 20, new int[] { 1998, 1999 }, new string[] { "McLaren" }));racers.Add(new Racer("Michael", "Schumacher", "Germany", 250, 91, new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, new string[] { "Benetton", "Ferrari" }));racers.Add(new Racer("Fernando", "Alonso", "Spain", 132, 21, new int[] { 2005, 2006 }, new string[] { "Renault" }));racers.Add(new Racer("Kimi", "Rikknen", "Finland", 148, 17, new int[] { 2007 }, new string[] { "Ferrari" }));racers.Add(new Racer("Lewis", "Hamilton", "UK", 44, 9, new int[] { 2008 }, new string[] { "McLaren" }));return racers;}}class Program{static void Main(string[] args){IList<Racer> racers = Racer.GetChampions();for (int i = 0; i < racers.Count; i++){Racer r = racers[i];FileStream fs = File.Create($"out/Racer - {r:N}.txt");StreamWriter sw = new StreamWriter(fs);sw.WriteLine($"{r:A}");sw.Write("Cars: ");for (int j = 0; j < r.Cars.Length; j++){sw.Write(r.Cars[j] + "\t");}sw.WriteLine();sw.Write("Years: ");for (int k = 0; k < r.Years.Length; k++){sw.Write(r.Years[k] + "\t");}sw.Close();}SortedDictionary<int, string> sd = new SortedDictionary<int, string>();for (int i = 0; i < racers.Count; i++){Racer r = racers[i];FileStream fs = File.Open($"out/Racer - {r:N}.txt",FileMode.Open);StreamReader sr = new StreamReader(fs);string s=sr.ReadToEnd();Console.WriteLine(s);string name = s.Substring(0, s.IndexOf(","));Console.WriteLine(name);string[] years = s.Substring(s.IndexOf("Years: ") + 7).Split('\t');Console.WriteLine(years);foreach(string year in years){Console.WriteLine(year);if(year!="")sd.Add(int.Parse(year),name);}}foreach (KeyValuePair<int, string> pair in sd){Console.WriteLine(pair.Key + ":" + pair.Value);}}}
}

运行结果

参考文章

C#——文件处理和字符串处理DEMO相关推荐

  1. java 读取流的字符编码格式_如何使用Java代码获取文件、文件流或字符串的编码方式...

    标签: 今天通过网络资源研究了一下如何使用Java代码获取文件.文件流或字符串的编码方式,现将代码与大家分享: package com.ghj.packageoftool; import info.m ...

  2. java文件中查找字符串_Java 在本地文件中查找固定字符串

    适用范围:只适用于在文本文档中查找(如,txt.java.c等等,并不适用与doc.xls等等这些文件),可嵌套文件夹.但是对中文不支持. 例如:文件夹:F:/demo 子文件夹:F:/demo/er ...

  3. ajax发送动态字符传,如何发送ajax请求文件与其他字符串的变量?

    我想创建ajax调用并发送数据与文件和其他变量,我也使用django,如果它的帮助.如何发送ajax请求文件与其他字符串的变量? 我尝试: js文件: $("#save-new-reques ...

  4. python3查找文件中指定字符串_Python3在指定路径下递归定位文件中出现的字符串...

    [本文出自天外归云的博客园] 脚本功能:在指定的路径下递归搜索,找出指定字符串在文件中出现的位置(行信息). 用到的python特性: 代码如下: import os import sys __all ...

  5. 【Linux】查询文件中指定字符串的记录

    语法 cat 文件 |grep 查询字符串 例如现在有文件file.dat,文件中内容如下: zhangsan Lisi wangwu123 wangwu890 zhangsan28290 现在想从文 ...

  6. linux sed 批量替换多个文件中的字符串

    原文: http://blog.csdn.net/kauu/article/details/1757325 一.linux sed 批量替换多个文件中的字符串 sed -i "s/oldst ...

  7. 在目录下所有文件中查找字符串

    目录下的所有文件中查找字符串 find .| xargs grep -ri "class" 目录下的所有文件中查找字符串,并且只打印出含有该字符串的文件名 find .| xarg ...

  8. python怎么读文件内容-Python读取文件内容为字符串的方法(多种方法详解)

    以下笔记是我在 xue.cn 学习群之数据分析小组所整理分享的心得.相关背景是:我选择中文词频统计案例作为考察大家python基础功掌握程度. 以小见大,下面是2个小技能的具体实战: 如何灵活地处理文 ...

  9. 使用C#程序处理PowerPoint文件中的字符串

    最近, 有同事偶然发现Microsoft  Office PowerPoint可以被看作是一个压缩包,然后通过WinRAR解压出来一组XML文件.解压出来的文件包括: 一个索引文件名称为:[Conte ...

最新文章

  1. python 虚拟环境 tensorflow GPU
  2. 虚拟主机上用Asp.net实现Urlrewrite
  3. 计算机术语表达祝福,考研祝福| | 计算机 愿你们历经千帆,终达彼岸
  4. MapReduce_自学过程(一)
  5. Mysql修改binlog日志过期时间
  6. android 布局圆变椭圆,Android实现自定义圆形、圆角和椭圆ImageView(使用Xfermode图形渲染方法)...
  7. 处理动态SQL语句的参数
  8. 中国民间秘术大揭露【实用】
  9. USACO 3.3.1 Riding the Fences 骑马修栅栏(欧拉回路)
  10. 仿摩拜单车APP(包括附近车辆、规划路径、行驶距离、行驶轨迹记录,导航等)...
  11. 大一新生 电脑小白如何选择电脑 电脑知识全面讲解
  12. 性能衡量指标-吞吐量与响应时间
  13. cv::fitLine用法
  14. uni-app修改页面背景色:
  15. 根据流程图写python程序_根的解释|根的意思|汉典“根”字的基本解释
  16. 【Kotlin】标准库函数 ③ ( with 标准库函数 | also 标准库函数 )
  17. 狗年说狗--忠诚的卫士
  18. 解决IBM Lenovo x3650M5不开机报错system board fault
  19. 如何禁止Mathtype在公式后面自动添加一个空格
  20. 长安大学计算机基础,计算机基础知识-长安大学.PPT

热门文章

  1. 字符串原样输出程序python_Python格式化字符串(格式化输出)
  2. 以某一用户名和密码 登录请求脚本_linux expect自动交互脚本
  3. 浅谈python_浅谈python-Django
  4. 画出该lti系统的幅频特性响应曲线_一文带你通俗理解幅频响应和相频响应
  5. java动态语言_java动态类型语言支持(三)
  6. java 继承 私有变量_java – 继承和私有变量
  7. linux 定时器中断 imx,NXP iMX8 存储性能测试
  8. java hibernate sqlserver自增_怎样在hibernate中实现oracle的主键自增策略?
  9. 多个安卓设备投屏到电脑_辅助多手机同时直播控场 TotalControl手机投屏软件
  10. matlab矩阵按坐标取,在Matlab中获取inlier点的坐标(Get coordinates of inlier points in Matlab)...