Jump to content

Hey guys, 

 

I am currently in a computer science class in High school and have to create a calculator in the program Visual studio 2015. I cannot use if statements for the assignment either.

I have no idea what I am doing and the teachers "instructional videos" are absolute garbage and do not help at all. Not asking for anyone to write the code for me, just something to help me get going.

Thanks for any and all help!

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/
Share on other sites

Link to post
Share on other sites

8 minutes ago, Littlelaner said:

Hey guys, 

 

I am currently in a computer science class in High school and have to create a calculator in the program Visual studio 2015. I cannot use if statements for the assignment either.

I have no idea what I am doing and the teachers "instructional videos" are absolute garbage and do not help at all. Not asking for anyone to write the code for me, just something to help me get going.

Thanks for any and all help!

Assuming this is just a simple calculator, you can control the inputs by using a state machine. For a basic calculator, there's these states: operand 1, operation, operand 2, output. You can use swith-case statements entirely with this taking the requirement of "no if-statements" to the technical end.

 

That's the gist of it. The rest is up to you to figure out.

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9329303
Share on other sites

Link to post
Share on other sites

17 minutes ago, ONOTech said:

You can create one using switch statements. 

 

10 minutes ago, M.Yurizaki said:

You can use swith-case statements entirely with this taking the requirement of "no if-statements" to the technical end.

 

Have not learned this yet in class, think my teacher would be impressed/not impressed if I used this to do this assignment? 

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9329357
Share on other sites

Link to post
Share on other sites

1 minute ago, Littlelaner said:

Have not learned this yet in class, think my teacher would be impressed/not impressed if I used this to do this assignment? 

I wouldn't worry about it. The requirement of "no if-statements" which a nice challenge, seems kind of dickish to me. Especially since you haven't learned about switch-case statements.

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9329362
Share on other sites

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

I wouldn't worry about it. The requirement of "no if-statements" which a nice challenge, seems kind of dickish to me. Especially since you haven't learned about switch-case statements.

Awesome, thanks. Might be back for help later tonight if I still can't figure it out.

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9329375
Share on other sites

Link to post
Share on other sites

Im not sure if my description below is very clear so please read this sub typing section from wikipedia  https://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping

 

1. create an class with an empty method for run() which would be c#'s equivalent to java's abstract. Call it Operator.class or whatever.

2. create classes for each type of operation + - / * that will extend the class above and overrides the run(). run() will be programmed to return the result of the operation.

3. have an array of int, size 2 for number input. When you enter number it stores it to array[index++]. then do index = index % 2. this way you can store numbers in alternate slots in the array without if statements.

4. Have a global variable of the type of class from step 1. When you click + - / * have the appropriate object from a class from step 2 assigned to this variable.

5. the second number entered will be the same code from step 3, if you understand my instructions this second number will be inserted into the second slot in the array

6. press equals will run the .run() method of the operator using the values in the array (step 3)

             ☼

ψ ︿_____︿_ψ_   

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9329960
Share on other sites

Link to post
Share on other sites

1 hour ago, SCHISCHKA said:

Im not sure if my description below is very clear so please read this sub typing section from wikipedia  https://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping

 

1. create an class with an empty method for run() which would be c#'s equivalent to java's abstract. Call it Operator.class or whatever.

2. create classes for each type of operation + - / * that will extend the class above and overrides the run(). run() will be programmed to return the result of the operation.

3. have an array of int, size 2 for number input. When you enter number it stores it to array[index++]. then do index = index % 2. this way you can store numbers in alternate slots in the array without if statements.

4. Have a global variable of the type of class from step 1. When you click + - / * have the appropriate object from a class from step 2 assigned to this variable.

5. the second number entered will be the same code from step 3, if you understand my instructions this second number will be inserted into the second slot in the array

6. press equals will run the .run() method of the operator using the values in the array (step 3)

Thanks! Ended up getting frustrated, quitting and retiring to netflix, bad habit to start I know...

 

Will definitely give this a go tomorrow, love the help that this forum always provides!

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9330294
Share on other sites

Link to post
Share on other sites

3 hours ago, Littlelaner said:

Have not learned [switch statements] yet in class

While it can be unfortunate, in my experience it's not uncommon for an assignment to be given out before all the material to do the assignment is covered in class. It's possible you'll cover switch statements before the due date.

 

3 hours ago, Littlelaner said:

think my teacher would be impressed/not impressed if I used this to do this assignment? 

You could always ask your teacher 

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9330363
Share on other sites

Link to post
Share on other sites

10 hours ago, madknight3 said:

While it can be unfortunate, in my experience it's not uncommon for an assignment to be given out before all the material to do the assignment is covered in class. It's possible you'll cover switch statements before the due date.

 

You could always ask your teacher 

totaly agree in uni we were given assignments a week before the lesson for it so we could go about trying the problem before given a way to answer it.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/734817-creating-a-calculator-c/#findComment-9331975
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×