我正在寫一個程序,雖然不像boggle,但是它的難度比較小。我的程序程序旨在接收字母行數(所有字母相同的字母數),然後在單獨的行上接收字母。通過話,我要猜的數量和話我會猜 如每行後進行如下圖所示:兩個不同的類雙鏈表和Pair類不會一起工作
5
SRENG
OLIOA
VISKE
THAOR
PDTAL
4
LORE
NOSE
ROILS
SAILORS
輸出將板很明顯,但需要的是和什麼特別的被發現的話,哪些是與succeded的話的方向不是沿着:
如:
LORE
"FOUND!"
"W, NE, E"
所以,現在這裏是它爲我變得混亂。我有我的主程序稱爲scramble.cc,其中包括解決實際問題的所有代碼,而我使用的其他程序是pair.h,pair.cc,list.h和list.cc(當然我的編譯器以及) 我對代碼:
#include <iostream>
#include <cstdlib>
#ifndef PAIR
#define PAIR
using namespace std;
struct Pair{
int r, c;
Pair(size_t r1, size_t c1);
Pair();
void print(ostream & ostr)const;
size_t get(size_t index);
};
ostream & operator<<(ostream & ostr, const Pair & p);
bool operator==(const Pair & p1, const Pair & p2);
bool operator!=(const Pair & p1, const Pair & p2);
#endif
pair.cc
#include <iostream>
#include <cstdlib>
#include "pair.h"
using namespace std;
Pair::Pair(size_t r1, size_t c1)
{
r=r1;
c=c1;
}
Pair::Pair()
{
r=c=0;
}
void Pair::print(ostream & ostr) const
{
ostr << r << "," << c;
}
ostream & operator<<(ostream & ostr, const Pair & p)
{
p.print(ostr)
return ostr;
}
bool operator==(const Pair & p1, const Pair & p2)
{
return p1.r == p2.r and p1.c == p2.c
}
bool operator!=(const Pair & p1, const Pair & p2)
{
return not(p1==p2)
}
我List.h:
#include <cstdlib>
#include <iostream>
#include "pair.h"
using namespace std;
class List
{
public:
typedef Pair ElementType;
typedef int ElementType //this is what I used before putting in pair.h/.cc
List();
~List();
List(const List & orig)
void add(const ElementType & item, size_t index);
void removeAt(size_t index);
void remove(const ElementType & item)
size_t find(const ElementType & item) const;
ELementType get(size_t index) const;
size_t getSize() const;
void output(std::ostream & ostr) const;
private:
struct Node{
Node *prev;
ELementType data;
Node*next;
Node();
Node(Node *p, Node *n);
Node(Node *p, const ElementType & d, Node *n);
};
void _setCurrentIndex(size_t index) const;
size_t size;
mutable size_t currentIndex;
Node *front;
Node *rear;
mutable Node *current;
};
我list.cc代碼:
#include <iostream>
#include <cassert>
#include <cstdlib>
#include "list.h"
using namespace std;
List::Node::Node()
{
prev = next = NULL;
}
List:: List()
{
front = new Node()
rear = new Node()
front->next = rear;
rear->prev = front;
currentIndex=0;
current = front->next;
size=0;
}
List::~List()
{
_setCurrentIndex(0);
while(current)
{
Node *temp = current;
current = current -> next;
delete temp;
}
//not showing deep copy function b/c it isn't important for this program
void List::add(const ElementType & item, size_t index)
{
assert(0<=index && index <= size);
_setCurrentIndex(index);
size++;
Node *born = new Node;
born->data = item;
born->prev = current->prev;
born->prev->next = current;
born->prev = born;
current = born;
}
void List::removeAt(size_t index)
{
assert(0<=index<=getSize());
_setCurrentIndex(index);
Node *old = current;
current->prev->next = current->next;
current->next->prev = current->prev;
delete old;
size--;
}
void List::remove(const ElementType & item)
{
for(size_t i=0; i<size; i++)
{
_setCurrentIndex(i);
if(find(item)<getSize())
{
Node *tempOld = current;
current->next->prev = current->prev;
current->prev->next = current->next;
current = current->next;
delete tempOld;
size--;
}
}
}
size_t List::find(const ElementType & item) const
{
for(size_t i=0; i<size; i++)
{
_setCurrentIndex(i)
if(get(i) == item)
return get(i);
}
return getSize();
}
List::ElementType List::get(size_t index) const
{
assert(0 <= index < size);
_setCurrentIndex(index);
assert(current->next != NULL);
return current->data;
}
size_t List::getSize() const
{
return size;
}
void List::output(std::ostream & ostr) const
{
for(size_t i=0; i<size; i++)
{
_setCurrentIndex(i);
ostr << current->data << " ";
}
ostr << endl;
}
void List:: _setCurrentIndex(size_t index) const
{
int x;
if(currentIndex > index)
x = currentIndex - index;
else
x = index-currentIndex;
if(index < (sizez_t)x)
{
current = front->next;
curentIndex=0;
while(currentIndex != index)
{
current = current->next;
currentIndex++;
}
}
else if((size-index) < (size_t)x)
{
current = rear;
currentIndex = size;
while(currentIndex != index)
{
current = current->prev;
currentIndex--;
}
}
else
{
if(currentIndex > index)
{
while(currentIndex!=index)
{
current = current->prev;
currentIndex--;
}
}
else
{
while(currentIndex!=index)
{
current = current->next;
currentIndex++;
}
}
}
}
我scramble.cc:
#include <iostream>
#include <cstdlib>
#include "list.h"
#include "pair.h"
using namespace std;
List history;
Pair p1 = Pair(1,1);
int main()
{
}
我的Makefile
scramble: scramble.o list.o pair.o
g++ -o scramble scramble.o list.o pair.o
scramble.o: scramble.cc list.h pair.h
g++ -c scramble.cc
list.o: list.cc list.h pair.h
g++ -c list.cc
pair.o pair.cc pair.h
g++ -c pair.cc
所以這是我的代碼,我不得不寫出來全部由手工B/C程序我用沒有按不要複製和粘貼,所以請原諒我,如果有小錯誤(如遺忘;或拼寫錯誤)。
所以我最大的問題是,當我嘗試製作對並將它們放入列表中時,它讓我無法將對轉換爲size_t中的函數(如Find),從而嚇倒了它。還有大量未定義的引用List :: List(),List ::〜List(),Pair :: Pair(unsigned long,unsigned long),List :: add(Pair const &,unsigned long),List :: getSize()常量,列表:: removeAt(無符號長)
所以任何人都可以請幫我在這裏我需要做我的List類和對?我的目標是將對存儲在列表中,並能夠刪除它們,這是我的主要目標。
非常感謝傢伙和女孩!
「b/c我使用的程序不會複製和粘貼」 - 呃,什麼? –
我現在最大的問題是,IDE不會讓我c/p :)這是一個太多的代碼,但我會看看我能否提供幫助。 – nullpotent
我沒有添加很多我scramble.cc,因爲我主要關心的是一般的類文件,當他們在一般的應用 –