r/learnprogramming • • 25d ago

C Calculator

Hi there !
I made a simple calculator code, its my first project !
i learn a good things like how to use switch , printf(""); and scanf("");

I did not use any AI on it

It solve those op :

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

Some features:

  • Handle division by zero
  • Support decimal numbers

I use VS code

Thanks and what do you suggest next <3
The code in github: [https://github.com/UseR-Name-Dz/Calculator-]

12 Upvotes

26 comments sorted by

2

u/abd53 25d ago

Try evaluating expressions next. For example, 3+2(5-6/2)

1

u/Ancient-Plankton-908 25d ago

Nice thx i will try it

1

u/Flame77ofc 25d ago

Is it on GitHub?

1

u/Ancient-Plankton-908 25d ago

yes it is

1

u/Flame77ofc 25d ago

link please? I give one star

1

u/Juukamen 25d ago

Awsome start mate.
As the others are saying, exand functions and make a GUI.

1

u/Ancient-Plankton-908 25d ago

I dont know how still new on C (last i learn is switch)

1

u/lehrerkind_ 25d ago

Did you forget to post the link to your code of did you just want to share that you have done something without sharing the code?

1

u/Ancient-Plankton-908 25d ago

I fear if it's not allowed
If I can tell me and i will edit

1

u/Dazzling_Music_2411 25d ago edited 25d ago

Make it behave more naturally.

Save the last result automatically. This will actually make it usable!

For instance if I type "2 + 3 =" it should return 5. If I then type "x 7" it should show "Ans x 7" and when I press "=" it should show "35".

I think that's how real modern calculators work.

Later, if you want to have a proper challenge, you can try larger expressions, e.g. 3 * (2 - 5) + 7 / 9 but his will require learning some "real" programming, including possibly the use of the bison parser, or similar. That is the real world of C. 😄

1

u/Ancient-Plankton-908 25d ago

Its work like this :
Example:
Enter a value for number1:

5

Enter an operation:

+

Enter a value for number2:

2

5 + 2 = 7

2

u/Dazzling_Music_2411 25d ago

I guessed so.

You need to lose the prompts to have a real calculator. IOW, after typing a number it should KNOW that the next thing is an operator without you having to tell it. Anything else should be rejected. Then it should know that after that it will be a number again, etc...

That's how calculators work, they don't keep asking "what operation", do they?

If your operations only have two arguments, you don't even need an "=" sign. You'll need that once you are handling expressions, e.g. "2 + 3 + 4".

1

u/Ancient-Plankton-908 25d ago

yeah you right , how can i make the calculators know ?

1

u/Dazzling_Music_2411 25d ago edited 25d ago

Lots of different ways, but basically you have to keep monitoring your keypresses and checking if the input is acceptable. Think about it a bit and see what you come up with! Essentially you're creating a Finite State Machine (look it up).

If you do it with graphics, it's easier, because the graphics stop you from keying in non-permissible characters., e.g. letters. However, being able to do it at the command line is more fun, and you'll learn way more. However, then you will have the extra work of learning a GUI.

If you want to try the graphics approach ON the command line, look to ncurses, it might be a good halfway point, so you don't have to learn how to use a GUI straight away.

1

u/Agawell 25d ago

Add a gui - buttons and a screen

Add scientific function

Add graphing tool

1

u/Ancient-Plankton-908 25d ago

hmmm its hard for me now i need to learn css to make it

1

u/Agawell 25d ago

Don’t do a web page, create a desktop app and I wouldn’t think to use css for that

1

u/Ancient-Plankton-908 25d ago

How i can create a desktop app ?

1

u/Agawell 25d ago

Google is your friend

1

u/cheezballs 25d ago

Heck yea! Writing a calculator is also a really great introduction to Stack data structures (a more advanced topic when you get there)

I also had an assignment in school to implement stop-lights at an intersection, which is another pretty easy but pretty fun thing to do.

1

u/w8tA 19d ago

The goat my friend

0

u/[deleted] 25d ago

[removed] — view removed comment

1

u/Ancient-Plankton-908 25d ago

Thank you very much <3
Im using C