| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 986 人关注过本帖
标题:C程序修改成C++
只看楼主 加入收藏
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:10 
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
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:20 
就这样了:
#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=new rwait;

static struct Reader1 * R=new Reader1;

struct Writer1 * W=new Writer1;




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


    R->readr++;
    cout<<endl;
    cout<<"现在阅览室读者人数为:"<<R->readr;
    cout<<endl;
}
//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--;
    cout<<endl;
    cout<<"现在阅览室读者人数为:"<<R->readr;
   cout<<endl;
    cout<<"是否继续离开(是:1/否:0):";
    cin>>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++;
    cout<<endl;
    cout<<"现在阅览室写者人数为%d!"<<W->writer;
   cout<<endl;
    }
    else
    {
        cout<<"阅览室已有读者在工作,请您稍等!";
    }
}
//写者出操作
void  lwriter(struct Reader1 *R,struct Writer1 *W)
{
    int p;
while(1)
    {
       if(W->writer>0)
       {

       R->rdoor=0;

       W->writer--;

      cout<<endl;
       cout<<"现在阅览室写者人数为:"<<W->writer;

       cout<<endl;

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

      cin>>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)
{
              cout<<"   1.进入"<<endl;

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

              cout<<"   0.退出"<<endl;

              cout<<"请选择:"<<endl;

              cin>>num;
            switch(num)

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

                               flag++;
                          }

                         else
                              cout<<"进入前请输入你的角色(读者:1 写着:2):"<<endl;
   
                        
                              cin>>ch;
                              //以写者的身份申请进入
                             if(ch==2)
                             {
                                 //如果写者之门打开,则进入
                                 if(W->wdoor==1)

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

                                {
                                          L->wr++;
                                          cout<<"写者等待人数:"<<L->wr<<endl;

                                          cout<<"读者人数:"<<R->readr<<endl;
                                          R->rdoor=0;     //关闭读者进入之门
                                          cout<<"读者进入之门已关闭!"<<endl;

                                          //如果写者等待人数大于正在阅读的读者人数,则读者出加入到读者等待队列中,写者进入。
                                           if(L->wr>R->readr)/////////////////////
                                        {
                                                    cout<<"写者等待人数已经大于读者人数,所有读者请暂时离开阅览室,写者进入!"<<endl;
                                                    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)
                                        {
                                                    cout<<"现在只有您一个读者,请您暂时离开阅览室,写者进入!"<<endl;
                                                    R->readr=0;
                                                    R->rdoor=0;
                                                    W->wdoor=1;
                                                    writer(R,W);
                                        }/////////
                                       else
                                         
                                          cout<<"对不起,里面现在有<<endlR->readr<<位读者正在阅览,请您稍等!";
                                }
                             }

                             else if(ch==1)
                             {

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

                             else
                           
                                 cout<<"输入有误!";


                                 //goto Leep;

                             
                        

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

                             cin>>num2;

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

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

                       break;
                    }
        
      
   
                   else

                        cout<<"请输入要离开者的角色(读者:1写着:2):"<<endl;
   

                           cin>>ch;

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

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

                        else if(ch==1)
                        {

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

                      else
                      {
                             cout<<"输入有误!";
   
                      }
                   cout<<endl;

                     cout<<"如果想结束离开,请在这里输入9:";

                     cin>>num2;

                      if(num2==9)

                      break;
               }
                 break;
               

                 }
                 
               }
               
   
   

}

Maybe
2013-05-12 11:14
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:20 
#include<iostream.h>

带h的头文件不是都抛弃了么
2013-05-12 12:14
风雨123
Rank: 2
等 级:论坛游民
帖 子:84
专家分:65
注 册:2013-2-23
收藏
得分:20 
using namespace std;
2013-05-12 12:18
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:20 
这样轻易就能改成C++,都不需要什么面向对象的概念啦,C跟C++不是两个语言了。

授人以渔,不授人以鱼。
2013-05-12 12:31
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 
感觉没用到类的话,只用结构体c和c++没什么太大却别,还望你给指点指点

Maybe
2013-05-12 13:11
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
在C++中,结构和类几乎没有区别。他整个程序都是用面向过程的思维写的,设计的数据结构和函数规划也是面向过程的,根本不是改写的问题,而是需要重新设计。仅仅使用语法是不行的。我见到很多人总是说诸如cout <<之类就是C++云云,那是完全不懂C和C++区别何在的说法。

授人以渔,不授人以鱼。
2013-05-12 13:25
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
以下是引用TonyDeng在2013-5-12 13:25:51的发言:

在C++中,结构和类几乎没有区别。他整个程序都是用面向过程的思维写的,设计的数据结构和函数规划也是面向过程的,根本不是改写的问题,而是需要重新设计。仅仅使用语法是不行的。我见到很多人总是说诸如cout <<之类就是C++云云,那是完全不懂C和C++区别何在的说法。

+65535
2013-05-12 13:28
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
其实我说过很多次,一开始最好就从C++开始,在学C++的时候顺便就可以把C学了,不要先学C固化了面向过程的思维再回头学面向对象的C++,那样排斥性很强的。一般C++的教材,都会特别针对与C的异同进行论述,讲解C++为什么会进行这些改变和设计,知道了异,就等于知道了同样的东西C怎么写、C++怎么写、怎么写更好,比单纯学完C再学C++好得多。

授人以渔,不授人以鱼。
2013-05-12 13:56
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 
我也知道他是用面向过程写的,但是就你说的重新设计我就没有做,呵呵,谢谢指导

Maybe
2013-05-12 17:01
快速回复:C程序修改成C++
数据加载中...
 
   



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

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