clock程序-c语言实现


#include"math.h"
#include"dos.h"
#include"stdio.h"
#include"graphics.h"
main()
{
char s[30];
int gdriver,gmode;
int cosh,sinh,cosm,sinm,coss,sins;
struct time t;
char keydown=0;
int x=300,y=160,r=40;
clrscr();
gdriver=9; gmode=1;
initgraph(&gdriver,&gmode,"a://");/*需要说明的是,第三个参数a://是egavga.bgi这个文件的路径*/

/* install the graphic device.the third parameter is the path of the driver*/

setbkcolor(0);
setcolor(WHITE);
while(1)
{

circle(x,y,r); /*paint the circle*/

line(x,y+r-10,x,y+r-12);
line(x+r-4,y,x+r,y);
line(x-r,y,x-r+4,y);
line(x,y-r+10,x,y-r+10+2); /* draw the fout scales */

gettime(&t);
sprintf(s,"The current time is %2d:%02d:%02d/n",t.ti_hour,t.ti_min,t.ti_sec,t);
outtextxy(0,0,s); /* out put the current time */
outtextxy(0,10,"This clock is written by lijun"); /* show the auther */
coss=(int)((r-10)*cos(t.ti_sec*3.14f/30-3.14f/2)+x);
sins=(int)((r-10)*sin(t.ti_sec*3.14f/30-3.14f/2)+y);
cosm=(int)((r-19)*cos(t.ti_min*3.14f/30-3.14f/2)+x);
sinm=(int)((r-19)*sin(t.ti_min*3.14f/30-3.14f/2)+y);
cosh=(int)((r-28)*cos((t.ti_hour+(float)(t.ti_min)/60)*3.14f/6-3.14f/2)+x);
sinh=(int)((r-28)*sin((t.ti_hour+(float)(t.ti_min)/60)*3.14f/6-3.14f/2)+y);

/* calculate the position of the three points */
setcolor(14);
line(x,y,coss,sins);
setcolor(13);
line(x,y,cosm,sinm);
setcolor(10);
line(x,y,cosh,sinh);
setcolor(15);

/* draw the points */

sleep(1);
clrscr();
keydown=kbhit(); /* check whether key down */
if(keydown)
{
closegraph(); /* close graphic device */
exit(0);
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
页面置换算法 CLOCK (Second Chance) 算法是一种改进的先进先出 (FIFO) 算法,通过给每个页面设置一个“访问位”(也称“使用位”或“R(Referenced)位”)来实现。当操作系统需要将一个新页面载入内存时,它会检查这个页面的访问位。如果为 0,那么就直接使用该页面。如果为 1,则将该页面的访问位设置为 0,表示该页面已经被访问过一次,然后再查找下一个页面。这个过程一直持续到找到一个访问位为 0 的页面为止。 以下是使用 C 语言实现 CLOCK 算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_FRAMES 10 int frames[MAX_FRAMES]; int ref_bits[MAX_FRAMES]; int pointer = 0; int search(int page) { int i; for (i = 0; i < MAX_FRAMES; i++) { if (frames[i] == page) { return 1; } } return 0; } int replace(int page) { while (ref_bits[pointer] == 1) { ref_bits[pointer] = 0; pointer = (pointer + 1) % MAX_FRAMES; } frames[pointer] = page; ref_bits[pointer] = 1; pointer = (pointer + 1) % MAX_FRAMES; } void display() { int i; for (i = 0; i < MAX_FRAMES; i++) { if (frames[i] == -1) { printf("-- "); } else { printf("%2d ", frames[i]); } } printf("\t"); } int main() { int pages[20] = {0, 1, 2, 3, 2, 4, 5, 3, 4, 1, 6, 3, 7, 8, 7, 8, 9, 7, 8, 9}; int page_count = 20; int frame_count = 3; int i, j, k; for (i = 0; i < MAX_FRAMES; i++) { frames[i] = -1; ref_bits[i] = 0; } printf("Replacement Policy = CLOCK\n"); printf("-------------------------------------\n"); printf("Page Content of Frames\n"); printf("---- -----------------\n"); int faults = 0; for (i = 0; i < page_count; i++) { printf("%2d ", pages[i]); if (search(pages[i]) == 1) { display(); printf("Hit\n"); } else { faults++; replace(pages[i]); display(); printf("Fault\n"); } } printf("-------------------------------------\n"); printf("Number of page faults = %d\n", faults); return 0; } ``` 在这个示例中,我们模拟了一个有 20 个页面的内存,并且使用了 3 个物理页框。程序运行后,输出每一次页面访问的情况,以及当前内存中页面的状态(如果页面在内存中则输出页面编号,否则输出“--”表示该页框未被占用)。 要注意的是,在实现 CLOCK 算法时,我们需要维护一个指针来记录当前扫描到的页框位置,并且需要使用一个数组来记录每个页面的访问位。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值