• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

integer interval variable

I want to create a integer variable v in such a way that v olny take values between two integer constants.
Is there any way way of doing it in
      a) ansi c?
      b) c++?
thanks.
Juan Carlos Ruiz Gomez.
0
juanc
Asked:
juanc
  • 8
  • 7
1 Solution
 
mlaiosaCommented:
You could make a class and overload some operators (C++):

class Cint
 {
  private:
   int max,min,n;
  public:
   Cint(int a, int b) { max=a, min=b; };
   ~Cint();

   operator int() { return n; };
   int operator ==( int i ) { return n==i; };
   int operator =( int i ) { if (i <= max && i >= min) n=i, return n };
 }

this is a start.  You may need to overload some more operators, if you need more help, just say so.
 }
0
 
juancAuthor Commented:
Using the class you proppossed above, what should be the syntax to get the following effect?:
(the interval limits)
const int min1=mn1,min2=mn2,...,minN=mnN;
const int max1=mx1,max2=mx2,...,maxN=mxN;
                              (mnI,mxI : some integer constants)

(type definition)
range1 = min1..max1;
range2 = min2..max2;
.
rangeN = minN..maxN;

(variable declaration)
range1 a1,b1,c1;
range2 a2,b2,c2;
.
rangeN aN,bN,cN;

 
thanks,
Juan Carlos Ruiz Gomez
0
 
mlaiosaCommented:
#include <stdio.h>

//the code from above here

void main ()
 {
  Cint i(1,10);   //i will now have a limited range of 1..10
  i=7;
  printf("\ni == ", i);
 }


also, if you want somethign to have a range from 0..2^x-1, you could say:
unsigned int i:x;
0
Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

 
mlaiosaCommented:
#include <stdio.h>

//the code from above here

void main ()
 {
  Cint i(1,10);   //i will now have a limited range of 1..10
  i=7;
  printf("\ni == ", i);
 }


also, if you want somethign to have a range from 0..2^x-1, you could say:
unsigned int i:x;
0
 
juancAuthor Commented:
when i compile the following code:

#include <stdio.h>
class Cint
{
     private:
           int max,min,n;
     public:
           Cint(int a, int b) { max=a, min=b; };
      ~Cint();

      operator int() { return n; };
      int operator ==( int i ) { return n==i; };
      int operator =( int i ) { if (i <= max && i >= min) n=i, return n };
    }
}
void main(void)
{
      const int mn=2,mx=7;
      Cint iv(mn,mx);
      iv = 4;
      printf("\ni ==",iv);
}      




I got the following message:

rango.c++: In method `int Cint::operator =(int)':
rango.c++:12: parse error before `return'
rango.c++:12: confused by earlier errors, bailing out


0
 
mlaiosaCommented:
Ok, for the first time, I actually tried this code.  Fixed the problems and made it a little better.

with a Cint, you can use the ==, =, +=, -=, *= and /= operators.  It sould work for everything else, but if you need more help, that's no problem.

#include <stdio.h>

class Cint
 {
  private:
   int max,min,n;
  public:
   Cint(int a, int b) { max=b, min=a; };
   ~Cint() {};


   unsigned fix();

   operator int&() { return n; };
   int operator ==( int i ) { return n==i; };
   int operator =( int i );
   int operator +=( int i );
   int operator -=( int i );
   int operator *=( int i );
   int operator /=( int i );
 };

unsigned Cint::fix()
 {
  if (n > max)
   n=max;
  else if (n < min)
   n=min;
 }

int Cint::operator =( int i )
 {
  if (i <= max && i >= min)
   n=i;
  return n;
 }

int Cint::operator +=( int i )
 {
  n += i;
  fix();
 }

int Cint::operator -=( int i )
 {
  n -= i;
  fix();
 }

int Cint::operator *=( int i )
 {
  n *= i;
  fix();
 }

int Cint::operator /=( int i )
 {
  n /= i;
  fix();
 }

void main(void)
 {
  const int mn=2,mx=7;
  Cint iv(mn,mx);
  iv = 4;
  printf("\ni == %d\n",(int)iv);  //if you are sending this to printf
                                          //you need to typecast it to an int,
                                          //but not in most cases.
 }

0
 
juancAuthor Commented:
I modified a little main() fuction declaring two Cint variables (iv and iva) and initialized them with 1 and 8. When i ran the program i got iv == 134519032 iva==0. I always got the first (second) value for the first (second) variable I declared.

I was wondering what do I have to do for "typedef'ing" types of Cint variables, (for example tmm a type of Cint variable with values from m to n) and the way of constructing arrays of those type of variables. This could be my following question :)

thanks
J.C. Ruiz.

0
 
mlaiosaCommented:
remember that if you send them to printf, you need to typecast them as (int), boefore they have values assigned, the possibilites are random.

What compiler do you use?

ok, here is an arrary of Cint, with a range of 1..5

