一个关于选票的程序

moffatte 2004-09-24 11:04:13
题目算法:一个班级里选班长,团支书和学习 委员,用数组做。把一个班级每个人的选票存到数组中,然后,计算出票数,打印各候选人的票数,最后得出结果。
之前,我已经调试好了。现在我要加入新的功能:在输入名字时,可以中断输入,查看投票的情况,然后在继续输入投票。问题是,在运行时,当输入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;

}

...全文
177 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pini 2004-09-24
  • 打赏
  • 举报
回复
you are so foolish!
sanzheng 2004-09-24
  • 打赏
  • 举报
回复
不用.用string就够了
moffatte 2004-09-24
  • 打赏
  • 举报
回复
这个是刚刚修改过的代码:
# 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(!strcmp(name, "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;

}
moffatte 2004-09-24
  • 打赏
  • 举报
回复
对呀.是应该用strcmp比较方便。不过改了以后。还是没有实现--中断,再继续的功能。
我基本上快完成了,所以想在原来的基础上稍做修改,希望能实现那个功能。
另外,(name[4] == "stop") 这句话应该没问题的。我在VC下都通过了。就是比较正个字符串。
daylove 2004-09-24
  • 打赏
  • 举报
回复
string 类型的
daylove 2004-09-24
  • 打赏
  • 举报
回复
是否应该把它定义为一个结构数组??
wound979 2004-09-24
  • 打赏
  • 举报
回复
呵呵,同意楼上的,你可以用strcmp(name,"stop")来比较
daylove 2004-09-24
  • 打赏
  • 举报
回复
试了一下,下面的这个
if(name[4] == 'stop') 一直只false,大哥,你的那个数组是个字符数组,可以用这比较吗?

这个一次只能比较一个字符是否和另一个字符相同吧!
blade_zheng 2004-09-24
  • 打赏
  • 举报
回复
不好意思,总是感觉逻辑上也有问题
blade_zheng 2004-09-24
  • 打赏
  • 举报
回复
if(name[4] == "stop"){//当?入?stop ?,就中断

这是什么意思???编译都不过啊

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