好久之前就听闻,我们的软工老师不一般,恩,的确不一般.
我们的软工课也非同一般,不似国内传统的教学模式,虽然任务量大,但是确实让我上来就有那么一种感觉,如果好好抓住这次机会,我的专业水平将会得到一个很大的锻炼.
果不其然,上来的第一次作业就差点让人hold不住。
全英文的需求文档,看的磕磕绊绊,看完之后觉得还是不能很好的理解某些具体的需求,而且好不容易写完之后,老师紧接着改了3次需求文档,并且在极为紧张的时间下,要求按时提交(迟交零分,不交倒扣啊!)
虽说,对这种教学方式有点很不适应,但是,其实,这种情况是很真切的,因为以后的工作中肯定会遇到很多这样的突发情况,而且会读英文文档是计算机学生的一项应该必备的基本功。
言归正传,说到这次作业,就先要说说我自己。
因为前两年的规划问题,我并没有在专业和科技实践上投入过多的精力,甚至是欠缺,所以总体来讲自己编代码的能力还是蛮弱的。
所以,在这突如其来的第一次作业面前压力还是蛮大的。
我花了两天的时间熟悉VS12的环境,并学习了一些C#的基础知识,比如如何建立工程文件,读入输出文件等,当这些基础狂接弄清楚之后,我便用C语言面向过程的编程方法,运用数组存储和快排排序之后,简简单单的写出来程序,在我自己的小量测试数据上还是可以保证结果的准确信的,但是速度以及大容量数据的正确性却并未得到测试和验证,也实属有心无力,因为自己的码代码能力的确很需要练习。
看到同学们各种改进算法,哈希、LIST之类的,自己简直就是云里雾里,因为自己只是略微懂得一些最基本的知识,当然这些知识也得到了同学的一些帮助。
尽管,我现在并没有写出一个效率很好,很简洁的程序。
但是,我想通过我随后的不断学习、改进和努力,我可以在期末的时候写出一个优化的很好的程序。
尽管我的起点很低,但是我会好好努力,脚踏实地,慢慢地追赶上来。
我会把这个小程序一直做下去的,做到我满意为止。
以下是我现在写出来的低级版本:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace SP1_WFS_1._4{ class Program { //wordList store the word read //frequency statistics the frequnence of the word //numOfWord statistics the num of words //postfix the postfix of one word static string[] wordList; static int[] frequency; static int numOfWord; static string[] postfix; static void Main(string[] args) { wordList = new string[1000]; frequency = new int[1000]; numOfWord = 0; postfix = new string[1000]; //When the first args of the command line is '-e',carry put extengded_mode if (args[0] == "-e"){ extended_mode(args[1]); write(args[1] + "\\sky.suruo@gmail.com.txt"); } //otherwise,carry out simple_mode else{ simple_mode(args[0]); write(args[0] + "\\sky.suruo@gmail.com.txt"); } } static void extended_mode(string path) { DirectoryInfo folder = new DirectoryInfo(path); //if the route isn't exist if (folder.Exists == false) Console.WriteLine("Don't have this path!!!"); else{ string[] allFiles = Directory.GetFiles(path); for (int i = 0; i < allFiles.Count(); i++){ char tempLetter; StringBuilder tempWord = new StringBuilder(), tempDigits = new StringBuilder(); using (StreamReader sr = new StreamReader(allFiles[i])){ //digitFlag mark read digit yes or not //numOfLetter is the num of letter before digit int flag = 0, sumOfLetter = 0, digitFlag = 0; while (sr.Peek() > 0){ //filter the delimiters do{ tempLetter = (char)sr.Read(); } while (sr.Peek() > 0 && !(tempLetter >= 'a' && tempLetter <= 'z' || tempLetter >= 'A' && tempLetter <= 'Z' || tempLetter >= '0' && tempLetter <= '9')); do{ //store it in tempDigits when it's digit if (tempLetter >= '0' && tempLetter <= '9'){ tempDigits.Append(tempLetter); flag = 1; digitFlag = 1; } else{ //count the num of letter,treat them as a word if num >3 //when digitFlag==0,numOfLetter stop +1 if (digitFlag == 0 && (tempLetter >= 'a' && tempLetter <= 'z' || tempLetter >= 'A' && tempLetter <= 'Z')) sumOfLetter++; //treat them as a part of the word only when letter was read follow if (flag == 1){ tempWord.Append(tempDigits); tempDigits.Clear(); } tempWord.Append(tempLetter); } tempLetter = (char)sr.Read(); } while (tempLetter >= 'a' && tempLetter <= 'z' || tempLetter >= 'A' && tempLetter <= 'Z' || tempLetter >= '0' && tempLetter <= '9'); //insert the word which read right now to wordList if (sumOfLetter >= 3) insert(tempWord.ToString(), tempDigits.ToString()); //initialize variable digitFlag = 0; flag = 0; sumOfLetter = 0; tempDigits.Clear(); tempWord.Clear(); } } } string[] allSonFolder = Directory.GetDirectories(path); for (int i = 0; i < allSonFolder.Count(); i++){ extended_mode(allSonFolder[i]); } } } static void simple_mode(string path) { DirectoryInfo folder = new DirectoryInfo(path); //if the route isn't exist if (folder.Exists == false) Console.WriteLine("Don't have this path!!!"); else{ string[] allFiles = Directory.GetFiles(path); for (int i = 0; i < allFiles.Count(); i++){ char tempc; StringBuilder temps = new StringBuilder(); using (StreamReader sr = new StreamReader(allFiles[i])){ //same as above int numOfLetter = 0, digitFlag = 0; while (sr.Peek() > 0){ do{ tempc = (char)sr.Read(); } while (sr.Peek() > 0 && !(tempc >= 'a' && tempc <= 'z' || tempc >= 'A' && tempc <= 'Z' || tempc >= '0' && tempc <= '9')); do{ //same as above if (tempc >= '0' && tempc <= '9') digitFlag = 1; //same as above if (digitFlag == 0 && (tempc >= 'a' && tempc <= 'z' || tempc >= 'A' && tempc <= 'Z')) numOfLetter++; temps.Append(tempc); tempc = (char)sr.Read(); } while (tempc >= 'a' && tempc <= 'z' || tempc >= 'A' && tempc <= 'Z' || tempc >= '0' && tempc <= '9'); //same as above if (numOfLetter >= 3) insert(temps.ToString(), ""); numOfLetter = 0; digitFlag = 0; temps.Clear(); } } } } } static void insert(string temps, string digits) { //if the word is the first,assign it to the first if (numOfWord == 0){ wordList[0] = temps; postfix[0] = digits; frequency[0] = 1; numOfWord++; } else{ int i; for (i = 0; i < numOfWord; i++){ //if the wordList has store this word,it's frequence+1 if (wordList[i].ToLower() == temps.ToLower()){ frequency[i]++; if (string.CompareOrdinal(wordList[i], temps) > 0) wordList[i] = temps; if (postfix[i].CompareTo(digits) > 0) postfix[i] = digits; break; } } //if it's a new one,insert temps to the next of wordList if (i == numOfWord){ wordList[numOfWord] = temps; postfix[numOfWord] = digits; frequency[numOfWord++] = 1; } } } static void write(string path) { //sort it by it's frequnence before output my_sort(); using (StreamWriter sw = new StreamWriter(path)) { for (int i = 0; i < numOfWord; i++){ sw.WriteLine("<"+wordList[i] + postfix[i] + ">:" + frequency[i]); } } } static void my_sort() { //sort by frequnence first sort_frequency(0, numOfWord - 1); //if the frequnence are the same,then sort by Letters sequence int i = 0; while (i < numOfWord - 1){ int j = i + 1; //if the frequnence of two words are the same,then find all words which is the same frequence while (frequency[i] == frequency[j] && j < numOfWord) j++; if (j - i > 1) sort_alphabetical(i, j - 1); i = j; } } static void sort_frequency(int s, int t) { //QS int i, j; if (s < t){ i = s; j = t + 1; while (true){ do i++; while (!(frequency[s] >= frequency[i] || i == t)); do j--; while (!(frequency[s] <= frequency[j] || j == s)); if (i < j) my_swap(i, j); else break; } my_swap(s, j); sort_frequency(s, j - 1); sort_frequency(j + 1, t); } } static void sort_alphabetical(int s, int t) { //QS int i, j; if (s < t){ i = s; j = t + 1; while (true){ do i++; while (!((string.CompareOrdinal(wordList[s], wordList[i])) <= 0 || i == t)); do j--; while (!((string.CompareOrdinal(wordList[s], wordList[j]) >= 0 || j == s))); if (i < j) my_swap(i, j); else break; } my_swap(s, j); sort_alphabetical(s, j - 1); sort_alphabetical(j + 1, t); } } static void my_swap(int i, int j) { //a function to exchange two words int tempi; string temps; temps = wordList[i]; wordList[i] = wordList[j]; wordList[j] = temps; tempi = frequency[i]; frequency[i] = frequency[j]; frequency[j] = tempi; } }}
持续改进中。。。