| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 420 人关注过本帖
标题:请大家帮我看看这个程序,哪里出错了呢???
取消只看楼主 加入收藏
Kingsonsun
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-7-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
请大家帮我看看这个程序,哪里出错了呢???
在配对跳舞的时候总是出错,大家能帮忙看看吗

题目是:舞伴问题
功能:假设在周末舞会上,男士和女士进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一算法模拟上述舞伴配对问题。
分步实施:
1.    用循环队列组织数据,实现程序功能
2.    用链式结构的单循环队列组织数据,实现程序功能

程序代码:
/*****************************************************************/
// 跳舞成员管理系统
// 系统登陆密码:3110911038
/*****************************************************************/
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <conio.h>
#include <time.h>
#include <cmath>



#define MAXSIZE 200          /*跳舞成员最大个数*/
int static sum=0;            /*记录跳舞人数总数*/
#define PASSWORD "3110911038" //系统登陆密码

/************************跳舞成员信息结构体**********************/
typedef struct  /*跳舞成员个人信息结构体*/
{
    char name[20];
    char sex;
}Person;                  

typedef struct   /*男女队列顺序队列类型*/
{   
    Person *base;            /*初始化时动态分配队列存储空间*/  
    int front;               
    int rear;   
}SqQueue;      
/*初始化跳舞成员表*/
int Init_SqQueue(SqQueue &Q)   
{   
    Q.base=(Person*)malloc(MAXSIZE*sizeof(Person));   /*申请结点空间*/  
    if(Q.base==NULL)
        exit(-1);               
    Q.front=0;
    Q.rear=0;
    return 1;   
    }            
        
/*删除队列成员*/  
int Des_SqQueue(SqQueue &Q)   
{   
    delete []Q.base;   
    Q.base=NULL;   
    Q.front=0;
    Q.rear=0;   
    return 1;   
}   
                       
/*队员入队准备跳舞(从数组中插入元素e为Q的新的队尾元素)*/                        
int In_SqQueue(SqQueue &Q,Person &e)   
{   
    if((Q.rear+1)%MAXSIZE==Q.front)   
    {
        printf("队列已满!\n");
        return 0;     
    }
    else
    {
        Q.rear=(Q.rear+1)%MAXSIZE;
        Q.base[Q.rear]=e;
        return 1;   
    }  
}                           
     
/*队员出队跳舞(若队列不空,则删除Q的队头元素,用e返回其值)*/
int Out_SqQueue(SqQueue &Q,Person &e)   
{   
    if(Q.front==Q.rear)
    {
        printf("队列已空!\n");
        return 0;  
    }
    else
    {
        Q.front=(Q.front+1)%MAXSIZE;
        e=Q.base[Q.front];   
        return 1;   
    }   
}                  

/*判断队列是否为空,如果为空返回1*/
int Empty_SqQueue(SqQueue Q)
{
    if(Q.front==Q.rear)
        return 1;
    else
        return 0;
}                            /*用来判断各轮舞会结束后队列是否剩余人*/  



 

/************************函数块声明****************************/
void password(); /*密码验证*/
int menu();/*菜单界面*/





