⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clasta.cpp

📁 清华钱能C++教材第二版第14章第一题源代码
💻 CPP
字号:
//C++上机题
//By Peter Chan,2008-12-20
/*以STL中栈容器为资源,编程创建一个double栈,压入范围在100.0~200.0
的10个随机浮点数,去掉最后压入的3个数据,将剩下的栈中数据退栈输出;
再创建一个string栈,加入5个好友的名字,按相反的顺序输出。*/
//-----------------------------------
#include <iostream>
#include <stack>
#include <cmath>
#include <vector>
#include <string>
using namespace std;
//------------------------------------
stack <double> dsta;//创建double型stack类型资源dsta
stack <string> ssta;//创建string型stack类型资源ssta
int main()
{
	//double d[10]={100.5,110.0,120.0,130.0,140.0,150.0,160.0,170.0,180.0,198.0};
	vector<double> d;
	//随机产生10个浮点数
	for(int i=0;i<9;i++)
	{
	 d.push_back((double)rand());
	}
	//创建数组存储朋友的名字
	string s[5]={"Peter","Andy","Tony","Tom","Ann"};
//-------------------------------------
//double型数据元素进栈
     dsta.push(d[0]);
     dsta.push(d[1]);
     dsta.push(d[2]);
	 dsta.push(d[3]);
	 dsta.push(d[4]);
	 dsta.push(d[5]);
	 dsta.push(d[6]);
	 dsta.push(d[7]);
	 dsta.push(d[8]);
	 dsta.push(d[9]);
//------------------------------------
//去掉最后压入的3个数据,让它们出栈
	 dsta.pop();
	 dsta.pop();
	 dsta.pop();
//------------------------------------
//前7个元素出栈输出,每出一个数据,注意将该数据退栈
	 cout<<"---double stack---"<<endl;
	 cout<<dsta.top()<<" ";
	 dsta.pop();
	 cout<<dsta.top()<<" ";
	 dsta.pop();
	 cout<<dsta.top()<<" ";
	 dsta.pop();
	 cout<<dsta.top()<<" ";
	 dsta.pop();
	 cout<<dsta.top()<<" ";
	 dsta.pop();
	 cout<<dsta.top()<<" ";
	 dsta.pop();
	 cout<<dsta.top()<<endl;
//------------------------------------
//string型数据元素进栈
	 ssta.push(s[0]);
     ssta.push(s[1]);
     ssta.push(s[2]);
     ssta.push(s[3]);
     ssta.push(s[4]);
	 cout<<"-------------------"<<endl;
//-------------------------------------
//string型数据元素出栈
	 cout<<"---string stack---"<<endl;
	 cout <<ssta.top() <<" ";
	 ssta.pop();
	 cout <<ssta.top() <<" ";
	 ssta.pop();
	 cout <<ssta.top() <<" ";
	 ssta.pop();
	 cout <<ssta.top() <<" ";
	 ssta.pop();
	 cout <<ssta.top() <<endl;
       return 0;
}
	 
	 



	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -