第二章的程序

什么是变量(variable )

//program 2.1 What is a variable?
                 #include

                 int  main(void)
                 {
                     printf("My salary is $10000");
                     return 0;
                 }

试试看:使用变量

  • 变量声明语句以分号结束

将上一个程序改为使用int型变量(用来存放变量的声明语句,变量声明,变量声明以分号结尾)

                     // program 2.2 Using a variable
                     #include < stdio.h >

                     int main(void)
                     {
                         int salary; 
                         salary =10000;
                         printf("My salary is %d.\n",salary);
                         return 0;
                      }

使用更多的变量

             //program2.3 Using more variables
             #include 

             int main (void)
             {
                 int brothers;
                 int brides;

                 brothers = 7;
                 brides=7;
                 printf("%d brides for %d brother\n",brothers,brides)
                 return 0;
             }

试试看:减和乘

//program 2.5 calculations with cookies
#include

                int main ()
                {  
                    int cookies =5;
                    int cookie_calories =125;
                    int total_eaten =0;

                    int eaten =2;
                    cookies = cookies -eaten;
                    total_eaten =total_eaten +eaten
                    printf("\nI have eaten %d cookies. There are %d cookies left",eaten,cookies);

                    eaten = 3;
                    cookies =cookies -eaten;
                    total_eaten =total_eaten+eaten;
                    printf("\nI have eaten %d more.Now there are %d cookies left\n",eaten,cookies);                                          
                    printf("\nTotal energy consumed is %d calories.\n",total_eaten*cookie_calories);
                    return 0;
                }

试试看:除法和取模运算符

假设你有一罐饼干(其中有45块饼干)和7个孩子,要把饼干平分给每个孩子,计算每个孩子可得到几块饼干,分完后剩下几块饼干。

              //program 2.6 Cookies and kids
              #include 
              int main (void)
              {
                 int cookies =45;
                 int children =7;
                 int cookies_per_child =0;
                 int cookies_left_over =0 ;

                 cookies_per_child = cookies/children;
                 printf("You have %d children and %d cookies\n", children, cookies);
                 printf("Give each child %d cookies.\n",cookies_left_child);

                 cookies_left_over = cookies%children;
                 printf("There are %d cookies left over.\n",cookies_left_child);
                 return 0;
              }

试试看:使用float类型值的除法

将10尺长的厚板均分成4块时
用一个浮点数除以另一个浮点数,然后显示其结果

  //program 2.7 Divisdion with float values
#include

int main (void)
{
   float plank_length =10.0f;
   float piece_count =4.0f;
   float piece_length = 0.0f;

   piece_length = plank_length/piece_count;
   printf("A plank %f feet long can be cut into %f feet long.\n",plank_length,piece_count,piece_length);
   return 0;
}

