1. 程式人生 > >WC.EXE項目設計

WC.EXE項目設計

ons else if () bre length -1 spl [] ==

Wc c = new Wc();//創建對象
            Console.Write("wc.exe");
            string msg = Console.ReadLine();
            string[] newm = msg.Split(new char[] { ‘ ‘ });//去掉空格 拆分成幾個字符串
            int n = newm.Length;
            string str = newm[n - 1];//記錄文件路徑
            foreach (string i in newm)
            {
                if (i == "-c")
                {
                    c.Read(str, 1);//字符數目功能
                }
                else if (i == "-w")
                {
                    c.Read(str, 2);//單詞數
                }
                else if (i == "-l")
                {
                    c.Read(str, 3);//行數目
                }
//byte[] byData = new byte[10000];//應該註釋掉,一開始用的byte類型,後來發現有點問題
        FileStream file = new FileStream(a, FileMode.Open);
        StreamReader sr = new StreamReader(file);//訪問文件並讀取
        //file.Read(byData, 0, 10000);
        switch (b) 
        {case 1:
                {
                    /*while (byData[i] != ‘\0‘)
                    {
                        c++;
                        i++;
                    }*/
                    int Ichar = 0;
                    while ((Ichar = sr.Read()) != -1)    // 不再有可用的字符,則為 -1  
                    {
                        if (Ichar != ‘ ‘)
                        {
                            c++;
                        }
                    }
                    Console.WriteLine("字符數為:" + c);
                    file.Close();
                } break;
            case 2:
                {
                    int Ichar = 0; //增加 char[] no = { ‘ ‘, ‘\n‘, ‘{‘, ‘}‘, ‘(‘, ‘)‘, ‘=‘,                     ‘+‘, ‘_‘, ‘*‘, ‘%‘ };                   
            while ((Ichar = sr.Read()) != -1)    
                    {
                        if (Ichar == ‘ ‘ || Ichar == ‘\n‘)//這我在想想更準確的判斷方法 - -!
                        {                               //  for (int ss = 0; ss < 11; ss++)
                            w++;                        //  {
                        }                              //    if (Ichar ==no[ss])    這個更加                     準確!
                    }
                    Console.WriteLine("單詞數為:" + w);
                    file.Close();
                } break;
            case 3:
                {
                    int Ichar = 0;
                    while ((Ichar = sr.Read()) != -1)    
                    {
                        if (sr.ReadLine() != null)
                        {
                            l++;
                        }
                    }
                    Console.WriteLine("行數為:" + l);
                    file.Close();
                } break;
        }

  

WC.EXE項目設計