#2
jklr112008-12-06 12:21
#include"stdio.h"
#include<stdlib.h> #include<conio.h> #define getpch(type)(type*)malloc(sizeof(type)) #define NULL 0 #define contain 160 struct jcb{ char name[10]; int length; int fir; int tail; struct jcb *pre; struct jcb *next; }*head,*now,*spare; typedef struct jcb JCB; del() {char name[10];JCB *p; printf("输入你要删除的进程名:"); scanf("%s",name); p=head; if(strcmp(head->name,name)==0){strcpy(p->name,"space");if(strcmp(p->next->name,"space")==0){p->length=p->length+p->next->length;p->tail=p->next->tail;p->next=p->next->next;}} else { do{p=p->next;}while(strcmp(p->name,name)!=0&&p!=head); if(p==head)printf("没有找到你需要删除的文件,请检查是否输入错误!!"); else { strcpy(p->name,"space"); if(strcmp(p->next->name,"space")==0){p->length=p->length+p->next->length;p->tail=p->next->tail;p->next=p->next->next;} if(strcmp(p->pre->name,"space")==0){p->pre->length=p->pre->length+p->length;p->pre->tail=p->tail;p->pre->next=p->next;} }} getchar(); } restore() {JCB *p,*q;int i=0; q=getpch(JCB);q->pre=NULL; p=head; do {if(strcmp(p->name,"space")==0) i=i+p->length; p->next->fir=p->next->fir-i; p->next->tail=p->next->tail-i; p=p->next; }while(p->next!=head);if(strcmp(p->name,"space")==0)i=i+p->length; if(strcmp(head->name,"space")==0)head=head->next;p=head; do {if(strcmp(p->name,"space")!=0) {p->pre=q;q->next=p;q=p;} p=p->next; }while(p!=head); strcpy(spare->name,"space"); spare->length=i; spare->fir=q->tail+1; spare->tail=q->tail+i; spare->pre=q; spare->next=p; head->pre=spare; } newj() {JCB *p;char a; printf("\n 请输入作业名:(注意:不能用'space'做作业名!)"); now=getpch(JCB); scanf("%s",now->name); printf("\n 请输入作业大小:"); scanf("%d",&now->length); if(now->length>contain)printf("\n 内存空间不足,分配失败!!"); else{ if(spare->fir==1){now->fir=1;now->tail=now->length;now->pre=spare;now->next=spare;head=now;spare->pre=head;spare->next=head;spare->fir=head->length+1;spare->length=contain-head->length;} else {p=head; while((strcmp(p->next->name,"space")!=0||p->next->length<now->length)&&p->next!=head)p=p->next; if(p->next==head) {printf("\n 内存空间不足,分配失败!!是否整理内存?(Y/N)");a=getchar();a=getchar(); if(a=='Y'||a=='y'){restore();printf("\n 内存整理完成!请尝试回主界面重新输入作业!");} else printf("不整理内存!作业将无法分配,分配失败!"); } else{ now->fir=p->tail+1; now->tail=p->tail+now->length; now->pre=p; if(p->next->length>now->length){ p->next->fir=now->tail+1; p->next->length=p->next->length-now->length; now->next=p->next; } else now->next=p->next->next; p->next=now; } }}getchar(); } disp() {JCB *p;int i,j; p=head; clrscr(); printf("内存空间分配情况为:\n"); do{ i=p->length; if(strcmp(p->name,"space")!=0)for(j=0;j<i;j++)printf("■"); else for(j=0;j<i;j++)printf("□"); p=p->next;}while(p!=head); printf("\n各作业以及可用空间详细信息:\n"); printf("作业名\t\t使用空间\t头地址\t\t尾地址"); do{ printf("\n%s\t\t%d\t\t%d\t\t%d",p->name,p->length,p->fir,p->tail); p=p->next;}while(p!=head); getchar(); } void main() {char c='0'; spare=getpch(JCB); strcpy(spare->name,"space"); spare->length=contain; spare->fir=1; spare->tail=contain; while(c!='5'){clrscr(); printf(" ************ 欢迎使用内存分配程序 ************ \n\n"); printf(" ********************************************************** \n"); printf(" * * \n"); printf(" * 1.新建一个作业 * \n\n"); printf(" 2.删除一个作业 \n\n"); printf(" 3.显示当前作业分配情况 \n\n"); printf(" 4.整理当前内存 \n\n"); printf(" * 5.退出程序 * \n"); printf(" * * \n"); printf(" ********************************************************** \n"); printf(" 请选择你要做的操作:"); c=getchar(); switch(c){ case '1':newj();break; case '2':del();break; case '3':disp();break; case '4':restore();break; case '5':break; default :printf("\n输入错误!请重新输入!"); }getchar(); } } |
近期编了一个c程序,想弄成图形界面来显示,不知道怎样能从图形界面输入参数,然后又从那里输出,不知道怎么修改,不会要重写一遍吧,用c++语言。我的程序如下: