结构体数组应用,C++课本P209页例题7.2,投票选举情况


问题概要:

    对候选人得票的统计程序。设有3个候选人,最终只能有1人当选领导。 今有10个人参加投票,从键盘先后输入这10个人所投的候选人的名字,要求最后输出这3个人的最后得票结果。

java解决:

     2种方法(方法都定义在struct类中了)

  1. 内部类
  2. Map<String,Integer>
代码如下:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class Person {
	String username;// 投票人的名字
	int count;// 投票人的得票情况

	// 定义无参方法,可以实例化无参数的对象。如 Person person1=new Person();
	public Person() {
	}

	// 定义2个参数,可以实例化对象如:Person person1=new Person("zhangsan",0);
	public Person(String username, int count) {
		this.count = count;
		this.username = username;
	}
}

public class Struct {
	// leader_name[10]:10个人所投人的名字,即10张票上所写的姓名
	private static String[] leader_name = new String[10];
	// 无效票
	private static int no_vote = 0;

	/*
	 * 对候选人得票的统计程序。设有3个候选人,最终只能有1人当选领导。 
	 * 今有10个人参加投票,从键盘先后输入这10个人所投的候选人的名字,
	 * 要求最后输出这3个人的最后得票结果。
	 */
	public static void main(String[] args) {
		int persons = 10;// 参加投票的总人数
		// 控制台输入
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i < leader_name.length; i++) {
			System.out.println("候选人的名字是:1、Luoyanchao,2、ChenT,3、WangMiss");
			System.out.println("请输入候选人对应的序号");
			leader_name[i] = sc.nextLine();
		}
		class_way();
		// map_way();
	}

	public static void map_way() {
		Map<String, Integer> person = new HashMap<String, Integer>();
		int[] votes = { 0, 0, 0 };// 票数
		// 初始化3个候选人 1、Luoyanchao,2、ChenT,3、WangMiss
		person.put("1", 0);
		person.put("2", 0);
		person.put("3", 0);
		// 统计投票情况
		for (int i = 0; i < leader_name.length; i++) {
			if (leader_name[i].equals("1")) {
				votes[0]++;
				person.put("Luoyanchao", votes[0]);
			} else if (leader_name[i].equals("2")) {
				votes[1]++;
				person.put("ChenT", votes[1]);
			} else if (leader_name[i].equals("3")) {
				votes[2]++;
				person.put("WangMiss", votes[2]);
			} else {
				no_vote++;
			}
		}
		for (Map.Entry<String, Integer> entry : person.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
		System.out.println("无效票为:" + no_vote);
		// System.out.println("Luoyanchao:" + person.get("Luoyanchao"));
		// System.out.println("ChenT: " + person.get("ChenT"));
		// System.out.println("WangMiss: " + person.get("WangMiss"));
	}

	public static void class_way() {
		Person[] leaders = new Person[3];
		// 初始化三个对象,即3个候选人
		leaders[0] = new Person("Luoyanchao", 0);
		leaders[1] = new Person("ChenT", 0);
		leaders[2] = new Person("WangMiss", 0);

		// 统计投票情况
		for (int i = 0; i < leader_name.length; i++) {
			if (leader_name[i].equals("1"))
				leaders[0].count++;
			else if (leader_name[i].equals("2"))
				leaders[1].count++;
			else if (leader_name[i].equals("3"))
				leaders[2].count++;
			else {
				no_vote++;
			}
		}

		for (int i = 0; i < leaders.length; i++) {
			System.out.println(leaders[i].username + ": " + leaders[i].count);
		}
		System.out.println("无效票为:" + no_vote);

	}
}

C++代码:

// Votes.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
struct Person{
	char username[20];//候选人姓名
	int count;//得票情况
};

int main()
{
	//定义3个候选人的姓名和当前得票情况
	Person leader[3] = {"Luoyanchao",0,"ChenT",0,"WangMiss",0};
	int i, j = 0;
	char leader_name[20];//为投票人所选的人的姓名,即候选人的姓名
	//int no_vote = 0;
	for ( i = 0; i < 10; i++){
		cin >> leader_name;
		//将票上姓名与3个候选人的姓名比较
		for (j = 0; j < 3; j++)
			//如果与某一候选人的兴民相同,就给他加一票
			if (strcmp(leader_name, leader[j].username) == 0)
				leader[j].count++;
			/*else{
			*	no_vote++;
			*}*/
		}
	cout << endl;
	//输出3个候选人的姓名和最后得票情况
	for ( i = 0; i < 3; i++)
	{
		cout << leader[i].username << ": " << leader[i].count << endl;
		
	}
	//cout << "无效票数" << no_vote << endl;
	system("pause");
	return 0;
}




你可能感兴趣的:(C语言,VC,Java和C++)