C 如何从循环中查找百分比并返回浮点值
我一直在试图弄清楚如何从循环中获取多个候选人的数据,并通过在同一个函数中声明他们的姓名、ID和总投票率,从中找出一个获胜者。我还需要从函数中返回获胜的候选人ID。例如:候选人1——名叫史蒂文斯,ID为13.07,总票数为1500票;候选人2——名叫乔治,ID为17.49,总票数为2000票。我需要输出的获胜者是ID为17.49的乔治和他在3500张选票中所占的百分比。这是我到目前为止所拥有的,但我对编程是新手,不知道如何解决这个问题。任何帮助都将不胜感激C 如何从循环中查找百分比并返回浮点值,c,C,我一直在试图弄清楚如何从循环中获取多个候选人的数据,并通过在同一个函数中声明他们的姓名、ID和总投票率,从中找出一个获胜者。我还需要从函数中返回获胜的候选人ID。例如:候选人1——名叫史蒂文斯,ID为13.07,总票数为1500票;候选人2——名叫乔治,ID为17.49,总票数为2000票。我需要输出的获胜者是ID为17.49的乔治和他在3500张选票中所占的百分比。这是我到目前为止所拥有的,但我对编程是新手,不知道如何解决这个问题。任何帮助都将不胜感激 float CandidateData(
float CandidateData(int N, int X) {
char candName[21];
int i;
double candID;
int candVotes;
int voteSum;
int j = 0;
double max = 0;
double first;
for (i = 0; i < N; i++) { //N is the number of candidates
printf("Enter candidates name (20 characters max): ");
scanf("%s", &candName);
printf("Candidate name: %s\n\n", candName);
printf("Enter candidate ID (float 01.01-52.99): ");
candID = CandidateID(52.99);
printf("Candidate ID is: %g\n\n", candID);
printf("Enter %d precinct votes (int 1-1000): ", X);
voteSum = 0;
for (j = 0; j < X; j++) { //X is number of precincts
candVotes = NumberOfCandidatesAndPrecincts(1000);
printf("Precinct #%d = %d\n", j + 1, candVotes);
voteSum = voteSum + candVotes;
}
printf("Total candidate votes = %d\n", voteSum);
printf("Average votes for county = %d\n\n", voteSum / X);
if (voteSum > max) {
max = voteSum;
}
}
printf("The winning candidate is %s with ID %g with %d% of total votes", candName, candID, )
return (candID);
}
float候选数据(int N,int X){
字符名称[21];
int i;
双重坦白;
不记名投票;
内窥镜;
int j=0;
双最大值=0;
双优先;
对于(i=0;i最大值){
max=voteSum;
}
}
printf(“获胜候选人为%s,ID为%g,总票数为%d%”,candName,candID,)
返回(坦诚);
}
我发现您的代码中几乎没有错误
首先,它应该是:
scanf("%s", candName);
不是
因为candName
已经是数组第一个元素的地址
另一个问题,假设一个用户为candName
键入whicher-President
。然后试试这个:
printf("Candidate name: %s\n\n", candName);
您将看到只打印任何人,因为scanf(“%s”,candName)
在出现空白之前接受输入。它可以是空白('
)、新行(\n
)或制表符(\t
)
相反,我强烈建议您使用fgets
fgets (candName, 21, stdin);
其中21
是您想要的最大缓冲区长度
现在,您可以使用printf
打印candName
,并观察到fgets
在字符串末尾添加了额外的新行('\n'
)。您可以通过以下方式轻松完成:
请记住,同时使用fgets
和scanf
是有问题的,因为scanf
函数将\n
留在输入缓冲区中,在fgets
从缓冲区读取输入后,您将遇到意外结果。使用getchar()代码>在每个scanf
之后。它消耗\n
。问题消失了
您犯的下一个错误是CandidateData
函数的return\u type
。它的返回类型是float
,但candID
变量的类型是double
,因此只对其中一个变量进行更改。在任务中,对candID
使用float
类型就足够了
最后一个printf
函数中也有一些打字错误。应该是:
printf("The winning candidate is %s with ID %f with %d of total votes", candName, candID, voteSum);
让我们来回答你的问题。若要存储获得最大投票数的候选人,可以在候选人的投票数超过当前最大值时声明一个struct
,并填充相关字段
我添加了最终代码:
#include <stdio.h>
#include <string.h>
struct President{
char candName[21];
float candID;
int candVotes;
};
float CandidateData(int N, int X) {
struct President winnerCandidate = {.candVotes = 0};
char candName[21];
float candID;
int candVotes;
int voteSum;
int i,j;
for (i = 0; i < N; i++) { //N is the number of candidates
printf("Enter candidates name (20 characters max): ");
fgets (candName, 21, stdin);
if ((strlen(candName) > 0) && (candName[strlen (candName) - 1] == '\n')){
candName[strlen (candName) - 1] = '\0';}
printf("Candidate name: %s\n", candName);
printf("Enter candidate ID (float 01.01-52.99): ");
scanf("%f",&candID);
getchar();
printf("Candidate ID is: %.2f\n", candID);
voteSum = 0;
printf("Enter %d precinct votes (int 1-1000):\n", X);
for (j = 1; j <= X; j++) { //X is number of precincts
scanf("%d",&candVotes);
getchar();
printf("Precinct #%d = %d\n", j , candVotes);
voteSum = voteSum + candVotes;
}
printf("Total candidate votes = %d\n", voteSum);
printf("Average votes for county = %.3f\n\n", voteSum/(float)X);
if(voteSum > winnerCandidate.candVotes){
winnerCandidate.candVotes = voteSum;
winnerCandidate.candID = candID;
strcpy(winnerCandidate.candName,candName);
}
}
printf("The winning candidate is %s with ID %.2f with %d of total votes", winnerCandidate.candName, winnerCandidate.candID, winnerCandidate.candVotes);
return winnerCandidate.candID;
}
int main(){
float winner_candID = CandidateData(3,2);
printf("\nWinner Candidate ID is: %.3f",winner_candID);
return 0;
}
#包括
#包括
结构主席{
字符名称[21];
浮糖;
不记名投票;
};
浮点候选数据(整数N,整数X){
结构主席winnerCandidate={.candVoces=0};
字符名称[21];
浮糖;
不记名投票;
内窥镜;
int i,j;
对于(i=0;i0)和&(candName[strlen(candName)-1]=='\n')){
candName[strlen(candName)-1]='\0';}
printf(“候选名称:%s\n”,candName);
printf(“输入候选人ID(float 01.01-52.99):”;
scanf(“%f”&坦率);
getchar();
printf(“候选人ID为:%.2f\n”,坦率);
voteSum=0;
printf(“输入%d个选区选票(整数1-1000):\n”,X);
对于(j=1;j winnerCandidate.candVoces){
winnerCandidate.candVotes=voteSum;
winnerCandidate.candID=candID;
strcpy(winnerCandidate.candName,candName);
}
}
printf(“获胜候选人是%s,ID为%.2f,总票数为%d”,winnerCandidate.candName,winnerCandidate.candID,winnerCandidate.candVoces);
返回winnerCandidate.candID;
}
int main(){
浮动优胜者=候选数据(3,2);
printf(“\n内部候选人ID为:%.3f”,获胜者\u坦率);
返回0;
}
如果你有任何问题,请告诉我 您应该发布一个包含一些输入和预期与实际输出的简单示例的示例。在编写代码时,参数名称如N
和X
即使在当前上下文中也是毫无意义的。请使用有意义的变量(和参数)名称。OT:关于如下语句:scanf(“%s”、&candName)代码>输入格式转换说明符“%s”允许用户为候选名称输入任意数量的字符。即使代码通知用户名称需要为20个(或更少)字符,这也不会对用户造成限制。此外,裸数组名称降级为数组第一个字节的地址&candName
具有完全不同的
printf("The winning candidate is %s with ID %f with %d of total votes", candName, candID, voteSum);
#include <stdio.h>
#include <string.h>
struct President{
char candName[21];
float candID;
int candVotes;
};
float CandidateData(int N, int X) {
struct President winnerCandidate = {.candVotes = 0};
char candName[21];
float candID;
int candVotes;
int voteSum;
int i,j;
for (i = 0; i < N; i++) { //N is the number of candidates
printf("Enter candidates name (20 characters max): ");
fgets (candName, 21, stdin);
if ((strlen(candName) > 0) && (candName[strlen (candName) - 1] == '\n')){
candName[strlen (candName) - 1] = '\0';}
printf("Candidate name: %s\n", candName);
printf("Enter candidate ID (float 01.01-52.99): ");
scanf("%f",&candID);
getchar();
printf("Candidate ID is: %.2f\n", candID);
voteSum = 0;
printf("Enter %d precinct votes (int 1-1000):\n", X);
for (j = 1; j <= X; j++) { //X is number of precincts
scanf("%d",&candVotes);
getchar();
printf("Precinct #%d = %d\n", j , candVotes);
voteSum = voteSum + candVotes;
}
printf("Total candidate votes = %d\n", voteSum);
printf("Average votes for county = %.3f\n\n", voteSum/(float)X);
if(voteSum > winnerCandidate.candVotes){
winnerCandidate.candVotes = voteSum;
winnerCandidate.candID = candID;
strcpy(winnerCandidate.candName,candName);
}
}
printf("The winning candidate is %s with ID %.2f with %d of total votes", winnerCandidate.candName, winnerCandidate.candID, winnerCandidate.candVotes);
return winnerCandidate.candID;
}
int main(){
float winner_candID = CandidateData(3,2);
printf("\nWinner Candidate ID is: %.3f",winner_candID);
return 0;
}