主题:[讨论]初学者学c++,编了个电梯调度的算法。。求指点。。程序运行错误!
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
class Clift{
private:
int m_index; //开始的值
int m_count; //数值的数量
int* m_pvalue;//临时指针
public:
explicit Clift(const int num);
~Clift();
int* Cnumber();
int Ccount() const;
int Ccontrol(int value);
};//定义一个电梯调度的类
inline Clift::Clift(const int num)
{
m_count = num;
}//构造函数
inline Clift::~Clift()
{
cout<<"这是析构函数"<<endl;
delete [] m_pvalue;
}//析构函数
inline int* Clift::Cnumber()
{
int i(0);
m_pvalue = new int [50];
srand((unsigned)time(NULL));
cout<<"随机数是:(0~500)"<<endl;
for(i;i<m_count;i++)
{
cout<<setw(4)<<rand()%500;
*(m_pvalue+i) = rand();
if(i%4 == 0)
cout<<endl;//一行四个数
}
return m_pvalue;
}//生成随机数
inline int Clift::Ccount() const
{
srand((unsigned)time(NULL));
return rand()%m_count;
}//计算开始值所在的位置
inline int Clift::Ccontrol(int value)
{
int j(0);
int k(1); //临时变量
int count;
m_index = *(m_pvalue+value);
int* pindex = new int[50];
pindex = m_pvalue;
for (j;j<m_count-1;j++)
for(k;k<m_count;k++)
{
if(*(pindex+j)>*(pindex+k))
count = *(pindex+k);
*(pindex+k) = *(pindex+j);
*(pindex+j) = count;//冒泡排序
}
int sum = 0;//总值
//从外向内
cout<<"从外向内的调度顺序为:"<<endl;
for(j=value;j>=0;j--)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == 0)
{
j=value+1;
for(j;j<m_count;j++)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == m_count-1 )
exit(1);
}
}
}
cout<<endl;cout<<"总值为:"<<sum<<endl;
//从内向外
sum = 0;
cout<<"从内向外的调度顺序为:"<<endl;
for(j=value;j<m_count;j++)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == m_count-1 )
{
j=value-1;
for(j;j>=0;j--)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == 0 )
exit(1);
}
}
}
cout<<endl;
cout<<"总值为:"<<sum<<endl;
delete [] pindex;
}//调度算法
int main()
{
Clift lift(8);
lift.Cnumber();
int m = lift.Ccount();
lift.Ccontrol(m);
}
#include<time.h>
#include<stdlib.h>
#include<iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
class Clift{
private:
int m_index; //开始的值
int m_count; //数值的数量
int* m_pvalue;//临时指针
public:
explicit Clift(const int num);
~Clift();
int* Cnumber();
int Ccount() const;
int Ccontrol(int value);
};//定义一个电梯调度的类
inline Clift::Clift(const int num)
{
m_count = num;
}//构造函数
inline Clift::~Clift()
{
cout<<"这是析构函数"<<endl;
delete [] m_pvalue;
}//析构函数
inline int* Clift::Cnumber()
{
int i(0);
m_pvalue = new int [50];
srand((unsigned)time(NULL));
cout<<"随机数是:(0~500)"<<endl;
for(i;i<m_count;i++)
{
cout<<setw(4)<<rand()%500;
*(m_pvalue+i) = rand();
if(i%4 == 0)
cout<<endl;//一行四个数
}
return m_pvalue;
}//生成随机数
inline int Clift::Ccount() const
{
srand((unsigned)time(NULL));
return rand()%m_count;
}//计算开始值所在的位置
inline int Clift::Ccontrol(int value)
{
int j(0);
int k(1); //临时变量
int count;
m_index = *(m_pvalue+value);
int* pindex = new int[50];
pindex = m_pvalue;
for (j;j<m_count-1;j++)
for(k;k<m_count;k++)
{
if(*(pindex+j)>*(pindex+k))
count = *(pindex+k);
*(pindex+k) = *(pindex+j);
*(pindex+j) = count;//冒泡排序
}
int sum = 0;//总值
//从外向内
cout<<"从外向内的调度顺序为:"<<endl;
for(j=value;j>=0;j--)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == 0)
{
j=value+1;
for(j;j<m_count;j++)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == m_count-1 )
exit(1);
}
}
}
cout<<endl;cout<<"总值为:"<<sum<<endl;
//从内向外
sum = 0;
cout<<"从内向外的调度顺序为:"<<endl;
for(j=value;j<m_count;j++)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == m_count-1 )
{
j=value-1;
for(j;j>=0;j--)
{
cout<<setw(4)<<*(pindex+j);
sum += *(pindex+j);
if(j == 0 )
exit(1);
}
}
}
cout<<endl;
cout<<"总值为:"<<sum<<endl;
delete [] pindex;
}//调度算法
int main()
{
Clift lift(8);
lift.Cnumber();
int m = lift.Ccount();
lift.Ccontrol(m);
}