Jump to content

a simple calculator in C please helppp

- - - - -

  • Please log in to reply
9 replies to this topic

#1
nexaas

nexaas

    Newbie

  • Members
  • Pip
  • 5 posts
hi; ı have a pproject please help me

In this first project you have to build a non-scientific calculator. The user should give the instructions as a sequence ending with sentinel “=” and your program should calculate the result without considering the precedence of the operators. In addition to these, your calculator has to be able to store values into a single variable, (i.e. called A below), which will probably be used in another input.

The variable name must consist of a single letter. If the user enters a non-letter character, you must warn the user until a letter is entered.

You can assume that the input sequence will consist of operators (defined below), numbers and the variable that is defined by the user. You can also assume that the input contains a valid expression terminated by “=”. So, you do not need to do any input check for the sequence. There will be no space between operators and numbers.

You must read the numbers as characters and convert them to number equivalents. There can only be positive integers. The operators that must be used are:

+ (addition), - (subtraction), * (multiplication), / (division), ^ (power)

• +, -, * are self explanatory,
• / performs normal division, not integer division:
5/2 = 2.5
• and ^ means power:
3^2 = 9 (meaning 3*3)
2^4 = 16 (meaning 2*2*2*2)
Note that you must implement this power operation by yourselves. There is a function in “math.h” called “pow”, but you cannot use that function in your project.

SAMPLE RUN:
(Your program’s output is given in blue. User input is in red.)
>>Welcome to the non-scientific calculator.
>>Please enter the variable name to use in the calculations

>>&
>>You must enter a letter! Please re-enter the variable name:
>>A
>>Please enter the sequence:
>>
3+1*4+5/2=
>>Result = 10.5
>>What do you want to do now?
a)Store the result in a variable
b)Make another calculation
c)Quit the program
Choice>>
a
>>The variable A with the value 10.5 is stored.
>>What do you want to do now?
a)Store the result in a variable
b)Make another calculation
c)Quit the program
Choice>>
b
>>Please enter the sequence:
>>
A+4*2-15=
>> Result = 14.0
>>What do you want to do now?
a)Store the result in a variable
b)Make another calculation
c)Quit the program
Choice>>
b
>>Please enter the sequence:
>>
A+2^2=
>> Result = 156.25
>>What do you want to do now?
a)Store the result in a variable
b)Make another calculation
c)Quit the program
Choice>>
c

Thank you for using the calculator…

• As you can see from the sample run, the program has to be menu driven.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
So, what's your question?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
nexaas

nexaas

    Newbie

  • Members
  • Pip
  • 5 posts
so my quastion is that the user will enter an input that an array of a sequance for example 3+2*5+5/2=
and ı must find result .....the sequance can be 4+5= or 5 +7+6+8+9*76-435= how it can be;;,,, please helpppp

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You will need to accept the input either character by character or as a string, and then analyze the characters to determine what should be done with them.

You may want to start with thinking about what characters would be legal, then move on from there.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
nexaas

nexaas

    Newbie

  • Members
  • Pip
  • 5 posts
coulld you give some codes please

#6
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
I don't think someone will give...without your effort!
If you have tried it,give the code here even if it doesn't works well.

Warrior

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Give some kind of code, and we can guide you. You know you have to accept the user's input, then process it. Make a stub function for the processing and make sure you can get input, draw the menu, etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
nexaas

nexaas

    Newbie

  • Members
  • Pip
  • 5 posts
int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10.....;
char c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
scanf("%d%c%d%c%d%c%d%c%d%c%d%c.......",,&a1,&c1,&a2,&c2,&a3,&c3,&a4,&c4,&a5,&c5,&a5,&c5,&a6,&c6.........);
op are c1 c2 c3....
numbers a1 a2 a3....
and ı need a method to calculate especially my program calcualte 5+5*5=50 it is wrong because firstly adds 5 to 5 then multiply but it must be 30 because it must first multiply 5 to 5 then adds 5

#9
nexaas

nexaas

    Newbie

  • Members
  • Pip
  • 5 posts
ı want you use atoi ...please help ı am ı student comp engineering in turkey and this is my first year in school

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Once you define a variable, your input won't work.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users