首页 > 其他 > 详细

1.2 Variables and Arithmetic Expressions

时间:2014-03-01 02:43:57      阅读:446      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣

bubuko.com,布布扣
/*Print Fahrenheit-Celsius table: for fahr =0, 20, ... ,300*/
void ConvertFahrenheittoCelsius()
{
    int Fahr, Celsius;
    int lower, higher, step;
    
    lower = 0;
    higher = 300;
    step = 20;

    while (lower < higher)
    {
        Fahr = lower;
        Celsius = 5 * (Fahr - 32) / 9;
        printf("\t%d\t%d\n", Fahr, Celsius);
        lower = lower + step;
    }
bubuko.com,布布扣
 
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            float Fahr, Celsius;
            int lower, higher, step;

            lower = 0;
            higher = 300;
            step = 20;

            Fahr = lower;
            while (Fahr < higher)
            {
                Celsius = (float)((5.0 / 9.0)*(Fahr - 32.0)); 
Console.WriteLine(
"{0}, \t {1}", Fahr, Celsius); Fahr = Fahr + step; } Console.ReadLine(); } } } 
bubuko.com,布布扣
bubuko.com,布布扣

#include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { float Fahr, Celsius; int lower, higher, step; lower = 0; higher = 300; step = 20;    Fahr = lower; /* The int is converted to float before the operation is done.*/ while (Fahr <= higher) { Celsius = (5.0 / 9.0) * (Fahr - 32); printf("%3.0f %6.1f \n", Fahr, Celsius); Fahr = Fahr + step; } return 0; }
bubuko.com,布布扣

 

The reason for multiplying by 5 and then dividing by 9 instead of just multiplying by 5/9 is that in C,as in many other languages,integer division truncates: any fractional part is discarded.

Printf is a general-purpose output formatting function, and it is just a useful function from the standard library of functions that are normally accessible to C programs.

  

In C Sharp,double cannot implicitly be converted to float.


The printf conversion specification %3.0f says that a floating-point number is to be printed at least three
characters wide, with no decimal point and no fraction digits.

1.2 Variables and Arithmetic Expressions,布布扣,bubuko.com

1.2 Variables and Arithmetic Expressions

原文:http://www.cnblogs.com/limeina/p/3573708.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!