//这个程序是我自己想的,很简单,只能录入单科成绩并排名,处理了几个输入异常时情况。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PaiMing
{
public partial class Form1 : Form
{
private int p = 0; //p起标记作用,很重要
public Form1()
{
InitializeComponent();
}
private void zengjia_Click(object sender, EventArgs e)
{
int isInt=-1;
if (xingming.Text.Length > 4||xingming.Text==""){
MessageBox.Show("姓名不能为空且最大长度为4,请重新输入");
return;
}
try {
isInt = Convert.ToInt32(chengji.Text);
}
catch {
MessageBox.Show("成绩输入错误,必须为数字"); return;
}
if (isInt < 0 || isInt > 100){
MessageBox.Show("成绩输入错误,在数字1~100之间");
return;
}
if (textBox1.Lines.Length == 0 || p != 0){
textBox1.AppendText(xingming.Text + " : " + chengji.Text);
p = 0;
}
else textBox1.AppendText("\r\n" + xingming.Text + " : " + chengji.Text);
}
private void qiufen_Click(object sender, EventArgs e)
{
if (p > 0) {
MessageBox.Show("已经排好序了!");
return;
}
if (textBox1.Text == "") {
MessageBox.Show("请先增加成绩记录!");
return;
}
else{
p++;
int i = 0;
string[] lines = textBox1.Lines;
int l = textBox1.Lines.Length;
string maxName = "";
int maxScore = -1;
string[] shuzu = new string[l];
int[] paixu = new int[l];
foreach (string line in lines){
shuzu[i] = line;
string[] s = line.Split(':');
string name = s[0];
string strScore = s[1];
int score = Convert.ToInt32(strScore);
paixu[i++] = score;
if (score >= maxScore)
{ maxName = name; maxScore = score; }
}
string stemp="";
int temp=0;
for (int j = 0; j < l-1; j++){
for (int k = 0; k < l - j-1; k++){
if (paixu[k] > paixu[k + 1]){
temp = paixu[k];
paixu[k] = paixu[k + 1];
paixu[k + 1] = temp;
stemp = shuzu[k];
shuzu[k] = shuzu[k + 1];
shuzu[k + 1] = stemp;
}
}
}
textBox1.Text = "";
int mingci = 1;
textBox2.Text = "";
for (int m = l - 1; m >= 0; m--){
textBox1.AppendText(shuzu[m] + "\r\n");
textBox2.AppendText(mingci++ + "\r\n");
}
MessageBox.Show(string.Format("{0}是第一名,成绩为{1}", maxName, maxScore));
return;
}
}
private void button1_Click(object sender, EventArgs e)
{
p = 0;
textBox1.Text = "";
textBox2.Text = "";
}
}
}