實驗4:獨佔設備的靜態分配模擬
實驗內容:(1)設計設備類表的結構,根據模擬的要求,自己決定必須設置的字段
(2)設計設備分配表的結構,根據模擬的要求,自己決定必須設置的字段
(3)編程實現獨佔設備的靜態模擬程序
copyright (C), 2014-2015, Lighting Studio. Co., Ltd. File name: Author:hxj Version:0.1 Date:2016.11.22 Description: Funcion List: *****************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_SIZE 1024 struct equip_alloc { int num; //設備的絕對號 int rnum; //設備的相對號 int alloc; //是否分配(0/1) int damage; //是否損壞(0/1) char job[MAX_SIZE]; //佔用的作業名 }; struct equip_type { char type[MAX_SIZE]; //設備類 int count; //該設備類的設備總數 int usable_num; //可用設備數 struct equip_alloc p[MAX_SIZE]; //指向設備分配表 struct equip_type *next; }; typedef struct equip_type link; typedef link* Link; void creat(Link *head) //直接賦值,可以根據newnode指向的結構體中設備類個數決定P[i]i取值,這裏圖個最簡了 { Link temp = *head; Link newnode1; newnode1 = (Link)malloc(sizeof(link)); Link newnode2 = (Link)malloc(sizeof(link)); newnode1->count = 1; newnode1->usable_num = 1; strcpy(newnode1->type,"disk"); newnode1->p[0].num = 1001; newnode1->p[0].rnum = 1; newnode1->p[0].alloc = 0; newnode1->p[0].damage = 0; strcpy(newnode1->p[0].job,"leisure"); newnode2->count = 1; newnode2->usable_num = 1; strcpy(newnode2->type,"printer"); newnode2->p[0].num = 1001; newnode2->p[0].rnum = 1; newnode2->p[0].alloc = 0; newnode2->p[0].damage = 0; strcpy(newnode2->p[0].job,"leisure"); printf("\n"); temp->next = newnode1; newnode1->next = newnode2; newnode2->next = NULL; } void init(Link *head) { *head = (Link)malloc(sizeof(struct equip_type)); (*head)->next = NULL; } void alloc(Link *head) { char type[MAX_SIZE]; char job[MAX_SIZE]; Link temp = (*head)->next; printf("input type:"); scanf("%s",type); while(temp != NULL) { if(0 == strcmp(temp->type,type)) { if(0 == temp->usable_num) { printf("no one can be used!\n"); break; } else { printf("please input job:"); scanf("%s",job); temp->usable_num = temp->usable_num - 1; temp->p[0].alloc = temp->p[0].alloc + 1; strcpy(temp->p[0].job,job); printf("alloc success\n"); getchar(); break; } } temp = temp->next; } } void recycle(Link *head) { int i; Link temp = (*head)->next; char type[MAX_SIZE]; int num; printf("please input which type recy:"); scanf("%s",type); while(temp != NULL) { if(0 == strcmp(temp->type,type)) { printf("please input which num:"); scanf("%d",&num); for(i = 0; i < temp->count; i++) { if(temp->p[i].num == num) { strcpy(temp->p[i].job,"leisure"); temp->p[i].alloc = 0; temp->usable_num++; printf("rec success\n"); } } } temp = temp->next; } } void display(Link head) { int i; Link temp = head->next; while(temp != NULL) { printf("\t\toutput equip_type:\n"); printf("\t\ttype\tcount\tusable_num\n"); printf("\t\t%s\t%d\t%d\n",temp->type,temp->count,temp->usable_num); printf("\t\tout put equip_alloc:\n"); printf("\t\tnum\trnum\talloc\tdamage\tjob\n"); for(i = 0; i < temp->count; i++) { printf("\t\t%d\t%d\t%d\t%d\t%s\n",temp->p[i].num,temp->p[i].rnum,temp->p->alloc,temp->p[i].damage,temp->p[i].job); } printf("\n"); temp = temp->next; } } /* 打算加文件操作,由於是兩個結構體,待完成後續更新 void save(Link head) { Link temp = head->next; FILE *fp; int i; if((fp = fopen("/root/OS_TEST/text.txt","w")) == NULL) { printf("open error\n"); exit(0); } while(temp != NULL) { fprintf(fp,"%s %d %d\n",temp->type,temp->count,temp->usable_num); temp = temp->next; } fclose(fp); } */ int main() { system("clear"); int chose; Link head; init(&head); creat(&head); while(1) { printf("\n\t\t my demo_4 \n"); printf("\t\t**********1.exit*************\n"); printf("\t\t**********2.alloc************\n"); printf("\t\t**********3.recycle**********\n"); printf("\t\t**********4.display**********\n"); printf("\t\tplease choose:"); scanf("%d",&chose); printf("\n"); switch(chose) { case 1: { save(head); exit(0); } case 2: { system("clear"); alloc(&head); printf("\n\t\tinput any num exit:"); getchar(); getchar(); break; } case 3: { system("clear"); recycle(&head); printf("\n\t\tinput any num exit:"); getchar(); getchar(); break; } case 4: { system("clear"); display(head); printf("\n\t\tinput any num exit:"); getchar(); break; } default: break; } system("clear"); } return 0; }
大體功能實現
如若程序有不足之處,還請留言