C++ 我的rolodex作业代码将无法正常工作
我一直有这个任务的问题,该计划将无法工作,我不知道为什么。 在自动检查器中,有一个重要的部分禁止程序运行,我对此毫无意义 对于“这不起作用”一句话,输出应为: 是 不 这 工作 (每个单词在单独的行上) 这是当前的代码 (main.cpp) (rolodex.h)C++ 我的rolodex作业代码将无法正常工作,c++,C++,我一直有这个任务的问题,该计划将无法工作,我不知道为什么。 在自动检查器中,有一个重要的部分禁止程序运行,我对此毫无意义 对于“这不起作用”一句话,输出应为: 是 不 这 工作 (每个单词在单独的行上) 这是当前的代码 (main.cpp) (rolodex.h) \ifndef ROLODEX\u H #定义ROLODEX_H #包括 结构节点{ std::字符串数据; 节点*下一步; 节点*prev; }; Rolodex类{ 公众: /** *创建一个新的空Rolodex。 */ 显式Ro
\ifndef ROLODEX\u H
#定义ROLODEX_H
#包括
结构节点{
std::字符串数据;
节点*下一步;
节点*prev;
};
Rolodex类{
公众:
/**
*创建一个新的空Rolodex。
*/
显式Rolodex();
/**
*如果Rolodex位于第一张卡之前,则返回true。
*/
bool是beforefirst()常量;
/**
*如果Rolodex位于最后一张卡之后,则返回true。
*/
bool isAfterLast()常量;
/**
*将Rolodex向前旋转一张卡。
*/
void rotateForward();
/**
*将Rolodex向后旋转一张卡。
*/
void rotateBackward();
/**
*返回当前卡的值。
*/
常量std::string¤tValue()常量;
/**
*在当前卡之后插入新卡并定位Rolodex
*在新插入的卡上。
*
*@param value插入新卡的值。
*/
void insertAfterCurrent(const std::string和value);
/**
*在当前卡之前插入新卡并定位Rolodex
*在新插入的卡上。
*
*@param value插入新卡的值。
*/
void insertBeforeCurrent(const std::string和value);
私人:
//在此处添加实例变量
节点*哨兵;
节点*电流;
};
#endif//ROLODEX_H
非常感谢您的帮助。“行不通”不是一个好问题。究竟什么不起作用;如果我不知道问题出在哪里?它应该存储输入句子中的独立单词,例如,这很好,可以将独立单词输出到单独的行中。你可能不知道原因是什么,但有一些方法可以告诉你它不起作用。怎么用?我们正在寻找一个关于X和Y的详细信息的“我期望它做X,但它做了Y”代码>和
当前->上一个代码>什么都不要做。(一个体面的编译器应该对此发出警告。)
#include <iostream>
#include <string>
#include <unistd.h>
#include "Rolodex.h"
using namespace std;
auto verbose = false; // Log Rolodex actions
auto printCurrent = false; // Output only the current Rolodex card
void rolodex()
{
string word;
Rolodex rolodex;
while (cin >> word) {
if (rolodex.isBeforeFirst() || rolodex.currentValue() <= word) {
while (!rolodex.isAfterLast() && rolodex.currentValue() < word) {
rolodex.rotateForward();
if (verbose)
cerr << "rotateForward\n";
}
rolodex.insertBeforeCurrent(word);
if (verbose)
cerr << "insertBeforeCurrent\n";
}
else if (rolodex.isAfterLast() || rolodex.currentValue() >= word) {
while (!rolodex.isBeforeFirst() && rolodex.currentValue() > word) {
rolodex.rotateBackward();
if (verbose)
cerr << "rotateBackward\n";
}
rolodex.insertAfterCurrent(word);
if (verbose)
cerr << "insertAfterCurrent\n";
}
}
if (printCurrent) {
cout << rolodex.currentValue() << '\n';
}
else { // Output all Rolodex card values (default)
while (!rolodex.isBeforeFirst()) {
rolodex.rotateBackward();
}
rolodex.rotateForward(); // Go to the first card
while (!rolodex.isAfterLast()) {
cout << rolodex.currentValue() << '\n';
rolodex.rotateForward();
}
}
}
int main(int argc, char** argv)
{
int c;
while ((c = getopt(argc, argv, "vc")) != EOF) {
switch (c) {
case 'v':
verbose = true;
break;
case 'c':
printCurrent = true;
break;
}
}
argc -= optind;
argv += optind;
rolodex();
return 0;
}
#include "Rolodex.h"
Rolodex::Rolodex()
{
sentinel = new node;
current = sentinel;
sentinel->next = sentinel;
sentinel->prev = sentinel;
}
/**
* Returns true if the Rolodex is positioned before the first card.
*/
bool Rolodex::isBeforeFirst() const
{
// Replace with real code
return (current == sentinel);
}
/**
* Returns true if the Rolodex is positioned after the last card.
*/
bool Rolodex::isAfterLast() const
{
// Replace with real code
return (current == sentinel);
}
/**
* Rotates the Rolodex one card forwards.
*/
void Rolodex::rotateForward()
{
current->next;
// Add code here
}
/**
* Rotates the Rolodex one card backwards.
*/
void Rolodex::rotateBackward()
{
current->prev;
// Add code here
}
/**
* Returns the value of the current card.
*/
const std::string& Rolodex::currentValue() const
{
// Temporary hack to keep the compiler happy
// Replace both lines with real code
//static std::string data = current->data;
return current->data;
}
/**
* Inserts a new card after the current card and positions the Rolodex
* at the newly inserted card.
*
* @param value The value to insert into a new card.
*/
void Rolodex::insertAfterCurrent(const std::string& value)
{
node* newNode = new node;
newNode->data = value;
newNode->prev = current;
newNode->next = current->next;
current->next->prev = newNode;
current->next = newNode;
current = newNode;
// Add code here
}
/**
* Inserts a new card before the current card and positions the Rolodex
* at the newly inserted card.
*
* @param value The value to insert into a new card.
*/
void Rolodex::insertBeforeCurrent(const std::string& value)
{
node* newNode = new node;
newNode->data = value;
newNode->next = current;
newNode->prev = current->prev;
current->prev->next = newNode;
current->prev = newNode;
current = newNode;
// Add code here
}
#ifndef ROLODEX_H
#define ROLODEX_H
#include <string>
struct node {
std::string data;
node *next;
node *prev;
};
class Rolodex {
public:
/**
* Creates a new empty Rolodex.
*/
explicit Rolodex();
/**
* Returns true if the Rolodex is positioned before the first card.
*/
bool isBeforeFirst() const;
/**
* Returns true if the Rolodex is positioned after the last card.
*/
bool isAfterLast() const;
/**
* Rotates the Rolodex one card forwards.
*/
void rotateForward();
/**
* Rotates the Rolodex one card backwards.
*/
void rotateBackward();
/**
* Returns the value of the current card.
*/
const std::string& currentValue() const;
/**
* Inserts a new card after the current card and positions the Rolodex
* at the newly inserted card.
*
* @param value The value to insert into a new card.
*/
void insertAfterCurrent(const std::string& value);
/**
* Inserts a new card before the current card and positions the Rolodex
* at the newly inserted card.
*
* @param value The value to insert into a new card.
*/
void insertBeforeCurrent(const std::string& value);
private:
// Add instance variables here
node* sentinel;
node* current;
};
#endif //ROLODEX_H