| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 986 人关注过本帖
标题:C程序修改成C++
取消只看楼主 加入收藏
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:0 
C程序修改成C++
我刚学C++,不会用,这是用C语言写出来的,谁能把这个改成C++,
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#define MAX 20
//定义结构体
struct rwait
{  
      int  rw;//读者等待人数
      int  wr;//写者等待人数
      
      

};
struct Reader1
{
    int  readr;
   
    int  rdoor;//rdoor==1表示门未上锁
   
};
struct Writer1
{
    int  writer;

    int  wdoor;
};
struct rwait * L=(struct rwait*)malloc(sizeof(struct rwait));

static struct Reader1 * R=(struct Reader1*)malloc(sizeof(struct Reader1));

struct Writer1 * W=(struct Writer1*)malloc(sizeof(struct Writer1));




//读者操作
//读者进操作,第一个进来的读者把写者之门关上
//
void  reader(struct Reader1 *R,struct Writer1 *W)
{
    W->wdoor=0;


    R->readr++;
    printf("\n");
    printf("现在阅览室读者人数为%d!",R->readr);
    printf("\n");
}
//du zhe chu cao  zuo
void  lreader(struct Reader1 *R,struct Writer1 *W)
{   
    int p;
    while(1)
    {
       if(R->readr>0)
       {

       W->wdoor=0;

       R->readr--;
    printf("\n");
    printf("现在阅览室读者人数为%d!",R->readr);
    printf("\n");
    printf("是否继续离开(是:1/否:0):");
    scanf("%d",&p);
     if(p==1)break;

       }
        else
       {
        W->wdoor=1;
        break;
       }
    }

}
//写者操作
//写者进操作
void writer(struct Reader1 *R,struct Writer1 *W)
{
    if(W->writer==0)
    {

    R->rdoor=0;
    W->writer++;
        printf("\n");
    printf("现在阅览室写者人数为%d!",W->writer);
    printf("\n");
    }
    else
    {
        printf("阅览室已有读者在工作,请您稍等!");
    }
}
//写者出操作
void  lwriter(struct Reader1 *R,struct Writer1 *W)
{
    int p;
while(1)
    {
       if(W->writer>0)
       {

       R->rdoor=0;

       W->writer--;

       printf("\n");

       printf("现在阅览室写者人数为%d!",W->writer);

       printf("\n");

       printf("是否继续离开(是:1/否:0):");

       scanf("%d",&p);

       if(p==1)break;

       }
        else
       {
        R->rdoor=1;//写者出去完之后,把读者之门打开
        break;
       }
    }
}


