欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > c# >内容正文

c#

c#——文件处理和字符串处理demo -尊龙游戏旗舰厅官网

发布时间:2024/10/5 c# 24 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 c#——文件处理和字符串处理demo 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

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

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

提示:用好string类的indexof, split, substring等方法,提取信息

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

ilist 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, iformattable{public racer(string firstname = null, string lastname = null, string country = null, int starts = 0, int wins = 0, ienumerable years = null, ienumerable cars = null){this.firstname = firstname;this.lastname = lastname;this.country = country;this.starts = starts;this.wins = wins;var yearslist = new list();foreach (var year in years){yearslist.add(year);}this.years = yearslist.toarray();var carlist = new list();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 getchampions()//1950-2008年一级方程式锦标赛冠军{ilist racers = new list(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 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 sd = new sorteddictionary();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 pair in sd){console.writeline(pair.key ":" pair.value);}}} }

总结

以上是尊龙游戏旗舰厅官网为你收集整理的c#——文件处理和字符串处理demo的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图