一个关于选票的程序
题目算法:一个班级里选班长,团支书和学习 委员,用数组做。把一个班级每个人的选票存到数组中,然后,计算出票数,打印各候选人的票数,最后得出结果。
之前,我已经调试好了。现在我要加入新的功能:在输入名字时,可以中断输入,查看投票的情况,然后在继续输入投票。问题是,在运行时,当输入stop 时,没有按预想的一样,中断输入。大家帮我看看。先谢了!
代码如下:
# include <string.h>
# include <iostream.h>
//using namespace std;
const SIZE = 5;
const NUM = 3;
int read(int a[SIZE], int SIZE, char* name, int n, int temp);//读入姓名,用于记票
void pickArray(int a[SIZE], int a1[SIZE], int a2[SIZE], int a3[SIZE], int SIZE, int counter);//共选三次,这个函数用于每次选择要输入的数组
void countVotes(int a[SIZE], int SIZE, int b[NUM], int NUM);//记票
int compareTwo(int b[NUM], int NUM);//比较谁的票数多
void print(int b[NUM], int NUM, int result, int n);//打印出结果
int main(){
char name [8]= " ";
int a[SIZE] = {0};
int a1[SIZE] = {0};
int a2[SIZE] = {0};
int a3[SIZE] = {0};
int b[NUM] = {0};//为了便于查看,b[0] 就空着不用了
int s = 0, temp2 = 0;
for(int i = 1; i <= NUM; i++){//for 循环用于控制三次循环
if(i == 1){
temp2 = read(a1, SIZE, name, i, temp2);//返回的值用于重新输入用
if(temp2 != SIZE){
temp2 = read(a1, SIZE, name, i, temp2);
}
}
else if(i == 2){
temp2 = read(a2, SIZE, name, i, temp2);
if(temp2 != SIZE){
temp2 = read(a2, SIZE, name, i, temp2);
}
}
else{ temp2 = read(a3, SIZE, name, i, temp2);
if(temp2 != SIZE){
temp2 = read(a3, SIZE, name, i, temp2);
}
}
pickArray(a, a1, a2, a3, SIZE, i);
countVotes(a, SIZE, b, NUM);
s = compareTwo(b, NUM);
print(b, NUM, s, i);
}
return(0);
}
int read(int a[SIZE], int SIZE, char *name , int n, int temp){
int temp1 = 0, i = 0;
if(n == 1)//开始选择循环
cout <<"The first time, we vote the Monitor.\n";
else if(n == 2)
cout <<"The second time, we vote the secretary of member.\n";
else if(n == 3)
cout <<"The third time, we vote the commissioner of study.\n";
cout <<"Enter the names: " <<'\n';
if(temp == 0){
i = 0;
}
else{
i = temp;
}
for(; i < SIZE; i++){
cout <<"name: ";//输入姓名,然后再转换成数组来计算
cin >>name ;
if(name[4] == "stop"){//当输入为stop 时,就中断
break;
}
else{
if(!strcmp(name , "Jorden") ||!strcmp( name , "Kobe") || !strcmp(name ,"Yao")){
a[i] = 1;
}
else if(!strcmp(name ,"Marlon" )|| !strcmp(name ,"Dancon") || !strcmp(name ,"O'neal"))
a[i] = 2;
else{
cout <<"The name you entered was a wrong name, so this vote will not be counted. \n";
continue;
}
}
}
cout <<'\n';
//return temp1 = i;
return temp1 = i;
}
void pickArray(int a[SIZE], int a1[SIZE], int a2[SIZE], int a3[SIZE], int SIZE, int counter){
if(counter == 1)
for(int i = 0; i < SIZE; i++)
a[i] = a1[i];
else if(counter == 2)
for(int j = 0; j <SIZE; j++)
a[j] = a2[j];
else for(int k = 0; k < SIZE; k++)
a[k] = a3[k];
}
void countVotes(int a[SIZE], int SIZE, int b[NUM], int NUM){
for(int i = 0; i < SIZE; i++)
if(a[i] == 1)
b[1]++;
else if(a[i] == 2)
b[2]++;
}
int compareTwo(int b[NUM], int NUM){
if(b[NUM - 1] > b[NUM])
return 1;
else return 2;
}
void print(int b[NUM], int NUM, int result, int n){
char name[8] = " ";
cout <<"The result is: ";
if(n == 1)
if(result == 1)
strcpy(name, "Jorden");
else
strcpy(name, "Marlon");
if(n == 2)
if(result == 1)
strcpy(name, "Kobe");
else
strcpy(name, "Dancon");
if(n == 3)
if(result == 1)
strcpy(name, "O'neal");
else
strcpy(name, "Yao");
if(b[NUM] > b[NUM - 1]){//各人得票数的排名
for(int i = 2; i > 0; i--)
cout <<"b[" <<i <<"]: "
<<b[i]
<<"\t";
}
else{
for(int i = 1; i < 3; i++)
cout <<"b[" <<i <<"]: "
<<b[i]
<<"\t";
}
cout <<'\n';
cout <<"So "
<<name
<<" wins, congraturation!"
<<'\n'
<<endl;
for(int j = 0; j < NUM; j++)
b[j] = 0;
}