void main()
{
   
    int ch;
    int flag=0,num,num2,flag2=1;
    int mark=1;
    L->wr=L->rw=0;
R->readr=0;W->writer=0;
 R->rdoor=1;
 W->wdoor=1;
   


   
    while(1)
{
              printf("   1.进入\n");

              printf("   2.离开\n");
              //printf("   3.等待问题\n");

              printf("   0.退出\n");

              printf("请选择:\n");

              scanf("%d",&num);
            switch(num)

             {      case 0:
                         printf("谢谢使用!\n");
                       return ;
                    case 1:
                        while(1)
                        {
                          if(flag==0)
                               //输入第一个进入者的信息
                          {
                              printf("现在阅览室为空,进入前请输入你的角色(读者:1 写着:2):\n");

                               flag++;
                          }

                         else
                              printf("进入前请输入你的角色(读者:1 写着:2):\n");
   
                        
                              scanf("%d",&ch);
                              //以写者的身份申请进入
                             if(ch==2)
                             {
                                 //如果写者之门打开,则进入
                                 if(W->wdoor==1)

                                        //执行写者操作
                                          writer(R,W);
                                 //如果门被锁,则等待
                                else

                                {
                                          L->wr++;
                                          printf("写者等待人数:%d \n",L->wr);

                                          printf("读者人数:%d \n",R->readr);
                                          R->rdoor=0;     //关闭读者进入之门
                                          printf("读者进入之门已关闭!\n");

                                          //如果写者等待人数大于正在阅读的读者人数,则读者出加入到读者等待队列中,写者进入。
                                           if(L->wr>R->readr)/////////////////////
                                        {
                                                    printf("写者等待人数已经大于读者人数,所有读者请暂时离开阅览室,写者进入!\n");
                                                    L->rw=R->readr+L->rw;
                                                    R->readr=0;
                                                    R->rdoor=0;
                                                    W->wdoor=1;
                                                    writer(R,W);
                                        }
                                        //如果只有一名读者在读,而有至少有一名写者在等时,则写者进,读者出。
                                       else  if(R->readr==1 && L->wr>=1)
                                        {
                                                    printf("现在只有您一个读者,请您暂时离开阅览室,写者进入!\n");
                                                    R->readr=0;
                                                    R->rdoor=0;
                                                    W->wdoor=1;
                                                    writer(R,W);
                                        }/////////
                                       else
                                         
                                          printf("对不起,里面现在有%d位读者正在阅览,请您稍等!",R->readr);
                                }
                             }

                             else if(ch==1)
                             {

                                 if(R->rdoor==1)
   
                                        reader(R,W);
                                 else
                                 {
                                        L->rw++;
                                        //printf("读者人数:%d \n",R->readr);
                                        printf("读者等待人数:%d \n",L->rw);
                                        if((10*L->wr)<R->readr)/////////////////////
                                        {
                                                    printf("读者等待人数已经远远大于写者人数,请写者暂时离开阅览室,读者进入!\n");
                                                    L->wr++;
                                                    W->writer=0;
                                                    R->rdoor=1;
                                                    W->wdoor=0;
                                                    reader(R,W);
                                        }
                                       /*else  if(R->readr==1 && L->wr>=1)
                                        {
                                                    printf("现在只有您一个读者,请您暂时离开阅览室,写者进入!\n");
                                                    R->readr=0;
                                                    R->rdoor=0;
                                                    W->wdoor=1;
                                                    writer(R,W);
                                        }*/
                                        if(R->readr==0)
                                        printf("对不起,里面现在有写者正在工作,请您稍等! \n");
                                        else
                                            printf("现在有写者正在等待,您暂时无法进入,请您稍等!\n");
                                       /////////
                                 }
                             }

                             else
                           
                                 printf("输入有误!");


                                 //goto Leep;

                             
                        

                          
                        //printf("\n R->rdoor=%d  \n",R->rdoor);
                         printf("\n如果想结束进入,请在这里输入9,继续请输入其他数字:");

                             scanf("%d",&num2);

                        if(num2==9)
                        {
                            break;
                        }
                        }
                        break;

                case 2:
                 while(1)
                 {
                    if(flag==0)
                    {
                //输入第一个出去者的信息
   
                      printf("不好意思,现在阅览室为空,请选择别的操作:\n");

                       break;
                    }
        
      
   
                   else

                        printf("请输入要离开者的角色(读者:1写着:2):\n");
   

                           scanf("%d",&ch);

                         if(ch==2)
                         {
                                  if(W->writer!=0)

                                     //执行读者操作
                                     lwriter(R,W);
                                  else
                                  {
           
                                     printf("对不起,阅览室已空!\n");break;
                                  }
                         }

                        else if(ch==1)
                        {

                                   if(R->readr!=0)
   
                                     lreader(R,W);
                       else
                       {
            
                               printf("对不起,阅览室已空!\n");break;
                       }
                        }
                    

                      else
                      {
                             printf("输入有误!");
   
                      }
                     printf("\n");

                     printf("如果想结束离开,请在这里输入9:");

                     scanf("%d",&num2);

                      if(num2==9)

                      break;
               }
                 break;
               

                 }
                 
               }
               
   
   
    //default :
        //break;
        
   

}
搜索更多相关主题的帖子: 等待 include 结构体 
2013-05-12 10:41
快速回复:C程序修改成C++
数据加载中...
 
   



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

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