C 算術(shù)運(yùn)算符

2018-07-05 16:32 更新

學(xué)習(xí)C - C算術(shù)運(yùn)算符

C支持四種基本的算術(shù)運(yùn)算,如加法,減法,乘法和除法。

算術(shù)語句具有以下形式:

variable_name = arithmetic_expression;

=運(yùn)算符右側(cè)的算術(shù)表達(dá)式使用存儲(chǔ)在變量或顯式數(shù)字中的值進(jìn)行計(jì)算,該值使用算術(shù)運(yùn)算符進(jìn)行組合,如加法(+),減法( - ),乘法(*),和除法(/)。

total_count = birds + tigers + pigs + others;

下表列出了基本算術(shù)運(yùn)算符。

運(yùn)算符操作
+加法
-減法
*乘法
/除法
%求余

運(yùn)算符應(yīng)用于的值稱為操作數(shù)。需要兩個(gè)操作數(shù)(如%的運(yùn)算符稱為二進(jìn)制運(yùn)算符。

適用于單個(gè)值的運(yùn)算符稱為一元運(yùn)算符。因此 - 是表達(dá)式a-b中的二進(jìn)制運(yùn)算符和表達(dá)式-data中的一元運(yùn)算符。


    #include <stdio.h> 

    int main(void) 
    { 
      int cakes = 5; 
      int CALORIES = 125;                     // Calories per cookie 
      int total_eaten = 0;              
      
      int eaten = 2;                       
      cakes = cakes - eaten;               
      total_eaten = total_eaten + eaten; 
      printf("I have eaten %d cakes.  There are %d cakes left", eaten, cakes); 
      
      eaten = 3;                            // New value for cakes eaten 
      cakes = cakes - eaten;                
      total_eaten = total_eaten + eaten; 
      printf("\nI have eaten %d more.  Now there are %d cakes left\n", eaten, cakes); 
      printf("\nTotal energy consumed is %d calories.\n", total_eaten * CALORIES); 
      return 0; 
    } 

上面的代碼生成以下結(jié)果。


例子

以下是基本算術(shù)的代碼說明:


  #include <stdio.h> 

  int main() { 
     int a,b,c; 
     a = 10; 
     b = 6; 

     c = a + b; 
     printf("%d + %d = %d\n",a,b,c); 

     c = a - b; 
     printf("%d - %d = %d\n",a,b,c); 

     c = a * b; 
     printf("%d * %d = %d\n",a,b,c); 

     float d = a / b; 
     printf("%d / %d = % .2f\n",a,b,d); 

     float e =(float)a / b; 
     printf("%d / %d = % .2f\n",a,b,e); 

     return 0; 
  } 

上面的代碼生成以下結(jié)果。

除法和求余運(yùn)算符

假設(shè)你有45個(gè)蛋糕和一組七個(gè)孩子。

你會(huì)把蛋糕從小孩中分出來,并計(jì)算每個(gè)孩子的數(shù)量。

然后你會(huì)找出剩下多少蛋糕。


#include <stdio.h> 

int main(void) { 
  int cakes = 45;                         
  int children = 7;                       
  int cakes_per_child = 0;                
  int cakes_left_over = 0;                
  
  // Calculate how many cakes each child gets when they are divided up 
  cakes_per_child = cakes/children;     
  printf("%d children and %d cakes\n", children, cakes); 
  printf("each child has %d cakes.\n", cakes_per_child); 
  
  cakes_left_over = cakes%children; 
  printf("%d cakes left over.\n", cakes_left_over); 
  return 0; 
} 

上面的代碼生成以下結(jié)果。

op= 賦值形式

C是一種非常簡(jiǎn)潔的語言,它為您提供了指定一些操作的縮寫方式。

考慮下面的語句:

number = number + 10;

這種賦值有一個(gè)簡(jiǎn)寫版本:

number += 10;

變量名后的+ =運(yùn)算符是op =運(yùn)算符族的一個(gè)例子。

這個(gè)語句與前一個(gè)語句的效果完全相同,它節(jié)省了一些打字。

OP中的OP可以是這些算術(shù)運(yùn)算符中的任何一個(gè):

+  -  *  /  %

如果你假設(shè)number的值為10,你可以寫下面的語句:

number *= 3;               // number will be set to number*3 which is 30 
number /= 3;               // number will be set to number/3 which is 3 
number %= 3;               // number will be set to number%3 which is 1 

op = 中的 op 也可以是一些你還沒有遇到的其他操作符:

<<  >>  &  ^  |

op = 操作符集始終以相同的方式工作。

使用 op = 的語句的一般形式:

lhs op= rhs;

其中rhs表示op =運(yùn)算符右側(cè)的任何表達(dá)式。

效果與以下語句形式相同:

lhs = lhs op (rhs);

首先,考慮這個(gè)語句:

variable *= 12;

這是相同的:

variable = variable * 12;

您現(xiàn)在有兩種不同的方式將整數(shù)變量自增1。

以下兩個(gè)語句遞增計(jì)數(shù) 1:

count = count + 1; 
count += 1; 

因?yàn)閛p在op =適用于計(jì)算rhs表達(dá)式的結(jié)果,語句為:

a /= b + 1;

是相同的

a = a/(b + 1);

我們所知道的除法


  #include <stdio.h> 
  int main(void) 
  { 
       printf("integer division:  5/4   is %d \n", 5/4); 
       printf("integer division:  6/3   is %d \n", 6/3); 
       printf("integer division:  7/4   is %d \n", 7/4); 
       printf("floating division: 7./4. is %1.2f \n", 7./4.); 
       printf("mixed division:    7./4  is %1.2f \n", 7./4); 
   
       return 0; 
  }    

上面的代碼生成以下結(jié)果。

例2

以下代碼顯示了如何編寫一個(gè)簡(jiǎn)單的計(jì)算器,當(dāng)一個(gè)數(shù)字除以另一個(gè)數(shù)字時(shí),可以添加,減法,乘法,除法和查找余數(shù)。


#include <stdio.h>

int main(void) {
  double number1 = 0.0;           //* First operand value a decimal number  *//
  double number2 = 0.0;           //* Second operand value a decimal number *//
  char operation = 0;             //* Operation - must be +, -, *, /, or %  *//

  printf("\nEnter the calculation(eg. 1+2)\n");
  scanf("%lf %c %lf", &number1, &operation, &number2);

  switch(operation)
  {
    case "+":                     // No checks necessary for add
      printf("= %lf\n", number1 + number2);
      break;

    case "-":                     // No checks necessary for subtract
      printf("= %lf\n", number1 - number2);
      break;

    case "*":                    // No checks necessary for multiply
      printf("= %lf\n", number1 * number2);
      break;

    case "/":
      if(number2 == 0)           // Check second operand for zero
        printf("\n\n\aDivision by zero error!\n");
      else
        printf("= %lf\n", number1 / number2);
      break;

    case "%":                    // Check second operand for zero
      if((long)number2 == 0)
         printf("\n\n\aDivision by zero error!\n");
      else
        printf("= %ld\n", (long)number1 % (long)number2);
      break;

    default:                     // Operation is invalid if we get to here
      printf("\n\n\aIllegal operation!\n");
      break;
  }

  return 0;
}

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)