Cint array[10](1,5)
I thnk that's it, either that or:
Cint array(1,5)[10]

Im not sure what you mean by 'typedef-ing'.  
0
 
juancAuthor Commented:
What i mean with "typedef-ing" in the above context is to use the "typedef" keyword for defining a new type of (Cint) variables. Then when I do that I could declare some (for example, array) variables of this type. By the way neither
      Cint array[10](1,5)
nor
      Cint array(1,5)[10]
works under
      gcc version egcs-2.90.29 980515 (egcs-1.0.3 release)
for declaring arrays (i got this version by typing: g++ -v)
(My pc has Linux 2.0.34 (Slackware 3.5))

0
 
mlaiosaCommented:
I've talked with some coluges, and that "typedef-ing" doesnt seem to be practicle.  Here is how you can define the array:

Cint * iv = new Cint[10](2,7);  //create an array of Cint's with bounds 2 and 7

I use gcc 2.7.2.1      I use win98 and the DJGPP port of GCC
0
 
juancAuthor Commented:
i get the messages
 c++ inter.c++ -o i
inter.c++: In function `int main(...)':
inter.c++:70: no matching function for call to `Cint::Cint (int)'
inter.c++:21: candidates are: Cint::Cint(const Cint &)
inter.c++:8:                 Cint::Cint(int, int)

when i compile (c++ inter.c++ -o i) the following code:
     #include <stdio.h>

     class Cint
      {
       private:
        int max,min,n;
       public:
        Cint(int a, int b) { max=b, min=a; };
        ~Cint() {};


        unsigned fix();

        operator int&() { return n; };
        int operator ==( int i ) { return n==i; };
        int operator =( int i );
        int operator +=( int i );
        int operator -=( int i );
        int operator *=( int i );
        int operator /=( int i );
      };

     unsigned Cint::fix()
      {
       if (n > max)
        n=max;
       else if (n < min)
        n=min;
      }

     int Cint::operator =( int i )
      {
       if (i <= max && i >= min)
        n=i;
       return n;
      }

     int Cint::operator +=( int i )
      {
       n += i;
       fix();
      }

     int Cint::operator -=( int i )
      {
       n -= i;
       fix();
      }

     int Cint::operator *=( int i )
      {
       n *= i;
       fix();
      }

     int Cint::operator /=( int i )
      {
       n /= i;
       fix();
      }

     void main(void)
      {
       const int mn=2,mx=7,t=10;
         Cint *iv = new Cint[t](mn,mx);
      }

0
 
juancAuthor Commented:
i get the messages:

inter.c++: In function `int main(...)':
inter.c++:70: no matching function for call to `Cint::Cint (int)'
inter.c++:21: candidates are: Cint::Cint(const Cint &)
inter.c++:8:                 Cint::Cint(int, int)

when i compile (c++ inter.c++ -o i) the following code:

     #include <stdio.h>

     class Cint
      {
       private:
        int max,min,n;
       public:
        Cint(int a, int b) { max=b, min=a; };
        ~Cint() {};


        unsigned fix();

        operator int&() { return n; };
        int operator ==( int i ) { return n==i; };
        int operator =( int i );
        int operator +=( int i );
        int operator -=( int i );
        int operator *=( int i );
        int operator /=( int i );
      };

     unsigned Cint::fix()
      {
       if (n > max)
        n=max;
       else if (n < min)
        n=min;
      }

     int Cint::operator =( int i )
      {
       if (i <= max && i >= min)
        n=i;
       return n;
      }

     int Cint::operator +=( int i )
      {
       n += i;
       fix();
      }

     int Cint::operator -=( int i )
      {
       n -= i;
       fix();
      }

     int Cint::operator *=( int i )
      {
       n *= i;
       fix();
      }

     int Cint::operator /=( int i )
      {
       n /= i;
       fix();
      }

     void main(void)
      {
       const int mn=2,mx=7,t=10;
         Cint *iv = new Cint[t](mn,mx);
      }

0
 
mlaiosaCommented:
You indicate that you are not using GCC to compile.   Compile with g++ file.cc (or gpp file.cc depending on you port)
0
 
juancAuthor Commented:
The results do not change when i compile the code with c++ or g++ or gcc and using any of the extensions .c++, .cpp, .cxx, .C or .cc for the source file

0
 
mlaiosaCommented:
    class Cint
             {
              private:
               int max,min,n;
              public:
               Cint(int a, int b) { max=b, min=a; };
               ~Cint() {};

                .......

Try changing that to:

     class Cint
             {
              private:
               int max,min,n;
              public:
               Cint(int a, int b) { max=b, min=a; };
               Cint() { max = 32000, min = -32000; };    //this is the new line
               ~Cint() {};


0
 
streamer102198Commented:
Have you considered using the enum-type ?
Isn't this what you really want, and in a very easy way?

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now