gpt4 book ai didi

c - 为什么我的变量计算不正确?

转载 作者:行者123 更新时间:2023-11-30 19:03:00 26 4
gpt4 key购买 nike

我正在为类(class)制作一个简单的程序,以便能够根据基于选举团的系统找出新的市长/总统。数组中的输入以及结果表的后续输出都运行良好。 ..我的变量 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

最佳答案

你的函数中有一些逻辑错误declareNewMayor .

1> 您将字符串文字分配给字符变量 winner 。这将导致未定义的行为

 winner = "A";
winner = "B";
winner = "C";

所以应该替换为:

 winner = 'A';
winner = 'B';
winner = 'B';

2> 函数中的 if ... else declareNewMayor 。其他情况你就不查了。例如:aStatesWon == bStatesWon == cStatesWon == dStatesWon 。这种情况下结果如何呢?如果不关心其他情况,可以初始化2个变量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

关于c - 为什么我的变量计算不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54735975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com