• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Char -> Int and Int -> Char  RSS feed

 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I have a Char stack:


The stack is filled with numbers and characters (only '+' and '*' ), but I need to make calculations, so I need the INT values of the numbers i pop from the stack. So each time I need to make a calculation, I have to pop two numbers, convert them into INT ,then add/multiply them, and put them back into the stack again, but as CHAR (because the stack is not accepting them to be added as INT)

Can someone please help me do this properly? I tried everything I could find on the internet, no luck!
Appearenlty, this doesn't work:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're making it harder on yourself than it needs to be. Why don't you just have one stack for the operands (numbers) and another stack for the operators (chars)? Intermediate results have to pushed back on the number stack anyway. And hasn't Campbell already asked you to search for Shunting Yard Algorithm in another thread?
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You're making it harder on yourself than it needs to be. Why don't you just have one stack for the operands (numbers) and another stack for the operators (chars)? Intermediate results have to pushed back on the number stack anyway. And hasn't Campbell already asked you to search for Shunting Yard Algorithm in another thread?


Yeah, but that's in case I wanna solve it with that method, in this case I want to solve it this way, and I need help particulary with that part of the solution.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying you want to do it the hard way? Sorry, but that doesn't make sense to me. Why struggle with code and inappropriate data structures when there's a way to do it that you have don't have to struggle?
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Are you saying you want to do it the hard way? Sorry, but that doesn't make sense to me. Why struggle with code and inappropriate data structures when there's a way to do it that you have don't have to struggle?


The more languages you know, the better. Same goes here. I'm not gonna sell this program, or give it to a company, I just wanna learn this way too. Can you help please, if not, that's ok, but this time I'm not asking for methods or ways to solve my problem, I'm just asking help for that specific part.

Thank you.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaspersky Ukshini wrote:Can someone please help me do this properly?

Well, I'm getting mixed messages from you because you asked for help to do it properly and IMO, two stacks, one for operands and one for operators is the proper way to do it.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Kaspersky Ukshini wrote:Can someone please help me do this properly?

Well, I'm getting mixed messages from you because you asked for help to do it properly and IMO, two stacks, one for operands and one for operators is the proper way to do it.


I'm sorry dude, but you got me wrong. I was refering to that part, I wouldn't be oppening a new topic if I was talking about the same thing as in the other one. In this one, I'm just asking how to convert one char variable to int, and the other way around.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am to assume that since you are trying to convert one character to an int directly, that your operands are single digit numbers? If so, then you can do char arithmetic:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaspersky Ukshini wrote:I'm sorry dude, but you got me wrong. I was refering to that part, I wouldn't be oppening a new topic if I was talking about the same thing as in the other one. In this one, I'm just asking how to convert one char variable to int, and the other way around.

Simple answer: You can't. At least not without making your Stack a Stack<Object> (Integer and Character only share Object as a superclass) and checking the type of every item you retrieve - which kind of defeats the purpose of an object-oriented language, don't you think?

The fact is that you don't even need to push operators on that Stack. As I said in your other thread, implement an RPN calculator, which is basically a glorified Stack, and operators will be a thing of the past.

HIH

Winston
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting no error when making the operatin now, I guess it works. I just have to push the same value to the stack(which is characters defined) again..

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaspersky Ukshini wrote:I'm getting no error when making the operatin now, I guess it works...

There's no "guess" about it. You need to KNOW.

Question: If you're simply converting by its binary value, how do you tell between the number 43 and the operator '+'?

Winston

[Edit] There is one (narrow) way in which this "Stack" (which I presume is the precursor to a "calculator") will work: If you only allow single-digit numbers.

Alternatively, you could store everything as Strings (ugh, bite your tongue Winston) and try to convert everything that isn't an operator to a number. I hate the idea with every fibre of my being though.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Kaspersky Ukshini wrote:I'm getting no error when making the operatin now, I guess it works...

There's no "guess" about it. You need to KNOW.

Question: If you're simply converting by its binary value, how do you tell between the number 43 and the operator '+'?

Winston


I was actualy checking if it's correct by writing a System.out right under the line where I assign the values, It's not correct. This is what I did, and my output is not the same as my input:



I'm getting some values that are not even in the stack... Appearently I'm not getting the numeric value from the char variables...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaspersky Ukshini wrote:I'm getting some values that are not even in the stack... Appearently I'm not getting the numeric value from the char variables...

Then maybe it's time you re-read Junilu's advice - because it's good.

Better still: Explain to us WHAT this "Stack of characters" is supposed to achieve. (Note: WhatNotHow (←click)).

Winston
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, I was wondering if the other part of the code is ok, which I believe is... :/

Don't worry about the way it's written guys, because I will only get the arithmetic expressions in this way:
((5+6)-(2+2))

No need to worry about multiplication or division..




 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaspersky Ukshini wrote:No need to worry about multiplication or division..

What about negative numbers? Eg:
((-5+6)-(2+2))
or even worse:
((5+-6)-(2+2))

Programming is NOT simple, Kaspersky - which is why you'll hear me say (ad nauseam) on these forums: you need to understand the problem before you start coding.

Have you looked up the Shunting Yard algorithm that both Campbell and I pointed you to in your other thread? It really is the basis for this sort of problem because, to put it simply: infix notation is horrible.

HIH

Winston
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

A simple question here - are you really clear what a char type is?

If so, from your example: how are you going to push an 11 onto the stack (after you 5+6 example)?

-steve
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Kaspersky Ukshini wrote:No need to worry about multiplication or division..

