主题:[讨论]求助!!!回帖必加分....
/////后面主函数有一个Goto语句....能不能用别的方法实现...求高手帮忙
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
const int MaxSize = 50;
template<class T>
class List;
//异常类
class OutOfBounds{};
template<class T>
class Node
{
friend List<T>;
private:
T data;
Node<T> *next;//指向表中下一个结点的指针
};
template<class T>
class List
{
private:
Node<T> *last;//表中最后一个结点的指针
int length;
public:
List();
~List();
bool Empty() const {return length == 0;}
int Length() const {return length;}
bool Retrieve(int k,T &x) const;
int Locate(const T& x) const;
List<T>& Insert(int k,const T& x);
List<T>& Delete(int k, T &x);
void Print() const;
T Josephus(const int &n,const int &m);
};
template<class T>
List<T>::List()
{
Node<T> *y = new Node<T>;
y -> next = y;
last = y;
length = 0;
}
template<class T>
List<T>::~List()
{
Node<T> *next;
while(!Empty()) {
next = last->next;
delete last;
length--;
last = next;
}
delete last;
}
template<class T>
bool List<T>::Retrieve ( int k, T &x ) const
{
if (k < 1||k > length)
return false;
Node<T> *p = last->next->next;
int index = 1;
while (index < k)
{
p = p->next;
index++;
}
x = p->data;
return true;
}
template<class T>
int List<T>::Locate(const T& x) const
{
Node<T> *p = last->next->next;
int index = 1;
last->next->data = x;
while(p->data != x)
{
p = p->next;
index++;
}
if(p != last->next) return index;
return 0;
}
template<class T>
List<T>& List<T>::Insert(int k,const T& x)
{
if(k < 0||k > length) throw OutOfBounds();
Node<T> *p = last->next;
for(int index = 1;index <= k;index++)
p = p->next;
Node<T> *y = new Node<T>;
y->data = x;
y->next = p->next;
p->next = y;
if(k == length) last = y;
length++;
return *this;
}
template<class T>
List<T>& List<T>::Delete(int k, T &x)
{
if (k < 1 || k > length) throw OutOfBounds( );
Node<T> *q = last->next;
for(int index = 1;index <= k-1;index++)
q = q->next;
Node<T> *p = q->next;
q->next = p->next;
if(k == length) last = q;
x = p->data;
delete p;
length--;
return *this;
}
template<class T>
void List<T>::Print() const
{
Node<T> *temp;
for(temp = last->next->next;temp != last->next;temp = temp->next)
cout << temp->data << " ";
}
template<class T>
T List<T>::Josephus(const int &n,const int &m)
{
T array[MaxSize];
int i = m,j=0;
while(1)
{
while(i > length)
{
int k = length-i+m;
i = m-k;
}
Delete(i,array[j++]);
i += m-1;
if(length == 1)
{
Delete(1,array[j]); break;
}
}
for(int k = 0; k < n; k++)
Insert(k,array[k]);
//逐个输出被删除的元素
return array[--k];
}
//主函数测试
int main()
{
ifstream in("input.txt");
if(!in)
{
cout<<"Can't open the file!";
exit (-1);
}
ofstream out("output.txt");
if(!out) {
cout<<"Can't open the file!";
exit(-1);
}
//输出指定的数组
int n,k,b[MaxSize];
string index,a[MaxSize],c[MaxSize];
in >> n >> k;
for(int i = 0; i < k; i++)
in >> b[i];
//依次输出数组
for(i = 0; i < n; i++)
in >> a[i];
for(i = 0; i < k; i++)
c[i] = a[b[i]-1];
//定义一个临时变量
int temp = 1;
string str;
List<string> slist;
//////下面这个循环有没有办法不用GOTO语句啊????/帮帮忙
A: while(1) {
for(i = 0; i < n; i++)
slist.Insert(i,a[i]);
slist.Josephus(n,temp);
int y = 0;
for(i = slist.Length()-k+1; i <= slist.Length(); i++)
{
slist.Retrieve(i,str);
if(str == c[y]) {y++;continue;}
else {
for(i = 0; i < n; i++)
{
string h;
slist.Delete(1,h);
}
temp++;
goto A;///////////******8
}
}
break;
}////***8
out << temp << endl;
//输出前面的n-k位
for(i = 1; i <= slist.Length()-k; i++)
{
slist.Retrieve(i,str);
out << str << endl;
}
return 0;
}
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
const int MaxSize = 50;
template<class T>
class List;
//异常类
class OutOfBounds{};
template<class T>
class Node
{
friend List<T>;
private:
T data;
Node<T> *next;//指向表中下一个结点的指针
};
template<class T>
class List
{
private:
Node<T> *last;//表中最后一个结点的指针
int length;
public:
List();
~List();
bool Empty() const {return length == 0;}
int Length() const {return length;}
bool Retrieve(int k,T &x) const;
int Locate(const T& x) const;
List<T>& Insert(int k,const T& x);
List<T>& Delete(int k, T &x);
void Print() const;
T Josephus(const int &n,const int &m);
};
template<class T>
List<T>::List()
{
Node<T> *y = new Node<T>;
y -> next = y;
last = y;
length = 0;
}
template<class T>
List<T>::~List()
{
Node<T> *next;
while(!Empty()) {
next = last->next;
delete last;
length--;
last = next;
}
delete last;
}
template<class T>
bool List<T>::Retrieve ( int k, T &x ) const
{
if (k < 1||k > length)
return false;
Node<T> *p = last->next->next;
int index = 1;
while (index < k)
{
p = p->next;
index++;
}
x = p->data;
return true;
}
template<class T>
int List<T>::Locate(const T& x) const
{
Node<T> *p = last->next->next;
int index = 1;
last->next->data = x;
while(p->data != x)
{
p = p->next;
index++;
}
if(p != last->next) return index;
return 0;
}
template<class T>
List<T>& List<T>::Insert(int k,const T& x)
{
if(k < 0||k > length) throw OutOfBounds();
Node<T> *p = last->next;
for(int index = 1;index <= k;index++)
p = p->next;
Node<T> *y = new Node<T>;
y->data = x;
y->next = p->next;
p->next = y;
if(k == length) last = y;
length++;
return *this;
}
template<class T>
List<T>& List<T>::Delete(int k, T &x)
{
if (k < 1 || k > length) throw OutOfBounds( );
Node<T> *q = last->next;
for(int index = 1;index <= k-1;index++)
q = q->next;
Node<T> *p = q->next;
q->next = p->next;
if(k == length) last = q;
x = p->data;
delete p;
length--;
return *this;
}
template<class T>
void List<T>::Print() const
{
Node<T> *temp;
for(temp = last->next->next;temp != last->next;temp = temp->next)
cout << temp->data << " ";
}
template<class T>
T List<T>::Josephus(const int &n,const int &m)
{
T array[MaxSize];
int i = m,j=0;
while(1)
{
while(i > length)
{
int k = length-i+m;
i = m-k;
}
Delete(i,array[j++]);
i += m-1;
if(length == 1)
{
Delete(1,array[j]); break;
}
}
for(int k = 0; k < n; k++)
Insert(k,array[k]);
//逐个输出被删除的元素
return array[--k];
}
//主函数测试
int main()
{
ifstream in("input.txt");
if(!in)
{
cout<<"Can't open the file!";
exit (-1);
}
ofstream out("output.txt");
if(!out) {
cout<<"Can't open the file!";
exit(-1);
}
//输出指定的数组
int n,k,b[MaxSize];
string index,a[MaxSize],c[MaxSize];
in >> n >> k;
for(int i = 0; i < k; i++)
in >> b[i];
//依次输出数组
for(i = 0; i < n; i++)
in >> a[i];
for(i = 0; i < k; i++)
c[i] = a[b[i]-1];
//定义一个临时变量
int temp = 1;
string str;
List<string> slist;
//////下面这个循环有没有办法不用GOTO语句啊????/帮帮忙
A: while(1) {
for(i = 0; i < n; i++)
slist.Insert(i,a[i]);
slist.Josephus(n,temp);
int y = 0;
for(i = slist.Length()-k+1; i <= slist.Length(); i++)
{
slist.Retrieve(i,str);
if(str == c[y]) {y++;continue;}
else {
for(i = 0; i < n; i++)
{
string h;
slist.Delete(1,h);
}
temp++;
goto A;///////////******8
}
}
break;
}////***8
out << temp << endl;
//输出前面的n-k位
for(i = 1; i <= slist.Length()-k; i++)
{
slist.Retrieve(i,str);
out << str << endl;
}
return 0;
}