首页 > 代码库 > R C语言程序设计(1)

R C语言程序设计(1)

1.2变量与算数表达式

 1 #include <stdio.h>
 2 /*当fahr=0,20,···,300时,分别打印华氏温度与摄氏温度对照表*/ 
 3 int main()
 4 {
 5     int fahr, celsius;
6 int lower, upper, step; 7 8 lower = 0; 9 upper = 300; 10 step = 20; 11 12 fahr = lower; 13 while(fahr <= upper) { 14 celsius = 5 * (fahr-32) / 9; 15 printf("%d\t%d\n", fahr, celsius); 16 fahr = fahr + step; 17 18 } 19 return 0; 20 }

 

浮点数版本:

#include <stdio.h>
/*当fahr=0,20,···,300时,分别打印华氏温度与摄氏温度对照表浮点数版本*/
int main()
{
    float fahr, celsius;
    int lower, upper, step;
    
    lower = 0;
    upper = 300;
    step = 20;
    
    fahr = lower;
    while(fahr <= upper) {
        celsius = 5.0 / 9.0 * (fahr-32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        //%3.0待打印浮点数至少3个字符,且不带小数点部分,6.1表小数点后面有1位数字
        fahr = fahr + step;
        
    }
      return 0;
    }

 

1.3 for 语句

逆序for语句

#include <stdio.h>
/*当fahr=0,20,···,300时,分别打印华氏温度与摄氏温度对照表浮点数版本*/
int main()
{
    int fahr;
    
    for (fahr = 300; fahr >= 0; fahr = fahr - 20)
        printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
        
    
      return 0;
    }

1.4 符号常量

#define LOWER  0

//大概同 const int LOWER = 0 的用法是一致 效果相同 区别位置

#include <stdio.h>
/*当fahr=0,20,···,300时,分别打印华氏温度与摄氏温度对照表浮点数版本*/

int main()
{
    int fahr;
    const int UPPER = 300;
    const int LOWER = 0;
    const int STEP = 20;
    
    
   for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP)
        printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
        
    
      return 0;
    }

 

R C语言程序设计(1)