#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
const int n = 20;
定义结构体和列表
typedef struct pole{
int year;
char county[n];
char ID_election[n];
char ID_electionUser[n];
int num_candidates;
char name_candidate[n];
int limit_votes;
}election;
typedef struct polelist{
election voting;
struct polelist *next_pole;
}node_pole;
功能
void createElection(node_pole *head_pole, election *info);
void insertElection(node_pole **head_pole, election *info);
void printElection(node_pole *head_pole);
主要
int main (){
node_pole *head_pole = NULL;
election *info = (election*)malloc(sizeof(election));
int i = 0;
我创建选举,然后插入节点。 我打印了刚刚创建的选举并且它起作用了(我可以读到候选人的名字)。
createElection(head_pole, info);
insertElection(&head_pole, info);
printf("\nDo you want to visualize your election? Y/N: ");
printf("\n----- Last election created -----\n ");
printf("\nID Election:%s \nID Elect_user:%s \nYear:%d\nCounty:%s \nMax voters:%d", info->ID_election, info->ID_electionUser, info->year, info->county, info->limit_votes);
printf("\nCandidates' number:%d", info->num_candidates);
for(i = 0; i < info->num_candidates; i++){
printf("\nCandidates' name [%d]: %s", i+1, ((info + i)->name_candidate));
}
这是终端上的结果
ID Election:D1 ID Elect_user:S1 Year:2023 County:USA Max voters:5 Candidates' number:3 Candidates' name [1]: A1 Candidates' name [2]: B2 Candidates' name [3]: C3
我再次打印选举结果,但使用函数 printElection。 我在这里遇到了问题。我看不清候选人的名字,只能读到第一个。
printf("\n\t\t\t-ALL-\n\n");
printElection(head_pole);
return 0;
}
通过终端中的功能打印选择:
ID Election:D1 ID Elect_User: S1 Year:2023 County:USA Max voters:5 Candidates' number:3 Candidates' name [1]: A1 Candidates' name [2]: Candidates' name [3]:
功能定义
void createElection(node_pole *head_pole, election *info){
election *pole = info;
int i = 0;
printf("\nInsert data: \n Year: ");
scanf("%d", &pole->year);
printf(" County:");
scanf("%s", pole->county);
printf(" Voters' limit:");
scanf("%d", &pole->limit_votes);
printf("ID_Election: ");
scanf("%s", pole->ID_election);
printf("ID_Election_User: ");
scanf("%s", pole->ID_electionUser);
printf("Candidates' number: ");
scanf("%d", &pole->num_candidates);
for(i = 0; i < pole->num_candidates; i++){
printf("\n Candidates' names [%d]:", i+1);
scanf("%s", (pole+i)->name_candidate);
}
return;
}
void insertElection(node_pole **head_pole, election *info){
node_pole *newnode = (node_pole*)malloc(sizeof(node_pole));
newnode->voting = *info;
newnode->next_pole = *head_pole;
*head_pole = newnode;
return;
}
void printElection(node_pole *head_pole){
int i;
node_pole *tmp = head_pole;
if(tmp == NULL){
printf("\nNo poll in the database.");
return;
}
while(tmp != NULL){
printf("\n----- Your election ----- ");
printf("\nID Election:%s \nID Elect_User: %s \nYear:%d \nCounty:%s \nMax voters:%d", tmp->voting.ID_election, tmp->voting.ID_electionUser, tmp->voting.year, tmp->voting.county, tmp->voting.limit_votes);
printf("\nCandidates' number:%d", tmp->voting.num_candidates);
for(i = 0; i < tmp->voting.num_candidates; i++){
printf("\nCandidates' name [%d]: %s", i + 1, (tmp+i)->voting.name_candidate);
}
tmp = tmp->next_pole;
}
return;
}
正如评论中所指出的,目前选举中只能容纳一个候选人姓名(20 - 1 个字符长)。
这是错误的:
printf("\nCandidates' name [%d]: %s", i+1, ((info + i)->name_candidate));
像上面这样的引用将
info
视为数组,并且等同于 info[i]->name_candidate
。
info
不是数组。
推动这一进程的小改变(并希望增加OP的理解):
#define MAX_CANDIDATES 8 // an arbitrary number
typedef struct pole{
int year;
char county[n];
char ID_election[n];
char ID_electionUser[n];
int num_candidates;
char name_candidate[MAX_CANDIDATES][n]; // 2D array to hold up to 8 x 20 characters
int limit_votes;
} election;
和
printf("# of Candidates: ");
scanf("%d", &pole->num_candidates);
if( pole->num_candidates > MAX_CANDIDATES )
// handle erroneous entry
for(i = 0; i < pole->num_candidates; i++){
printf("\n Candidate's name [%d]:", i+1);
scanf("%19s", pole->name_candidate[i] );
}
并修复打印语句:
printf("Candidate name [%d]: %s\n", i+1, info->name_candidate[i]);
这些变化应该能让OP取得一些成功并推动项目向前发展。值得重新审视这一点,既不要为较少的候选人使用过多的空间,也不要限制 8 名候选人。这是留给读者的练习。
最后:每次调用
scanf()
都会返回一个当前被忽略的值。获取用户输入充满挑战。为读者提供的另一个练习,以提高此代码的稳健性。