return – > 返回到该方法的结尾处, 若有返回值 必须使用return返回
List 和 ArrayList的使用方法是一样的, 以后编程我们都使用List
#读写计算题
文件1.txt的式子经过计算把式子和结果写入新文档
<
1 + 5 =
25 + 6 =
1024 + 1024 =
16 * 15 =
15 / 0 =
2147483647 + 5=
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);
}
}
}
#去除注释
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);
}
}
}