Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren+ 6
What is the code behind "(" and ")"
In programming a scientific calculator, what is the code behind the brackets button so that the code inside the brackets can be executed first? https://code.sololearn.com/WL79pjz3ike3/?ref=app
24 Antworten
+ 3
Probably because the calculator on your phone does not just consist of one eval. It can probably be resolved with regular expressions
+ 4
Why would it be the same?
+ 4
Airree, I've never studied anything science before neither am I a professional in coding, I'm a business student and its the passion I have for coding that is helping me progress
+ 3
I was expecting 4 but it outputs an error
+ 3
Well, that's because the input is invalid. That is pretty obvious, I thought you would know that. But you can still catch the error though
+ 3
So how can this be fixed?
+ 3
try {
calMo.value = eval(calMo.value);
} catch (e) {
calMo.value = "Invalid input!";
}
Seriously, you don't know this?
+ 3
Because 2(2 is the same as 2*2 @~ swim ~
+ 3
Well, I have never heard of that. Maybe you could do it with regular expressions, but I don't think you need multiple possibilities for multiplication in a simple calculator.
+ 3
~ swim ~ Yes, I heard of that, but I was talking about 2(2.
+ 3
~ swim ~, I am talking about a(2+3) = a*5
+ 3
Airree, The calculator preinstalled on my phone which I use gives an output of 4 whenever I type 2(2 or 10 when I type 2(2+3 without even closing the brackets
+ 2
You start evaluating from there first. But I don't get why you are asking this, since it works perfectly. eval does that by default
+ 2
Airree it doesn't. Why don't you try this on the calculator and see what it outputs "2(2"
+ 2
So how is it possible that the calculator on my phone does not produce an error 🤔
+ 2
I'll post a picture of how the calculator preinstalled on my new phone does it without producing an error and I'll tag you to the post
+ 2
~ swim ~ that's exactly what I'm talking about, now we're on the same page. So what I'm asking is how do I write a program for those brackets in my code?
+ 2
Or Airree maybe you can help me with the question plizzzzz
+ 2
Airree Wait a minute, what are regular expressions?