C程序(5.3-5.7)

来源:互联网 发布:dota2新天梯 知乎 编辑:程序博客网 时间:2024/05/17 06:03

53    86_7

# include <stdio.h>

# define switch_1 3.785f      //1加仑约等于3.785

# define switch_2 1.609f      //1英里约等于1.609公里

 

int main(void)

{

       float mile,gallon,litre;

       float a,b;

 

       printf("please enter the miles you went and the gallons you used:/n");

       scanf("%f%f",&mile,&gallon);

       a=mile/gallon;

       printf("one gallon can go %.1f miles./n",a);

       b=a*switch_2/switch_1;

       litre=100.0f/b;

       printf("go 100 kilometres need %.1f litres./n",litre);

      

       return 0;

}

 

 

54    114_8

# include <stdio.h>

const double a=1.8;

const double b=32.0;

const double c=273.16;

int Temperature (double Fahren);

 

int main (void)

{

       double Fahrenheit;

       printf("Please enter a temperature,use Fahrenheit(q to quit):/n");

       while (scanf("%lf",&Fahrenheit)==1)

       {

              Temperature (Fahrenheit);

              printf("Please enter a temperature,use Fahrenheit(q to quit):/n");

       }

       return 0;

}

 

int Temperature (double Fahren)

{

       double Celsius,Kelvin;

       Celsius=a*Fahren+b;

       Kelvin=Celsius+c;

       printf("the temperature input is %.2f F./n",Fahren);

       printf("its Celsius is %.2f C./n",Celsius);

       printf("its Kelvin is %.2f K./n",Kelvin);

       return 0;

}

 

 

55      150_6

# include <stdio.h>

# include <string.h>     //提供strlen()函数

 

int main (void)

{

       char word[30];

 

       printf("Please enter a word./n");

       scanf("%s",word);

       printf("the word you enter is %s.",word);

       printf("/n");

 

       for(int i=strlen(word);i>=0;i--)

       {

              printf("%c",word[i]);

       }

       printf("/n");

 

       return 0;

}

 

 

 

56     150_8

# include <stdio.h>

float calculate (float,float);

 

int main(void)

{

       float a,b;

       printf("please enter two numbers(q to quit):/n");

 

       while ( scanf("%f%f",&a,&b)==2 )

       {

              printf("the result is %f./n",calculate(a,b));

              printf("please enter another two numbers(q to quit):/n");

       }

       return 0;

}

 

float calculate (float m,float n)

{

       float c,d;

       d=(m<n)?n-m:m-n;

       c=d/(m*n);

 

       return c;

}

 

 

 

57      150_11

# include <stdio.h>

double func1 (int);

double func2 (int);

 

int main (void)

{

       int terms;

       printf("please enter the number(q to quit):/n");

 

       while ( scanf("%d",&terms) )

       {

              printf("when terms=%d,func1=%f,func2=%f./n",

                        terms,func1(terms),func2(terms));

              printf("please enter other number(q to quit):/n");

       }

       return 0;

}

 

double func1 (int n1)

{

       double f1=0;

       for(n1;n1>=1;n1--)

       {

              f1+=1.0/n1;

       }

       return f1;

}

 

double func2 (int n2)

{

       double f2=0;

       int i;

 

       for(i=1;i<=n2;i++)

       {

              if(i%2==0)

                     f2+=(-1.0/i);

              else

                     f2+=1.0/i;

       }

       return f2;

}

原创粉丝点击