试试看:算术运算
利用输入的直径计算一个圆桌的周长和面积。计算圆的周长及面积时,其数学公式
要使用PI(周长=2R.,面积=R*R >>使用函数scanf()时,要在变量前加上寻址运算符&,而使用printf()函数时不用添加
在scanf()的控制字符串后面有多少个参数,控制字符串就有多少个格式说明符

//program 2.8  calculations on  the table
#include

int main(void)
{
    float radius =0.0f;
    float diameter = 0.0f;
    float circumference = 0.0f;
    float area =0.0f;
    float pi = 3.14159265f;

    printf("Input the diameter of the table:");
    scanf("%f", &diameter);

    radius = diameter/2.0f;
    circumference = 2.0f*pi*radius;
    area = pi*radius*radius;

    printf("\nThe circumference is %.2f", circumference);
    printf("\nThe area is %.2f\n", area);
    return 0;
}

试试看:定义一个常量

在C语言中有一个通用的约定的:#define语句中的标识符都是大写

            //program 2.9 more round tables
            #include 
            #define PI 3.14159f

            int main ()
            {
               float radius =0.0f;
               float diameter = 0.0f;
               float circumference = 0.0f;
               float area =0.0f;

               printf("Input the diameter of a table:");
               scanf_s("%f", &diameter);

               radius = diameter/2.0f;
               circumference = 2.0f*PI*radius;
               area =PI*radius*radius;

               printf("\nThe circumference is %.2f. ", circumference);
               printf("\nThe area is %.2f.\n",area);
               return 0;
            }

试试看:定义一个其值固定的变量

//program 2.10 Round tables again  but  shorter
#include 

int main(void)
{
    float diameter =0.0f;
    float  radius = 0.0f;
    const  float  pi = 3.14159f;

    printf("Input the diameter of the table:");
    scanf("%f", &diameter);

    printf("\nThe circumference is  %.2f.", 2.0f*pi*radius);
    printf("\nThe area is %.2f.\n", pi*radius*radius);
    return 0;
}

试试看:找出极限值

//program 2.11 Finding the limits
#include
#include
#include

 int main (void)
{
   printf("Variables of type char store values from %d to %d\n",CHAR_MIN,CHAR_MIAX);
   printf("Variables of type unsigned char store values from 0 to %u\n", UCHAR_MAX);
   printf("Variables of type short store values from %d to %d\n", SHRT_MIN,SHRT_MAX);
   printf("Variables of type unsigned short store values from 0 to %u\n",USHRT_MAX);
   printf("Variables of type long store values from %ld to %ld\n", INT_MIN, INT_MAX);
   printf("Variables of type unsigned long store values from 0 to %lu\n",ULONG_MAX);
   printf("Variables of type long long store values from %ld to %ld\n",LLONG_MIN,LLONG_MAX);
   printf("Variables of type unsignd long long store values from 0 to %llu\n",ULLONG_MAX);

   printf("\nThe size of the smalest positive non-zero value of type float is %.3e\n",FLT_MIN);
   printf("The size of the largest value of type float is %.3e\n",FLT_MAX);
   printf("The size of the smallest non-zero value of type double is %.3e\n",DBL_MIN);
   printf("The size of the largest value of type double is %.3e\n",DBL_MAX);
   printf("The size of the smalllest non-zero value of type long double is %.3Le\n", LDBL_MAX);
   printf("The size of the largest value of  type  long double is %.3Le\n",LDBL_MAX);
  
   printf("\n Variables of type float provide %u decimal digits precision.\n"; FLT_DIG);
   printf("Variables of type double provide %u decimal digits precision.\n", DBL_DIG);
   printf("Variables of type long double provide %u decimal digits precision.\n",LDBL_DIG);

   return 0;
}
//program 2.12 Finding the size of a type

#include 

int main(void)
{
   printf("Variables  of type char occupy %u bytes\n", sizeof(char));
   printf("Variables  of type short occupy  %u bytes\n", sizeof(short));
   printf("Variables  of type short occupy %u bytes\n", sizeof(int));
   printf("Variables of  type long occupy %u bytes\n",sizeof(long));
   printf("Variables of type long long occupy %u bytes\n",sizeof(long,long));
   printf("Variables of type float occupy %u bytes\n",sizeof(float));
   printf("Variables of type double occupy %u bytes\n", sizeof(double));
   printf("Variables of type long double occupy %u bytes\n",sizeof(long,double));
   return 0;
}

试试看:字符的建立
%C转换说明符将变量的内容解释为单个字符
%d转换说明符将变量的内容解释为整数

//program 2.15 characters and number
#include 

int main(void)
{
   char first = 'T';
   char second  =  63;

   printf("The first example as a letter looks like this -%c\n", first);
   printf("The first example as a number looks like this-%d\n",first);
   printf("The second example as a letter looks like this-%c\n",second);
   printf("The second example as a number looks like this-%d\n",second);
   return 0;
 }

试试看:用字符的对应整数值进行算术运算
标准库 ctype.h头文件提供的toupper()和tolower()函数可以把字符转换为大写和小写

//program 2.16 Using type char
#include 

int main(void)
{
   char  first = 'A';
   char second  = 'B';
   char  last  ='Z'';

   char number  = 40;
   
    char  ex1 =first +2;
    char  ex2 =second -1;
    char  ex3 = last  +2;

    printf("Character values  %-5c%-5c%-5c\n",ex1,ex2,ex3);
    printf("Numerical equivalents %-5c%-5c%-5c\n",ex1,ex2,ex3);
    printf("The number %d is the code for the character %c\n",number,number);
    return 0;
}
//program 2.17 Calculating  the height of a tree
#include 

int main(void)
{
   long shorty = 0L;
   long lofty = 0L;
   long feet = 0L;
   long inches =0L;
   long shorty_to_lofty =0 L;
   long lofty_to_tree = 0L;

   printf("Enter Lofty's height to the top of his/her head,in whole feet:");
   scanf("%ld",&feet);

你可能感兴趣的:(第二章的程序)