YTU 2631: B1 能存各种类型数据的Store类
2631: B1 能存各种类型数据的Store类
时间限制: 1 Sec 内存限制: 128 MB
提交: 245 解决: 177
题目描述
输入
输出
样例输入
240 56.7183
样例输出
240
56.72
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
template<class T>
class Store
{
private:
T item; //用于存放任意类型的数据
int haveValue; //用于标记item是否为空,0表示为空,1表示有数据
public:
Store(); //默认构造构造函数
T getElem(); //提取数据,返回item的值
void putElem(T x);//存入数据
};
template<class T>
Store<T>::Store(void):haveValue(0) {};
template<class T> //提取数据函数的实现,返回item中的数据
T Store<T>::getElem(void)
{
if (haveValue==0) //如果试图提取未初始化的数据,则终止程序
{
cout<<"NO item present!\n";
exit(1);
}
return item;
}
template<class T>//存入数据的实现
void Store<T>::putElem(T x)
{
haveValue=1;
item = x;
}
int main()
{
Store<int> si;
Store<double> sd;
int i;
double d;
cin>>i>>d;
si.putElem(i);
sd.putElem(d);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<si.getElem()<<endl;
cout<<sd.getElem()<<endl;
return 0;
}
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
template<class T>
class Store
{
private:
T item; //用于存放任意类型的数据
int haveValue; //用于标记item是否为空,0表示为空,1表示有数据
public:
Store(); //默认构造构造函数
T getElem(); //提取数据,返回item的值
void putElem(T x);//存入数据
};
template<class T>
Store<T>::Store(void):haveValue(0) {};
template<class T> //提取数据函数的实现,返回item中的数据
T Store<T>::getElem(void)
{
if (haveValue==0) //如果试图提取未初始化的数据,则终止程序
{
cout<<"NO item present!\n";
exit(1);
}
return item;
}
template<class T>//存入数据的实现
void Store<T>::putElem(T x)
{
haveValue=1;
item = x;
}
int main()
{
Store<int> si;
Store<double> sd;
int i;
double d;
cin>>i>>d;
si.putElem(i);
sd.putElem(d);
cout <<setiosflags(ios::fixed)<<setprecision(2);
cout<<si.getElem()<<endl;
cout<<sd.getElem()<<endl;
return 0;
}
YTU 2631: B1 能存各种类型数据的Store类的更多相关文章
- java基础类型数据与String类包装类之间的转换与理解
数据类型转换一般分为三种: 在java中整型,实型,字符型视为简单数据类型,这些数据类型由低到高分别为:(byte,short,char--int-long-float-double) 简单数据类型之 ...
- 根据dateFormatter创建NSDate类型数据
根据dateFormatter 2000-01-01 创建NSDate类型数据 NSDateFormatter *dateFormatter = [NSDate shareDateFormatter] ...
- .SQL Server中 image类型数据的比较
原文:.SQL Server中 image类型数据的比较 在SQL Server中如果你对text.ntext或者image数据类型的数据进行比较.将会提示:不能比较或排序 text.ntext 和 ...
- Cassandra存储time series类型数据时的内部数据结构?
因为我一直想用Cassandra来存储我们的数字电表中的数据,按照之前的文章(getting-started-time-series-data-modeling)的介绍,Cassandra真的 ...
- (工具类)double类型数据运算
package com.flf.util;import java.math.BigDecimal;/** * double类型数据运算 * @author Yancy 2016-12-14 * */p ...
- MySQL varchar类型数据转tinyint类型
在mysql数据库中性别字段以前存的是'男'和'女',使用varchar类型存储的,但是在我mongo库中这个字段使用的是'1'和'0'存储的,在两个库之间的数据转换就很不方便,于是想要统一存储类型, ...
- PHP-----JSOM类型数据
JS里的数据类型 JS里的一种数据类型,JSOM类型数据 JSOM这种数据类型,在使用JS和jquery时经常使用的到,比较重要.用起来比较简单. <title>无标题文档</tit ...
- 整型变量修饰符,char类型数据存储原理,字节数,
//------------------整型变量修饰符 修饰符(int short long longlong signed unsigned)所有修饰符都是用来修整形 int 4short %hd ...
- Java的各类型数据在内存中分配情况详解
1. 有这样一种说法,如今争锋于IT战场的两大势力,MS一族偏重于底层实现,Java一族偏重于系统架构.说法根据无从考证,但从两大势力各自的社区力量和图书市场已有佳作不难看出,此说法不虚,但 ...
随机推荐
- 【spring】jar包详解与模块依赖关系
以spring3.X为例 jar包详解 1. spring-core.jar:包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心: 2. spri ...
- ecshop 修改支持php7 方案
修改方法 http://jsb.php-php.com/2016/05/472/ 修改数据库配置 data/config.php
- layuiAdmin 项目修改
layuiAdmin修改 index.js 修改登录url user/login=>publics/login config.js 修改 name 项目名称, tokenName token字段 ...
- Python基础知识点总结
Python基础知识与常用数据类型 一.Python概述: 1.1.Python的特点: 1.Python是一门面向对象的语言,在Python中一切皆对象 2.Python是一门解释性语言 3.Pyt ...
- mac 文本处理命令分享
mac 文本处理命令分享 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src {background-color ...
- [thrift] thrift基本原理及使用
参考文章RPC 基本原理与 Apach Thrift 初体验 RPC基本原理 RPC(Remote Procedure Call),远程过程调用,大部分的RPC框架都遵循如下三个开发步骤: 1. 定义 ...
- Codeforces 651C Watchmen【模拟】
题意: 求欧几里得距离与曼哈顿距离相等的组数. 分析: 化简后得到xi=xj||yi=yj,即为求x相等 + y相等 - x与y均相等. 代码: #include<iostream> #i ...
- POJ1068 Parencodings 解题报告
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
- 消息队列RabbitMQ使用教程收集
学习应该要系统,最好的方式是看书. RabbitMQ最权威的教程应该参考官方文档. 下面是收集的一些教程: 官方: https://www.rabbitmq.com/getstarted.html h ...
- Servlet实现点击计数器
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/hits-counter.html: 一.Web页面的点击计数器 很多时候,可能有兴趣知道网站的某 ...