模拟算法
㈠ 时间片轮转算法的模拟
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"iostream.h"
typedef struct node
{
char name[10];
int prio;
int round;
int cputime;
int needtime;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run;
int N;
void firstin()
{run=ready;<br/>run->state='R';<br/>ready=ready->next;<br/>}
void prt1(char a)
{if(toupper(a)=='p')<br/>cout<<" "<<endl;<br/>cout<<"进程名 占用CPU时间 到完成还要的时间 优先级 状态"<<endl;<br/>}
void prt2(char a,PCB *q)
{if(toupper(a)=='P')<br/>cout<<q->name<<" "<<q->cputime<<" "<<q->needtime<<" "<<q->prio<<" "<<q->state<<endl;<br/>}
void prt(char algo)
{
PCB *p;
prt1(algo);
if(run!=NULL)
prt2(algo,run);
p=ready;
while(p!=NULL)
{prt2<br/>(algo,p);<br/>p=p->next;<br/>}
p=finish;
while(p!=NULL)
{prt2(algo,p);<br/>p=p->next;<br/>}
getchar();
}
/*
void insert(PCB *q)
{PCB *p1,*s,*r;<br/>int b;<br/>s=q;<br/>p1=ready;<br/>r=p1;<br/>b=1;<br/>while((p1!=NULL)&&b)<br/>if(p1->prio>=s->prio)<br/>{r=p1;<br/>p1=p1->next;<br/>}
else
b=0;
if(r!=p1)
{r->next=s;<br/>s->next=p1;<br/>}
else
{s->next=p1;<br/>ready=s;<br/>}
}
*/
void insert(PCB *q)
{PCB *p1,*s,*r;<br/>//int b;<br/>s=q;<br/>p1=ready;<br/>r=p1;<br/>//b=1;<br/>while(p1!=NULL)<br/>//if(p1->prio<s->prio)<br/>{r=p1;<br/>p1=p1->next;<br/><br/>}
//else
//b=0;
if(r!=p1)
{r->next=s;<br/>s->next=p1;<br/>}
else
{s->next=p1;<br/>ready=s;<br/>}
}
void create(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
cout<<"输入进程名及其需要运行的时间:"<<endl;
for(i=1;i<=N;i++)
{p=new PCB;<br/>cin>>na;<br/>cin>>time;<br/>strcpy(p->name,na);<br/>p->cputime=0;<br/>p->needtime=time;<br/>p->state='w';<br/>p->prio=i;<br/>if(ready!=NULL)<br/> insert(p);<br/>else<br/>{p->next=ready;<br/>ready=p;<br/>}
cout<<"输入进程名及其需要运行的时间:"<<endl;
}
prt(alg);
run=ready;
ready=ready->next;
run->state='R';
}
㈡ 有c语言模拟调度算法吗
/*本c程序为按优先数的进程调度*/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct pcb)
struct pcb
{ int pid;
struct pcb *next;
int time;
int priority;
char status;
};
int n,m;
struct pcb *head,*runpcb;
main()
{ struct pcb *pcbi;
char str;
struct pcb *cshlist();
void insertproc(struct pcb *insproc);
void pintlist(struct pcb *L);
void execproc();
void delproc();
m=0;
head=cshlist();
printf("first linelist is:\n");
pintlist(head);
while(head!=NULL)
{ scanf("%c",&str);
runpcb=head;
head=runpcb->next;
runpcb->status='Z';
printf("output linelist is:\n");
printf("%d\n",m);
printf("%2d %5d %5d %3c\n",runpcb->pid,runpcb->time,runpcb->priority,runpcb->status);
pintlist(head);
printf("\n");
printf("\n");
execproc();
m=m+1;
if(runpcb->time==0)
delproc();
else insertproc(runpcb);
}
}
void pintlist(struct pcb *L)
{
struct pcb *p;
int i;
p=L;
if(L!=NULL)
do
{ printf("%2d %5d %5d %3c\n",p->pid,p->time,p->priority,p->status);
p=p->next;
}while (p!=NULL);
}
struct pcb *cshlist()
{
struct pcb *ql;
n=0;
ql=(struct pcb *)malloc(LEN);
ql->pid=n+1;
ql->status='R';
printf("enter time and priority:\n");
scanf("%ld,%d",&ql->time,&ql->priority);
head=NULL;
while(ql->time!=0)
{
n=n+1;
insertproc(ql);
ql=(struct pcb *)malloc(LEN);
printf("enter timeand priority:\n");
ql->pid=n+1;
ql->status='R';
}
return(head);
}
void insertproc(struct pcb *insproc)
{
struct pcb *p0,*p1,*p2;
int pri;
p1=head;
p0=insproc;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
pri=p0->priority;
if(p1->priority<=pri)
{
p0->next=head;
head=insproc;
}
else
{
while((p1->next!=NULL)&&(p1->priority>pri))
{ p2=p1;
p1=p1->next;
}
if((p1->next!=NULL)||(p1->priority<=pri))
{
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
}
}
void execproc()
{
runpcb->time=runpcb->time-1;
runpcb->priority=runpcb->priority-1;
}
void delproc()
{
struct pcb *p;
p=runpcb;
p->status='E';
printf("process P");
printf("%d",p->pid);
printf(" is finish\n");
printf("\n");
free(runpcb);
}
优先数调度算法方面和时间片轮转调度算法(再给你个c++的)
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int n;
class PCB
{
public:
int pri;//进程优先数
int runtime;//进程运行CPU时间
int pieceOftime;//轮转时间片
string procname;//进程名
string state;//进程状态
int needOftime;//还需要时间
int Counter;
PCB * next;
};
PCB * run = NULL;
PCB * ready = NULL;
PCB * finish = NULL;
PCB * tial = ready;
void Dtime(int t);
void Prinft(int a)
{
if(a==1)
{
cout<<"进程名称"<<"\t"<<"优先数"<<"\t"<<"还需要时间"<<"\ t"<<"已运行时间"<<"\t"<<"状态:"<<endl;
}
else
cout<<"进程名称"<<"\t"<<"已运行时间"<<"\t"<<"还需要时间"<< "\t"<<"计数器"<<"\t"<<"时间片"<<"\t"<<"状态"<<endl;
}
void Prinft(int b,PCB * p)
{
if(b==1)
{
cout<<p->procname<<"\t\t"<<p->pri<<"\t"<<p->needOftime<<"\t\t"<<p->runtime<<"\t\t"<<p->state<<endl;
}
else
cout<<p->procname<<"\t\t"<<p->runtime<<"\t\t"<<p->needOftime<<"\t\t"<<p->Counter<<"\t"<<p->pieceOftime<<"\t"<<p->state<<endl;
}
void display(int c)
{
PCB *p;
if(run!=NULL) /*如果运行指针不空*/
Prinft(c,run); /*输出当前正在运行的PCB*/
//Dtime(2);
p=ready; /*输出就绪队列PCB*/
while(p!=NULL)
{
Prinft(c,p);
p=p->next;
}
//Dtime(2);
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
Prinft(c,p);
p=p->next;
}
}
void insert(PCB *p)//插入就绪队列按Pri大小
{
PCB *S1,*S2;
if(ready==NULL)
{
p->next = NULL;
ready = p;
}
else
{
S1 = ready;
S2 = S1;
while(S1!=NULL)
{
if(S1->pri >= p->pri)
{
S2 = S1;
S1 = S1->next;
}
else
break;
}
if(S2->pri >= p->pri)
{
S2->next = p;
p->next = S1;
}
else
{
p->next = ready;
ready = p;
}
}
}
bool CTProcessOfPri()
{
PCB * Node;
cout <<"输入创建进程的数目:"<<endl;
cin >>n;
for(int j = 0;j < n; j++)
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout <<"输入进程的名称,进程需CPU时间:"<<endl;
cin >>Node->procname>>Node->needOftime;
Node->runtime = 0;
Node->state ="就绪";
Node->pri =Node->needOftime;
cout <<"进程"<<Node->procname<<"创建完毕!"<<endl;
}
insert(Node);
}
return true;
}
void priority(int i)
{
run = ready;
ready = ready->next;
run->state = "运行";
Prinft(i);
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
{
run->runtime=run->runtime+1;
run->needOftime=run->needOftime-1;
run->pri=run->pri-1; /*每运行一次优先数降低1个单位*/
if(run->needOftime==0) /*如所需时间为0将其插入完成队列*/
{
run->state = "完成";
run->next = finish;
finish = run;
run=NULL; /*运行队列头指针为空*/
if(ready!=NULL) /*如就绪队列不空*/
{
run = ready;
run->state = "运行";
ready = ready->next;
}
}
else if((ready!=NULL)&&(run->pri<ready->pri))
{
run->state="就绪";
insert(run);
run = ready;
run->state = "运行";
ready = ready->next;
}
display(i); /*输出进程PCB信息*/
}
}
void queue(PCB *p)
{
if(ready==NULL)
{
p->next = NULL;
ready = p;
tial = p;
}
else
{
tial->next = p;
tial = p;
p->next = NULL;
}
}
bool CTProcessOfRuntime()
{
PCB * Node;
int m;
cout <<"输入创建进程的数目:"<<endl;
cin >>n;
cout <<"输入时间片:"<<endl;
cin >>m;
for(int j = 0;j < n; j++)
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout <<"输入进程的名称,进程需CPU时间:"<<endl;
cin >>Node->procname>>Node->needOftime;
Node->runtime = 0;
Node->state ="就绪";
Node->Counter = 0;
Node->pieceOftime = m;
cout <<"进程"<<Node->procname<<"创建完毕!"<<endl;
}
queue(Node);
}
return true;
}
void Runtime(int c)
{
run = ready;
ready = ready->next;
run->state = "运行";
Prinft(c);
while(run!=NULL)
{
run->runtime=run->runtime+1;
run->needOftime=run->needOftime-1;
run->Counter = run->Counter + 1;
if(run->needOftime==0)
{
run->state = "完成";
run->next = finish;
finish = run;
run = NULL;
if(ready!=NULL)
{
run = ready;
ready = ready->next;
}
}
else if(run->Counter == run->pieceOftime)
{
run->Counter = 0;
run->state = "就绪";
queue(run);
run=NULL;
if(ready!=NULL)
{
run = ready;
run->state = "运行";
ready = ready->next;
}
}
display(c);
}
}
int main()
{
int i;
cout <<"*******************************************"<<endl;
cout <<"* 1 优先数调度算法 2 循环时间片轮转算法*"<<endl;
cout <<"*************** 0 退出 *******************"<<endl;
cin >>i;
switch(i)
{
case 1:
CTProcessOfPri();
priority(i);
break;
case 2:
CTProcessOfRuntime();
Runtime(i);
break;
default:
break;
}
return 0;
{
time(& current_time);
}while((current_time-start_time)<t);
}
}
void Dtime(int t)
{
time_t current_time;
time_t start_time;
time(&start_time);
do
㈢ 遗传算法、数值算法、爬山算法、模拟退火 各自的优缺点
遗传算法:其优点是能很好地处理约束,跳出局部最优,最终得到全局最优解。缺点是收敛速度慢,局部搜索能力弱,运行时间长,容易受到参数的影响。
模拟退火:具有局部搜索能力强、运行时间短的优点。缺点是全局搜索能力差,容易受到参数的影响。
爬山算法:显然爬山算法简单、效率高,但在处理多约束大规模问题时,往往不能得到较好的解决方案。
数值算法:这个数值算法的含义太宽泛了,指的是哪种数值算法,阵列算法与爬山算法一样,各有优缺点。
(3)模拟算法扩展阅读:
注意事项:
遗传算法的机制比较复杂,在Matlab中已经用工具箱中的命令进行了打包,通过调用可以非常方便的使用遗传算法。
函数GA:[x,Fval,reason]=GA(@fitnessfun,Nvars,options)x为最优解,Fval为最优值,@Fitnessness为目标函数,Nvars为自变量个数,options为其他属性设置。系统的默认值是最小值,所以函数文档中应该加上一个减号。
要设置选项,您需要以下函数:options=GaOptimset('PropertyName1','PropertyValue1','PropertyName2','PropertyName3','PropertyValue3'…)通过该函数,可以确定一些遗传算法的参数。
㈣ 用C语言编程模拟处理机调度(实现一种算法)
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定义进程控制块PCB */
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
void input() /* 建立进程控制块函数*/
{
int i,num;
system("cls"); /*清屏*/
printf("\n 请输入进程数: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先数:");
scanf("%d",&p->super);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='W';
p->link=NULL;
sort(); /* 调用sort函数*/
}
}
int space()
{
int l=0;
PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n 进程名\t 状态\t 优先数\t 需要运行时间\t 已经运行时间\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:\n"); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n **** 当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",p->name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
(p->super)--;
p->state='W';
sort(); /*调用sort函数*/
}
}
void main() /*主函数*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("-----------------------------------------------------");
printf("\n 现在是第%d次运行: \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n 按任意键继续......\n");
}
printf("\n\n 进程已经完成.\n");
}
㈤ 用C语言采用模拟DFA算法编写一个扫描器(词法分析器)
(1)滤掉源程序中的无用成分,如空格;
这个”源程序“是指?不是只要识别像
bbbbaa+1,
aa-1
这样的字符串么?
㈥ 本人初二 , 学习c++一年,学会了基本的语法 。 正在准备自学算法和数据结构 。 请问模拟算法是什么东东。
推荐先看数据结构C++版,严蔚敏的,再看算法导论,你算法就算过关了,消息来源华夏联盟
㈦ 进程调度模拟算法,发到邮箱[email protected]
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定义进程控制块PCB */
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
void input() /* 建立进程控制块函数*/
{
int i,num;
system("cls"); /*清屏*/
printf("\n 请输入进程个数:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先数:");
scanf("%d",&p->super);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='w';
p->link=NULL;
sort(); /* 调用sort函数*/
}
}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf(" \n qname\tstate\tsuper\tndtime\truntime\n");
printf(" %s\t",pr->name);
printf("%c\t",pr->state);
printf("%d\t",pr->super);
printf("%d\t",pr->ntime);
printf("%d\t",pr->rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",p->name);
free(p);
}
void running() /* 建立进程就绪函数,进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
(p->super)--;
p->state='w';
sort(); /*调用sort函数*/
}
}
void main() /*主函数*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("\n The execute number:%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n 按任一键继续......");
ch=getchar();
}
printf("\n\n 进程已经完成.\n");
ch=getchar();
}
㈧ 编程模拟作业调度算法 拜托了···· 下午就要交了··· 求编程高手帮助···
float arry[4][2];
arry[0][0]=8.00;
arry[0][1]=2.00;
arry[1][0]=8.50;
arry[1][1]=0.50;
arry[2][0]=9.00;
arry[2][1]=0.10
arry[3][0]=9.50
arry[3][1]=0..20
//用fcfs算法
int shunxu[4][1];
for(int i =0;i<4;i++)
shunxu[i][0]=i;
for(int i =0;i<4;i++){
for(int j =i;j<4;j++)
{
if(arry[shunxu[j][0]][0]<arry[shunxu[i][0]][0])
{
int temp =shunxu[j][0];
shunxu[j][0]=shunxu[i][1];
shunxu[i][1]=temp;
}
}
}
//顺序是
for(int i =0;i<4;i++)
printf("%d\n",&shunxu[i][0]);
㈨ 进程调度算法模拟程序设计
这么有技术含量的问题,应该要更有技术含量的回答,你给个30分悬赏,高手都不睬你~!