Advertisement

C Primer Plus--第5章 运算符、表达式和语句

阅读量:

5.11编程练习

复制代码
    /*习题1:把分钟转换为小时和分钟*/
    #include<stdio.h>
    #define HOUR 60   
    
    int main(void)
    {
    	
    	int minute,time_h,time_m;
    	
    printf("Please enter minutes:\n");
    scanf("%d",&minute); // 读入分钟 
    
    	while(minute>0)
    {
    	time_h = minute / HOUR; //得到的小时数 
    	time_m = minute % HOUR;//剩下的分钟数 
    
    	printf("%d hour is %d minutes.\n",time_h,time_m);
    	printf("Enter next values.(<=0 quit):\n");
    	scanf("%d",&minute);
    	}	
    
    	printf("Done!\n");
    	
    	return 0;
     } 
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    Please enter minutes:
    Enter next values.(<=0 quit):
    366
    6 hour is 6 minutes.
    Enter next values.(<=0 quit):
    0
    Done!
    --------------------------------
    Process exited after 6.489 seconds with return value 0
    请按任意键继续. . .
    
    
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    /*习题2:编写一个程序,此程序要求输入一个整数,然后打印出从(包括)输入的值到(包括)比输入的值大10的所有整数值*/
    #include<stdio.h>
    
    int main(void)
    {
    	int num,i;
    	printf("Please enter a number:\n");
    	scanf("%d",&num);
    	
    	while(i++<11)
    printf("%5d",num++);
    	
    printf("\n");
    	
    	return 0;
     } 
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    Please enter a number:
    5
    5    6    7    8    9   10   11   12   13   14   15
    --------------------------------
    Process exited after 4.136 seconds with return value 0
    请按任意键继续. . .
    
    
      
      
      
      
      
      
    
    AI写代码
复制代码
    /*习题3:要求输入天数,然后将该值转换为周数和天数*/ 
    #include<stdio.h>
    #define DAY_PER_WEEK 7;
    
    int main(void)
    {
    	int day,week,num_d;
    	
    	printf("Conver days to weeks and days!\n");
    	printf("Please enter days(<=0 quit):\n");
    	scanf("%d",&day);
    
    	while(day>0)
    	{
    	    week = day / DAY_PER_WEEK;
    	    num_d = day % DAY_PER_WEEK;
    	    printf("%d days are %d weeks, %d days.\n",day,week,num_d);
    	
    	    printf("Enter the next value.(<=0 quit):\n");
    	    scanf("%d",&day);
    	}
    	
    	    printf("Done!\n");
    	
    	    return 0;
     } 
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    Conver days to weeks and days!
    Please enter days(<=0 quit):
    12
    12 days are 1 weeks, 5 days.
    Enter the next value.(<=0 quit):
    11
    11 days are 1 weeks, 4 days.
    Enter the next value.(<=0 quit):
    14
    14 days are 2 weeks, 0 days.
    Enter the next value.(<=0 quit):
    0
    Done!
    --------------------------------
    Process exited after 17.81 seconds with return value 0
    请按任意键继续. . .
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    /*习题4:编写一个程序让用户按厘米输入一个高度值,然后,程序按照厘米和英尺英寸显示这个高度值*/
    #include<stdio.h>
    #define INCHES_FEET 12
    #define CM_INCHES 2.54
    
    int main(void)
    {
    	float centimeters,inches,left;
    	int  feet;
    	printf("Conver cm to feet and inches!\n");
    	printf("Please enter a height in centimeters:\n");
    	scanf("%f",&centimeters);
    	
    	while(centimeters>0)
    	{
    		inches = centimeters / CM_INCHES;
    		feet = (int)inches / INCHES_FEET;
    		left = inches - feet * INCHES_FEET;
    		
    		printf("%.1f cm are %d feet, %.1f inches.\n",centimeters,feet,left);
    		printf("Enter the next centimeters.(=<0 quit):\n");
    		scanf("%f",&centimeters);	
    	}
    	printf("bye!\n");
    	return 0;
     } 
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    Conver cm to feet and inches!
    Please enter a height in centimeters:
    182
    182.0 cm are 5 feet, 11.7 inches.
    Enter the next centimeters.(=<0 quit):
    168
    168.0 cm are 5 feet, 6.1 inches.
    Enter the next centimeters.(=<0 quit):
    0
    bye!
    --------------------------------
    Process exited after 13.92 seconds with return value 0
    请按任意键继续. . .
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    /*第5题、第6题*/
    
    #include<stdio.h>
    
    int main(void)
    {
    	int count,sum;
    	int n;
    	count = 0;
    	sum = 0;
    	
    	printf("Enter the limit numb:\n");
    	scanf("%d",&n);
    	
    	while(count++ <n)
    sum = sum + count;//第6题这里在*count便可;
    	printf("sum = %d\n",sum);
    
    	return 0;
     } 
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    Enter the limit numb:
    100
    sum = 5050
    --------------------------------
    Process exited after 1.373 seconds with return value 0
    请按任意键继续. . .
    
    
      
      
      
      
      
      
    
    AI写代码
