置换算法

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
{  
 void Inputsource(char *sourcefile);
 void Outputdoc(char *docfile);
 void encode(char *sourcefile,char *codefile);
 void decode(char *codefile,char *docfile);

 char *sourcefile="source_doc.dat";
 char *codefile="code.dat";
 char *docfile="doc.dat";
 Inputsource(sourcefile);
 encode(sourcefile,codefile);
 decode(codefile,docfile);
 Outputdoc(sourcefile);
 Outputdoc(codefile);
 Outputdoc(docfile);
}
void Inputsource(char *sourcefile)
{
 FILE *fpw_source;
 char ch;
 if((fpw_source=fopen(sourcefile,"w"))==NULL)
 {
  printf("Open source file Failure!!/n");
  exit(0);
 }
 printf("In put your source doc ,and '!' for end :/n");
 while((ch=getchar())!='!')
 {
  fputc(ch,fpw_source);
 }
 fclose(fpw_source);
}
void Outputdoc(char *docfile)
{
 FILE *fpr_doc;
 char ch;
 if((fpr_doc=fopen(docfile,"r"))==NULL)
 {
  printf("Open docfile file Failure!!/n");
  exit(0);
 }
 printf("The text of %s is :/n",docfile);
 while((ch=fgetc(fpr_doc))!=EOF)
 {
  putchar(ch);
 }
 fclose(fpr_doc);
}
char encode_func(char ch)
{
 short int chint=ch;
 short int chintz='z';
 short int chintZ='Z';
 if(isalpha(chint))
 {
  if( ch>='a' )
   chint=(chint-'a'+5)%26+'a';
  else
   chint=(chint-'A'+5)%26+'A';
 }
 return char(chint);
}
char decode_func(char ch)
{
 short int chint =ch;
 short int chintz='a';
 short int chintZ='A';
 if(isalpha(chint))
 {
  if( ch<='Z' )
   chint=(chint-'A'+21)%26+'A';
  else
   chint=(chint-'a'+21)%26+'a';
 }
 return char(chint);
}
void encode(char *sourcefile,char *codefile)
{
 FILE *fpr_source,*fpw_code;
 char ch;
 if((fpr_source=fopen(sourcefile,"r"))==NULL)
 {
  printf("Open source file Failure!!/n");
  exit(0);
 }
 if((fpw_code=fopen(codefile,"w"))==NULL)
 {
  printf("Open code file Failure!!/n");
  exit(0);
 }
 while((ch=fgetc(fpr_source))!=EOF)
 {
  ch=encode_func(ch);
  fputc(ch,fpw_code);
 }
 fclose(fpr_source);
 fclose(fpw_code);
}
void decode(char *codefile,char *docfile)
{
 FILE *fpw_doc,*fpr_code;
 char ch;
 if((fpw_doc=fopen(docfile,"w"))==NULL)
 {
  printf("Open doc file Failure!!/n");
  exit(0);
 }
 if((fpr_code=fopen(codefile,"r"))==NULL)
 {
  printf("Open code file Failure!!/n");
  exit(0);
 }
 while((ch=fgetc(fpr_code))!=EOF)
 {
  ch=decode_func(ch);
  fputc(ch,fpw_doc);
 }
 fclose(fpw_doc);
 fclose(fpr_code);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
页面置换算法是操作系统中用于管理内存的一种重要技术,它决定了在物理内存不够用时,将哪些页面从内存中调出,以便为新的页面腾出空间。常见的页面置换算法有以下几种: 1. 最佳适应算法(Optimal Algorithm):该算法是一种最理想的页面置换算法,它总是选择未来最长时间内不再被引用的页面进行置换。然而,由于无法预测未来的页面访问情况,实际中无法实现。 2. 先进先出算法(FIFO Algorithm):该算法是最简单的页面置换算法,它总是选择最早进入内存的页面进行置换。缺点是存在"Belady异常",即内存分配增加时,缺页中断反而会增加。 3. 最近最久未使用算法(LRU Algorithm):该算法是一种基于"局部性原理"的页面置换算法,它总是选择最近最久未使用的页面进行置换。这种算法较好地利用了程序的局部性特征,但实现较为复杂。 4. 时钟置换算法(Clock Algorithm):该算法基于环形链表实现,每个页面对应一个标志位。当页面被访问时,标志位置1;当需要置换页面时,从当前位置开始,找到第一个标志位为0的页面进行置换。如果找不到标志位为0的页面,则将所有标志位置为0,并再次进行搜索。 5. 最不常用算法(LFU Algorithm):该算法根据页面被访问的频率进行置换,总是选择使用次数最少的页面进行置换。这种算法适用于频繁访问某些页面的场景。 以上是常见的页面置换算法,每种算法都有其优缺点,选择哪种算法取决于具体应用场景和需求。在Java中,你可以根据需要实现这些算法,并应用于你的项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值