把这个类转为结构体 class Process { public: SegItem SegTable[10]; int SegNum; bool InMem; Process(int SN) //创建进程,进程对应的段数,以及段长 { int length; SegNum = SN; InMem = false; for(int i=0;i<SegNum;i++){ cout<<"请输入该进程第"<<i<<"个段的长度:"; cin>>length; cout<<endl; SegTable[i].SegLen=length; } } void InToMem() //将新建的进程调入内存 { int total=0; for(int i=0;i<SegNum;i++) total+=SegTable[i].SegLen; if (total> memModle.availableMem) cout << "内存空间不足,请先调出其他进程" << endl; else { for (int i=0; i<SegNum; i++) { for (int j=0; j<100; j++) { if (memModle.mem[j] == 0) { for(int k=j;k<SegTable[i].SegLen;k++) memModle.mem[k]=1; SegTable[i].SegBase=j; break; } } } memModle.availableMem -= total; InMem = true; cout << "进程已调入内存" << endl<<endl; } } void OutOfMem() //将内存中某进程调出内存 { int total=0; for (int i=0; i<SegNum; i++){ for(int j=0;j<SegTable[i].SegLen;j++) memModle.mem[SegTable[i].SegBase+j]=0; total+=SegTable[i].SegLen; } memModle.availableMem += total; InMem = false; cout << "进程已调出内存" << endl<<endl; } ~Process() { OutOfMem(); cout << "进程已删除" << endl; } };
时间: 2024-01-21 14:04:09 浏览: 25
将类 Process 转换为结构体 ProcessStruct,需要注意以下几点:
1. 结构体没有构造函数,因此需要手动初始化结构体的成员变量。
2. 结构体没有析构函数,因此需要手动释放结构体的成员变量所占用的内存。
3. 结构体的成员变量默认为 public,因此需要将类中的 private 成员变量改为 public。
下面是一个示例:
```c++
struct SegItem {
int SegLen;
int SegBase;
};
struct ProcessStruct {
SegItem SegTable[10];
int SegNum;
bool InMem;
void create(int SN) {
int length;
SegNum = SN;
InMem = false;
for(int i = 0; i < SegNum; i++) {
cout<<"请输入该进程第"<<i<<"个段的长度:";
cin>>length;
cout<<endl;
SegTable[i].SegLen = length;
}
}
void InToMem() {
int total = 0;
for(int i = 0; i < SegNum; i++)
total += SegTable[i].SegLen;
if(total > memModle.availableMem)
cout << "内存空间不足,请先调出其他进程" << endl;
else {
for(int i = 0; i < SegNum; i++) {
for(int j = 0; j < 100; j++) {
if(memModle.mem[j] == 0) {
for(int k = j; k < SegTable[i].SegLen; k++)
memModle.mem[k] = 1;
SegTable[i].SegBase = j;
break;
}
}
}
memModle.availableMem -= total;
InMem = true;
cout << "进程已调入内存" << endl<<endl;
}
}
void OutOfMem() {
int total = 0;
for(int i = 0; i < SegNum; i++) {
for(int j = 0; j < SegTable[i].SegLen; j++)
memModle.mem[SegTable[i].SegBase + j] = 0;
total += SegTable[i].SegLen;
}
memModle.availableMem += total;
InMem = false;
cout << "进程已调出内存" << endl<<endl;
}
};
int main() {
ProcessStruct p;
p.create(2);
p.InToMem();
p.OutOfMem();
return 0;
}
```
在上面的示例中,我们将 Process 类转换为 ProcessStruct 结构体。由于 Process 类的成员变量是 private 的,我们需要在结构体中将其改为 public。同时,由于结构体没有构造函数和析构函数,我们需要手动添加 create、InToMem 和 OutOfMem 函数来完成类中的功能。最后在 main 函数中调用这些函数即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)