📄 lj.cpp
字号:
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <iostream.h>
#define NULL 0 //定义空地址
#define LEN sizeof(struct file) //定义struct file类型数据的长度
#define RAND_MAX 0x7fff //定义随机数是在0~RAND_MAX之间平均分布的
struct file
{long fsize;
struct file*next;
};
int n; //n为全局变量,随机产生文件的个数,本文件模块中各函数均可使用它
struct file *creat(void) //定义函数。此函数带回一个指向链表头的指针
{
struct file *head;
struct file *p1,*p2; //P1指向新开的文件,P2指向链表中最后一个文件
n=0;
p1=p2=(struct file*)malloc(LEN); //开辟一个长度为LEN的内存区
srand( (unsigned)time( NULL ) );
int a = rand()%10+3;
printf("随机产生文件数为:%d\n",a);
int i=0;
srand( (unsigned)time( NULL ) );
int b = rand();
printf("\n链表头指针指向的文件空间大小是:%d\n", b);
p1->fsize=b;
head=NULL;
srand( (unsigned)time( NULL ) );
for(int t=0;t<a;t++)
{
i=i+1;
n=n+1;
if(n==1)head=p1;
else p2->next=p1; //把P1所指的结点连接在P2所指的结点后面
p2=p1;
p1=(struct file*)malloc(LEN);
int b = rand();
printf("\n第 %d 个文件的空间大小是:%d\n", i,b);
p1->fsize=b;
}
p2->next=NULL;
return(head); //返回链表的头地址
}
void print(struct file *head)
{struct file *p;
printf("\n这 %d 个文件在系统内占用存储空间按顺序是:\n",n);
p=head->next;
if(head!=NULL)
do
{
printf("%ld\n",p->fsize);
p=p->next;
}while(p!=NULL);
printf("最后一个文件占用剩余存储空间!\n");
}
void main()
{
srand( (unsigned)time( NULL ) );
unsigned long int size=rand()*200;
printf("随机产生存储空间大小为:%d\n",size);
struct file *head;
head=creat();
print(head);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -