this is my code
all that im lacking is that
when i input a any string it wont get copied and must only be inputed by a int
im having a hard time when it come to string
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
char lookup_table[29]={'\0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char input_stream[100];
/*coded by Juan Carlos E. Suarez */
/*development period february 14-onwards*/
/*in partial compliance of CPE415L*/
struct data{
char JOB_ID;
int BURST_TIME;
int burstime;
int ARR_TIME;
int PROIR_TIME;
int ATT;
int AWT;
int status;
struct data *n;
}*first, *last;
void gotoxy(short x, short y){
COORD pos = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int displayMenu(){
int option;
printf("\nPROCESS SCHEDULER:");
printf("\n1. First Come - First Served");
printf("\n2. Shortest Job Next");
printf("\n3. Priority Scheduling");
printf("\n4. Shortest Remaining Time");
printf("\n5. Round Robin");
printf("\n6. All");
printf("\nSelect option: ");
scanf("%d", &option);
if (option < 7 && option > 0){ return option; }
else { printf("\nPlease choose from 1 to 6."); return 0; }
}
void init(){//creates the linked list
struct data *new, *temp;
new = (struct data *)malloc(sizeof(struct data));
new->JOB_ID = '0';
new->BURST_TIME = 0;
new->burstime = 0;
new->ARR_TIME = 0;
new->PROIR_TIME = 0;
new->ATT = 0; new->AWT = 0;
new->status = 5;
if (first == NULL && last == NULL){
new->n = NULL;
first = last = new;
}else{
temp = last;
temp->n = new;
new->n = NULL;
last = new;
}
}
void insertJob(char jobid, int btime, int atime, int ptime){
struct data *new, *temp;
new = (struct data *)malloc(sizeof(struct data));
new->JOB_ID = jobid;
new->BURST_TIME = btime;
new->burstime = btime;
new->ARR_TIME = atime;
new->PROIR_TIME = ptime;
new->ATT = 0; new->AWT = 0;
new->status = 0;
if (first == NULL && last == NULL){
new->n = NULL;
first = last = new;
}else{
temp = last;
temp->n = new;
new->n = NULL;
last = new;
}
}
void deleteData(){
struct data *temp,*temp1;
temp = first;
if (first->n !=NULL){
temp1= first->n;
first = temp1;
} else {
first = NULL;
free(first);
}
temp->n = NULL;
free(temp);
}
void displayJob(){
struct data *temp;
temp = first;
printf("\n\nJOB_ID\tBURST_TIME\tARR_TIME\tPRIORITY_TIME\n");
if (temp->n==NULL && temp->JOB_ID != '0'){
printf("%c\t%d\t\t%d\t\t%d\n",temp->JOB_ID, temp->BURST_TIME, temp->ARR_TIME, temp->PROIR_TIME);
}
while (temp->n!=NULL){
if (temp->JOB_ID != '0'){
printf("%c\t%d\t\t%d\t\t%d\n",temp->JOB_ID, temp->BURST_TIME, temp->ARR_TIME, temp->PROIR_TIME);
}
temp = temp->n;
if (temp->n==NULL && temp->JOB_ID != '0'){
printf("%c\t%d\t\t%d\t\t%d\n",temp->JOB_ID, temp->BURST_TIME, temp->ARR_TIME, temp->PROIR_TIME);
}
}
}
void displayburstime(){
struct data *temp;
temp = first;
printf("\n\nJOB_ID\tBURST_TIME\tArr\t\tPRIO\tFINISH\tSTART\n");
if (temp->n==NULL && temp->JOB_ID != '0'){
printf("%c\t%d\t\t%d\t\t%d\t%d\t%d\n",temp->JOB_ID, temp->burstime, temp->ARR_TIME, temp->PROIR_TIME, temp->ATT, temp->AWT);
}
while (temp->n!=NULL){
if (/*temp->status==0 &&*/ temp->JOB_ID != '0'){
printf("%c\t%d\t\t%d\t\t%d\t%d\t%d\n",temp->JOB_ID, temp->burstime, temp->ARR_TIME, temp->PROIR_TIME, temp->ATT, temp->AWT);
}
temp = temp->n;
if (temp->n==NULL && temp->JOB_ID != '0'){
printf("%c\t%d\t\t%d\t\t%d\t%d\t%d\n",temp->JOB_ID, temp->burstime, temp->ARR_TIME, temp->PROIR_TIME, temp->ATT, temp->AWT);
}
}
}
void copyburstime(){
struct data *temp;
temp = first;
if (temp->n==NULL && temp->JOB_ID != '0'){
temp->burstime = temp->BURST_TIME;
temp->status = 0;
temp->ATT = 0; temp->AWT = 0;
}
while (temp->n!=NULL){
if (temp->JOB_ID != '0'){
temp->burstime = temp->BURST_TIME;
temp->status = 0;
temp->ATT = 0; temp->AWT = 0;
}
temp = temp->n;
if (temp->n==NULL && temp->JOB_ID != '0'){
temp->burstime = temp->BURST_TIME;
temp->status = 0;
temp->ATT = 0; temp->AWT = 0;
}
}
}
void rrBT(){
struct data *temp;
temp = first;
if (temp->n==NULL && temp->burstime==0){
temp->status = 1;
} else {
temp->status = 0;
}
while (temp->n!=NULL){
if (temp->burstime==0){
temp->status = 1;
} else {
temp->status = 0;
}
temp = temp->n;
if (temp->n==NULL && temp->burstime==0){
temp->status = 1;
} else {
temp->status = 0;
}
}
}
int smallestAT(){
struct data *temp;
temp = first;
int arrtime = 100;
if (temp->n==NULL && temp->status==0){
arrtime = temp->ARR_TIME;
}
while (temp->n!=NULL){
if (temp->status == 0 && temp->ARR_TIME < arrtime){
arrtime = temp->ARR_TIME;
}
temp = temp->n;
if (temp->n==NULL && temp->status==0 && temp->ARR_TIME < arrtime){
arrtime = temp->ARR_TIME;
}
}
return arrtime;
}
struct data * smallestATime(){
struct data *temp, *min;
temp = first;
int arrtime = 100;
if (temp->n==NULL && temp->status==0){
arrtime = temp->ARR_TIME;
min = temp;
}
while (temp->n!=NULL){
if (temp->status == 0 && temp->ARR_TIME < arrtime){
arrtime = temp->ARR_TIME;
min = temp;
break;
}
temp = temp->n;
if (temp->n==NULL && temp->status==0 && temp->ARR_TIME < arrtime){
arrtime = temp->ARR_TIME;
min = temp;
break;
}
}
return min;
}
struct data * smallestBTA(int smallestAT){
struct data *temp, *min;
temp = first;
int mintime = 100;
if (temp->n==NULL && temp->status == 0 && temp->ARR_TIME == smallestAT && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
while (temp->n!=NULL){
if (temp->status == 0 && temp->ARR_TIME == smallestAT && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
temp = temp->n;
if (temp->n==NULL && temp->status == 0 && temp->ARR_TIME == smallestAT && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
}
return min;
}
int smallestPN(){
struct data *temp;
temp = first;
int prior = 100;
if (temp->n==NULL && temp->status==0){
prior = temp->PROIR_TIME;
}
while (temp->n!=NULL){
if (temp->status == 0 && temp->PROIR_TIME < prior){
prior = temp->PROIR_TIME;
}
temp = temp->n;
if (temp->n==NULL && temp->status==0 && temp->PROIR_TIME < prior){
prior = temp->PROIR_TIME;
}
}
return prior;
}
struct data * smallestBTP(int smallestPN){
struct data *temp, *min;
temp = first;
int mintime = 100;
if (temp->n==NULL && temp->status == 0 && temp->PROIR_TIME == smallestPN && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
while (temp->n!=NULL){
if (temp->status == 0 && temp->PROIR_TIME == smallestPN && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
temp = temp->n;
if (temp->n==NULL && temp->status == 0 && temp->PROIR_TIME == smallestPN && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
}
return min;
}
struct data * smallestBTE(int endtime){
struct data *temp, *min;
temp = first;
int mintime = 100;
if (temp->n==NULL && temp->status == 0 && temp->ARR_TIME <= endtime && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
while (temp->n!=NULL){
if (temp->status == 0 && temp->ARR_TIME <= endtime && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
temp = temp->n;
if (temp->n==NULL && temp->status == 0 && temp->ARR_TIME <= endtime && temp->burstime < mintime){
mintime = temp->burstime;
min = temp;
}
}
return min;
}
struct data * srtBT(struct data *hold, int time){
struct data *temp, *min;
temp = first;
if (temp->n==NULL && temp->status == 0 && temp->ARR_TIME == time && temp->burstime < hold->burstime){
min = temp;
} else { min = hold; }
while (temp->n!=NULL){
if (temp->status == 0 && temp->ARR_TIME == time && temp->burstime < hold->burstime){
min = temp;
}
temp = temp->n;
if (temp->n==NULL && temp->status == 0 && temp->ARR_TIME == time && temp->burstime < hold->burstime){
min = temp;
}
}
return min;
}
float resultATT(int jobnum){
struct data *temp;
float result=0;
temp = first;
// printf("\njobnum = %d\n",jobnum);
while (temp->n!=NULL){
// printf("\n%f += %d - %d",result,temp->ATT,temp->ARR_TIME);
result = result + ((temp->ATT) - (temp->ARR_TIME));
// printf("\nresult = %f",result);
temp = temp->n;
if (temp->n==NULL){
// printf("\n%f += %d - %d",result,temp->ATT,temp->ARR_TIME);
result = result + ((temp->ATT) - (temp->ARR_TIME));
// printf("\nresult = %f",result);
}
}
result /= jobnum;
// printf("\nresult return = %f",result);
return result;
}
float resultAWT(int jobnum){
struct data *temp;
float result=0;
temp = first;
// printf("\njobnum = %d\n",jobnum);
while (temp->n!=NULL){
// printf("\n%f += %d - %d",result,temp->AWT,temp->ARR_TIME);
result = result + ((temp->AWT) - (temp->ARR_TIME));
// printf("\nresult = %f",result);
temp = temp->n;
if (temp->n==NULL){
// printf("\n%f += %d - %d",result,temp->AWT,temp->ARR_TIME);
result = result + ((temp->AWT) - (temp->ARR_TIME));
// printf("\nresult = %f",result);
}
}
result /= jobnum;
// printf("\nresult return = %f",result);
return result;
}
void fcfs(int jobnum){
struct data *hold;
int time=-1, res=jobnum;
char chkey = ' ';
hold = smallestATime();
// hold = smallestBTA(smallestAT());
jobnum--;
while ((chkey = ' ') && (jobnum != -1)){
system("cls");
printf("FIRST COME - FIRST SERVE");
time++;
if (hold->burstime==0 && jobnum != -1){
if (jobnum == 0){
hold->status = 1;
hold->ATT = time;
break;
}else{
hold->status = 1;
hold->ATT = time;
hold = smallestATime();
// hold = smallestBTA(smallestAT());
hold->AWT = time;
jobnum--;
hold->burstime--;
}
}else{
hold->burstime--;
}
printf("\nUNIT TIME ELAPSE: %d", time);
displayburstime();
printf("\npress <SPACEBAR> to continue...");
chkey = getch();
}
printf("\nUNIT TIME ELAPSE: %d", time);
printf("\n\nSIMULATION COMPLETE!");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",resultATT(res),resultAWT(res));
printf("\n\npress <SPACEBAR> to continue...");
}
void sjn(int jobnum){
struct data *hold;
int time=-1, res=jobnum;
char chkey = ' ';
hold = smallestBTA(smallestAT());
jobnum--;
while ((chkey = ' ') && (jobnum != -1)){
system("cls");
printf("SHORTEST JOB NEXT");
time++;
if (hold->burstime==0 && jobnum != -1){
if (jobnum == 0){
hold->status = 1;
hold->ATT = time;
break;
}else{
hold->status = 1;
hold->ATT = time;
hold = smallestBTE(time);
hold->AWT = time;
jobnum--;
hold->burstime--;
}
}else{
hold->burstime--;
}
printf("\nUNIT TIME ELAPSE: %d", time);
displayburstime();
printf("\npress <SPACEBAR> to continue...");
chkey = getch();
}
printf("\nUNIT TIME ELAPSE: %d", time);
printf("\n\nSIMULATION COMPLETE!");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",resultATT(res),resultAWT(res));
printf("\n\npress <SPACEBAR> to continue...");
}
void ps(int jobnum){
struct data *hold;
int time=-1, res=jobnum;
char chkey = ' ';
hold = smallestBTP(smallestPN());
jobnum--;
while ((chkey = ' ' )&& (jobnum != -1)){
system("cls");
printf("PRIORITY SCHEDULING");
time++;
if (hold->burstime==0 && jobnum != -1){
if (jobnum == 0){
hold->status = 1;
hold->ATT = time;
break;
}else{
hold->status = 1;
hold->ATT = time;
hold = smallestBTP(smallestPN());
hold->AWT = time;
jobnum--;
hold->burstime--;
}
}else{
hold->burstime--;
}
printf("\nUNIT TIME ELAPSE: %d", time);
displayburstime();
printf("\npress <SPACEBAR> to continue...");
chkey = getch();
}
printf("\nUNIT TIME ELAPSE: %d", time);
printf("\n\nSIMULATION COMPLETE!");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",resultATT(res),resultAWT(res));
printf("\n\npress <SPACEBAR> to continue...");
}
/*
kini lisud kay swapping kung kinsa ang gamay nlng ug time :3
*/
void srt(int jobnum){
struct data *hold;
int time=-1, res=jobnum;
char chkey = ' ', chHold;
hold = smallestBTA(smallestAT());
chHold = hold->JOB_ID;
jobnum--;
while ((chkey = ' ' )&& (jobnum != -1)){
system("cls");
printf("SHORTEST REMAINING TIME");
time++;
if (hold->burstime==0 && jobnum != -1){
if (jobnum == 0){
hold->status = 1;
hold->ATT = time;
break;
}else{
hold->status = 1;
hold->ATT = time;
hold = smallestBTE(time);
chHold = hold->JOB_ID;
hold->AWT = time;
jobnum--;
hold->burstime--;
}
}else{
hold = srtBT(hold,time);
if ( chHold != hold->JOB_ID) {
hold->AWT = time;
chHold = hold->JOB_ID;
} else {
chHold = hold->JOB_ID;
}
hold->burstime--;
}
printf("\nUNIT TIME ELAPSE: %d", time);
displayburstime();
// printf("job num = %d",jobnum);
printf("\npress <SPACEBAR> to continue...");
chkey = getch();
}
printf("\nUNIT TIME ELAPSE: %d", time);
printf("\n\nSIMULATION COMPLETE!");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",resultATT(res),resultAWT(res));
printf("\n\npress <SPACEBAR> to continue...");
}
/*
swapping according to quantum time
*/
int checkflag (int flag, int jobnum){
if (flag==0){
flag = jobnum;
rrBT();
}
return flag;
}
void rr(int jobnum){
struct data *hold;
int time=-1, res=jobnum, QT=0, flag=jobnum,QTl;
char chkey = ' ';
hold = smallestBTA(smallestAT());
do{
system("cls");
printf("\n Enter Quantum time");
}while(!checkinput(&QTl));
// jobnum--;
while ((chkey = ' ') && (jobnum != -1)){
system("cls");
printf("ROUND ROBIN");
time++;
// printf("\n\nprev: QT = %d , hold job = %c, flag = %d, jobnum = %d", QT, hold->JOB_ID,flag,jobnum);
if (hold->burstime==0 && jobnum != -1){
// printf("\nif");
if (jobnum == 0 && flag == 0){
// printf("\nif if");
QT=0;
hold->status = 1;
hold->ATT = time;
break;
}else{
QT=0;
hold->status = 1;
hold->ATT = time;
flag--;
jobnum--;
flag = checkflag (flag,jobnum);
if (jobnum == 0 && flag == 0){
QT=0;
hold->status = 1;
hold->ATT = time;
break;
}
hold = smallestBTA(smallestAT());
hold->AWT = time;
hold->burstime--;
QT++;
}
} else if (QT==QTl){
QT=0;
hold->status = 2;
hold->ATT = time;
flag--;
flag = checkflag (flag,jobnum);
hold = smallestBTA(smallestAT());
hold->AWT = time;
hold->burstime--;
QT++;
}else{
// printf("\nelse");
hold->burstime--;
QT++;
}
printf("\nUNIT TIME ELAPSE: %d", time);
displayburstime();
// printf("\n\nQT = %d , hold job = %c, flag = %d, jobnum = %d", QT, hold->JOB_ID,flag,jobnum);
printf("\npress <SPACEBAR> to continue...");
chkey = getch();
}
printf("\nUNIT TIME ELAPSE: %d", time);
printf("\n\nSIMULATION COMPLETE!");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",resultATT(res),resultAWT(res));
printf("\n\npress <SPACEBAR> to continue...");
}
/*
tanan simulate ! determine which algorithm performs best based in the jobs given
*/
void all(jobnum){
int i, best;
float ATT[5], AWT[5], min=100;
printf("SIMULATING ALL:");
printf("\n\npress <SPACEBAR> to continue...");
fcfs(jobnum);
ATT[0]=resultATT(jobnum);
AWT[0]=resultAWT(jobnum);
getch();
copyburstime();
sjn(jobnum);
ATT[1]=resultATT(jobnum);
AWT[1]=resultAWT(jobnum);
getch();
copyburstime();
ps(jobnum);
ATT[2]=resultATT(jobnum);
AWT[2]=resultAWT(jobnum);
getch();
copyburstime();
srt(jobnum);
ATT[3]=resultATT(jobnum);
AWT[3]=resultAWT(jobnum);
getch();
copyburstime();
rr(jobnum);
ATT[4]=resultATT(jobnum);
AWT[4]=resultAWT(jobnum);
getch();
system("cls");
printf("ALL SIMULATION COMPLETE!");
for (i=0; i<5; i++){
if (AWT[i]<min){
min = AWT[i];
}
}
for (i=0; i<5; i++){
if (AWT[i]==min){
best = i;
switch(best){
case 0:
printf("\n\nFIRST COME - FIRST SERVE PROCESS SCHEDULING ALGORITHM performs best \nbased in the jobs given");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",ATT[best],AWT[best]);
break;
case 1:
printf("\n\nSHORTEST JOB NEXT PROCESS SCHEDULING ALGORITHM performs best \nbased in the jobs given");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",ATT[best],AWT[best]);
break;
case 2:
printf("\n\nPRIORITY SCHEDULING PROCESS SCHEDULING ALGORITHM performs best \nbased in the jobs given");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",ATT[best],AWT[best]);
break;
case 3:
printf("\n\nSHORTEST REMAINING TIME PROCESS SCHEDULING ALGORITHM performs best \nbased in the jobs given");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",ATT[best],AWT[best]);
break;
case 4:
printf("\n\nROUND ROBIN PROCESS SCHEDULING ALGORITHM performs best \nbased in the jobs given");
printf("\n\nAverage turn around time = %.2f\nAverage Waiting Time= %.2f",ATT[best],AWT[best]);
break;
default: break;
}
}
}
printf("\n\npress <SPACEBAR> to continue...");
}
/**---------------------------------------------------------------------------------------MAIN---------------------------------------------------------------------------------------------------**/
int checkinput(int* variable){
int x;
fflush(stdin);
scanf("%s",input_stream);
for(x=0;input_stream[x]!='\0';x++){//determine if the input has a character;
if(!isdigit(input_stream[x])||input_stream[x]==' '||input_stream[x]=='0'){//breaks the loop and returns 0;
return 0;
}
}
if(input_stream[x]=='\0'){
sscanf(input_stream,"%d",variable);
return 1;
}
}
int main() {
fflush(stdin);
int i, job_num, bursttime, arrtime, priortime, option=0,index,l;
char key = 'y';
init();//creates the linked list
while (key == 'y'){
do{
system("cls");
printf("PROCESS SCHEDULING ALGORITHM");
printf("\nINPUT NUMBER OF JOBS: ");
}while(!checkinput(&job_num));
for(index=1;index<=job_num;index++){
system("cls");
fflush(stdin);
printf("this job has been automatically assigned the ID of: ");
putch(lookup_table[index]);
printf("\nEnter burst time: ");
do{}while(!checkinput(&bursttime));
printf("\nEnter arrival time: ");
do{}while(!checkinput(&arrtime));
printf("\nEnter priority: ");
do{}while(!checkinput(&priortime));
insertJob(lookup_table[index],bursttime,arrtime,priortime);
}
system("cls");
displayJob();
while (option == 0){
option = displayMenu();
}
switch (option){
case 1: fcfs(job_num); break;
case 2: sjn(job_num); break;
case 3: ps(job_num); break;
case 4: srt(job_num); break;
case 5: rr(job_num); break;
case 6: all(job_num); break;
default: break;
}
printf("\nAgain? [y]Yes or [n]No: ");
key = getch();
deleteData();
option=0;
init();
for (i=0; i<job_num; i++){
deleteData();
}
}
printf("\n");
system("pause");
return 0;
}