复制代码
    /*习题7:编写一个程序,该程序要求输入一个float型数并打印改数的立方值。使用您自己设计的函数来计算改值并打印出来,main()程序把输入的值传递给该函数。*/
    #include<stdio.h>
    
    void my_print(float n);
    int main(void)
    
    {
    	float val;
    	printf("Please enter a float number.\n");
    	scanf("%f",&val);
    	
    	my_print(val);
    	printf("Very Good!\n");
    	return 0;
    }
    
    void my_print(float n)
    
    {
    	printf("The cobe of number %.2f is %.2f.\n",n,n*n*n);
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    Please enter a float number.
    2
    The cobe of number 2.00 is 8.00.
    Very Good!
    --------------------------------
    Process exited after 0.6425 seconds with return value 0
    请按任意键继续. . .
    
    
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    /*习题8:编写一个程序,该程序要求用户输入一个华氏温度。程序以double类型读入温度值,并将它作为一个参数传递给用户提供的函数Temperatures()。该函数将计算相应的摄氏温度和绝对温度,并以小数点右边有两位数字的精度显示这三种温度。它应该用每个值所代表的温度刻度来标识这3个值。下面是将华氏温度转换成摄氏温度的方程:
    Celsius = 1.8 * Fahreneit + 32.0
    通常科学上的绝对温度的刻度是0代表绝对零,是可能温度的下界。下面是将摄氏温度转换为绝对温度的方程:
    Kelvin=Celsius+273.16
    Temperatures()函数使用const来创建代表该转换里的3个常量的符号。main()函数将使用一个循环来允许用户重复地输入温度,当用户输入q或其他非数字值时,循环结束。*/
    #include <stdio.h>
    
    void Temperatures(double n);
    
    int main(void)
    {
    	float Fahrenheit;
    	printf("Please enter Fahrenheit:\n");
    	while (scanf("%f",&Fahrenheit))
    	{
    	    Temperatures(Fahrenheit);
    	    printf("Input the next value:\n");
    	}
    	return 0;
    	printf("Bye!\n");
     } 
     
     void Temperatures(double n)
     {
     	const double A = 1.8;
     	const double B = 32.0;
     	const double C = 273.16;
     	
     	double celsius,kelvin;
     	
     	celsius = A * n + B;
     	kelvin = celsius + C;
     	
     	printf("Display %.2f Fahrenheit = %.2f celsius = %.2f kelvin.\n",n,celsius,kelvin);
     }
     /*按照书中给定公式计算,但是书中公式貌似存在错误。*/
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    Please enter Fahrenheit:
    100
    Display 100.00 Fahrenheit = 212.00 celsius = 485.16 kelvin.
    Input the next value:
    200
    Display 200.00 Fahrenheit = 392.00 celsius = 665.16 kelvin.
    Input the next value:
    y
    Bye!
    --------------------------------
    Process exited after 5.919 seconds with return value 0
    请按任意键继续. . .
    
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

全部评论 (0)

还没有任何评论哟~