| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 961 人关注过本帖
标题:求助各位大侠之:小程序的迷惑
只看楼主 加入收藏
mckingstar
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-7-6
收藏
 问题点数:0 回复次数:2 
求助各位大侠之:小程序的迷惑
小弟最近c语言实习,编了一个班委投票的程序,但苦经调试,还是不得要害,恳请各位大侠帮帮忙,小弟不胜感激涕零!
#include<malloc.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define N 20
#define LEN sizeof(struct stu)
char lname[20];
int good=0;
int done=0;
int a[20][2];
struct stu *create();
struct stu jipiao(struct stu *head);
struct stu *search1(struct stu *toupiao,struct stu *head);
struct stu *paixu1(struct stu *head);
struct stu *paixu2(struct stu *head);
struct stu *print1(struct stu *head);
struct stu *print2(struct stu *head);






struct stu
{
    char name[20];
    char num[3];
    char toupiao[6][3];
    int dep;
    struct stu *next;
};
//**************************************创建链表*******************************
struct stu *create(void)
{
    struct stu *head,*p,*q;
    int n=1;
    good=1;
    p=(struct stu *)malloc(LEN);
    head=q=p;
    printf("input a name:\n");
    flushall();
    gets(p->name);
    printf("input num:\n");
    flushall();
    gets(p->num);
    while(n<=N)
    {
         p=(struct stu  *)malloc(LEN);
         gets(p->name);
         gets(p->num);
         q->next=p;
         q=p;
         n++;
    }
    q->next=NULL;
    return head;
}
//****************************************计票************************************
struct stu *jipiao(struct stu *head)
{
     struct stu *p,*q1,*q2;
     int i,n=0;
     printf("输入所有人的投票选择:投票人的学号和所选人的学号\n");
     flushall();
     p=head;
     while(p!=NULL)
     {
         p->dep=0;
         p=p->next;
     }
     p=head;
     while(n<N)
     {
get:gets(lname);
         q1=search1(lname,head);
         if(q1!=NULL)
         {
             {
             for(i=0;i<6;i++)
         {
             scanf("%s",q1->toupiao[i]);
             flushall();
             q2=search1(q1->toupiao[i],head);
             if(q2!=NULL)
             {q2->dep++;}
         }
         }n++;
         }
         else goto get;
     }
     return  head;
}
//****************************************search 1********************************
struct stu *search1(struct stu *toupiao,struct stu *head)
{
    struct *p;
    int ok=0;
    p=head;
    while(p!=NULL)
    {
         if(strcmp(toupiao->num,p->num)==0)
         {return p;ok=1;break;}
         p=p->next;
    }
    if(!ok)
    {printf("not found!");return NULL;}
}
//****************************************paixu1  ********************************
struct stu *paixu1(struct stu *head)
{
     struct stu *p,*q,*t;
     int i,k=0,;
     p=head;
     for(i=0;i<N&&k!=0;i++)
     {
         k=0;
         p=head;
     while(q->next!=NULL)
     {
         q=p->next;
         if(strcmp(p->num,q->num)>0)
         {
             t=p;
             p=q;
             q=t;
             k=1;
         }
         p=p->next;
     }
     }
    return head;
}
//****************************************paixu2  ********************************
struct stu *paixu2(struct stu *head)
{
     struct stu *p,*q,*t;
     int i,k=0,t1,t2,dpiao,locate;
     done=1;
     p=head;
     for(i=0;i<N&&k!=0;i++)
     {
         k=0;
         p=head;
     while(q->next!=NULL)
     {
         q=p->next;
         if(p->dep<q->dep)
         {
             t=p;
             p=q;
             q=t;
             k=1;
         }
         p=p->next;
     }
     }
     p=head;
     for(i=0;i<N;i++)
     {
         a[i][0]=p->dep;
         a[i][1]=(p->num[1]-48)*10+p->num[0]-48;
         p=p->next;
     }
     //求出当选的六位班委
     for(i=0;i<N;i++)
        for(k=i;k<N-1;k++)
        {
            if(a[k][0]<a[k+1][0])
            {
                t1=a[k][0];
                t2=a[k][1];
                a[k][0]=a[k+1][0];
                a[k][1]=a[k+1][1];
                a[k+1][0]=t1;
                a[k+1][1]=t2;
            }
        }
     dpiao=a[5][0];
     locate=5;
     for(i=6;dpiao==a[i][0];i++)
         locate++;
     locate=random(locate-5+1)+5;//得票相同的随即抽取一个作为班委
     {
         t1=a[locate][0];
         t2=a[locate][1];
         a[locate][0]=a[5][0];
         a[locate][1]=a[5][1];
         a[5][0]=t1;
         a[5][1]=t2;
     }
     return head;
}
//****************************************print  1********************************
struct stu  *print1(struct stu  *head)
{
    struct stu  *p;
    p=head;
    while(p!=NULL)
    {
        printf("%s    %s",p->name,p->num);
        p=p->next;
    }
    return head;
}
//****************************************print  2********************************
struct stu *print2(struct stu *head)//输出当选班委的名单
{
     struct stu *P;
     int i,numb;
     p=head;
     numb=(p->num[1]-48)*10+p->num[0]-48;
     for(i=0;i<5;i++)
     {
        while(p->next!=NULL&&numb!=a[i][1])
        {
            p=p->next;
            numb=(p->num[1]-48)*10+p->num[0]-48;
        }
        printf("%s    %s\n",p->num,p->name);
     }
     return head;
}
main()
{
    struct stu  *head,*p,*q;
    int k=1;
    
    printf("***************************************\n");
    printf("*               main menu             *\n");
    printf("*            1   creat                *\n");
    printf("*            2   toupiao & jipiao     *\n");
    printf("*            3   putout all           *\n");
    printf("*            4   putont mingdan       *\n");
    printf("*            0   exit(0)              *\n");
    printf("¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ \n");
menu:
    printf("\n请输入控制字符k:\n");
    scanf("%d",&k);
    switch(k)
    {
good: case 1:head=create();break;
    case 2:{if(good=1)
           {jipiao(head);break;}
            else goto good;
            break;
           }
    case 3:print1(head);break;
    case 4:{paixu(head);print2(head);break;}
    case 0: exit(0);
    default :goto menu;
    }
}
调试结果
--------------------Configuration: sellect - Win32 Debug--------------------
Compiling...
sellect.c
D:\小组程序\sellect.c(60) : error C2040: 'jipiao' : 'struct stu *(struct stu *)' differs in levels of indirection from 'struct stu (struct stu *)'
D:\小组程序\sellect.c(75) : warning C4133: 'function' : incompatible types - from 'char [20]' to 'struct stu *'
D:\小组程序\sellect.c(83) : warning C4133: 'function' : incompatible types - from 'char [3]' to 'struct stu *'
D:\小组程序\sellect.c(98) : warning C4133: '=' : incompatible types - from 'struct stu *' to 'struct $S1 *'
D:\小组程序\sellect.c(101) : error C2037: left of 'num' specifies undefined struct/union '$S1'
D:\小组程序\sellect.c(101) : error C2198: 'strcmp' : too few actual parameters
D:\小组程序\sellect.c(102) : warning C4133: 'return' : incompatible types - from 'struct $S1 *' to 'struct stu *'
D:\小组程序\sellect.c(103) : error C2037: left of 'next' specifies undefined struct/union '$S1'
D:\小组程序\sellect.c(112) : error C2059: syntax error : ';'
D:\小组程序\sellect.c(182) : warning C4013: 'random' undefined; assuming extern returning int
D:\小组程序\sellect.c(210) : error C2065: 'p' : undeclared identifier
D:\小组程序\sellect.c(210) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct stu *'
D:\小组程序\sellect.c(211) : error C2223: left of '->num' must point to struct/union
D:\小组程序\sellect.c(211) : error C2223: left of '->num' must point to struct/union
D:\小组程序\sellect.c(214) : error C2223: left of '->next' must point to struct/union
D:\小组程序\sellect.c(216) : error C2223: left of '->next' must point to struct/union
D:\小组程序\sellect.c(217) : error C2223: left of '->num' must point to struct/union
D:\小组程序\sellect.c(217) : error C2223: left of '->num' must point to struct/union
D:\小组程序\sellect.c(219) : error C2223: left of '->num' must point to struct/union
D:\小组程序\sellect.c(219) : error C2223: left of '->name' must point to struct/union
D:\小组程序\sellect.c(248) : warning C4013: 'paixu' undefined; assuming extern returning int
执行 cl.exe 时出错.

sellect.obj - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: c语言 要害 班委 感激涕零 
2008-07-06 10:46
himpo
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:192
专家分:123
注 册:2008-5-16
收藏
得分:0 
没有细看。

1、建议把结构体定义放到函数声明的前面。。
2、在struct stu *print2(struct stu *head)//输出当选班委的名单
   有个struct stu *P;
   把P改成小写吧,估计是粗心的毛病。
3、主函数中有个
   case 2:{if(good=1)  这样的if貌似没用。。
4、goto看得很费劲。

别的没看。。

[[it] 本帖最后由 himpo 于 2008-7-6 11:28 编辑 [/it]]
2008-07-06 11:21
cxhiou
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-6-13
收藏
得分:0 
good ==1 吧

2008-07-06 12:37
快速回复:求助各位大侠之:小程序的迷惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017644 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved