求以下问题的完整代码,要求使用c++面向对象的程序设计和构造函数
运行结果:
代码:
#include <iostream>
using namespace std;
class MyStr
{
public:
MyStr(){}
int mystrstr(char a[], char b[])
{
int i = 0, j = 0;
for (i = 0; a[i] != '\0'; i++)
{
for (j = 0; b[j] != '\0' && a[i + j] != '\0'; j++)
{
if (a[i + j] != b[j])
break;
}
if (b[j] == '\0')
return i;
}
return -1;
}
};
int main()
{
char a[510] = { 0 }, b[510] = { 0 };
MyStr s;
cin.getline(a, 500);
cin.getline(b, 500);
cout << s.mystrstr(a, b);
return 0;
}
参考:https://blog.csdn.net/xiaoyaolangwj/article/details/126415606
下面是完善界面后的源代码,以及一次运行的过程截图:
#include<iostream>
#include<time.h>
#include<string>
#include<map>
using namespace std;
#define random(x) rand()%(x)
int main()
{
srand(time(NULL));
map<int, int>insstream;
int count = 0;
while (count < 320) {
int m = random(319);
count++;
insstream.insert(pair<int, int>(count, m + 1));
int m1 = random(m + 1);
count++;
insstream.insert(pair<int, int>(count, m1));
count++;
insstream.insert(pair<int, int>(count, m1 + 1));
int m2 = random(319 - (m1 + 2) + 1) + m1 + 2;
count++;
insstream.insert(pair<int, int>(count, m2));
}
/*检测指令流
for (auto ins = insstream.begin(); ins != insstream.end(); ins++)
cout << ins->second << " ";
*/
map<int, int>pagestream;
for (auto ins = insstream.begin(); ins != insstream.end(); ins++)
pagestream.insert(pair<int, int>(ins->first, ins->second / 10));
//检测页面流
cout << "页面流如下:" << endl;
for (auto page = pagestream.begin(); page != pagestream.end(); page++)
{
cout << page->second << " ";
if (page->first % 32 == 0) { cout << endl; }
}
string choose;
cout << "*************************************************" << endl;
cout << "欢迎使用本程序~" << endl;
int allocation = random(6 - 3 + 1) + 3;
cout << "本次分配的物理块数为:" << allocation << "块!" << endl;
while (true) {
cout << "请选择您需要的算法(FIFO、LRU、OPT、LFU):" << endl;
cout << "或输入S或s停止程序~" << endl;
cin >> choose;
double miss = 0;
map<int, int>run;
if (choose == "FIFO" || choose == "fifo") {
for (auto page = pagestream.begin(); page != pagestream.end(); page++)
{
for (auto r = run.begin(); r != run.end(); r++)
{
if (r->second == page->second) { goto skip; }
}
run.insert(pair<int, int>(page->first, page->second));
miss++;
if (run.size() > allocation)
run.erase(run.begin());
skip:
cout << "*****************" << endl;
//检测运行流
for (auto r = run.begin(); r != run.end(); r++)
cout << r->second << " ";
cout << endl;
}
cout << "*********************" << endl;
cout << "FIFO算法的页面命中率=" << 1 - (miss / 320) << endl;
cout << "*********************" << endl;
}
else if (choose == "LRU" || choose == "lru") {
for (auto page = pagestream.begin(); page != pagestream.end(); page++)
{
for (auto r = run.begin(); r != run.end(); r++)
{
if (r->second == page->second) {
run.erase(r);
goto skip2;
}
}
miss++;
skip2:
run.insert(pair<int, int>(page->first, page->second));
if (run.size() > allocation)
run.erase(run.begin());
//检测运行流
cout << "*****************" << endl;
for (auto r = run.begin(); r != run.end(); r++)
cout << r->second << " ";
cout << endl;
}
cout << "*********************" << endl;
cout << "LRU算法的页面命中率=" << 1 - (miss / 320) << endl;
cout << "*********************" << endl;
}
else if (choose == "OPT" || choose == "opt") {
for (auto page = pagestream.begin(); page != pagestream.end(); page++)
{
for (auto r = run.begin(); r != run.end(); r++)
{
if (r->second == page->second) { goto skip3; }
}
if (run.size() == allocation) {
multimap<int, int>length;
for (auto r = run.begin(); r != run.end(); r++) {
for (auto page2 = page; page2 != pagestream.end(); page2++) {
if (r->second == page2->second) {
int len = page2->first - page->first;
length.insert(pair<int, int>(len, r->first));
break;
}
if (page2 == pagestream.find(pagestream.size())) {
int len = 321 - page->first;
length.insert(pair<int, int>(len, r->first));
break;
}
}
}
auto last = length.rbegin();
if (last != length.rend()) {
auto r = run.find(last->second);
if (r != run.end()) {
run.erase(r);
}
}
}
run.insert(pair<int, int>(page->first, page->second));
miss++;
skip3:
cout << "*****************" << endl;
//检测运行流
for (auto r = run.begin(); r != run.end(); r++)
cout << r->second << " ";
cout << endl;
}
cout << "*********************" << endl;
cout << "OPT算法的页面命中率=" << 1 - (miss / 320) << endl;
cout << "*********************" << endl;
}
else if (choose == "LFU" || choose == "lfu") {
for (auto page = pagestream.begin(); page != pagestream.end(); page++)
{
for (auto r = run.begin(); r != run.end(); r++)
{
if (r->second == page->second) { goto skip4; }
}
if (run.size() == allocation) {
int time = 0;
multimap<int, int>times;
for (auto r = run.begin(); r != run.end(); r++) {
for (auto page2 = pagestream.begin(); page2 != page; page2++) {
if (r->second == page2->second) {
time++;
}
}
times.insert(pair<int, int>(time, r->first));
time = 0;
}
run.erase(times.begin()->second);
}
run.insert(pair<int, int>(page->first, page->second));
miss++;
skip4:
cout << "*****************" << endl;
//检测运行流
for (auto r = run.begin(); r != run.end(); r++)
cout << r->second << " ";
cout << endl;
}
cout << "*********************" << endl;
cout << "LFU算法的页面命中率=" << 1 - (miss / 320) << endl;
cout << "*********************" << endl;
}
else if (choose == "S" || "s") { break; }
else {
cout << "您的输入有误!请输入:FIFO、LRU、OPT、LFU中的一个!" << endl;
}
}
cout << "感谢您的使用~祝您生活愉快!" << endl;
cout << "*************************************************" << endl;
}
抱歉,该问题需要更具体的说明以便理解,不知道具体是什么需求需要实现面向对象和构造函数的代码。请提供更具体的问题描述。