C基础上的选民系统

选民该如何写???

1.先对选民初始化;即定义候选人。
2.投票;即比对候选人名单进行统计。
3.打印出结果;既把统计的结果打印出来,可以利用从大到小的顺序进行排列。

需要的知识体系

结构体、数组
本次代码只定义三个候选人,总共10张票,没有定义废票的情况,接下来就敲敲代码把

#include <stdio.h>
 	#include <stdlib.h>
 	struct vote {
 	    char *name;
 	    int poll;
 	};
 	void initXuanMin(struct vote *XuanMin, int len) {
 	    int i;
 	    for (i = 0; i < len; i++) {
 	        printf("input %d name:\n", i + 1);
 	        XuanMin[i].name = (char *)malloc(20);
 	        scanf("%s", XuanMin[i].name);
 	        XuanMin[i].poll = 0;
 	    }
 	}
 	
 	void getMatXuanMin(struct vote *XuanMin, int len) {
 	    int i;
 	    int j;
 	    char name[128];
 	    for (i = 0; i < 10; i++) {
 	        printf("input %d vote's name:\n", i + 1);
 	        scanf("%s", name);
 	        for (j = 0; j < len; j++) {
 	            if (strcmp(name, XuanMin[j].name) == 0) {
 	                XuanMin[j].poll++;
 	            }
 	        }
 	    }
 	}
 	int printXuanMinForPaiXu(struct vote *XuanMin, int len) {
 	    int i;
 	    int tmp;
 	    printf("ret:\n");
 	    for (i = 0; i < len; i++) {
 	        if (XuanMin[i].poll < XuanMin[i + 1].poll) {
 	            tmp = XuanMin[i].poll;
 	            XuanMin[i].poll = XuanMin[i + 1].poll;
 	            XuanMin[i + 1].poll = tmp;
 	        }
 	        printf("name:%s,poll:%d\n", XuanMin[i].name, XuanMin[i].poll);
 	    }
 	}
 	
 	int main() {
 	    struct vote XuanMin[3];
 	    int len;
 	    len = sizeof(XuanMin) / sizeof(struct vote);
 	    initXuanMin(XuanMin, 3);
 	    getMatXuanMin(XuanMin, 3);
 	    printXuanMinForPaiXu(XuanMin, 3);
 	    return 0;
 	}

快去试试运行结果把!!!

优秀班委选举系统 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 优秀班委选举 { public partial class Form1 : Form { private ListBox listBox; public Form1() { InitializeComponent(); } public Form1(ListBox listBox) { InitializeComponent(); this.listBox = listBox; } private void button1_Click(object sender, EventArgs e) { if (listBox1.Items.Count > 0) { if (listBox1.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取左边listBox1中选中的内容 string cont = listBox1.SelectedItem.ToString(); //左边listBox1中删除选中的内容 listBox1.Items.Remove(cont); //把获得的内容加入到右边的listBox2中 listBox2.Items.Add(cont); } } } private void button2_Click(object sender, EventArgs e) { if (listBox2.Items.Count > 0) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { //获取右边listBox2中选中的内容 string cont = listBox2.SelectedItem.ToString(); //右边listBox2中删除选中的内容 listBox2.Items.Remove(cont); //把获得的内容加入到左边的listBox1中 listBox1.Items.Add(cont); } } } //退出 private void button5_Click(object sender, EventArgs e) { Application.Exit(); } //显示结果 private void button4_Click(object sender, EventArgs e) { //传递listBox2到Form2窗口中 Form2 form2 = new Form2(listBox2); form2.Show(); } //修改名单 private void button3_Click(object sender, EventArgs e) { if (listBox2.SelectedItem == null) { MessageBox.Show("请先选择一个对象!"); } else { string cont = listBox2.SelectedItem.ToString(); int index = listBox2.SelectedIndex; Form3 form3 = new Form3(cont, index, listBox2); form3.Show(); this.Hide(); } } //加载窗口 private void Form1_Load(object sender, EventArgs e) { if (listBox != null) { foreach (string str in listBox.Items) { listBox2.Items.Add(str); } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值