zoukankan      html  css  js  c++  java
  • 1.2 Variables and Arithmetic Expressions

    
    
    /*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("	%d	%d
    ", Fahr, Celsius);
            lower = lower + step;
        }
     
    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}, {1}", Fahr, Celsius); Fahr = Fahr + step; } Console.ReadLine(); } } } 

    #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 ", Fahr, Celsius); Fahr = Fahr + step; } return 0; }

    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.

  • 相关阅读:
    poj3167
    poj2752 bzoj3670
    poj2886
    poj3294
    [luoguP2564][SCOI2009]生日礼物(队列)
    [luoguP1866]滑动窗口(单调队列)
    [luoguP1198][JSOI2008] 最大数(线段树 || 单调栈)
    [HDU4348]To the moon(主席树)
    [luoguP1168]中位数(主席树+离散化)
    [HDU4417]Super Mario(主席树+离散化)
  • 原文地址:https://www.cnblogs.com/limeina/p/3573708.html
Copyright © 2011-2022 走看看