您的位置:首页 > 其它

几道笔试题的解法(四)

2010-03-31 20:56 381 查看
题目: 编写一个单词逆序输出的算法。例如:“I am a student”, 要求输出为:”student a am I”
分析:本题考查的是C语言的指针

代码如下:

第一种: //用C语言知识实现

void ReverseWord(char* str, char* outstr)
{
char* head = str;

while(*str++);

int count = 0;

for ( str -= 2; *str; str-- )
{
if (str == head)
{
do
{
*outstr++ = *str++;
}while(count--);

break;
}
if ( *str == ' ')
{
char *temp = str + 1;
while(count--)
*outstr++ = *temp++;
*outstr++ = ' ';
count = 0;
}
else
{
count++;
}
}
*outstr = 0;
}
第二种: //直接通过创建栈来实现
#include <cassert>
#include <iostream>
#include <ostream>

using namespace std;

template<class T>
class stackNode
{
public:
T data;
stackNode<T> *Next;
};

template<class T>
class Stack_Node
{
public:
Stack_Node() : first(0)
{
}
virtual ~Stack_Node()
{
stackNode<T> *p;
while (first)
{
p = first->Next;
delete first;
first = p;
}
}
bool IsEmpty() const
{
return first == 0;
}
bool IsFull() const
{
try
{
stackNode<T> *p = new stackNode<T>;
delete p;
return false;
}
catch (...)
{
return true;
}
}
T GetTop() const
{
if (IsEmpty())
{
throw out_of_range("The stack is empty!");
}
return first->data;
}
Stack_Node<T>& Push(const T& x)
{
stackNode<T> *p = new stackNode<T>;
p->data = x;
p->Next = first;
first = p;

return *this;
}

Stack_Node<T>& Pop(T& x)
{
if (IsEmpty())
{
throw out_of_range("The stack is empty");
}
x = first->data;
stackNode<T> *p = first;

first = first->Next;
delete p;
return *this;
}

int Length() const
{
int len = 0;
stackNode<T> *p = first;
while (p)
{
len++;
p = p->Next;
}
return len;
}

void Print(ostream& os) const
{
stackNode<T> *p = first;
if (IsEmpty())
{
throw out_of_range("The stack is empty!");
}
while (p)
{
os << p->data << " ";
p = p->Next;
}
}
private:
stackNode<T> *first;
};

template<class T>
ostream& operator<<(ostream& os, const Stack_Node<T>& x)
{
x.Print(os);
return os;
}

int GetLength(const char* _in)
{
int _length = 0;
while (*_in++ != '/0')
{
_length++;
}

return _length;
}

int GetSpecialCharCount(const char* _source, const char ch)
{
int _count = 0;

while (*_source++ != '/0')
{
if (*_source == ch)
{
_count++;
}
}

return _count;
}

int FindChar(const char* _source, const char ch, int _position = 0)
{
int ret = 0;
assert(_position >= 0 && _position <= GetLength(_source));

while (_source[_position] != '/0')
{
if (_source[_position] == ch)
{
ret = _position + 1;
break;
}
_position++;
}

return ret;
}

char* CutString(const char* _source, int len, int _position = 0)
{
assert(_position >= 0 && _position <= GetLength(_source));
assert(len >= 0 && len <= GetLength(_source));

char* retStr = new char[len + 1];
memset(retStr, 0, len + 1);
int i = 0;

for (; i < len; ++i)
{
retStr[i] = _source[_position];
_position++;
}

*(retStr + len) = '/0';

return retStr;
}

char* DelString(char* _source, int len)
{
assert(len >= 0 && len <= GetLength(_source));
return (_source + len);
}

int main(void)
{
Stack_Node<char*> s;

char *str = "I am a student";
char *temp = NULL;
int index = 0;
int _count = GetSpecialCharCount(str, ' ');

for (int i = 0; i <= _count ; ++i)
{
index = FindChar(str, ' ');
temp = CutString(str, index);
if (i == _count)
{
temp = str;
}
s.Push(temp);
str = DelString(str, index);
}

cout << s << '/n';
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: