几道笔试题的解法(四)
来源:互联网 发布:淘宝介入会打电话吗 编辑:程序博客网 时间:2024/05/01 16:57
题目: 编写一个单词逆序输出的算法。例如:“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'; }
- 几道笔试题的解法(四)
- 几道笔试题的解法(一)
- 几道笔试题的解法(二)
- 几道笔试题的解法(三)
- 几道笔试题的解法(五)
- 几道笔试题的解法(六)
- 几道笔试题的解法(七)
- 几道笔试题的解法(八)
- 一个笔试题的递归解法
- 一道笔试题的搞笑解法
- 几道笔试题
- 几道笔试题
- 几道笔试题
- 几道笔试题
- 几道笔试题
- 几道笔试题
- 几道笔试题
- MSN CDC的几道笔试题
- Teachmyself进度3
- Linux TCP/IP协议栈剖析【体系结构篇】
- Collection的toArray()使用上需要注意的地方
- 指针与引用的区别
- 宽带连接错误691,623,678......
- 几道笔试题的解法(四)
- 程序开发中版本管理之命名规则及格式
- Java编程思想学习笔记——并发
- struts 2获取Action带的参数
- Word2007 从第x页码开始设置页码
- 职场英语三--道歉与致谢
- 矮于屋檐的幸福
- 关于IT中的时间表示
- 【转】我从11楼跳下去……才知道....