how do I turn this int into a float

This is a discussion on how do I turn this int into a float within the C++ Programming forums, part of the General Programming Boards category; I need to put use the stacks for floats instead of integers but I tried turning every thing from int ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Question how do I turn this int into a float

    I need to put use the stacks for floats instead of integers but I tried turning every thing from int to float but it did not work. Here is my code. Any help will be appreciated.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    class STACK
    {
    private:
      int S[100];//stack
      int Ssize;//stack size
    
    public:
      STACK();//constructor
      void push(int c);//push char onto stack
      int pop(); //remove char from the stack
      int size(); // returns the number of items on the stack
      bool full(); // true if stack is full
    };
    
    STACK::STACK()
    {
      Ssize =0;
    }
    void STACK::push(int c)
    {
      if (Ssize != 100)
        S[Ssize++]=c;
      else
        cout<<"***Stack overflow-program terminated..."<<endl;
     }
    
     int STACK::pop()
     {
       return S[--Ssize];
     }
    
     bool STACK::full()
     {
       if (Ssize==100)
         return true;
       else
         return false;
      }
    
      int STACK::size()
      {
        return Ssize;
      }
    
    
    void main(void)
    {
      int a,b,end;
    
      int i;
      char M[]="(((4+8)/4)*(9-7));";
    
      STACK V,O;
      i=0;
      while (M[i]!=';')
      {
        if (M[i]==')')
        {
          b=V.pop();
          a=V.pop();
          switch (O.pop())
          {
          case 0:
            V.push(a+b);
            break;
          case 1:
            V.push(a-b);
            break;
          case 2:
            V.push(a*b);
            break;
          case 3:
            V.push(a/b);
            break;
          }
        }
        else
          switch(M[i])
          {
            case'0':
              V.push(0);
              break;
    
            case'1':
              V.push(1);
              break;
    
            case'2':
              V.push(2);
              break;
    
            case'3':
              V.push(3);
              break;
    
            case'4':
              V.push(4);
              break;
    
            case'5':
              V.push(5);
              break;
    
            case'6':
              V.push(5);
              break;
    
            case'7':
              V.push(7);
              break;
    
            case'8':
              V.push(8);
              break;
    
            case'9':
              V.push(9);
              break;
    
            case'+':
              O.push(0);
              break;
    
            case'-':
              O.push(1);
              break;
    
            case'*':
              O.push(2);
              break;
    
            case'/':
              O.push(3);
              break;
        }
        i++;
    }
    cout<<V.pop()<<endl;
    cin>>end;
      return 0;
    }
    Last edited by fastlane29; 12-04-2002 at 05:10 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If you change everything to float you will indeed have a problem. You are "switching" on the popped value of your stack. You cannot switch on a floating point number. You will have to do something differetly. You can use a binary tree for your expressions if you want.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, you could do:

    float a;
    switch((int)a){

    or:

    float a;
    int i = (int)a;
    switch(i){
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Question

    I tried changing that but now it acts like it is trapped in a loop.
    Here is my code now.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    
    class STACK
    {
    private:
      int S[100];//stack
      int Ssize;//stack size
    
    public:
      STACK();//constructor
      void push(int c);//push char onto stack
      int pop(); //remove char from the stack
      int size(); // returns the number of items on the stack
      bool full(); // true if stack is full
    };
    
    STACK::STACK()
    {
      Ssize =0;
    }
    void STACK::push(int c)
    {
      if (Ssize != 100)
        S[Ssize++]=c;
      else
        cout<<"***Stack overflow-program terminated..."<<endl;
     }
    
     int STACK::pop()
     {
       return S[--Ssize];
     }
    
     bool STACK::full()
     {
       if (Ssize==100)
         return true;
       else
         return false;
      }
    
      int STACK::size()
      {
        return Ssize;
      }
    
    
    void main(void)
    {
      int a,b;
    
      int i,end;
      char M[]="(((4+8)/4)*(9-7));";
    
      STACK V,O;
      i=0;
      while (M[i]!=';')
      {
        if (M[i]==')')
        {
          b=V.pop();
          a=V.pop();
          switch (O.pop())
          {
          case 0:
            V.push(a+b);
            break;
          case 1:
            V.push(a-b);
            break;
          case 2:
            V.push(a*b);
            break;
          case 3:
            V.push(a/b);
            break;
          }
        }
        else
          float a;
          int i = (int)a;
          switch(i){
    
         // switch(M[i])
          //{
            case'0':
              V.push(0.0);
              break;
    
            case'1':
              V.push(1.1);
              break;
    
            case'2':
              V.push(2.1);
              break;
    
            case'3':
              V.push(3.1);
              break;
    
            case'4':
              V.push(4.1);
              break;
    
            case'5':
              V.push(5.1);
              break;
    
            case'6':
              V.push(6.1);
              break;
    
            case'7':
              V.push(7.1);
              break;
    
            case'8':
              V.push(8.1);
              break;
    
            case'9':
              V.push(9.1);
              break;
    
            case'+':
              O.push(0);
              break;
    
            case'-':
              O.push(1);
              break;
    
            case'*':
              O.push(2);
              break;
    
            case'/':
              O.push(3);
              break;
        }
        i++;
    }
    cout<<V.pop()<<endl;
    cin>>end;
      return 0;
    }
    Last edited by fastlane29; 12-06-2002 at 05:11 AM.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hey, just a thought, but I thought that stacks are supposed to be dynamically-allocated, instead of having a fixed array?

    (i.e. the array starts out with 0 elements or something, then when you Push() a new object on, it resizes to 1 element, and when you Push() another on, it resizes to 2 elements, etc.)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    Your guess is good as mine Hunter2 im a nubee and dont have a clue.

    Fastlane29

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, never mind it's not really important, just an idea if you ever decide to expand on the stack class.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linker 2019 issues
    By werdy666 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2009, 03:12 AM
  2. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21