求以下问题的完整代码,要求使用c++面向对象的程序设计方法和构造函数
用空格分隔字符串,然后逐个计算长度
运行结果:
代码:
#include <iostream>
using namespace std;
class StrWords
{
public:
StrWords() {}
void split(const char* p)
{
int i = 0;
int len = 0;
char pre = '\0';
int flag = 0;
while (p[i] != '\0')
{
if (p[i] == '.' && pre != ' ') //结束
{
if (flag == 0)
{
cout << len;
flag = 1;
}
else
cout << " " << len;
len = 0;
break;
}
else if (p[i] == ' ' && pre != ' ')
{
if (flag == 0)
{
cout << len;
flag = 1;
}
else
cout << " " << len;
len = 0;
i++;
pre = ' ';
}
else
{
len++;
pre = p[i];
i++;
}
}
}
};
int main()
{
char buf[10000] = { 0 };
cin.getline(buf, 10000 - 1);
StrWords w;
w.split(buf);
return 0;
}
看下这个是不是符合你的要求
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine().trim(); // 去除首尾空格
input = input.replaceAll("\\s+", " "); // 将多个连续的空格替换成一个空格
String[] words = input.split(" ");
for (String word : words) {
int length = word.length();
// 如果单词以.结尾,将.去掉
if (word.charAt(length - 1) == '.') {
length--;
}
System.out.print(length + " ");
}
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!下面是完善界面后的源代码,以及一次运行的过程截图:
#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;
}