Am not sure it is a good idea to embed the integer as a string. Aside from the inefficiency of storing a 5-digit number as a string, what would be the expected result of adding two IntStr objects together? E.g., "40" + "2" could either be "42" (add), or "402" (append) depending on your intentions. And if you wished to add an IntStr and an integer, such as "40" + 2, then you'd have to convert the embedded string back to an int, perform the addition, and if the sum is less than 100000, then convert the sum back to a string. This is highly inefficient. Personally, I'd embed the integer itself and provide accessors and casts to explicitly convert it to a string on an as-required basis only, since the numeric form is likely to be far more useful to you than the string form.
However, to answer the question as it stands, the following is one possible solution.
#include<iostream>
#include<string>
#include<sstream>
class IntStr
{
private:
std::string m_string;
public:
IntStr():m_string("0") {}
IntStr(const unsigned int decimal):m_string("0")
{
set_int(decimal);
}
IntStr& operator=( const unsigned int decimal )
{
return( set_int( decimal ));
}
std::string get_int()const{ return(m_string); }
IntStr& set_int(const unsigned int decimal)
{
if( decimal<100000 )
{
std::stringstream ss;
ss<<decimal;
m_string = ss.str();
}
return( *this );
}
};
int main()
{
IntStr a; // default constructor.
a = 42; // assignment operator.
IntStr b = a; // copy constructor.
IntStr c(1000); // user-defined constructor.
IntStr d(100000); // overflow check.
std::cout<<"a = "<<a.get_int().c_str()<<std::endl;
std::cout<<"b = "<<b.get_int().c_str()<<std::endl;
std::cout<<"c = "<<c.get_int().c_str()<<std::endl;
std::cout<<"d = "<<d.get_int().c_str()<<std::endl;
}
Example output:
a = 42
b = 42
c = 1000
d = 0
Some smart compilers will not allow you do that. If you managed to do that, there are couple different results, the most predictible is decimal part will be cut, only what left will be shown. If it exceed the amount of memory reserved for int (double > float > int), it will show the maximum or positive or negative value for int type, or it will shifted by some amount from the maximum or minimum.
That varies from each programming language. As a matter of fact, many languages do not put a limit on the maximum size of a variable. It will handle any string, integer, resource, pointer, or other type size, as long as it fits into the memory of the machine running the process.
The smallest value is -32,768 and the maximum is 32,767
if it is a signed int the the range is -32768 to 32767if its unsigned then 0 to 65535
The maximum number of elements will depend on the type of array and the available memory. An array of char requires only 1 byte per element but an array of pointers requires 4 bytes per element (8 bytes on 64-bit systems). Arrays of objects or structures would likely require more memory per element.For all practical purposes, the maximum size is 2,147,483,647 elements, which is the maximum positive range for a 4-byte integer (0x7FFFFFFF). At 1 byte per element, that works out at 2GB.
The following algorithm works for any number of integers: Assume the first number is the maximum - maximum = (first number). Compare your assumed maximum with the second number. If the second number is larger than the assumed maximum, replace the old assumed maximum with the second number. Repeat for the third number, for the fourth, etc. - always copying the nth. element to the assumed maximum if you find one that is larger than your previous maximum.
An integer is any whole number. For example, 4, 16, -8 are all integers. There is no decimal part. Obviously, there is no "maximum number" as these numbers continue up to infinity.
Some smart compilers will not allow you do that. If you managed to do that, there are couple different results, the most predictible is decimal part will be cut, only what left will be shown. If it exceed the amount of memory reserved for int (double > float > int), it will show the maximum or positive or negative value for int type, or it will shifted by some amount from the maximum or minimum.
An unsigned integer cannot be negative. It has a maximum positive value twice that of a signed integer. Max signed: 128 Max signed: 256 I could be off by one there, though.
A 5-bit binary counter, interpreted as an unsigned integer, has a range of 0 to 31. Interpreted as a two's complement signed integer, it has a range of -16 to +15.
A maximum cash stack is 2,147,483,647 gp. It only goes to this because it is the largest positive value of a signed integer on a 32 bit operating system. See the related links for a Wikipedia article for more information on it.
3 perfect scores.
minimum is '0' and maximum cannot be defined
Since you mention two's complement, we have to assume that you are dealing with positive and negative numbers which means that the highest order bit is the sign bit. So the maximum positive number would be hex 7F which is equivalent to 127 and the maximum negative number would be -128
An infinite amount of numbers can theoretically be placed after the decimal point.
Four billion, 4294967296, is the maximum integer that can be stored into a 32 bit integer variable.
That varies from each programming language. As a matter of fact, many languages do not put a limit on the maximum size of a variable. It will handle any string, integer, resource, pointer, or other type size, as long as it fits into the memory of the machine running the process.