去註釋去空格行,將1.txt文字內容計算結果寫入2.txt文字
阿新 • • 發佈:2018-12-09
去註釋和去空格
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 去處註釋行
{
class Program
{
static void ReadFileInfo(string path, List<string> info)
{
string str = string .Empty;
using (StreamReader reader = new StreamReader(path,Encoding.UTF8))
{
while ((str = reader.ReadLine()) != null)
{
if(!string.IsNullOrEmpty(str))
{
if (str.Trim().Length != 0) // 不是空行
{
if ( str.IndexOf("//") != 0) // 有// 但是不確定啊 ***// --> "//" *** //
{
//沒有雙引號就取消註釋
if (str.IndexOf("//") != -1 && !str.Contains('"'))
{
// 文字 //
//
str = str.Substring(0, str.IndexOf("//"));
if(str.Trim().Length != 0)
info.Add(str);
}
// 純文字 有雙引號
else
{
info.Add(str);
}
}
}
}
}
}
}
static void WriteFileInfo(string path, List<string> info)
{
using (StreamWriter writer = new StreamWriter(path,false,Encoding.UTF8))
{
for (int i = 0; i < info.Count; i++)
{
writer.WriteLine(info[i]);
}
writer.WriteLine("//by 自動去除註釋行工具");
}
}
static void Main(string[] args)
{
//開啟
Console.WriteLine("取消註釋前請保留一份,操作有風險.....");
List<string> list = new List<string>();
string path = Console.ReadLine();
string rePath = path.Insert(path.Length, ".bak");
if (File.Exists(rePath))
{
File.Delete(rePath);
}
File.Copy(path,rePath);
ReadFileInfo(path,list);
//寫入
WriteFileInfo(path,list);
}
}
}
將1.txt文字內容計算結果寫入2.txt文字
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;
namespace 讀檔案 { class Program { static bool CheckNumber(string str) { bool returnValue = true; for (int i = 0; i < str.Length; i++) { if (!char.IsDigit(str[i])) { returnValue = false; break;
}
}
return returnValue;
}
static bool IsNumber(string number01, string number02)
{
bool returnValue = true;
if (! (CheckNumber(number01) && CheckNumber(number02)))
{
returnValue = false;
}
return returnValue;
}
static void CalcNumberByOper(string[] infos, out string finalStr)
{
finalStr = string.Empty;
long Number01 = Convert.ToInt64(infos[0]);
long Number02 = Convert.ToInt64(infos[2]);
switch (infos[1])
{
case "+":
//字串的格式化 就是把N個東東按照規則拼成一個字串
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01+Number02);
break;
case "-":
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 - Number02);
break;
case "*":
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 * Number02);
break;
case "/":
if (Number02 == 0)
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], "除數不能為0");
}
else
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01/Number02);
}
break;
case "%":
if (Number02 == 0)
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], "除數不能為0");
}
else
{
finalStr = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], Number01 % Number02);
}
break;
}
}
/// <summary>
/// 通過將分割之後資料進行分析判斷返回一個結果 10+20 = 30
/// </summary>
/// <param name="infos">分割之後的東東 N1 OPE N2 = </param>
/// <returns></returns>
static string CalcStringArray(string[] infos)
{
string returnValue = string.Empty;
if (! IsNumber(infos[0], infos[2]))
{
returnValue = string.Format("{0} {1} {2} {3} {4}", infos[0], infos[1], infos[2], infos[3], "原始檔不是數字");
goto retTip;
}
CalcNumberByOper(infos,out returnValue);
retTip:
return returnValue;
}
// 讀檔案
// 儲存檔案資料的 -->List<string> fileInfo
static void ReadFileToList(string filePath, List<string> fileInfo)
{
string tmpStr = string.Empty;
using (StreamReader reader = new StreamReader(filePath,Encoding.UTF8))
{
while ((tmpStr = reader.ReadLine()) != null)
{
if(!string.IsNullOrEmpty(tmpStr))
fileInfo.Add(tmpStr);
}
}
Console.WriteLine("測試 讀取資料{0}行",fileInfo.Count);
}
//計算形成一種格式
static void CalcListInfo(List<string> fileInfo)
{
//fileInfo Number01 ope Number02 =
for (int i = 0; i < fileInfo.Count; i++)
fileInfo[i] = CalcStringArray(fileInfo[i].Split(' '));
}
// 將計算之後的內容挨個放入一個新的文字
static void WriteListInfoToFile(string oldPath, List<string> fileInfo)
{
string tmpPath = oldPath.Insert(oldPath.IndexOf('.'), "_copy");
using (StreamWriter writer = new StreamWriter(tmpPath,false,Encoding.UTF8))
{
for (int i = 0; i <fileInfo.Count ; i++)
{
writer.WriteLine(fileInfo[i]);
}
}
}
static void Main(string[] args)
{
//定義儲存空間
List<string> infoList = new List<string>();
//輸入路徑
string path = Console.ReadLine();
//讀檔案
ReadFileToList(path,infoList);
//計算
CalcListInfo(infoList);
//寫文字
WriteListInfoToFile(path,infoList);
}
}
}