我正在制作一个简单的上课程序,以便能够基于基于选举学院的系统找到新的市长/总统。我输入的数组以及结果表的后续输出都可以很好地工作。 ..我的变量aStatesWon,bStatesWon,cStatesWon和dStatesWon(分别适用于候选A,B,C&D)即使我以为我已正确地计算了它们,也无法正确计算。我得到的数字类似于-2324252或62341,而不是我期望的2或3。这是代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STATES 5
#define CANDIDATES 4
void declareNewMayor(int aStatesWon,int bStatesWon,int cStatesWon, int dStatesWon);
void main() {
int votes[STATES][CANDIDATES];
int StateWon = -1;
int StateWinner;
int aStatesWon = 0;
int bStatesWon = 0;
int cStatesWon = 0;
int dStatesWon = 0;
int newMayor;
FILE* open;
int i,j;
for (i = 0; i < STATES; i++)
{
StateWinner = 0;
StateWon = -1;
for (j = 0; j < CANDIDATES; j++)
{
printf("Please enter the votes for candidate %d for state %d\n",(j+1),(i + 1));
scanf("%d",&votes[i][j]);
if (votes[i][j] > StateWon)
{
StateWon = votes[i][j];
StateWinner = j;
} //see if the won that state
}
printf("Winner of State %d is Candidate %d\n",i,j);
if (StateWinner = 0)
{
aStatesWon++;
}
else if (StateWinner = 1)
{
bStatesWon++;
}
else if (StateWinner = 2)
{
cStatesWon++;
}
else if (StateWinner = 3)
{
dStatesWon++;
}//increase number of states won by that candidate if the won that state
printf("States Won A:%d B:%d C:%d D:%d", aStatesWon, bStatesWon, cStatesWon, dStatesWon);
}//enter in votes for each candidate for each state
open = fopen("votes.txt", "w");
if (open == NULL)
{
printf("\nFile could not be opened\n");
}
else
{
printf("STATES CANDIDATE A CANDIDATE B CANDIDATE C CANDIDATE D\n");
fprintf(open,"STATES CANDIDATE A CANDIDATE B CANDIDATE C CANDIDATE D\n");
for (i = 0; i < STATES; i++)
{
printf("STATE %d", (i + 1));
fprintf(open,"STATE %d", (i + 1));
for (j = 0; j < CANDIDATES; j++)
{
printf(" %d ", votes[i][j]);
fprintf(open," %d ", votes[i][j]);
}
printf("\n");
}//print out table
}
declareNewMayor(aStatesWon,bStatesWon,cStatesWon,dStatesWon);
}//main
void declareNewMayor(int aStatesWon, int bStatesWon, int cStatesWon, int dStatesWon)
{
char winner;
int statesWon;
if (aStatesWon > bStatesWon&&aStatesWon > cStatesWon&&aStatesWon > dStatesWon)
{
statesWon = aStatesWon;
winner = "A";
}
else if (bStatesWon > aStatesWon&&bStatesWon > cStatesWon&&bStatesWon > dStatesWon) {
statesWon = bStatesWon;
winner = "B";
}
else if (cStatesWon > aStatesWon&&cStatesWon > bStatesWon&&cStatesWon > dStatesWon) {
statesWon = cStatesWon;
winner = "C";
}
else if (dStatesWon > aStatesWon&&dStatesWon > cStatesWon&&dStatesWon > aStatesWon) {
statesWon = dStatesWon;
winner = "C";
}
printf("The new Mayor is candiate %c with %d states won!", winner,statesWon);
}//calculate new mayor and declare it
答案 0 :(得分:1)
函数declareNewMayor
中存在一些逻辑错误。
1>您已将字符串文字分配给字符变量winner
。它将导致不确定的行为
winner = "A";
winner = "B";
winner = "C";
因此应替换为:
winner = 'A';
winner = 'B';
winner = 'B';
2>如果...,则在函数declareNewMayor
中为...。您不检查其他情况。例如:aStatesWon == bStatesWon == cStatesWon == dStatesWon
。在那种情况下结果如何?如果您不关心其他情况,则可以使用一些值初始化两个变量winner
,statesWon
,以避免使用未初始化的变量进行未定义的行为(这就是为什么您的输出与-2324252或62341奇怪的原因)。这是我的解决方法
void declareNewMayor(int aStatesWon, int bStatesWon, int cStatesWon, int dStatesWon)
{
char winner = '?';
int statesWon = 0;
if (aStatesWon > bStatesWon&&aStatesWon > cStatesWon&&aStatesWon > dStatesWon)
{
statesWon = aStatesWon;
winner = 'A';
}
else if (bStatesWon > aStatesWon&&bStatesWon > cStatesWon&&bStatesWon > dStatesWon) {
statesWon = bStatesWon;
winner = 'B';
}
else if (cStatesWon > aStatesWon&&cStatesWon > bStatesWon&&cStatesWon > dStatesWon) {
statesWon = cStatesWon;
winner = 'C';
}
else if (dStatesWon > aStatesWon&&dStatesWon > cStatesWon&&dStatesWon > aStatesWon) {
statesWon = dStatesWon;
winner = 'D';
}
printf("The new Mayor is candiate %c with %d states won!", winner,statesWon);
}//calculate new mayor and declare it