最近做 os 家庭作业,sublime Text3 编辑 c++程序,遇到了输入读不完就退出了运行了。
只是用了指针来实现?求大佬指点小弟。
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#define mp make_pair
#define fi first
#define se second
#define llt long long
using namespace std;
char s[10][1100];
const int WORKSIZE=32;
typedef pair<int,int> pii;
pii WorkBlock[WORKSIZE];
int mixnTime;
int startTime=0;
int useSize=0;
struct PBlock{
int id;
int priority;
int time;
int blocksize;
struct PBlock* nxt;
};
struct PBlock* readying;
void add(struct PBlock* tmp){
// 队列为空
if(readying == NULL){
readying = tmp;
return ;
}
//队首优先级小于 tmp
if((readying->priority)<(tmp->priority)){
tmp->nxt = readying;
readying = tmp;
return;
}
struct PBlock* pre=readying;
struct PBlock* suf=pre->nxt;
//cout<<(pre->priority)<<" ";
while(suf!=NULL){
if((suf->priority)<(tmp->priority))
break;
pre = suf;
suf = pre->nxt;
//cout<<(pre->priority)<<" ";
}
tmp->nxt = pre->nxt;
pre->nxt = tmp;
//cout<<pre->priority<<endl;
return ;
}
void Print_Line(){
for(int i=0;i<170-3;++i)
printf("%c","-\n"[i==170-4]);
}
void Print(){
printf("Time: %d\n",startTime );
Print_Line();
printf("| %s |",s[0]);
for(int i=0;i<WORKSIZE;++i)
printf(" %2d |",i);
printf("\n");
Print_Line();
printf("| %s |",s[1]);
for(int i=0;i<WORKSIZE;++i)
if(WorkBlock[i].se>0)printf(" %2d |",WorkBlock[i].fi);
else printf(" %2c |",' ');
printf("\n");
Print_Line();
}
void Running(){
if((readying==NULL)||WORKSIZE-useSize<(readying->blocksize)){
int costTime=mixnTime;
mixnTime=0x3f3f3f3f;
for(int i=0;i<WORKSIZE;++i){
if(WorkBlock[i].se==0) continue;
WorkBlock[i].se-=costTime;
if(WorkBlock[i].se==0) --useSize;
else mixnTime=min(mixnTime,WorkBlock[i].se);
}
//cout<<mixnTime<<endl;
Print();
startTime+=costTime;
return;
}
for(int i=0;i<WORKSIZE;++i){
if((readying->blocksize)==0)break;
if(WorkBlock[i].se>0)continue;
WorkBlock[i].fi=readying->id;
WorkBlock[i].se=readying->time;
++useSize;
--(readying->blocksize);
}
mixnTime=min(mixnTime,readying->time);
struct PBlock* tmp=readying->nxt;
free(readying);
readying=tmp;
}
int main(){
scanf("%s",s[0]);
scanf("%s",s[1]);
//freopen("in.txt","r",stdin);
memset(WorkBlock,0,sizeof WorkBlock);
int BlockSize;
cin>>BlockSize;
for(int i=0;i<BlockSize;++i){
struct PBlock* tmp = (struct PBlock*) malloc(sizeof (struct PBlock*));
tmp->nxt = NULL;
scanf("%d%d%d%d",&(tmp->id),&(tmp->priority),&(tmp->time),&(tmp->blocksize));
//cout<<(tmp->id)<<" "<<(tmp->priority)<<" "<<(tmp->time)<<endl;
add(tmp);
}
//cout<<1<<endl;
while((useSize>0)||(readying!=NULL))
Running();
return 0;
}