@Rumia
2016-03-23T15:56:45.000000Z
字数 4662
阅读 888
C_programing_language
Name : Ding Chenchao
Number : 5140809061
The original code with a heading "Fahrenheit to Centigrade"
#include <stdio.h>
/*print Fahrenheit - Celsius table for fahr = 0,20,...,300;*/
main() {
float fahr,celsius;
int lower,upper,step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
printf("Fahrenheit to Centigrade\n");
while (fahr <= upper) {
celsius = (5.0/9.0)*(fahr - 32.0);
printf("%7.0f%14.1f\n",fahr,celsius );
fahr = fahr + step;
}
}
In order to compile this sourse code, I use bash to change directory to where it exists, and then compile it with gcc. Finally I run it, and the result is printed on terminal :
rumia@marve:~$ cd C_Hw/
rumia@marve:~/C_Hw$ gcc -o try F2C.c # to compile
rumia@marve:~/C_Hw$ ./try # to test & run
Fahrenheit to Centigrade
0 -17.8
20 -6.7
40 4.4
60 15.6
80 26.7
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9
This version is modified from the original sourse code to look more symmentric when executed to print the table. Adding the Heading "Fahrenheit to Centigrade" can be realized by print it before the loop.
Code for printing the corresponding Centigrade to Fahrenheit table
#include <stdio.h>
/*print Celsius - Fahrenheit table for celsius = 0,20,...,300;*/
main() {
float fahr,celsius;
int lower,upper,step;
lower = 0;
upper = 300;
step = 20;
celsius = lower;
printf("Centigrade to Fahrenheit\n");
while (celsius <= upper) {
fahr = (9.0/5.0)*celsius + 32;
printf("%8.1f%12.0f\n",celsius,fahr );
celsius = celsius + step;
}
}
rumia@marve:~$ cd C_Hw/
rumia@marve:~/C_Hw$ gcc -o try C2F.c # to compile
rumia@marve:~/C_Hw$ ./try # to test & run
Centigrade to Fahrenheit
0.0 32
20.0 68
40.0 104
60.0 140
80.0 176
100.0 212
120.0 248
140.0 284
160.0 320
180.0 356
200.0 392
220.0 428
240.0 464
260.0 500
280.0 536
300.0 572
According to the formula :
I mainly transform the line :
celsius = (5.0/9.0)*(fahr - 32.0);
into :
fahr = (9.0/5.0)*celsius + 32;
i.e. :
to calculate the fahrenheit given the centigrade, and exchange them in "printf" in order to print a whole "C2F" table.
However I haven't changed the values of "lower","upper" and "step", therefore this program results in printing a Celsius - Fahrenheit table for celsius = 0,20,...,300.
Code employing "for" loop instead of "while" loop
#include <stdio.h>
/*print Fahrenheit - Celsius table for fahr = 0,20,...,300;*/
main() {
float fahr,celsius;
printf("Fahrenheit to Centigrade\n");
for (fahr = 0; fahr <= 300; fahr = fahr + 20) {
celsius = (5.0/9.0)*(fahr - 32.0);
printf("%7.0f%14.1f\n", fahr,celsius );
}
}
rumia@marve:~$ cd C_Hw/
rumia@marve:~/C_Hw$ gcc -o try F2C_1.c
rumia@marve:~/C_Hw$ ./try
Fahrenheit to Centigrade
0 -17.8
20 -6.7
40 4.4
60 15.6
80 26.7
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9
To employ "for" loop, I delete the declaration line as well as assignment statements of the variables "lower","upper" and "step", for all of their functions can be realized within "()" following "for" :
for (fahr = 0; fahr <= 300; fahr = fahr + 20) {
- "fahr = 0" in place of "lower = 0"
- "fahr <= 300" in place of "upper = 300"
- "fahr = fahr + 20" in place of "step = 20"
In the last it turns out the same as code with "while" loop :)
Code for printing the table marked with "Comfortable!"
#include <stdio.h>
/*print Fahrenheit - Celsius table for fahr = 0,20,...,300;*/
main() {
float fahr,celsius;
int lower,upper,step;
lower = 0;
upper = 300;
step = 20;
fahr = lower;
printf("Fahrenheit to Centigrade\n");
while (fahr <= upper) {
celsius = (5.0/9.0)*(fahr - 32.0);
if (celsius >= 15 && celsius <= 27 ) {
printf("%7.0f%14.1f\t%s\n",fahr,celsius,"Comfortable!" );
}
else
printf("%7.0f%14.1f\n",fahr,celsius );
fahr = fahr + step;
}
}
rumia@marve:~$ cd C_Hw/
rumia@marve:~/C_Hw$ gcc -o try F2C_2.c # to compile
rumia@marve:~/C_Hw$ ./try # to test & run
Fahrenheit to Centigrade
0 -17.8
20 -6.7
40 4.4
60 15.6 Comfortable!
80 26.7 Comfortable!
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9
To print additional information at the third column such as "Comfortable!" when the centigrade is ranging from 15 to 27, I choose to use an "if-else" structure to control the output :
if (celsius >= 15 && celsius <= 27 ) {
printf("%7.0f%14.1f\t%s\n",fahr,celsius,"Comfortable!" );
}
else
printf("%7.0f%14.1f\n",fahr,celsius );
fahr = fahr + step;
When the hypothesis of "if" is true, sentences in "{}" is going to be executed. And in order to print an extra string "Confortable!", I add "%s" (format for string type) to the end of "%7.0f%14.1f". However if I compile & run it now, you'll find there is no space between the 2nd and 3rd column. To address this problem I apply "\t" before "%s", and finally it looks better.
When false, the sentence following "else" is to be executed, here I just keep the output the same as F2C.c.