/************************主函数****************************/
int main(void)
{
    system("color 3F");   /*改变背景,字体颜色*/
    SqQueue man_q,woman_q;      /*定义男队,女队*/
    Init_SqQueue(man_q);
    Init_SqQueue(woman_q);
    int num=0;                  /*跳舞人数*/
    int i=0 ;                   /*用于for函数循环*/   
    int cishu=1;                /*跳舞次数*/
    Person wtmp,mtmp;           /*用于保存DeQueue返回的值*/
    int keepmnum=0;             /*用于保存男队伍剩下人数*/
    int keepwnum=0;             /*用于保存女队伍剩下人数*/
    int newnum=0;               /*新加人数*/
    int dnum=0;                 /*删除人数*/
    Person pson[MAXSIZE];
    int in;
    time_t start,end;
    password(); //获取用户密码验证


    do
    {
        system("cls");
        in=menu(); //系统主菜单显示
        switch(in)
        {
        case 1 :
            {
                system("cls");
                cout<<endl<<endl;
                cout<<"舞池场地有限,人数不能超过200人哦!"<<endl;
                cout<<"请输入跳舞人数:";
                cin>>num;
                sum =num+newnum;
                while(num<1||num>200)
                {
                    cout<<"人数太多啦,场地有限 "<<endl;
                    cout<<"请重新输入(人数不要超过200人哦):";
                    cin>>num;
                    
                }              /*while 判断num是否在允许范围内*/
                cout<<endl;
                cout<<"输入舞者姓名和性别:"<<endl;
                for(i=0;i<num;i++)
                {
                    cout<<"姓名:";
                    cin>>pson[i].name;
                    
                    cout<<"性别(b/g):";
                    cin>>pson[i].sex;
                    cout<<endl;
                }              /*输入姓名和性别*/
                for(i=0;i<num;i++)
                {
                    if(pson[i].sex=='b')
                    {
                        In_SqQueue(man_q,pson[i]);
                    }         /*男队入列*/
                    else
                        In_SqQueue(woman_q,pson[i]);
                }             /*女队入列*/  
               
                break;
            }
        case 2 :
            {
                system("cls");
                cout<<endl<<endl;
                cout<<"--------------------------------------"<<endl;
                cout<<"开始配对进场跳舞咯:"<<endl;
                if(Empty_SqQueue(man_q))
                {
                    cout<<"男队为空不能配对 "<<endl;
                    break;
                    if(Empty_SqQueue(woman_q))
                    {
                        cout<<"女队为空不能配对 "<<endl;
                        break;
                    }
                }
                while(!Empty_SqQueue(man_q)&&!Empty_SqQueue(woman_q))
                {
                    Out_SqQueue(man_q,mtmp);
                    Out_SqQueue(woman_q,wtmp);
                    cout<<mtmp.name<<"与"<<wtmp.name<<"一块跳舞"<<endl;
                }           /*配对成功,出队跳舞*/
                if(!Empty_SqQueue(man_q))
                {
                    keepmnum=(man_q.rear-man_q.front+MAXSIZE)%MAXSIZE;                     
                }           /*用keepmnum保存配对后男队剩下的人数*/                          
                if(!Empty_SqQueue(woman_q))
                {
                    keepwnum=(woman_q.rear-woman_q.front+MAXSIZE)%MAXSIZE;
                }           /*用keepmnum保存配对后女队剩下的人数*/                       
                cout<<"第 "<<cishu<<"舞曲结束!!!"<<endl;
                cishu++;
                break;
            }
        case 3 :
            {
                system("cls");
                for(i=0;i<sum;i++)
                {
                    int tag=0;
                    if(pson[i].sex =='b')
                    {
                        if(keepmnum!=0)
                        {
                            for(int t=0;t<keepmnum;t++)
                            {                           
                                if(strcmp(pson[i].name,man_q.base[man_q.front+t].name)==1)
                                {                  
                                    t=1000;
                                    tag=1;          /*标识   如果tag=1 ,表明队列中有相同的人名*/
                                }
                            }
                            if (tag==0)
                            {                                                              
                                In_SqQueue(man_q,pson[i]);
                            }
                        }
                        else                                                                  
                            In_SqQueue(man_q,pson[i]);
                    }
                    if(pson[i].sex =='g')
                    {
                        if(keepwnum!=0)
                        {
                            for(int tt=0;tt<keepwnum;tt++)
                            {                                                               
                                if(strcmp(pson[i].name,woman_q.base[woman_q.front+tt].name)==1)
                                {
                                    tt=1000;
                                    tag =1;
                                }                                                                       
                            }
                            if (tag==0)
                            {
                                In_SqQueue(woman_q,pson[i]);
                            }
                            else
                                break;
                        }
                        else
                            In_SqQueue(woman_q,pson[i]);
                    }                                          
                }
                break;
            }
        case 4 :
            {
                system("cls");
                cout<<"输入新加进人数:"<<endl;
                cin>>newnum;
                sum=newnum+num;                              
                for(i=num;i<sum;i++)
                {
                    cout<<"姓名:";
                    cin>>pson[i].name;
                    cout<<endl;
                    cout<<"性别 (b/g):";
                    cin>>pson[i].sex;
                    cout<<endl;
                }
                break;
            }
        case 5:
            break;
        default: printf("没有此选项,请按任意键返回重新选择!");
            getch();
            system("cls");
            break;
        }
    }while(in!=5);
    system("cls");
    printf("\n\n\n\n\n\n\n\n\t\t\t");
    printf("谢 谢 使 用 本 系 统 !");
    start=time(NULL);
    end=time(NULL);
    while(end-start<1.5)
        end=time(NULL);
    return 0;
}
   

