| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 664 人关注过本帖
标题:[改写]将一下C语言改写成C++,会的请举手……
只看楼主 加入收藏
cenriter
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-12-4
收藏
 问题点数:0 回复次数:6 
[改写]将一下C语言改写成C++,会的请举手……

各位有能力的同仁帮帮忙……
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define MAXPRI 100
#define NIL -1
struct
{
int id;
char status;
int nextwr;
int priority;
}
pcb [3];
struct
{
int value;
int firstwr;
}
sem[2];
char savearea[3][4],addr;
int i,s1,s2,seed, exe=NIL;
init()
{
int j;
for (j=0;j<3;j++)
{
pcb[j].id=j;
pcb[j].status='r';
pcb[j].nextwr=NIL;
printf("\n process%d priority?",j+1);
scanf("%d",&i);
pcb[j].priority=i;
}
sem[0].value=1;
sem[0].firstwr=NIL;
sem[1].value=1;
sem[1].firstwr=NIL;
for(i=1;i<3;i++)
for(j=0;j<4;j++)
savearea[i] [j]='0';
}
float random()
{
int m;
if (seed<0) m=-seed;
else m=seed;
seed=(25173*seed+13849)%65536;
return(m/32767.0);
}
timeint(ad)
char ad;
{
float x;
x=random();
if((x<0.33)&&(exe==0))return(FALSE);
if((x<0.66)&&(exe==1))return(FALSE);
if((x<1.0)&&(exe==2))return(FALSE);
savearea[exe][0]=i;
savearea[exe][1]=ad;
pcb[exe].status='t';
printf("times silce interrupt'\n process%d enter into ready.\n",exe+1);
exe=NIL;
return(TRUE);
}
scheduler()
{
int pd;
if ((pd=find())==NIL && exe==NIL)
return(NIL);
if (pd!=NIL)
{
if (exe==NIL)
{
pcb[pd].status='e';
exe=pd;
printf("process%d is executing.\n",exe+1);
}
else if(pcb[pd].priority<pcb [exe].priority)
{
pcb[exe].status='r';
printf("process%d enter into ready\n",exe+1);
pcb[pd].status='e';
exe=pd;
printf("process%d is executing\n",exe+1);
}
}
i=savearea[exe][0];
addr=savearea[exe][1];
return(exe);
}
find()
{
int j,pd=NIL,w=MAXPRI;
for (j=0;j<3;j++)
if(pcb[j].status=='r')
if(pcb[j].priority<w)
{
w=pcb[j].priority;pd=j;
}
if (pd==NIL)
for(j=0;j<3;j++)
if (pcb[j].status=='t')
if (pcb[j].priority<w)
{
w=pcb[j].priority;pd=j;
}
return(pd);
}
p(se,ad)
int se;
char ad;
{
if(--sem[se].value>=0) return(FALSE);
block(se);
savearea[exe][0]=i;
savearea[exe][1]=ad;
exe=NIL;
return(TRUE);
}
block(se)
int se;
{
int w;
printf("process%d is blocked\n",exe+1);
pcb[exe].status='w';
pcb[exe].nextwr=NIL;
if((w=sem[se].firstwr)==NIL)
sem[se].firstwr=exe;
else
{
while(pcb[w].nextwr!=NIL)
w=pcb[w].nextwr;
pcb[w].nextwr=exe;
}
}
v(se,ad)
int se;
char ad;
{
if(++sem[se].value>0) return(FALSE);
wakeup(se);
savearea[exe][1]=ad;
savearea[exe][0]=i;
return(TRUE);
}
wakeup(se)
int se;
{
int w;
w=sem[se].firstwr;
if(w!=NIL)
{
sem[se].firstwr=pcb[w].nextwr;
pcb[w].status='r';
printf("process%d is waken up\n",w+1);
}
}
process1()
{
if(addr=='a') goto a1;
if(addr=='b') goto b1;
if(addr=='c') goto c1;
if(addr=='d') goto d1;
if(addr=='e') goto e1;
if(addr=='f') goto f1;
for(i=1;i<6;i++)
{
printf("process1 calls P on the semaphore 1\n");
if(p(0,'a')) break;
a1: printf("process1 is executing in the cretical section 1\n");
if(timeint('b')) break;
b1: printf("s1=%d\n",++s1);
printf("process1 calls V on semaphore1 and quit cretical section 1.\n");
if(v(0,'c')) break;
c1:printf("process1 calls P on semaphore1 2.\n");
if (p(1,'d')) break;
d1:printf("process1 is execting creting cretical section 2.\n");
if (timeint('e')) break;
e1:printf("s2=%d\n",++s2);
printf("process1 calls V on semaphore2 and quit cretical section2.\n");
if(v(1,'f')) break;
f1:printf("process1 cycle count=%d\n",i);
}
if(i<6) return;
eexit(0);
}
process2()
{
if(addr=='a') goto a2;
if(addr=='b') goto b2;
if(addr=='c') goto c2;
if(addr=='d') goto d2;
if(addr=='e') goto e2;
if(addr=='f') goto f2;
for(i=1;i<6;i++)
{
printf("process2 calls P on semaphore2\n");
if(p(1,'a')) break;
a2: printf("process2 is executing in the cretical section2.\n");
if(timeint('b')) break;
b2: printf("s2=%d\n",++s2);
printf("process2 calls V on semaphore2 and quit cretical section2.\n");
if(v(1,'c')) break;
c2:printf("process2 calls P on semaphore1.\n");
if (p(0,'d')) break;
d2:printf("process2 is execting creting cretical section1.\n");
if (timeint('e')) break;
e2:printf("s1=%d\n",++s1);
printf("process2 calls V on semaphore1 and quit cretical section1.\n");
if(v(0,'f')) break;
f2:printf("process2 cycle count=%d\n",i);
}
if(i<6) return;
eexit(1);
}
process3()
{
if(addr=='a') goto a3;
if(addr=='b') goto b3;
if(addr=='c') goto c3;
for(i=1;i<6;i++)
{
printf("process3 calls P on semaphore2.\n");
if(p(1,'a')) break;
a3: printf("process3 is executing on its cretical section.\n");
if(timeint('b')) break;
b3: printf("s2=%d\n",++s2);
printf("process3 calls V on semaphore2 and quit cretical section.\n");
if(v(1,'c')) break;
c3:printf("process3 cycle count=%d\n",i);
}
if(i<6) return;
eexit(2);
}
eexit(n)
int n;
{
pcb[n].status='c';
printf("process%d is completed !\n",n+1);
exe=NIL;
}
main()
{
int k;
printf("********** process management ***********\n\n");
init();
printf("s1=%d,s2=%d\n",s1,s2);
printf("process1,process2,process3 are all in ready!\n");
for( ; ; )
if((k=scheduler())!=NIL)
switch(k)
{
case 0: process1();
break;
case 1: process2();
break;
case 2: process3();
break;
default: printf("process identifer error\n");
break;
}
else break;
printf("s1=%d,s2=%d\n",s1,s2);
printf("\n *************** END ****************\n");
}

