C language simple calculator for small projects

Yesterday just installed on devc ++, middle of the night thinking about it and thought the C language

So we see there is a calculator laboratory building project

Done once before, this is the main idea of ​​writing

First, we start with the principle of thinking jia, simple arithmetic calculator will have these, look ordinary calculator that is two numbers and addition, subtraction number entered, so to achieve this is very simple matter

The first step, do not forget our header file

#include <stdio.h>
main(){
    }

The main frame first written, then we want is not a simple operation requires two values, then subtraction, multiplication and division?

   double number1=0.0;
   double number2=0.0;
   char operation=0;

Under Here we use double, accurate write operation, you can not use int, because there is a decimal point can not be done accurately case

Do remind a user input and input rules, make a simple interactive

   the printf ( " \ n-\ n-input Calcd \ n-\ n- " ); 
   Scanf ( " % C%% LF LF " , & number1, & Operation, & number2);

Operation symbol values ​​and the user input to that which would exist three variables

Then we have entered, is not necessary to decide what he entered operation symbol is ah? So we are going to use a switch statement, if used, then a lot of trouble

   switch (operation)
   {
   case '+':
      printf("=%lf",number1+number2);
      break;
   case '-':
      printf("=%lf",number1-number2);
      break;
   case '*':
      printf("=%lf",number1*number2);
      break;
   case '/':
      if(number2 == 0 ) 
         the printf ( " \ n-\ n-\ A can not be divided by zero " );
       the else 
         the printf ( " =% LF " , number1 / number2);
       BREAK ;
     default : 
      the printf ( " \ n-enter the value! " );
       BREAK ;

In simple terms it, is to determine what operational symbol is entered by the user, if it is "+" sign, then we will be in operation and output statements output

Well, now basically simple calculator applet written coming!

However, what is running can only be calculated once again

We can add cycles, even more functionality

The small projects to this end, more features waiting for you to develop

#Do not redistribute without permission

 

Guess you like

Origin www.cnblogs.com/AxsCtion/p/12509641.html