/************************密码验证*****************************/
void password()
{
    char *pass=NULL; //初始化指针
    int count;
    time_t start,end; //时间函数

    pass=(char *)malloc(255*sizeof(char)); //分配内存
    while(1)
    {
        count=0;
        printf("\n\n\n\n\n\t\t 跳舞信息管理系统 \n");
        printf("\n\t\t 用户名:孙敬张 \n");
        printf("\n\t\t 密 码:");
        while((count>=0)&&(pass[count++]=getch())!=13) //密码输入
        {
            if(pass[0]=='\b')
            {
                count=0;
                continue;
            }
            else if(pass[count-1]=='\b')
            {
                printf("%c%c%c",'\b','\0','\b');
                count-=2;
            }
            else
                putchar('*');
        }
        pass[--count]='\0';
        if(!strcmp(pass,PASSWORD)) //开始验证
        {
            system("cls");
            printf("\n\n\n\n\n\n\n\n\n\t\t");
            printf(" 欢 迎 登 陆 跳 舞 配 队 管 理 系 统 !");
            start=time(NULL);
            end=time(NULL);
            while(end-start<2) //延时1秒执行以下程序
            {
                end=time(NULL);
            }
            system("cls");
            free(pass); //释放储存密码的内存
            return ;
        }
        free(pass); //释放密码错误的那块内存
        pass=(char *)malloc(255*sizeof(char)); //重新分配储存密码内存
        system("cls");
        printf("\a\n\n\n\n\n\n\n\n\t\t\t\t密 码 错 误 !");
        getch();
        system("cls");
    }
}





/***********************系统菜单************************/
int menu()
{
    int in;
    char ch;

    printf("\n\n\n\n");
    printf("\t ╭────────────────────────────╮\n");
    printf("\t ∣ 学生个人信息管理系统                                   ∣\n");
    printf("\t |————————————————————————————|\n");
    printf("\t | \t    1. 输入舞者成员信息             |\n");
    printf("\t |                            |\n");
    printf("\t | \t    2. 舞者配对跳舞                 |\n");
    printf("\t |                            |\n");
    printf("\t | \t    3. 下一轮跳舞                  |\n");
    printf("\t |                            |\n");
    printf("\t | \t      4. 输入新舞者成员信息              |\n");
    printf("\t |                            |\n");
    printf("\t | \t      5. 退      统                  |\n");
    printf("\t |                            |\n");
    printf("\t  ╰────────────────────────────╯\n");
    printf("\t 请您正确选择:");
    while(scanf("%d",&in)!=1)
    {
        while((ch=getchar())!='\n')
            putchar(ch);
        return in;
    }
    fflush(stdin);  /*清空输入缓冲区*/
    return in;
}






搜索更多相关主题的帖子: 管理系统 单循环 男士 
2013-07-02 10:56
Kingsonsun
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-7-1
收藏
得分:0 
回复 楼主 Kingsonsun
能给出些详细解答吗??新手改了好久都改不好
2013-07-02 11:05
Kingsonsun
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-7-1
收藏
得分:0 
回复 2楼 神经不正常
能给出些详细解答吗??新手改了好久都改不好
2013-07-02 11:06
快速回复:请大家帮我看看这个程序,哪里出错了呢???
数据加载中...
 
   



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

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