Problem C: 整数的封装
来源:互联网 发布:网络电信诈骗宣传片 编辑:程序博客网 时间:2024/04/28 06:09
Description
现在,请编写一个Integer类,将整数封装起来。目前,只需要你来实现最基本的功能:
1. 具有2个构造函数:
(1)Integer::Integer(int):根据参数构建一个整数对象。
(2)Integer::Integer(char*, int):根据给定的字符串和进制来构建一个整数对象。
2. 具有一个int Integer::getValue()方法,用于返回Integer类中所封装的整数的具体数值。
Input
输入分为多行。
第一行是一个正整数M,表示其后面的M行为M个整数,每行一个整数。
第M+2行是一个正整数N,表示其后有N行。每行由利用一个空格隔开的2部分组成:前半部分是一个字符串,后半部分是该字符串所使用的进制。
注意:
1. 所有的输入,均在int类型的表示范围内,且所有的输入均为合法输入。
2. 利用0~9和a~z可最大可以表示36进制的数值。
Output
输出为M+N行,每行为一个十进制整数,且输出顺序应与输入顺序相同。
Sample Input
2999-199940111 21a 16z 36a 16
Sample Output
999-19997263510
HINT
Append Code
append.cc,
l
#include <iostream>#include <cstring>using namespace std;class Integer{private: int n;public: Integer(int _n) { n = _n; } Integer(char* s,int t) { int len = strlen(s); int flag = 1; int sum = 0; for(int i = 0;i < len;i++) { if(s[i] == '-') { flag = -1; continue; } if(s[i] >= 'A' && s[i] <= 'Z') { s[i] += 32; } if(isalnum(s[i])) { sum *= t; if(isdigit(s[i])) { sum += (s[i] -'0'); } else { sum += (s[i] - 'a' + 10); } } } n = sum * flag; } int getValue() { return n; }};int main(){ char str[100]; int numOfData, numOfStr; int data, i, radix; cin>>numOfData; for (i = 0; i < numOfData; i++){ cin>>data; Integer anInteger(data); cout<<anInteger.getValue()<<endl; } cin>>numOfStr; for (i = 0; i < numOfStr; i++){ cin>>str>>radix; Integer anInteger(str,radix); cout<<anInteger.getValue()<<endl; } return 0;}
0 0
- Problem C: 整数的封装
- Problem C: 整数的封装
- Problem C: 整数的封装
- Problem C: 整数的封装
- SDUSTOJ Problem G: 整数的封装(该题目要注意尽量不要使用pow函数)
- Problem C: C语言习题 整数排序
- Problem C: C语言习题 整数排序
- Problem C: C语言习题 整数排序
- Problem C: C语言习题 整数排序
- HDOJ 1002 A + B Problem II 大整数相加高效率版的C语言实现
- Problem A: C语言习题5.9--整数n和m之间的素数个数
- Problem B: 大整数的加法运算
- Problem I: 大整数的加法运算
- Problem A: 大整数的加法运算
- Problem B: 字符类的封装
- Problem D: 字符类的封装
- Problem D: 字符类的封装
- Problem D: 字符类的封装
- uva 11082Matrix Decompressing
- 关于正则表达式的一些知识和函数总结
- AspNetPager+mysql+数据集+ObjectDataSource+ListView 做的翻页
- LeetCoe:Majority Element
- laravel5.1启动详解
- Problem C: 整数的封装
- 基础练习 时间转换
- 热图的绘制
- mysql结果集
- cache与buffer的差别
- ios target -- weight todayWeight 开发
- CentOS6.5_64下 nginx+uwsgi+Python +多站点环境搭建
- 产品经理之挂在嘴边的指标和概念-全栈工程师熊盼
- LeetCode Algorithms 34. Search for a Range