[英]C++ MyInteger Class with Overload [ ] operator so index returns digit in position i
The problem I have 我有问题
Define a class named MyInteger that stores an integer and has functions to get and set the intergers value. 定义一个名为MyInteger的类,该类存储一个整数,并具有获取和设置整数值的功能。 Then, overload the [] operator so that the index returns the digit in position i, where i = 0 is the least-significant digit.
然后,使[]运算符重载,以便索引返回位置i处的数字,其中i = 0是最低有效数字。 If no such digit exists then -1 should be returned.
如果不存在这样的数字,则应返回-1。
I understand the first part but I don't understand how to implement the second part, 我了解第一部分,但不了解如何实施第二部分,
Then, overload the [] operator so that the index returns the digit in position i, where i = 0 is the least-significant digit.
然后,使[]运算符重载,以便索引返回位置i处的数字,其中i = 0是最低有效数字。 If no such digit exists then -1 should be returned.
如果不存在这样的数字,则应返回-1。
My code is such: 我的代码是这样的:
#include <iostream>
using namespace std;
class MyInteger{
private:
int integer;
public:
MyInteger(int bInteger = 0)
{
integer = bInteger;
}
int getInteger(){ return integer; }
void setInteger(int bInteger){ integer = bInteger; }
void operator[](int x) //???
{
}
};
int main()
{
cout << "Enter an integer " << endl;
//cin >>
}
As i understand the second part you have to implement the operator to return a single digit. 据我了解的第二部分,您必须实现运算符以返回一位数字。 In a way that given
以某种方式
MyInteger x;
x.set(43210)
int a = x[0];
int b = x[1];
int c = x[5];
a should equal 0, b should equal 1 and c should equal -1. a应该等于0,b应该等于1,c应该等于-1。 To get the digits you could use modulo arithmetic:
要获取数字,可以使用模运算:
To get the first digit (index 0) of any integer "i" just return i%10. 要获取任何整数“ i”的第一位(索引0),只需返回i%10。 To get the second digit (index 1)
获取第二个数字(索引1)
int result = ((i - (i%10)) % 100) / 100
for the 2nd index 对于第二个索引
int result = ((i - (i%10) - (i%100)) % 1000) / 1000
This may be formulated more elegently with a for loop. 可以使用for循环更优雅地制定公式。
对于十进制数字,您的类应返回与位置(索引)相对应的十进制数字,例如,如果MyInteger m = 12347,则运算符m [0]将返回7,m [3]将返回2,而m [6]将返回-1。
For your first part of the question, I propose you the following: 对于问题的第一部分,我建议您采取以下措施:
int operator[](int x)
{
int ten;
for (ten = 10; x; x--)
ten *= 10;
int digit = (integer%ten) / (ten / 10);
return digit;
}
By the way, to ease tests, I propose you to define the following to permit using cin on MyInteger
: 顺便说一句,为了简化测试,我建议您定义以下内容以允许在
MyInteger
上使用cin:
istream& operator>> (istream& is, MyInteger& x)
{
int v;
is >> v;
x.setInteger(v);
return is;
}
For the second part , If no such digit exists then -1 should be returned. 对于第二部分 , 如果不存在这样的数字,则应返回-1。 , addresses the "out of bound" that you could have.
,解决了您可能拥有的“界限”。 For example, if you'd have x = 21 and you'd do x[3], you'd return 0 with the programme above whereas in reality there is no fourth digit.
例如,如果您有x = 21并执行x [3],则上面的程序将返回0,而实际上没有第四位数字。
So you could change the last instruction of operator[]
as follows: 因此,您可以按以下方式更改
operator[]
的最后一条指令:
return integer>=ten/10 ? digit:-1;
I can share you my code. 我可以分享你的代码。 But i not sure if it is best to solve your problem.
但是我不确定是否最好解决您的问题。
#include<iostream> #include<cmath> using namespace std; class MyInteger { public: MyInteger(int integer); MyInteger(); int operator[](int index); void set(int integer); int get(); private: int integer; }; int main() { MyInteger integer; integer.set(123456); cout<<"MyInteger: "<<integer.get()<<endl; int i; /*position*/ cout<<"Enter the reference from zero to fine digit>>"; cin>>i; if(integer[i]==-1) cout<<"The integer doesn't exit.\\n"; else cout<<"The digit is: "<<integer[i]; system("pause"); return 0; } MyInteger::MyInteger(int integer) { this->integer=integer; } MyInteger::MyInteger() { this->integer=0; } int MyInteger::operator[](int index) { /*檢測是否在範圍內*/ int size=0; int replacement=this->integer; do { replacement=replacement/10; size++; }while(replacement!=0); if(index>=size) { cout<<"It is not in range!\\n"; return -1; } /*找出位置上的digit*/ int target=0; int head=pow(10.0,index+1); /*去頭用*/ target=(this->integer)-((this->integer)-(this->integer)%head); int foot=pow(10.0,index); /*去尾用*/ target=target/foot; return target; } void MyInteger::set(int integer) { if((integer-(int)integer)==0) this->integer=integer; else { cout<<"It is not a intger!!"; exit(1); } } int MyInteger::get() { return this->integer; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.