出了个题目给GF做,自己也做一遍。
题目:
输入一串字符,分析字符串中出现了哪些字符,每个字符出现了多少次。
解答:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace yanysQustion
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.TextBox t_scr;
public System.Windows.Forms.TextBox t_res;
private System.Windows.Forms.Button button4;
public System.Windows.Forms.Label msg;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.t_scr = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.t_res = new System.Windows.Forms.TextBox();
this.msg = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// t_scr
//
this.t_scr.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.t_scr.Location = new System.Drawing.Point(0, 24);
this.t_scr.Multiline = true;
this.t_scr.Name = "t_scr";
this.t_scr.Size = new System.Drawing.Size(480, 72);
this.t_scr.TabIndex = 0;
this.t_scr.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(128, 16);
this.label1.TabIndex = 1;
this.label1.Text = "输入一串字符:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 104);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.TabIndex = 2;
this.label2.Text = "字符解析:";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button1.Location = new System.Drawing.Point(144, 128);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "解 析";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// t_res
//
this.t_res.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.t_res.Location = new System.Drawing.Point(0, 128);
this.t_res.Multiline = true;
this.t_res.Name = "t_res";
this.t_res.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.t_res.Size = new System.Drawing.Size(136, 224);
this.t_res.TabIndex = 0;
this.t_res.Text = "";
//
// msg
//
this.msg.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.msg.ForeColor = System.Drawing.Color.Red;
this.msg.Location = new System.Drawing.Point(224, 128);
this.msg.Name = "msg";
this.msg.Size = new System.Drawing.Size(256, 224);
this.msg.TabIndex = 4;
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button2.Location = new System.Drawing.Point(144, 208);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "背景颜色";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button3.Location = new System.Drawing.Point(144, 248);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "字体颜色";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(224, 112);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(100, 16);
this.label4.TabIndex = 5;
this.label4.Text = "分析:";
//
// button4
//
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button4.Location = new System.Drawing.Point(144, 168);
this.button4.Name = "button4";
this.button4.TabIndex = 3;
this.button4.Text = "清 空";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 357);
this.Controls.Add(this.label4);
this.Controls.Add(this.msg);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.t_scr);
this.Controls.Add(this.t_res);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button4);
this.Name = "Form1";
this.Text = "小y";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//背景颜色
private void button2_Click(object sender, System.EventArgs e)
{
//让用户可以打开颜色控件并细调颜色
colorDialog1.AllowFullOpen=true;
//显示帮助按钮
colorDialog1.ShowHelp=true;
//打开颜色选择框并等待用户选颜色后点击“确定”
if(colorDialog1.ShowDialog()==DialogResult.OK)
t_res.BackColor=colorDialog1.Color;
}
//字体颜色
private void button3_Click(object sender, System.EventArgs e)
{
//不让用户细调颜色
colorDialog1.AllowFullOpen=false;
//显示帮助按钮
colorDialog1.ShowHelp=true;
//打开颜色选择框并等待用户选颜色后点击“确定”
if(colorDialog1.ShowDialog()==DialogResult.OK)
t_res.ForeColor=colorDialog1.Color;
}
//解析
private void button1_Click(object sender, System.EventArgs e)
{
//字符串不能为空
if(t_scr.Text=="")
{
MessageBox.Show("请输入一串字符!");
return;
}
TimeSpan t1=new TimeSpan(DateTime.Now.Ticks);
string soruce=t_scr.Text;
int strLen=soruce.Length;
int realLen=0;
string realStr="";
for(int i=0;i<strLen;i++)
{
if(soruce.IndexOf(soruce[i],i)==soruce.LastIndexOf(soruce[i]))
realStr+=soruce[i];
}
realLen=realStr.Length;
t_res.Text=string.Empty;
for(int i=0;i<realLen;i++)
t_res.AppendText(realStr[i]+" 出现次数:"+(soruce.Split(realStr[i]).Length-1)+"\r\n");
TimeSpan t2=new TimeSpan(DateTime.Now.Ticks);
TimeSpan ts=t2.Subtract(t1).Duration();
msg.Text="原始字符串长度:"+strLen+"\r\n"+
"出现的非重复字符数:"+realLen+"\r\n"+
"运行时间:"+ts.Minutes+"分"+ts.Seconds+"秒"+ts.Milliseconds+"毫秒";
}
//清空
private void button4_Click(object sender, System.EventArgs e)
{
t_scr.Text=string.Empty;
t_res.Text=string.Empty;
msg.Text=string.Empty;
}
}
}