Zeno 12 months ago
parent f6d7ce667a
commit 6cfee81994

168
01.cpp

@ -1,168 +0,0 @@
#include<iostream>
#include<vector>
#include<string>
using namespace std;
typedef struct memoryBlock{
string jobName;
int startadress;
int length;
bool state;
}memoryBlock;
vector<memoryBlock> mb;
void init(){
memoryBlock m1,m2,m3,m4,m5,m6;
m1.jobName="作业1";m2.jobName="作业2";m3.jobName="未分配";m4.jobName="作业3";m5.jobName="未分配";m6.jobName="未分配";
m1.state=1;m2.state=1;m3.state=0;m4.state=1;m5.state=0;m6.state=0;
m1.startadress=60;m2.startadress=32;m3.startadress=60;m4.startadress=8;m5.startadress=32;m6.startadress=8;
m1.length=4;m2.length=16;m3.length=4;m4.length=20;m5.length=16;m6.length=20;
mb.push_back(m1);mb.push_back(m2);mb.push_back(m3);mb.push_back(m4);mb.push_back(m5);mb.push_back(m6);
}
void firstfit(){
string name;
cout<<"请输入要分配的作业名称:";
cin>>name;
int size;
cout<<"请输入作业主存量:";
cin>>size;
for(vector<memoryBlock>::iterator it = mb.begin();it!=mb.end();++it){
int s_pos = it->startadress+size;
int last_size = it->length-size;
int f = 0;
if(it->length>size){
f=1;
}
if(it->state==0&&it->length>=size){
it->state=1;
it->length=size;
it->jobName = name;
if(f){
memoryBlock m;
m.jobName="未分配";
m.length=last_size;
m.state=0;
m.startadress = s_pos;
it++;
mb.insert(it,m);
}
break;
}
}
}
void bestfit(){
string name;
cout<<"请输入要分配的作业名称:";
cin>>name;
int size;
cout<<"请输入作业主存量:";
cin>>size;
int min_last=128;
for(vector<memoryBlock>::iterator it = mb.begin();it!=mb.end();++it){
if(it->state==0&&it->length>=size){
int last_size = it->length-size;
if(last_size<min_last){
min_last=last_size;
}
}
}
for(vector<memoryBlock>::iterator it = mb.begin();it!=mb.end();++it){
int s_pos = it->startadress+size;
int last_size = it->length-size;
if(last_size==min_last){
it->state=1;
it->length=size;
it->jobName = name;
if(last_size>0){
memoryBlock m;
m.jobName="未分配";
m.length=last_size;
m.state=0;
m.startadress = s_pos;
it++;
mb.insert(it,m);
}
break;
}
}
}
void recycle(){
cout<<"请输入要回收的作业名称:";
string name;
cin>>name;
vector<memoryBlock>::iterator it_new;
for(vector<memoryBlock>::iterator it = mb.begin();it!=mb.end();++it){
if(it->jobName.compare(name)==0){
it->state=0;
it->jobName="未分配";
it_new = it;
break;
}
}
vector<memoryBlock>::iterator it_pre=--it_new;
it_new++;
vector<memoryBlock>::iterator it_next=++it_new;
it_new--;
if(it_pre->state==1&&it_next->state==0){
it_new->length+=it_next->length;
mb.erase(it_next);
}
else if(it_pre->state==0&&it_next->state==1){
it_pre->length+=it_new->length;
mb.erase(it_new);
}
else if(it_pre->state==0&&it_next->state==0){
it_pre->length+=it_new->length;
it_pre->length+=it_next->length;
mb.erase(it_new);
mb.erase(it_next);
}
}
void distribute(){
cout<<"*********************空闲区表*********************"<<endl;
cout<<"\t起止\t|\t长度\t|\t状态"<<endl;
for(vector<memoryBlock>::iterator it = mb.begin();it!=mb.end();++it){
if(it->state==0){
cout<<"\t"<<it->startadress<<"k\t|\t"<<it->length<<"k\t|\t"<<it->jobName<<endl;
}
}
cout<<"**************************************************"<<endl;
cout<<"*********************已分配表*********************"<<endl;
cout<<"\t起止\t|\t长度\t|\t名称"<<endl;
for(vector<memoryBlock>::iterator it = mb.begin();it!=mb.end();++it){
if(it->state==1){
cout<<"\t"<<it->startadress<<"k\t|\t"<<it->length<<"k\t|\t"<<it->jobName<<endl;
}
}
cout<<"**************************************************"<<endl;
}
int main(){
init();
cout<<"(本次实验分配主存采用最优适应算法)"<<endl<<"请输入x或y以继续实验";
char option1;
cin>>option1;
int option2;
int running = 1;
while(running){
cout<<"选择功能项1-退出2-分配主存3-回收主存4-显示主存)"<<endl<<"请输入选择的功能:";
cin>>option2;
switch (option2){
case 1: running = 1;break;
case 2: {
if(option1=='x'){
bestfit();
}
else if(option1=='y'){
bestfit();
}
break;
}
case 3: {recycle();break;}
case 4: {distribute();break;}
default:{
cout<<"输入有误!请重新选择"<<endl;
break;
}
}
}
return 0;
}

130
4.cpp

@ -1,130 +0,0 @@
#include<stdio.h>
#include<stdlib.h>
#define list_init_size 100
#define listincrement 10
#define ok 1
typedef int elemtype;
typedef int status;
typedef struct
{
elemtype *elem;
int length;
int listsize;
}sqlist;
status initlist(sqlist &l)//构造空线性表
{
l.elem=(elemtype * )malloc(list_init_size*sizeof(elemtype));
l.listsize=list_init_size;
l.length=0;
return ok;
}
status listlength(sqlist l)//得到元素的个数
{
return l.length;
}
int getelem(sqlist l,int i,elemtype &e)//得到数值
{
if(i<1||i>l.length)
{
printf("不合法");
return 0;
}
e=l.elem[i-1];
return ok;
}
status listinsert(sqlist &l,int i,elemtype e)//插入
{
elemtype *p,*q;
q=&(l.elem[i-1]);
for(p=&(l.elem[l.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++l.length;
return ok;
}
void listprint(sqlist l)//输出
{
int i;
for(i=0;i<l.length;i++)
printf("%d ",l.elem[i]);
printf("\n");
}
void mergelist(sqlist la,sqlist lb,sqlist &lc)
{
int i,j,k,ai,bj;
initlist(lc);
i=j=1;
k=0;
la.length=listlength(la);
lb.length=listlength(lb);
while((i<=la.length)&&(j<=lb.length))
{
getelem(la,i,ai);
getelem(lb,j,bj);
if(ai<=bj)
{
listinsert(lc,++k,ai);
++i;
}
else
{
listinsert(lc,++k,bj);
++j;
}
}
while(i<=la.length)
{
getelem(la,i++,ai);
listinsert(lc,++k,ai);
}
while(j<=lb.length)
{
getelem(lb,j++,bj);
listinsert(lc,++k,bj);
}
}
int main()
{
sqlist La,Lb,Lc;
int i,j;
elemtype x;
initlist(La);
printf("请输入La的长度 ");
scanf("%d",&i);
printf("请输入La中的元素");
for(j=1;j<=i;j++)
{
scanf("%d",&x);
listinsert(La,j,x);
}
printf("输出La中的元素 ");
listprint(La);
initlist(Lb);
printf("\n请输入Lb的长度 ");
scanf("%d",&j);
printf("请输入Lb中的元素");
for(i=1;i<=j;i++)
{
scanf("%d",&x);
listinsert(Lb,i,x);
}
printf("输出Lb中的元素 ");
listprint(Lb);
mergelist(La,Lb,Lc);
printf("\n输出合并后的顺序表Lc: ");
listprint(Lc);
return 0;
}

@ -1,18 +0,0 @@
#include<stdio.h>
int factorial(int n)
{
if(n==0)
return 1;
return n*factorial(n-1);
}
int main()
{
int n,x;
printf("请输入你想要求的阶乘的数:");
scanf("%d",&n);
x=factorial(n);
printf("%d的阶乘是%d\n",n,x);
return 0;
}

@ -1,12 +0,0 @@
------------------------------------------------------------------------
这是这个工程的README文件你可以在这里描述你的工程。告诉读者某些对这个
工程一无所知的人)所有他所需要知道的东西。你的说明通常至少应该包含以下几
个部分:
------------------------------------------------------------------------
工程名:
工程的目标:
版本或者日期:
如何启动这个工程:
作者
使用指南:

Binary file not shown.

@ -1,15 +0,0 @@
#BlueJ class context
comment0.target=card
comment0.text=\r\n\ Write\ a\ description\ of\ class\ card\ here.\r\n\ \r\n\ @author\ (your\ name)\ \r\n\ @version\ (a\ version\ number\ or\ a\ date)\r\n
comment1.params=
comment1.target=void\ act()
comment1.text=\r\n\ Act\ -\ do\ whatever\ the\ card\ wants\ to\ do.\ This\ method\ is\ called\ whenever\r\n\ the\ 'Act'\ or\ 'Run'\ button\ gets\ pressed\ in\ the\ environment.\r\n
comment2.params=
comment2.target=int\ getValue()
comment3.params=
comment3.target=boolean\ getFaceup()
comment4.params=
comment4.target=void\ turnFaceDown()
comment5.params=cardValue
comment5.target=card(int)
numComments=6

@ -1,59 +0,0 @@
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class card here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class card extends Actor
{
/**
* Act - do whatever the card wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
if(Greenfoot.mouseClicked(this)){
if(!isFaceUp){
setImage(faceUpImage);
isFaceUp = true;
}
}
}
// 获取这张牌的点数
public int getValue(){
return value;
}
// 获取这张牌是否已翻面
public boolean getFaceup(){
return isFaceUp;
}
// 将牌翻成背面朝上
public void turnFaceDown(){
isFaceUp=false;
setImage(faceDownImage);
}
private int value = -1; //初始点数为-1表示还没有生成确定的扑克牌。一旦生成了一张牌其点数就不为-1
private boolean isFaceUp = false; //isFaceUp=true则牌正面朝上否则背面朝上
private GreenfootImage faceUpImage = null;//faceUpImage表示牌的正面图案文件
private GreenfootImage faceDownImage = null;//faceDownImage表示牌的背面面图案文件
//Card类的构造方法
public card(int cardValue) {//cardValue是构造一张Card对象时传入的牌的点数
value = cardValue;
isFaceUp = false; //所有被构造的牌都是背面朝上的
String fileName = "hearts" + value + ".png"; //根据牌点数匹配的正面图案文件名
//生成牌的正面图像对象
faceUpImage = new GreenfootImage(fileName.toLowerCase());
faceDownImage = new GreenfootImage("blueflip.png");
//生成牌的背面图案对象
setImage(faceDownImage);//让牌背面朝上放在牌桌上
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 440 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 623 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 457 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 505 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 474 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 553 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 510 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 610 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 612 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 679 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 388 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 554 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 411 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 440 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 435 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 478 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 463 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 513 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 525 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 533 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 453 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 630 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 474 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 508 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 494 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 578 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 531 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 606 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 625 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 634 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 583 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 596 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 463 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 511 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 486 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 558 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 520 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 609 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 618 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 608 B

@ -1,46 +0,0 @@
#Greenfoot project file
class.card.image=blueflip.png
class.table.image=porkboard.jpg
dependency1.from=table
dependency1.to=card
dependency1.type=UsesDependency
editor.fx.0.height=0
editor.fx.0.width=0
editor.fx.0.x=0
editor.fx.0.y=0
height=584
package.numDependencies=1
package.numTargets=2
project.charset=UTF-8
publish.hasSource=false
publish.locked=true
publish.longDesc=
publish.shortDesc=
publish.tags=
publish.title=
publish.url=
readme.height=60
readme.name=@README
readme.width=49
readme.x=10
readme.y=10
simulation.speed=50
target1.height=70
target1.name=card
target1.showInterface=false
target1.type=ClassTarget
target1.width=120
target1.x=0
target1.y=0
target2.height=70
target2.name=table
target2.showInterface=false
target2.type=ClassTarget
target2.width=120
target2.x=0
target2.y=0
version=3.0.0
width=828
world.lastInstantiated=table
xPosition=271
yPosition=119

Binary file not shown.

@ -1,9 +0,0 @@
#BlueJ class context
comment0.target=table
comment0.text=\r\n\ Write\ a\ description\ of\ class\ table\ here.\r\n\ \r\n\ @author\ (your\ name)\ \r\n\ @version\ (a\ version\ number\ or\ a\ date)\r\n
comment1.params=
comment1.target=table()
comment1.text=\r\n\ Constructor\ for\ objects\ of\ class\ table.\r\n\ \r\n
comment2.params=
comment2.target=void\ act()
numComments=3

@ -1,68 +0,0 @@
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class table here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class table extends World
{
ArrayList<card> cards=new ArrayList<card>();
/**
* Constructor for objects of class table.
*
*/
public table()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
for (int i=1; i<=5; i++) { //向集合cards中添加两组共10张5点以下的牌
cards.add(new card(i));
cards.add(new card(i));
}
Collections.shuffle(cards); //集合类Collections的混排算法用于打乱集合cards中牌的顺序
int x=100, y=100; //牌桌上摆放牌的起点坐标
for (int i=0; i<5; i++) { //用for循环依次在牌桌上摆放每排5张共两排的扑克牌
addObject(cards.get(i) , x, y);
addObject(cards.get(i+5) , x, y + cards.get(i).getImage().getHeight() + 20);
x += cards.get(i).getImage().getWidth()+20;
}
}
public void act(){
card card1 = null, card2 = null;
int count = 0;
int card1Value = 0, card2Value = 0;
for(int i = 0; i < cards.size(); i++){
if(cards.get(i).getFaceup() == true){
count++;
if(count == 1){
card1 = cards.get(i);
card1Value = card1.getValue();
}
if(count == 2){
card2 = cards.get(i);
card2Value = card2.getValue();
if(card1Value == card2Value)
{
Greenfoot.delay(30);
removeObject(card1);
removeObject(card2);
cards.remove(card1);
cards.remove(card2);
}
else{
Greenfoot.delay(30);
card1.turnFaceDown();
card2.turnFaceDown();
}
}
}
}
}
}

@ -1,18 +0,0 @@
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n;
printf("请输入一个非负整数:");
scanf("%d", &n);
printf("%d! = %d\n", n, factorial(n));
return 0;
}

@ -1,46 +0,0 @@
#Greenfoot project file
class.card.image=blueflip.png
class.table.image=porkboard.jpg
dependency1.from=table
dependency1.to=card
dependency1.type=UsesDependency
editor.fx.0.height=0
editor.fx.0.width=0
editor.fx.0.x=0
editor.fx.0.y=0
height=584
package.numDependencies=1
package.numTargets=2
project.charset=UTF-8
publish.hasSource=false
publish.locked=true
publish.longDesc=
publish.shortDesc=
publish.tags=
publish.title=
publish.url=
readme.height=60
readme.name=@README
readme.width=49
readme.x=10
readme.y=10
simulation.speed=50
target1.height=70
target1.name=card
target1.showInterface=false
target1.type=ClassTarget
target1.width=120
target1.x=0
target1.y=0
target2.height=70
target2.name=table
target2.showInterface=false
target2.type=ClassTarget
target2.width=120
target2.x=0
target2.y=0
version=3.0.0
width=828
world.lastInstantiated=table
xPosition=271
yPosition=119

Binary file not shown.

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save