What about negative numbers? Eg:
((-5+6)-(2+2))
or even worse:
((5+-6)-(2+2))



Have you looked up the Shunting Yard algorithm that both Campbell and I pointed you to in your other thread? It really is the basis for this sort of problem because, to put it simply: infix notation is horrible.

HIH

Winston


I understand mate, and thank you for pointing it out for me, but in this particular exercise, none of those cases will happen. We have 10 test examples, which the program has to pass:

  • (1+2)
    ((4-9)+((8+1)-(1+1)))
    ((5+6)-(2+2))
    (((1+9)-((7+7)-(3+2)))+(2+4))
    (((1+1)+(1+1))+((1+1)+(1+1)))
    ((((1+2)+(3+4))+((5+6)+(7+8)))+(((8-7)-(6-5))-((4-3)-(2-1))))
    ((1-2)+((3+2)+((6-3)+(7+5))))
    (((1-1)+(2-2))-((1-1)+(2-2)))
    ((((1+5)-(2-3))-(2-8))+((1+2)-((5+6)-(3-7))))
    (((1+6)-(6-1))+(2+0))


  • Winston Gutkowski wrote:
    Programming is NOT simple, Kaspersky - which is why you'll hear me say (ad nauseam) on these forums: you need to understand the problem before you start coding.


    I absolutely understand that one too bro. But the thing is that my exam is in just a few hours, and I'm trying to rush things to get as much info as I can... An indicator and a proof for this are my other topics which I slowly and step by step solved my problems with you guys....

    Steve Fahlbusch wrote:Greetings,

    A simple question here - are you really clear what a char type is?

    If so, from your example: how are you going to push an 11 onto the stack (after you 5+6 example)?

    -steve


    You are right mate!
    I think I should try it with two stacks then... one for the values, and for for the signs...
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kaspersky Ukshini wrote:I understand mate, and thank you for pointing it out for me, but in this particular exercise, none of those cases will happen. We have 10 test examples, which the program has to pass:...

    OK, so no negative numbers. You do, however, still have all those nasty brackets, which you could eliminate if you converted to postfix notation - which is what the Shunting Yard algorithm is all about.

    However, two hours ain't enough time to learn it (trust me ).

    I think I now see why you were thinking about a Stack<Character> though; and it's smart but (I suspect) faulty thinking. This problem is not linear, it's recursive. Each part of your string that's enclosed in brackets could in fact be another input string to your program (that's how recursion works).

    And I suspect that you can indeed use a Stack to solve it (Stacks and recursion are closely linked) - just not a Stack<Character>.

    Unfortunately, none of this is going to get done in two hours. Next time, give us a bit more warning.

    Winston
     
    Kaspersky Ukshini
    Ranch Hand
    Posts: 122
    C++ Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Kaspersky Ukshini wrote:I understand mate, and thank you for pointing it out for me, but in this particular exercise, none of those cases will happen. We have 10 test examples, which the program has to pass:...

    OK, so no negative numbers. You do, however, still have all those nasty brackets, which you could eliminate if you converted to postfix notation - which is what the Shunting Yard algorithm is all about.

    However, two hours ain't enough time to learn it (trust me ).

    I think I now see why you were thinking about a Stack<Character> though; and it's smart but (I suspect) faulty thinking. This problem is not linear, it's recursive. Each part of your string that's enclosed in brackets could in fact be another input string to your program (that's how recursion works).

    And I suspect that you can indeed use a Stack to solve it (Stacks and recursion are closely linked) - just not a Stack<Character>.

    Unfortunately, none of this is going to get done in two hours. Next time, give us a bit more warning.

    Winston


    It's ok dude, none of this is your fault, in the countrary, you guys really helped me a lot! I'm pretty sure I wouldn't have made it without the help from this forum

    One last question, is there a type of stack that could hold both numbers (bigger than 9) and signs at the same time?
    How about String?
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kaspersky Ukshini wrote:How about String?

    Yup, String would do it. However, it has similar problems to a Stack<Object> - namely: you have to check what type of "thing" each element is (ie, is it an operator or a number). If you have two separate Stacks - one for operators, the other for numbers - you already know what the type is going to be.

    A Stack<String> does have one advantage though: it allows you to separate a '+'/'-' used as an operator, from one used to sign a number - eg, "-1".

    However, it's quite possible that your course may go on to show you how to implement an RPN calculator - in which case operators never need to get get pushed onto any Stack - and it's a fun exercise. And once you have an RPN calculator, you can do almost anything...

    Winston
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, Kaspersky certainly does seem to be on a lot of things at the same time...

    @Winston:
    the problem is that in everyday life the expressions you get are not in RPN form.
    I remember, way back in the old days, when I had a HP pocket calculator,
    and after an initial period of getting used to RPN, it really worked great.
    But I usually had to translate 'normal' expressions into RPN (with practise
    that becomes easy).

    So, even though you have a calculator that handles RPN, you probably
    need some converter that puts the initial expression into an RPN suitable form.
    Which brings you back to the starting point...

    I was thinking more of an Expression as an object, that knows how
    to determine its value. But also in this case you need a similar
    'expression parser'.

    Greetz,
    Piet
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Piet Souris wrote:So, even though you have a calculator that handles RPN, you probably
    need some converter that puts the initial expression into an RPN suitable form.

    Which is where the Shunting Yard algorithm comes in. Separating the two problems allows you to simplify things, and actually provides you with two useful classes, because SYA can be used for other things as well; and an RPN calulator might be useful in its own right.

    Slap the two together, and you have an infix calculator.

    Winston
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!