| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 790 人关注过本帖
标题:分析程序 大家来指点
只看楼主 加入收藏
jianglongyun
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-5-30
收藏
 问题点数:0 回复次数:12 
分析程序 大家来指点

这叫什么程序 有错误吗?能够运行吗?



#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX 32767
typedef struct node
{
int address,size;
struct node *next;
}RECT;
RECT *assignment(RECT *head,int application);
void acceptment1(RECT *head,RECT *back1);
void acceptment2(RECT *head,RECT *back1) ;
int backcheck(RECT *head,RECT *back1);
void print(RECT *head);
RECT *head,*back,*assign1,*p;
int application1,maxblocknum;
char way;
/*主函数*/
main()
{
char choose[10];
int check;
head=malloc(sizeof(RECT));
p=malloc(sizeof(RECT));
head->size=MAX;
head->address=0;
head->next=p;
maxblocknum=1;
p->size=MAX;
p->address=0;
p->next=NULL;
print(head);
printf("Enter the way(best or first(b/f)\n");
scanf("%c",&way);
do{
printf("Enter the assign or accept(as/ac)\n");
scanf("%s",choose); /*选择分配或回收*/
if(strcmp(choose,"as")==0)
{
printf("Input application:\n");
scanf("%d",&application1);
assign1=assignment(head,application1);
if(assign1->address==-1)
printf("Too large application!,assign fails!!\n\n");
else
printf("Success!!ADDRESS=%5d\n",assign1->address);
print(head);
}
else
if(strcmp(choose,"ac")==0) /*回收*/
{
back=malloc(sizeof(RECT));
printf("Input Adress and Size!!\n");
scanf("%d%d",&back->address,&back->size);
check=backcheck(head,back);
if(check==1)
{
if(tolower(way)=='f')/*首先适应算法*/
acceptment1(head,back); /*首先适应*/
else
acceptment2(head,back);/*最佳适应*/
print(head);
}
}
}while(!strcmp(choose,"as")||!strcmp(choose,"ac"));
}
/*分配函数*/
RECT *assignment(RECT *head,int application)
{
RECT *after,*before,*assign;
assign=malloc(sizeof(RECT));
assign->size=application;
assign->next=NULL;
if(application>head->size||application<=0)
assign->address=-1; /*申请无效*/
else
{
before=head;
after=head->next;
while(after->size<application)
{
before=before->next;
after=after->next;
}
if(after->size==application) /*结点大小等于申请大小则完全分配*/
{
if(after->size==head->size)
maxblocknum--;
before->next=after->next;
assign->address=after->address;
free(after);
}
else
{
if(after->size==head->size) maxblocknum--;
after->size=after->size-application; /*大于申请空间则截取相应大小分配*/
assign->address=after->address+after->size;
if(tolower(way)=='b')
{
before->next=after->next;
back=after;
acceptment2(head,back);
}
}
if(maxblocknum==0)
{
before=head;
head->size=0;
maxblocknum=1;
while(before!=NULL)
{
if(before->size>head->size)
{
head->size=before->size;
maxblocknum=1;
}
else
if(before->size==head->size)
maxblocknum++;
before=before->next;
}
}
}
assign1=assign;
return assign1;
}
void acceptment1(RECT *head,RECT *back1)/*首先适应*/
{
RECT *before,*after;
int insert;
before=head;
after=head->next;
insert=0;
while(!insert)
{
if((after==NULL)||
((back1->address<=after->address)&&
(back1->address>=before->address)))
{
before->next=back1;
back1->next=after;
insert=1;
}
else
{
before=before->next;
after=after->next;
}
}
if(back1->address==before->address+before->size)
{
before->size=before->size+back1->size;
before->next=back1->next;
free(back1);
back1=before;
}
if(after!=NULL&&(after->address==back1->address+back1->size))
{
back1->size=back1->size+after->size;
back1->next=after->next;
free(after);
}
if(head->size<back1->size)
{
head->size=back1->size;
maxblocknum=1;
}
else
if(head->size==back1->size)
maxblocknum++;
}
/*最佳适应*/
void acceptment2(RECT *head,RECT *back1)
{
RECT *before,*after;
int insert ;
insert=0;
before=head;
after=head->next;
if(head->next==NULL)
{
head->size=back1->size;
head->next=back1;
maxblocknum++;
back1->next=NULL;
}
else
{
while(after!=NULL)
if(back1->address==after->size+after->address)
{
before->next=after->next;
back->size=after->size+back1->size;
free(after);
after=NULL;
}
else
{
after=after->next;
before=before->next;
}
before=head;
after=head->next;
while(after!=NULL)
if(after->address==back1->size+back1->address)
{
back1->size=back1->size+after->size;
before->next=after->next;
free(after);
after=NULL;
}
else
{
before=before->next;
after=after->next;
}
before=head;
after=head->next;
do{
if(after==NULL||(after->size>back1->size))
{
before->next=back1;
back1->next=after;
insert=1;
}
else
{
before=before->next;
after=after->next;
}
}while(!insert);
if(head->size<back1->size)
{
head->size=back1->size;
maxblocknum++;
}
else
if(head->size==back1->size)
maxblocknum++;
}
}

