6.1-数据结构算法-堆栈与队列
#include <iostream> #include <fstream> using namespace std; int main (void) {ofstream ofs ("format.txt");if (! ofs) {perror ("打开文件失败");return -1;}ofs << 1234 << ' ' << 56.78 << ' ' << "tarena"<< '\n';ofs.close ();ofs.open ("format.txt", ios::app);if (! ofs) {perror ("打开文件失败");return -1;}ofs << "append_a_line\n";ofs.close ();ifstream ifs ("format.txt");if (! ifs) {perror ("打开文件失败");return -1;}int i;double d;string s1, s2;ifs >> i >> d >> s1 >> s2;cout << i << ' ' << d << ' ' << s1 << ' '<< s2 << endl;ifs.close ();return 0; }
istack.cpp
#include <iostream> using namespace std; // 基于链式表的堆栈 class Stack { public:// 构造过程中初始化为空堆栈Stack (void) : m_top (NULL) {}// 析构过程中销毁剩余的节点~Stack (void) {for (Node* next; m_top; m_top = next) {next = m_top->m_next;delete m_top;}}// 压入void push (int data) {/*Node* node = new Node;node->m_data = data;node->m_next = m_top;m_top = node;*/m_top = new Node (data, m_top);}// 弹出int pop (void) {if (empty ())throw UnderFlow ();int data = m_top->m_data;Node* next = m_top->m_next;delete m_top;m_top = next;return data;}// 判空bool empty (void) const {return ! m_top;} private:// 下溢异常class UnderFlow : public exception {const char* what (void) const throw () {return "堆栈下溢!";}};// 节点class Node {public:Node (int data = 0, Node* next = NULL) :m_data (data), m_next (next) {}int m_data; // 数据Node* m_next; // 后指针 };Node* m_top; // 栈顶 }; int main (void) {try {Stack stack;for (int i = 0; i < 10; ++i)stack.push (i);while (! stack.empty ())cout << stack.pop () << endl; // stack.pop (); }catch (exception& ex) {cout << ex.what () << endl;return -1;}return 0; }
putget.cpp
#include <iostream> #include <fstream> using namespace std; int main (void) {ofstream ofs ("putget.txt");if (! ofs) {perror ("打开文件失败");return -1;}for (char c = ' '; c <= '~'; ++c)if (! ofs.put (c)) {perror ("写入文件失败");return -1;}ofs.close ();ifstream ifs ("putget.txt");if (! ifs) {perror ("打开文件失败");return -1;}char c;while ((c = ifs.get ()) != EOF)cout << c;cout << endl;if (! ifs.eof ()) {perror ("读取文件失败");return -1;}ifs.close ();return 0; }
seek.cpp
#include <iostream> #include <fstream> using namespace std; int main (void) {fstream fs ("seek.txt", ios::in | ios::out);if (! fs) {perror ("打开文件失败");return -1;}fs << "0123456789";cout << fs.tellp () << endl;cout << fs.tellg () << endl;fs.seekp (-3, ios::cur);fs << "XYZ";fs.seekg (4, ios::beg);int i;fs >> i;cout << i << endl;cout << fs.tellg () << endl;cout << fs.tellp () << endl;fs.seekg (-6, ios::end);fs << "ABC";fs.close ();return 0; }
stack.cpp
#include <iostream> using namespace std; // 基于顺序表的堆栈 class Stack { public:// 构造过程中分配内存Stack (size_t size = 10) :m_array (new int[size]), m_size (size),m_top (0) {}// 析构过程中释放内存~Stack (void) {if (m_array) {delete[] m_array;m_array = 0;}}// 压入void push (int data) {if (full ())throw OverFlow ();m_array[m_top++] = data;}// 弹出int pop (void) {if (empty ())throw UnderFlow ();return m_array[--m_top];}// 判满bool full (void) const {return m_top >= m_size;}// 判空bool empty (void) const {return ! m_top;} private:// 上溢异常class OverFlow : public exception {const char* what (void) const throw () {return "堆栈上溢!";}};// 下溢异常class UnderFlow : public exception {const char* what (void) const throw () {return "堆栈下溢!";}};int* m_array; // 数组size_t m_size; // 容量size_t m_top; // 栈顶 }; void printb (unsigned int numb, int base) {Stack stack (256);do {stack.push (numb % base);} while (numb /= base);while (! stack.empty ()) {int digit = stack.pop ();if (digit < 10)cout << digit;elsecout << char (digit - 10 + 'A');}cout << endl; } int main (void) {try {Stack stack;for (int i = 0; ! stack.full (); ++i)stack.push (i); // stack.push (0);while (! stack.empty ())cout << stack.pop () << endl; // stack.pop (); }catch (exception& ex) {cout << ex.what () << endl;return -1;}cout << "输入一个整数:" << flush;int numb;cin >> numb;cout << "您想看几进制:" << flush;int base;cin >> base;cout << "结果:";printb (numb, base);return 0; }
stream.cpp
#include <iostream> #include <iomanip> #include <cmath> #include <fstream> #include <sstream> using namespace std; int main (void) {cout << sqrt (2) << endl;cout.precision (10); // 10位有效数字cout << sqrt (2) << endl;cout << sqrt (2) * 100 << endl;cout << setprecision (5) << sqrt (2) << endl<< sqrt (2) * 100 << endl;cout << "当前精度:" << cout.precision ()<< endl;cout << setprecision (2) << 1.24 << ' ' << 1.25<< ' ' << 1.26 << endl;cout << showbase << hex << 127 << endl;cout << oct << 127 << endl;cout << dec << 127 << endl;cout << noshowbase << hex << 127 << dec << endl;cout << setw (12) << 127 << 721 << endl;cout << setfill ('$') << left << setw (12)<< 127 << endl;cout.precision (10);cout.setf (ios::scientific);cout << sqrt (2) << endl;cout.setf (ios::fixed);cout << sqrt (2) << endl;cout << 12.00 << endl;cout << showpoint << 12.00 << endl;cout << noshowpoint << 12.00 << endl;ifstream ifs ("stream.txt");ifs.unsetf (ios::skipws);char c;while (ifs >> c)cout << c;ifs.setf (ios::skipws);ifs.clear (); // 复位 ifs.seekg (ios::beg);while (ifs >> c)cout << c;ifs.close ();cout << endl;int i = 1234;double d = 56.78;string s = "tarena";ostringstream oss;oss << i << ' ' << d << ' ' << s;string str = oss.str ();cout << str << endl;str = "hello 3.14 pai";istringstream iss;iss.str (str);iss >> s >> d >> str;cout << s << ' ' << d << ' ' << str << endl;return 0; }
write.cpp
#include <iostream> #include <fstream> #include <cstring> using namespace std; class Dog { public:Dog (const string& name = "", int age = 0) :m_age (age) {strcpy (m_name, name.c_str ());}void print (void) const {cout << m_name << "," << m_age << endl;} private:char m_name[128];int m_age; }; int main (void) {ofstream ofs ("dog.dat");Dog dog ("小白", 25);ofs.write ((char*)&dog, sizeof (dog));ofs.close ();ifstream ifs ("dog.dat");Dog dog2;ifs.read ((char*)&dog2, sizeof (dog2));dog2.print ();ifs.close ();return 0; }
xor.cpp
#include <iostream> #include <fstream> #include <stdexcept> #include <cstdlib> using namespace std; #define BUFSIZE (1024*10) int _xor (const char* src, const char* dst,unsigned char key) {ifstream ifs (src, ios::binary);if (! ifs) {perror ("打开源文件失败");return -1;}ofstream ofs (dst, ios::binary);if (! ofs) {perror ("打开目标文件失败");return -1;}char* buf = NULL;try {buf = new char[BUFSIZE];}catch (bad_alloc& ex) {cout << ex.what () << endl;return -1;}while (ifs.read (buf, BUFSIZE)) {for (size_t i = 0; i < BUFSIZE; ++i)buf[i] ^= key;if (! ofs.write (buf, BUFSIZE)) {perror ("写入文件失败");return -1;}}if (! ifs.eof ()) {perror ("读取文件失败");return -1;}for (size_t i = 0; i < ifs.gcount (); ++i)buf[i] ^= key;if (! ofs.write (buf, ifs.gcount ())) {perror ("写入文件失败");return -1;}delete[] buf;ofs.close ();ifs.close ();return 0; } int enc (const char* plain, const char* cipher) {srand (time (NULL));unsigned char key = rand () % 256;if (_xor (plain, cipher, key) == -1)return -1;cout << "密钥:" << (unsigned int)key << endl;return 0; } int dec (const char* cipher, const char* plain,unsigned char key) {return _xor (cipher, plain, key); } int main (int argc, char* argv[]) {if (argc < 3) {cerr << "用法:" << argv[0]<< " <明文文件> <密文文件>" << endl;cerr << "用法:" << argv[0]<< " <密文文件> <明文文件> <密钥>"<< endl;return -1;}if (argc < 4)return enc (argv[1], argv[2]);elsereturn dec (argv[1], argv[2],atoi (argv[3]));return 0; }
转载于:https://www.cnblogs.com/xuxaut-558/p/10018349.html
6.1-数据结构算法-堆栈与队列相关推荐
- 数据结构之堆栈与队列
堆栈与队列是两种重要的基础数据结构,一个是先入后出,一个是先入先出,有着广泛的应用,本文分别使用数组与链表实现堆栈与队列 顺序存储方式实现堆栈 #define MaxSize 20 #define E ...
- C# 数据结构 之 堆栈和队列
堆栈(Stack)是一种特殊的线性表,是一种操作只允许在尾端进行插入或删除等操作的线性表.表尾允许进行插入删除操作,称为栈顶(Top),另一端是固定的,称为栈底(Bottom).栈的操作使按照先进后出 ...
- python堆栈与队列_python语言的堆栈与队列类的实现
基于python语言的数据结构之堆栈与队列的实现 # 堆栈的实现 # -*- coding: utf-8 -*- """ 栈(stack), 是一种容器,可以存入数据元素 ...
- 数据结构与算法--4.使用堆栈模拟队列
问题: 队列的插入和删除遵循先入先出的原则,而堆栈遵循后进先出的原则. 用两个堆栈模拟队列,要求实现时不能分配超过O(1)的内存,时间复杂度必须是o(m). 思路: 用两个堆栈模拟队列,必须要支持两种 ...
- 《数据结构与算法基础 严蔚敏版》第三章 堆栈与队列
链接:https://pan.baidu.com/s/1z-kFKiNaezO0k2BWQ8l1Hw 提取码:asdf 1.堆栈 (1)堆栈的概念 栈:只允许在一段进行插入或删除操作的线性表 栈顶: ...
- Java LinkedList特有方法程序小解 使用LinkedList 模拟一个堆栈或者队列数据结构。...
package Collection;import java.util.LinkedList;/* LinkedList:特有的方法 addFirst()/addLast(); getFirst()/ ...
- 算法与数据结构(part5)--栈与队列
学习笔记,仅供参考,有错必纠 文章目录 算法与数据结构–基于python 栈 栈是什么 栈(ADT)的操作 栈的实现 队列 队列是什么 队列的操作 队列的实现 双端队列 双端队列是什么 双端队列的操作 ...
- linux 复制栈数据结构,算法-数据结构-堆栈
算法-数据结构-堆栈 发布时间:2005-09-09 17:46:22来源:红联作者:frog /*************************************************** ...
- 使用LinkedList模拟一个堆栈或者队列数据结构
使用LinkedList模拟一个堆栈或者队列数据结构. 堆栈:先进后出 如同一个杯子. 队列:先进先出 如同一个水管. import java.util.LinkedList;public cla ...
- 数据结构与算法之循环队列的操作
数据结构与算法之循环队列的操作 /* 循环队列的入队和出队算法设计 初始化循环队列 .打印队列.插入元素到循环队列.获取循环队列的首元素,元素不出队.出队.获取循环队列元素个数.判断循环队列的空和满. ...
最新文章
- 小程序之 转发/分享
- 怎样生成分布式情况下的唯一标示?必须包含网卡字段,以便不同机器生成的唯一标示肯定不一样...
- 2、Flume1.7.0入门:安装、部署、及flume的案例
- UniDBGrid增加显示记录数的label及隐藏refresh按钮
- Redis学习总结(8)——Redis常见使用场景总结
- zlib、libzip、 libzippp 库编译(windows + cmake + vs2013)
- 怎么用最短时间高效而踏实地学习Linux?
- C语言九九乘法表的五种输出形式(完整,左上,左下,右上,右下)
- 如何使用@PostConstruct初始化敏感词库和hutool过滤敏感词信息
- 在PSP上玩《大旋风 Twin Hawk》
- JAVA怎么打开pkg_PKG文件在电脑上怎么打开!(普通PC)
- vuetify图标不正常显示
- nlp课程_使用nlp阻止无请求的销售电子邮件的无服务器堆栈中的课程
- java广度优先爬虫示例_广度优先搜索与网络爬虫
- 大话信号与系统 --- 奇文共欣赏
- 一起白piao网页学知识吧
- 在 Jenkins 上轻松重用 Tekton 和 Jenkins X
- 物联网跟人的神经网络相似通过各种信息传感设备
- 完了!服务器沦为肉鸡了!排查过程!
- offsetParent解释