c#第一次作业

来源:互联网 发布:linux root为用户授权 编辑:程序博客网 时间:2024/05/16 09:49

程序代码如下:

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.IO;  
  11.   
  12. namespace WindowsFormsApplication4  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         string fileacc;  
  21.         // 总行数  
  22.         int lineA = 0;  
  23.         //代码行数  
  24.         int lineB = 0;  
  25.         //注释行数  
  26.         int lineC = 0;  
  27.         //空白行数  
  28.         int lineD = 0;  
  29.         private void label1_Click(object sender, EventArgs e)  
  30.         {  
  31.   
  32.         }  
  33.   
  34.         private void button1_Click(object sender, EventArgs e)  
  35.         {  
  36.             //当文件地址有效时  
  37.             try  
  38.             {  
  39.                 fileacc = textBox1.Text;  
  40.                 FileStream file = new FileStream(@fileacc, FileMode.Open);  
  41.                 StreamReader lq = new StreamReader(file);  
  42.   
  43.                 string line = lq.ReadLine();  
  44.                 while (line != null)  
  45.                 {  
  46.                     lineA++;  
  47.                     if (line.Trim() != "" && !(line.Trim().StartsWith(@"//")))  
  48.                     {  
  49.                         lineB++;  
  50.                     }  
  51.                     else if (line.Trim().StartsWith(@"//"))  
  52.                     {  
  53.                         lineC++;  
  54.                     }  
  55.                     else  
  56.                     {  
  57.                         lineD++;  
  58.                     }  
  59.   
  60.                     line = lq.ReadLine();  
  61.                 }  
  62.                 lq.Close();  
  63.                 //统计完成后弹出统计成功的消息框  
  64.                 MessageBox.Show("统计成功");  
  65.                 //显示统计结果  
  66.                 label3.Text = lineA.ToString();  
  67.                 label5.Text = lineB.ToString();  
  68.                 label7.Text = lineC.ToString();  
  69.                 label9.Text = lineD.ToString();  
  70.             }  
  71.             //当文件地址无效时  
  72.             catch  
  73.             {  
  74.                 //弹出文件地址无效,请重新输入的消息框  
  75.                 MessageBox.Show("文件地址无效,请重新输入!");  
  76.             }             
  77.         }  
  78.   
  79.         private void pictureBox1_Click(object sender, EventArgs e)  
  80.         {  
  81.   
  82.         }  
  83.   
  84.         private void radioButton1_CheckedChanged(object sender, EventArgs e)  
  85.         {  
  86.   
  87.         }  
  88.              
  89.     }  
  90.      
  91.   
  92.   
  93. }  
运行结果截图

开始时界面



统计成功时

统计结果


失败时

0 0
原创粉丝点击