C#中怎样在.txt文件中查询文字

C#中怎样在.txt文件中查询文字,第1张

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemText;

using SystemWindowsForms;

using SystemIO; //加入命名空间

//代码不规范简便之处请见谅我是初学者

//保存的文本文件的格式必须要按照程序的格式,否则会出错 科目分数必须要占3个长度,如果不够三个长度要用空格占位

//保存分数的方法实在是没有思路了

//程序的判断不是太严格,很多为空的判断都是没有写的,见谅

namespace text

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

FileStream fs;

StreamReader sr;

StreamWriter sw;

string[] subject ={ "大学语文", "高等数学", "大学英语", "大学物理", "数据结构" };

double[] score = new double[5];

string message = ""; //保存查询到的学员信息

//姓名单选

private void rdoName_CheckedChanged(object sender, EventArgs e)

{

if (thisrdoNameChecked == true)

{

txtNameEnabled = true;

}

else

{

txtNameEnabled = false;

txtNameText = "";

}

}

//学号单选

private void rdoNo_CheckedChanged(object sender, EventArgs e)

{

if (thisrdoNoChecked == true)

{

txtNoEnabled = true;

}

else

{

txtNoEnabled = false;

txtNoText = "";

}

}

#region 查询

private void btnSelect_Click(object sender, EventArgs e)

{

fs = new FileStream("infotxt", FileModeOpen); //文件在程序的bin\Debug下面 文件要保存为Unicode编码格式的 学号有相同的 已经修改过

sr = new StreamReader(fs);

string stuInfo = "";

string Search = "";

if (thisrdoNameChecked == true) //按姓名查询

{

Search = txtNameText;

}else if (thisrdoNoChecked == true) //按学号查询

{

Search = txtNoText;

}

if (rdoNameChecked==false&&rdoNoChecked==false)

{

//如果两项都没有选中则不执行

}

else

{

while ((stuInfo = srReadLine()) != null)

{

if (stuInfoIndexOf(Search) != -1)

{

message = stuInfo;

MessageBoxShow(stuInfo); //显示消息

///////////启用分数的控件

textBox0Enabled = true; //大学语文

textBox1Enabled = true; //高等

textBox2Enabled = true; //

textBox3Enabled = true;

textBox4Enabled = true;

double[] scoretemp = new double[5];

//取得当前学员的分数

for (int i = 0; i < subjectLength; i++)

{

int index = stuInfoIndexOf(subject[i]);

//向后面取三位,取到最后一个科目的是否会出错,但是在最后一个科目的分数之后添加一个空格占位即可解决

scoretemp[i] = ConvertToDouble(stuInfoSubstring(index + subject[i]Length + 1, 3)Trim());

}

textBox0Text =scoretemp[0]ToString();

textBox1Text = scoretemp[1]ToString();

textBox2Text = scoretemp[2]ToString();

textBox3Text = scoretemp[3]ToString();

textBox4Text = scoretemp[4]ToString();

break;

}

}

}

srClose();

fsClose();

}

#endregion

#region 统计

private void btnCount_Click(object sender, EventArgs e)

{

int num = 0; //不及格的人数

fs = new FileStream("infotxt", FileModeOpen);

sr = new StreamReader(fs);

string stuInfo = "";

while ((stuInfo = srReadLine()) != null)

{

//以下是循环取得每个学生的分数信息,并计算每个学生的总分,来计算不及格的人数

for (int i = 0; i < subjectLength; i++)

{

int index = stuInfoIndexOf(subject[i]);

//向后面取三位,取到最后一个科目的是否会出错,但是在最后一个科目的分数之后添加一个空格占位即可解决

score[i] = ConvertToDouble(stuInfoSubstring(index + subject[i]Length + 1, 3)Trim());

}

double zscore = 0; //总分

//由于您在问题中没有明确指出不及格的判定规则,我这里是按照平均分<60来计算的

foreach (double sc in score)

{

zscore += sc;

}

if (zscore / subjectLength < 60)

{

num++;

}

}

srClose();

fsClose();

MessageBoxShow(stringFormat("不及格的人数为:{0}人", numToString()));

}

#endregion

参见该帖

>

以上就是关于C#中怎样在.txt文件中查询文字全部的内容,包括:C#中怎样在.txt文件中查询文字、如何用matlab求解标准化矩阵、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10068153.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存