99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

C 算術運算符

2018-07-05 16:32 更新

學習C - C算術運算符

C支持四種基本的算術運算,如加法,減法,乘法和除法。

算術語句具有以下形式:

variable_name = arithmetic_expression;

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

total_count = birds + tigers + pigs + others;

下表列出了基本算術運算符。

運算符操作
+加法
-減法
*乘法
/除法
%求余

運算符應用于的值稱為操作數(shù)。需要兩個操作數(shù)(如%的運算符稱為二進制運算符。

適用于單個值的運算符稱為一元運算符。因此 - 是表達式a-b中的二進制運算符和表達式-data中的一元運算符。


    #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; 
    } 

上面的代碼生成以下結果。


例子

以下是基本算術的代碼說明:


  #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; 
  } 

上面的代碼生成以下結果。

除法和求余運算符

假設你有45個蛋糕和一組七個孩子。

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

然后你會找出剩下多少蛋糕。


#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; 
} 

上面的代碼生成以下結果。

op= 賦值形式

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

考慮下面的語句:

number = number + 10;

這種賦值有一個簡寫版本:

number += 10;

變量名后的+ =運算符是op =運算符族的一個例子。

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

OP中的OP可以是這些算術運算符中的任何一個:

+  -  *  /  %

如果你假設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 =運算符右側的任何表達式。

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

lhs = lhs op (rhs);

首先,考慮這個語句:

variable *= 12;

這是相同的:

variable = variable * 12;

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

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

count = count + 1; 
count += 1; 

因為op在op =適用于計算rhs表達式的結果,語句為:

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; 
  }    

上面的代碼生成以下結果。

例2

以下代碼顯示了如何編寫一個簡單的計算器,當一個數(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;
}

上面的代碼生成以下結果。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號