一个C语言链表编写的投票系统,还请各位大神为我解惑。

learner66 2014-10-19 07:00:45
这个程序的只能输入第一个竞选人的信息,而不能输入其他竞选人的信息。我认为主要错误在calculate()函数中,但自己又发现不了,还望路过的大神不吝赐教,程序很简单,只是错误没看出来。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct Position)
typedef struct Position //竞选人的结构体
{
char name[20];
int flag;
int account;
struct Position *next;
}Node;
Node* inlist();//建立动态结构链表
void print(Node *po);//输入结果
void vote();
Node *calculate(Node *po);//投票处理

Node* inlist()
{
Node *q,*p;
Node *head;
int n=0;
p=q=(Node*)malloc(LEN);
p->account=0;
scanf("%s%d",p->name,&p->flag);
head=NULL;
while(p->flag!=0)
{
n=n+1;
if(n==1)
head=p;
else
q->next=p;
q=p;
p=(Node*)malloc(LEN);
p->account=0;
scanf("%s%d",p->name,&p->flag);
}
q->next=NULL;
return head;
}
Node *calculate(Node *po)
{
Node *head=po;
Node *p=head;
char n;
[u]n=getchar();
while(p->next!=NULL)
{
while(n!='0')
{
if(n==p->flag)
p->account++;
n=getchar();
}
p++;
}
return head;
}


void print(Node *po)
{
Node *p=po;
for(;p->next!=NULL;p++)
printf("%s %d %d\n",p->name,p->flag,p->account);
}


void vote()
{
char m;
Node *head;
char position[20];
printf("请输入要竞选的职位:");
gets(position);
printf("请输入要参加该职位竞选的人员名字:\n");
head=inlist();
printf("请输入投票情况:");
head=calculate(head);
printf("得到的结果是:");
print(head);
}

int main()
{
printf(" 欢迎使用本系统!\n");
vote();
}
[size=24px][/size]
...全文
335 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33820637 2016-12-09
  • 打赏
  • 举报
回复
楼主怎么解决的这一道题啊?
百曉生 2014-10-21
  • 打赏
  • 举报
回复
引用 5 楼 learner66 的回复:
[quote=引用 4 楼 u012421456 的回复:] char n; [u]n=getchar();
是n=getchar();就是接受一个字符,看和哪一位的序号相同,一开始我使用数组的,就是让数组中的数和竞选人的序号相比较,相同册p->account增1.您看看这个到底为什么只能输出第一个竞选人的结果。[/quote] 那n前边的[u]什么意思,我本以为楼主写错了,没想到楼主不提它
xionggch 2014-10-21
  • 打赏
  • 举报