搜索更多相关主题的帖子: C语言 define int pcb 
2007-06-28 22:10
huozoo
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-6-28
收藏
得分:0 
#include<iostream>
#define TRUE 1
#define FALSE 0
#define MAXPRI 100
#define NIL -1
using namespace std;
struct
{
int id;
char status;
int nextwr;
int priority;
}
pcb [3];
struct
{
int value;
int firstwr;
}
sem[2];
char savearea[3][4],addr;
int i,s1,s2,seed, exe=NIL;
init()
{
int j;
for (j=0;j<3;j++)
{
pcb[j].id=j;
pcb[j].status='r';
pcb[j].nextwr=NIL;
cout<<"\n process"<<j+1<<"priority?";
cin>>i;
pcb[j].priority=i;
}
sem[0].value=1;
sem[0].firstwr=NIL;
sem[1].value=1;
sem[1].firstwr=NIL;
for(i=1;i<3;i++)
for(j=0;j<4;j++)
savearea[i] [j]='0';
}
float random()
{
int m;
if (seed<0) m=-seed;
else m=seed;
seed=(25173*seed+13849)%65536;
return(m/32767.0);
}
timeint(ad)
char ad;
{
float x;
x=random();
if((x<0.33)&&(exe==0))return(FALSE);
if((x<0.66)&&(exe==1))return(FALSE);
if((x<1.0)&&(exe==2))return(FALSE);
savearea[exe][0]=i;
savearea[exe][1]=ad;
pcb[exe].status='t';
cout<<"times silce interrupt'\n process"<<exe+1<<" enter into ready.\n";
exe=NIL;
return(TRUE);
}
scheduler()
{
int pd;
if ((pd=find())==NIL && exe==NIL)
return(NIL);
if (pd!=NIL)
{
if (exe==NIL)
{
pcb[pd].status='e';
exe=pd;
cout<<"process"<<exe+1<<" is executing.\n";
}
else if(pcb[pd].priority<pcb [exe].priority)
{
pcb[exe].status='r';
cout<<"proces"<<sexe+1<<" enter into ready\n";
pcb[pd].status='e';
exe=pd;
cout<<"process"<<exe+1<<" is executing\n";
}
}
i=savearea[exe][0];
addr=savearea[exe][1];
return(exe);
}
find()
{
int j,pd=NIL,w=MAXPRI;
for (j=0;j<3;j++)
if(pcb[j].status=='r')
if(pcb[j].priority<w)
{
w=pcb[j].priority;pd=j;
}
if (pd==NIL)
for(j=0;j<3;j++)
if (pcb[j].status=='t')
if (pcb[j].priority<w)
{
w=pcb[j].priority;pd=j;
}
return(pd);
}
p(se,ad)
int se;
char ad;
{
if(--sem[se].value>=0) return(FALSE);
block(se);
savearea[exe][0]=i;
savearea[exe][1]=ad;
exe=NIL;
return(TRUE);
}
block(se)
int se;
{
int w;
cout<<"process"<<exe+1<<" is blocked\n";
pcb[exe].status='w';
pcb[exe].nextwr=NIL;
if((w=sem[se].firstwr)==NIL)
sem[se].firstwr=exe;
else
{
while(pcb[w].nextwr!=NIL)
w=pcb[w].nextwr;
pcb[w].nextwr=exe;
}
}
v(se,ad)
int se;
char ad;
{
if(++sem[se].value>0) return(FALSE);
wakeup(se);
savearea[exe][1]=ad;
savearea[exe][0]=i;
return(TRUE);
}
wakeup(se)
int se;
{
int w;
w=sem[se].firstwr;
if(w!=NIL)
{
sem[se].firstwr=pcb[w].nextwr;
pcb[w].status='r';
cout<<"process"<<w+1<<" is waken up\n";
}
}
process1()
{
if(addr=='a') goto a1;
if(addr=='b') goto b1;
if(addr=='c') goto c1;
if(addr=='d') goto d1;
if(addr=='e') goto e1;
if(addr=='f') goto f1;
for(i=1;i<6;i++)
{
cout<<"process1 calls P on the semaphore 1\n";
if(p(0,'a')) break;
a1: cout<<"process1 is executing in the cretical section 1\n";
if(timeint('b')) break;
b1: cout<<"s1="<<++s1<<"\n";
cout<<"process1 calls V on semaphore1 and quit cretical section 1.\n";
if(v(0,'c')) break;
c1:cout<<"process1 calls P on semaphore1 2.\n";
if (p(1,'d')) break;
d1:cout<<"process1 is execting creting cretical section 2.\n";
if (timeint('e')) break;
e1:cout<<"s2="<<++s2<<"\n";
cout<<"process1 calls V on semaphore2 and quit cretical section2.\n";
if(v(1,'f')) break;
f1:cout<<"process1 cycle count="<<i<<"\n";
}
if(i<6) return;
eexit(0);
}
process2()
{
if(addr=='a') goto a2;
if(addr=='b') goto b2;
if(addr=='c') goto c2;
if(addr=='d') goto d2;
if(addr=='e') goto e2;
if(addr=='f') goto f2;
for(i=1;i<6;i++)
{
cout<<"process2 calls P on semaphore2\n";
if(p(1,'a')) break;
a2: cout<<"process2 is executing in the cretical section2.\n";
if(timeint('b')) break;
b2: cout<<"s2="<<++s2<<"\n";
cout<<"process2 calls V on semaphore2 and quit cretical section2.\n";
if(v(1,'c')) break;
c2:cout<<"process2 calls P on semaphore1.\n";
if (p(0,'d')) break;
d2:cout<<"process2 is execting creting cretical section1.\n";
if (timeint('e')) break;
e2:cout<<"s1="<<++s1<<"\n";
cout<<"process2 calls V on semaphore1 and quit cretical section1.\n";
if(v(0,'f')) break;
f2:cout<<"process2 cycle count="<<i<<"\n";
}
if(i<6) return;
eexit(1);
}
process3()
{
if(addr=='a') goto a3;
if(addr=='b') goto b3;
if(addr=='c') goto c3;
for(i=1;i<6;i++)
{
cout<<"process3 calls P on semaphore2.\n";
if(p(1,'a')) break;
a3: cout<<"process3 is executing on its cretical section.\n";
if(timeint('b')) break;
b3: cout<<"s2="<<++s2<<"\n";
cout<<"process3 calls V on semaphore2 and quit cretical section.\n";
if(v(1,'c')) break;
c3:cout<<"process3 cycle count="<<i<<"\n";
}
if(i<6) return;
eexit(2);
}
eexit(n)
int n;
{
pcb[n].status='c';
cout<<"process"<<n+1<<" is completed !\n";
exe=NIL;
}
main()
{
int k;
cout<<"********** process management ***********\n\n";
init();
cout<<"s1="<<s1<<",s2="<<s2<<"\n";
cout<<"process1,process2,process3 are all in ready!\n";
for( ; ; )
if((k=scheduler())!=NIL)
switch(k)
{
case 0: process1();
break;
case 1: process2();
break;
case 2: process3();
break;
default: printf("process identifer error\n");
break;
}
else break;
cout<<"s1="<<s1<<",s2="<<s2<<"\n";
cout<<"\n *************** END ****************\n";
}
我好象挺闲的。
2007-06-29 20:18
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 
不就是几个查找替换吗

Viva,espana!
2007-06-29 20:29
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
太多了吧?....

----------
ps. zkkpkk你确实挺闲的

女侠,约吗?
2007-06-29 21:55
huozoo
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-6-28
收藏
得分:0 
最近怎么这么多c换c++的```改了一个就不想改下一个了````
2007-06-30 09:44
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
还不是换汤不换药... 真要改基本得重写了..

女侠,约吗?
2007-06-30 09:51
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 
改include改输出,甚至你想效果好点的话还可以改结构体成类

Viva,espana!
2007-06-30 22:35
快速回复:[改写]将一下C语言改写成C++,会的请举手……
数据加载中...
 
   



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

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