Solved

integer interval variable

Posted on 1998-09-12
16
322 Views
Last Modified: 2008-03-04
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
Comment
Question by:juanc
  • 8
  • 7
16 Comments
 
LVL 1

Accepted Solution

by:
mlaiosa earned 200 total points
ID: 1252675
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
 

Author Comment

by:juanc
ID: 1252676
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
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1252677
#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
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1252678
#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
 

Author Comment

by:juanc
ID: 1252679
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
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1252680
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
 

Author Comment

by:juanc
ID: 1252681
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
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1252682
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:juanc
ID: 1252683
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
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1252684
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
 

Author Comment

by:juanc
ID: 1252685
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
 

Author Comment

by:juanc
ID: 1252686
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
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1252687
You indicate that you are not using GCC to compile.   Compile with g++ file.cc (or gpp file.cc depending on you port)
0
 

Author Comment

by:juanc
ID: 1252688
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
 
LVL 1

Expert Comment

by:mlaiosa
ID: 1252689
    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
 

Expert Comment

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

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now