回复
Node *calculate(Node *po) { Node *head=po; Node *p=head; char n; [u]n=getchar(); while(p->next!=NULL) { while(n!='0') { if(n==p->flag) p->account++; n=getchar(); } p++; //这不能有p++,链表分配的空间不是连续的,改成if( p->next!=NULL ) p=p->next; }
learner66 2014-10-21
  • 打赏
  • 举报
回复
引用 4 楼 u012421456 的回复:
char n; [u]n=getchar();
是n=getchar();就是接受一个字符,看和哪一位的序号相同,一开始我使用数组的,就是让数组中的数和竞选人的序号相比较,相同册p->account增1.您看看这个到底为什么只能输出第一个竞选人的结果。
learner66 2014-10-21
  • 打赏
  • 举报
回复
[quote=引用 6 楼 xionggch 的回复:] 一语中的,谢谢你的解答。以后多加交流
learner66 2014-10-21
  • 打赏
  • 举报
回复
[quote=引用 7 楼 u012421456 的回复:] [quote=引用 5 楼 learner66 的回复:] [quote=引用 4 楼 u012421456 的回复:] 已经解决了,谢谢您的解答,
learner66 2014-10-21
  • 打赏
  • 举报
回复
[quote=引用 6 楼 xionggch 的回复:]



[quote=引用 7 楼 u012421456 的回复:]
[quote=引用 5 楼 learner66 的回复:]
[quote=引用 4 楼 u012421456 的回复:]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct Position)
typedef struct Position //竞选人的结构体
{
char name[20];
int flag;
int account;
struct Position *next;
}Node;
Node* inlist();//建立动态结构链表
void print(Node *po);//输入结果
void vote();
Node *calculate(Node *po,int str[]);//投票处理

Node* inlist()
{
Node *q,*p;
Node *head;
int n=0;
p=q=(Node*)malloc(LEN);
p->account=0;
scanf("%s%d",p->name,&p->flag);
head=NULL;
while(p->flag!=0)
{
n=n+1;
if(n==1)
head=p;
else
q->next=p;
q=p;
p=(Node*)malloc(LEN);
p->account=0;
scanf("%s%d",p->name,&p->flag);
}
q->next=NULL;
return head;
}
Node *calculate(Node *po,int str[])
{
Node *p=po;

int i;
for(;p->next!=NULL;p=p->next)
{
for(i=0;str[i]!=0;i++)
if(str[i]==p->flag)
p->account++;
}

return po;
}


void print(Node *po)
{
Node *p=po;
for(;p->next!=NULL;p++)
printf("%s %d %d\n",p->name,p->flag,p->account);
}


void vote()
{
char m;
Node *head;
int i;
char position[20];
int str[1000];
printf("请输入要竞选的职位:");
gets(position);
printf("请输入要参加该职位竞选的人员名字:\n");
head=inlist();
printf("请输入投票情况:");
for(i=0;;i++)
{scanf("%d",&str[i]);
if(str[i]==0)break;
}
head=calculate(head,str);
printf("得到的结果是:");
print(head);
}

int main()
{
printf(" 欢迎使用本系统!\n");
vote();
}
这次我重新改了一下,就是您只能显示我,而不能显示后边两位的选票数。
百曉生 2014-10-20
  • 打赏
  • 举报
回复
char n; [u]n=getchar();
learner66 2014-10-20
  • 打赏
  • 举报
回复
谢谢您的解答,不过我的程序是一个投票系统,就是把竞选人给他们都标上号,然后根据号来进行投票。问题是我只能输出第一个人的票数,而不能输出后面的。
赵4老师 2014-10-20
  • 打赏
  • 举报
回复
仅供参考
//带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
    int          data;
    struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
    srand(time(NULL));

    //填写头节点数据
    H.data=-1;
    H.next=NULL;
    head=&H;

    //创建10个节点的单链表
    p=head;
    for (i=0;i<10;i++) {
        q=(struct NODE *)malloc(sizeof(struct NODE));
        if (NULL==q) return 1;
        q->data=rand()%100;//填写0..99的随机值
        q->next=NULL;
        p->next=q;
        p=q;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将值为5的结点插入到单链表的第k个结点前
    k=3;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=(struct NODE *)malloc(sizeof(struct NODE));
            if (NULL==q) return 1;
            q->data=5;
            q->next=p->next;
            p->next=q;
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //删除第k个节点
    k=5;
    n=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        n++;
        if (k==n) {
            q=p->next;
            if (q) {
                p->next=q->next;
                free(q);
            }
            break;
        }
        p=p->next;
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //从小到大排序
    for (p=head;p!=NULL && p->next!=NULL;p=p->next) {
        for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {
            if (p->next->data > q->next->data) {

                //交换data
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
//              t=p->next->data;p->next->data=q->next->data;q->next->data=t;

                //或者

                //交换next
//              printf("swap %02d %02d\n",p->next->data,q->next->data);
                s1=p->next;
                s2=p->next->next;
                s3=q->next;
                s4=q->next->next;

                if (s2!=s3) {
                     p->next=s3;
                    s3->next=s2;
                     q->next=s1;
                    s1->next=s4;
                } else {
                     p->next=s3;
                    s3->next=s1;
                           q=s3;
                    s1->next=s4;
                }

                //输出整个单链表
//              s=head->next;
//              while (1) {
//                  if (NULL==s) {
//                      printf("\n");
//                      break;
//                  }
//                  printf("%02d->",s->data);
//                  s=s->next;
//              }
//              getchar();
            }
        }
    }

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10
    m=4;
    n=6;
    k=0;
    p=head;
    while (1) {
        if (NULL==p) {
            break;
        }
        k++;
        if (m+1==k) {
            q=p;
        }
        s=p;
        p=p->next;
    }
    s1=head->next;
    head->next=q->next;
    s->next=s1;
    q->next=NULL;

    //输出整个单链表
    s=head->next;
    while (1) {
        if (NULL==s) {
            printf("\n");
            break;
        }
        printf("%02d->",s->data);
        s=s->next;
    }

    //释放所有节点
    p=head->next;
    while (1) {
        if (NULL==p) {
            break;
        }
        q=p->next;
        free(p);
        p=q;
    }

    return 0;
}
//18->94->58->17->27->20->43->57->75->78->
//18->94->05->58->17->27->20->43->57->75->78->
//18->94->05->58->27->20->43->57->75->78->
//05->18->20->27->43->57->58->75->78->94->
//43->57->58->75->78->94->05->18->20->27->
//
learner66 2014-10-19
  • 打赏
  • 举报
回复
错了,是只能输出第一个人的信息,而不能输出后面的。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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