void print(RECT *head)
{
RECT *before,*after;
int index,k;
before=head->next;
index=1;
if(head->next==NULL)
printf("NO part for assignment!!\n");
else
{
printf("*****index*******address********end*********size*****\n");
while(before!=NULL)
{
printf("----------------------------------------------------\n");
printf(" %-13d%-13d%-13d%-13d\n",index,before->address,before->address+before->size-1,before->size);
printf("----------------------------------------------------\n");
index++;
before=before->next;
}
}
}
int backcheck(RECT *head,RECT *back1)
{
RECT *before,*after;
int check=1;
if(back1->address<0||back1->size<0)
check=0;
before=head->next;
while((before!=NULL)&&check)
if(((back1->address<before->address)
&&(back1->address+back1->size>before->address))
||((back1->address>=before->address)
&&(back1->address<before->address+before->size)))
check=0;
else
before=before->next;
if(check==0)
printf("Error input!!\n");
return check;
}

搜索更多相关主题的帖子: head void address include 
2007-06-01 15:19
Arcticanimal
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:341
专家分:20
注 册:2007-3-17
收藏
得分:0 
伙计,要说找茬的话你弄个编译器就OK了,大段大段的代码又没有注释,很少有人会耐心看的

try new catch
2007-06-01 15:47
孤魂居士
Rank: 2
来 自:老A(中国地大)
等 级:论坛游民
威 望:4
帖 子:1142
专家分:18
注 册:2007-5-21
收藏
得分:0 

太长了```````````````

准备用3年做个高级软件工程师 10年也做不成。准备用10年做成高级软件工程师 3年就成了QQ 群 45771086
欢迎版主...欢迎JAVA爱好者...
一起从深夜 到凌晨...
2007-06-01 15:48
jianglongyun
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-5-30
收藏
得分:0 

那谁能帮忙编译一下 贴上来 解决下我的问题

2007-06-01 15:58
jianglongyun
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-5-30
收藏
得分:0 

这个是内存管理的程序吗?是最优适应算法吗?
有错误大家帮忙修改一下

2007-06-01 16:49
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

确实没什么耐心,

建议大家不要总贴大段大段的代码,具体到每个问题点上,大家才好帮助你!


Fight  to win  or  die...
2007-06-01 17:18
jianglongyun
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-5-30
收藏
得分:0 

帮帮吧 我也希望把它做好
无奈中
大家给看看哪有问题,帮忙解决一下!
谢谢各位了!

2007-06-01 17:34
yushui
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:1355
专家分:22
注 册:2006-7-19
收藏
得分:0 

我也没有编译器 现在 呵呵 你下个编译器就好了阿 很快的
我的电脑中毒了 什么都没有了 呵呵 这样看头大阿

[此贴子已经被作者于2007-6-1 18:21:55编辑过]


fighting!from now on!
2007-06-01 18:17
foreverlh
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-4-29
收藏
得分:0 
才疏学浅,看不明白你这是做什么用的.
不过错误给你修改完了
问题就在于vc++环境下类似
head=malloc(sizeof(RECT));
这样写是错误的.
应写成
head=(RECT *)malloc(sizeof(RECT));
这是个定式具体为什么我也说不清
你把程序中所有调用malloc函数的语句在等号后面加个(RECT *)就没问题了.
我再慢慢看看你这代码是干什么的....
没注释累死了.
楼主有时间把代码实现的目的告诉我一下

2007-06-01 20:39
lifujun117
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-11-9
收藏
得分:0 

去看看编程的规范。会提高你的程序可读性的。


2007-06-01 21:55
快速回复:分析程序 大家来指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018883 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved