⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attemperdlg.cpp

📁 此程序可以解决作业调度问题,几种常用的作业调度算法都有.
💻 CPP
字号:
// AttemperDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Attemper.h"
#include "AttemperDlg.h"
#include"math.h"
#include"time.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
TASK task[5];
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAttemperDlg dialog

CAttemperDlg::CAttemperDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAttemperDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAttemperDlg)
	m_time1 = 0;
	m_time2 = 0;
	m_time3 = 0;
	m_time4 = 0;
	m_time5 = 0;
	m_space2 = 0;
	m_space3 = 0;
	m_space4 = 0;
	m_space5 = 0;
	m_space1 = 0;
	m_fcfs = _T("");
	m_fcfstotaltime = 0;
	m_sjf = _T("");
	m_sjftotaltime = 0;
	m_smjf = _T("");
	m_smjftotaltime = 0;
	m_refer1 = COleDateTime::GetCurrentTime();
	m_refer3 = COleDateTime::GetCurrentTime();
	m_refer2 = COleDateTime::GetCurrentTime();
	m_refer4 = COleDateTime::GetCurrentTime();
	m_refer5 = COleDateTime::GetCurrentTime();
	m_default = false;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAttemperDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAttemperDlg)
	DDX_Control(pDX, IDC_START, m_start);
	DDX_Text(pDX, IDC_TIME1, m_time1);
	DDX_Text(pDX, IDC_TIME2, m_time2);
	DDX_Text(pDX, IDC_TIME3, m_time3);
	DDX_Text(pDX, IDC_TIME4, m_time4);
	DDX_Text(pDX, IDC_TIME5, m_time5);
	DDX_Text(pDX, IDC_SPACE2, m_space2);
	DDX_Text(pDX, IDC_SPACE3, m_space3);
	DDX_Text(pDX, IDC_SPACE4, m_space4);
	DDX_Text(pDX, IDC_SPACE5, m_space5);
	DDX_Text(pDX, IDC_SAPCE1, m_space1);
	DDX_Text(pDX, IDC_FCFS, m_fcfs);
	DDX_Text(pDX, IDC_FCFSTOTALTIME, m_fcfstotaltime);
	DDX_Text(pDX, IDC_SJF, m_sjf);
	DDX_Text(pDX, IDC_SJFTOTALTIME, m_sjftotaltime);
	DDX_Text(pDX, IDC_SMJF, m_smjf);
	DDX_Text(pDX, IDC_SMJFTOTALTIME3, m_smjftotaltime);
	DDX_Text(pDX, IDC_REFER1, m_refer1);
	DDX_Text(pDX, IDC_REFER3, m_refer3);
	DDX_Text(pDX, IDC_REFER2, m_refer2);
	DDX_Text(pDX, IDC_REFER4, m_refer4);
	DDX_Text(pDX, IDC_REFER5, m_refer5);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAttemperDlg, CDialog)
//{{AFX_MSG_MAP(CAttemperDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_CLICKOK, OnClickok)
ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_REFERATONCE, OnReferatonce)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAttemperDlg message handlers

BOOL CAttemperDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here	
	
  
    Init();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAttemperDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAttemperDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAttemperDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAttemperDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CAttemperDlg::OnClickok() 
{
	// TODO: Add your control notification handler code here
	TRACE("CEx06aDialog::OnClickedOk\n");	
	CDialog::OnOK();
}

void CAttemperDlg::OnStart() 
{
	// TODO: Add your control notification handler code here

    
   Init();
   //SetTime();
   AddTime(m_refer1,100);
  
	Fcfs(task,5);
	Sjf(task,5);
	Smjf(task,5);
	m_sjftotaltime = 0;
		m_fcfstotaltime = 0;
			m_smjftotaltime = 0;



	::SetDlgItemText(this->m_hWnd,IDC_START,"再来一次");
	
}

int CAttemperDlg::TimeToSpace(int time)
{
	return (time*(rand()%10+1));
}

void CAttemperDlg::Fcfs(TASK task[], int n)
{    
		TASK tasktemp[5];
	TASK temp;
	for(int j=0;j<n;j++)
		tasktemp[j] = task[j];
	
	for (int i=0;i<n-1;i++) 
	{ 
		for (int d=i+1;d<n;d++) 
			if(tasktemp[i].refer.GetHour()>tasktemp[d].refer.GetHour()) //若作业i后于作业d到达,此处比较其小时部分
			{
				temp=tasktemp[i]; 
				tasktemp[i]=tasktemp[d]; 
				tasktemp[d]=temp; 
			} 
			else if((tasktemp[i].refer.GetHour()==tasktemp[d].refer.GetHour())//若小时部分相同,而分钟部分不同.
				&&tasktemp[i].refer.GetMinute()>tasktemp[d].refer.GetMinute())
			{
				temp=tasktemp[i]; 
				tasktemp[i]=tasktemp[d]; 
				tasktemp[d]=temp; 
			} else if(((tasktemp[i].refer.GetHour()==tasktemp[d].refer.GetHour()))
				&&(tasktemp[i].refer.GetMinute()==tasktemp[d].refer.GetMinute())
				&&(tasktemp[i].refer.GetSecond()>tasktemp[d].refer.GetSecond()))
			{
				temp=tasktemp[i]; 
				tasktemp[i]=tasktemp[d]; 
				tasktemp[d]=temp; 
			}
	
	} 

	CString strtemp;
	strtemp.Format("%d",tasktemp[0].num);
	m_fcfs = "作业"+strtemp;
	for(int r=1;r<=n-1;r++)
	{
		strtemp.Format("%d",tasktemp[r].num);
		m_fcfs+="->作业"+strtemp;
	}

	
	
	for(int s = 0;s<n-1;s++ )

	{if(AddTime(tasktemp[s].refer,tasktemp[i].time)<=tasktemp[s+1].refer)
		continue;
	else if 
		(tasktemp[s+1].refer>AddTime(tasktemp[s].refer,tasktemp[s].time))
	        m_fcfstotaltime = (int)CountSecond(AddTime(tasktemp[s].refer,tasktemp[s].time-tasktemp[s+1].refer));
	}
	if(m_default)
	{
	for(int t = 0;t<n-1;t++ )
		m_fcfstotaltime += (n-t-1)*task[t].time;
	}
		ArrayToV();
	UpdateData(false); 
	
	
}

void CAttemperDlg::Sjf(TASK task[], int n)
{
		TASK tasktemp[5];
	TASK temp;
	for(int j=0;j<n;j++)
		tasktemp[j] = task[j];
	
	for (int i=0;i<n-1;i++) 
	{ 
		for (int d=i+1;d<n;d++) 
			if(tasktemp[i].time>tasktemp[d].time) 
			{
				temp=tasktemp[i]; 
				tasktemp[i]=tasktemp[d]; 
				tasktemp[d]=temp; 
			} 
	} 
	
		CString strtemp;
	strtemp.Format("%d",tasktemp[0].num);
	m_sjf = "作业"+strtemp;
	for(int r=1;r<=n-1;r++)
	{
		strtemp.Format("%d",tasktemp[r].num);
		m_sjf+="->作业"+strtemp;
	}
	
	for(int k = 0;k<n-1;k++ )
		m_sjftotaltime += (n-k-1)*tasktemp[k].time;
 

	UpdateData(false);

	
}

void CAttemperDlg::Smjf(TASK task[], int n)
{
	TASK tasktemp[5];
	TASK temp;
	for(int j=0;j<n;j++)
		tasktemp[j] = task[j];
	
	for (int i=0;i<n-1;i++) 
	{ 
		for (int d=i+1;d<n;d++) 
			if(tasktemp[i].space>tasktemp[d].space) 
			{
				temp=tasktemp[i]; 
				tasktemp[i]=tasktemp[d]; 
				tasktemp[d]=temp; 
			} 
	} 
	
	
	
	CString strtemp;
	strtemp.Format("%d",tasktemp[0].num);
	m_smjf = "作业"+strtemp;
	for(int r=1;r<=n-1;r++)
	{
		strtemp.Format("%d",tasktemp[r].num);
		m_smjf+="->作业"+strtemp;
	}
	for(int k = 0;k<n-1;k++ )
		m_smjftotaltime += (n-k-1)*tasktemp[k].time;
	
	UpdateData(false);
	
}

void CAttemperDlg::ArrayToV()
{
	m_time1 = task[0].time;
	m_space1 = task[0].space;
	m_time2 = task[1].time;
	m_space2 = task[1].space;
	m_time3 = task[2].time;
	m_space3 = task[2].space;
	m_time4 = task[3].time;
	m_space4 = task[3].space;
	m_time5 = task[4].time;
	m_space5 = task[4].space;
}


void CAttemperDlg::SetTime()
{
	srand((unsigned)time(0));
    
	UpdateData(false);
}

void CAttemperDlg::Init()
{  
	srand((unsigned)time(0));
	
	int hour,minute;
	hour = rand()%24;
	minute = rand()%60;
	m_time1 = rand()%60+1;
	m_time2 = rand()%60+1;
	m_time3 = rand()%60+1;
	m_time4 = rand()%60+1;
	m_time5 = rand()%60+1;
	m_space1 = TimeToSpace(m_time1);
	m_space2 = TimeToSpace(m_time2);
	m_space3 = TimeToSpace(m_time3);
	m_space4 = TimeToSpace(m_time4);
	m_space5 = TimeToSpace(m_time5);
    m_refer1.SetTime(hour,minute+rand()%10,rand()%60);
	m_refer2.SetTime(hour,minute+rand()%10,rand()%60);
	m_refer3.SetTime(hour,minute+rand()%10,rand()%60);
	m_refer4.SetTime(hour,minute+rand()%10,rand()%60);
	m_refer5.SetTime(hour,minute+rand()%10,rand()%60);

    
	
    task[0].time = m_time1;
	task[0].space = m_space1;
	task[0].num=1;
	
	task[1].time = m_time2;
	task[1].space = m_space2;
    task[1].num=2;
	task[2].time = m_time3;
	task[2].space = m_space3;
	task[2].num = 3;
	task[3].time = m_time4;
	task[3].space = m_space4;
	task[3].num = 4;
	task[4].time = m_time5;
	task[4].space = m_space5;
	task[4].num=5;
  
    task[0].refer=m_refer1;
	task[1].refer=m_refer2;
	task[2].refer=m_refer3;
	task[3].refer=m_refer4;
	task[4].refer=m_refer5;
	  UpdateData(false);
}



COleDateTime CAttemperDlg::AddTime(COleDateTime ctime, int n)
{  
	ctime.SetTime((+(ctime.GetMinute()+n/60+(ctime.GetSecond()+n)%60)/60)
	,((ctime.GetMinute()+n/60+(ctime.GetSecond()+n)%60)%60),((ctime.GetSecond()+n)%60)); 

		return ctime;
}

int CAttemperDlg::CountSecond(COleDateTime oletime)
{
return (oletime.GetHour()*3600+oletime.GetMinute()*60+oletime.GetSecond());
}

void CAttemperDlg::OnReferatonce() 
{
	// TODO: Add your control notification handler code here
//	UpdateData(true);
//   ASSERT("%d,%d,%d,%d,%d,%d",m_refer1.GetHour(),m_refer1.GetMinute(),m_refer1.GetSecond(),
//	   m_refer2.GetHour(),m_refer2.GetMinute(),m_refer2.GetSecond());
   UpdateData(false);
//	MessageBox()
	m_default = true;
   m_refer1.GetCurrentTime();
   m_refer2.GetCurrentTime();
   m_refer3.GetCurrentTime();
   m_refer4.GetCurrentTime();
   m_refer5.GetCurrentTime();
	Fcfs(task,5);
	Sjf(task,5);
	Smjf(task,5);

   
   m_default = false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -