[关闭]
@Rumia 2016-03-23T15:56:45.000000Z 字数 4662 阅读 888

Hw1_Report

C_programing_language

Name : Ding Chenchao
Number : 5140809061



F2C.c (modified)

The original code with a heading "Fahrenheit to Centigrade"

  1. #include <stdio.h>
  2. /*print Fahrenheit - Celsius table for fahr = 0,20,...,300;*/
  3. main() {
  4. float fahr,celsius;
  5. int lower,upper,step;
  6. lower = 0;
  7. upper = 300;
  8. step = 20;
  9. fahr = lower;
  10. printf("Fahrenheit to Centigrade\n");
  11. while (fahr <= upper) {
  12. celsius = (5.0/9.0)*(fahr - 32.0);
  13. printf("%7.0f%14.1f\n",fahr,celsius );
  14. fahr = fahr + step;
  15. }
  16. }

Result for compiling & running

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 :

  1. rumia@marve:~$ cd C_Hw/
  2. rumia@marve:~/C_Hw$ gcc -o try F2C.c # to compile
  3. rumia@marve:~/C_Hw$ ./try # to test & run
  4. Fahrenheit to Centigrade
  5. 0 -17.8
  6. 20 -6.7
  7. 40 4.4
  8. 60 15.6
  9. 80 26.7
  10. 100 37.8
  11. 120 48.9
  12. 140 60.0
  13. 160 71.1
  14. 180 82.2
  15. 200 93.3
  16. 220 104.4
  17. 240 115.6
  18. 260 126.7
  19. 280 137.8
  20. 300 148.9

Report for modifying & testing

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.


C2F.c

Code for printing the corresponding Centigrade to Fahrenheit table

  1. #include <stdio.h>
  2. /*print Celsius - Fahrenheit table for celsius = 0,20,...,300;*/
  3. main() {
  4. float fahr,celsius;
  5. int lower,upper,step;
  6. lower = 0;
  7. upper = 300;
  8. step = 20;
  9. celsius = lower;
  10. printf("Centigrade to Fahrenheit\n");
  11. while (celsius <= upper) {
  12. fahr = (9.0/5.0)*celsius + 32;
  13. printf("%8.1f%12.0f\n",celsius,fahr );
  14. celsius = celsius + step;
  15. }
  16. }

Result for compiling & running

  1. rumia@marve:~$ cd C_Hw/
  2. rumia@marve:~/C_Hw$ gcc -o try C2F.c # to compile
  3. rumia@marve:~/C_Hw$ ./try # to test & run
  4. Centigrade to Fahrenheit
  5. 0.0 32
  6. 20.0 68
  7. 40.0 104
  8. 60.0 140
  9. 80.0 176
  10. 100.0 212
  11. 120.0 248
  12. 140.0 284
  13. 160.0 320
  14. 180.0 356
  15. 200.0 392
  16. 220.0 428
  17. 240.0 464
  18. 260.0 500
  19. 280.0 536
  20. 300.0 572

Report for modifying & testing

According to the formula :

I mainly transform the line :

  1. celsius = (5.0/9.0)*(fahr - 32.0);

into :

  1. 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.


F2C_1.c

Code employing "for" loop instead of "while" loop

  1. #include <stdio.h>
  2. /*print Fahrenheit - Celsius table for fahr = 0,20,...,300;*/
  3. main() {
  4. float fahr,celsius;
  5. printf("Fahrenheit to Centigrade\n");
  6. for (fahr = 0; fahr <= 300; fahr = fahr + 20) {
  7. celsius = (5.0/9.0)*(fahr - 32.0);
  8. printf("%7.0f%14.1f\n", fahr,celsius );
  9. }
  10. }

Result for compiling & running

  1. rumia@marve:~$ cd C_Hw/
  2. rumia@marve:~/C_Hw$ gcc -o try F2C_1.c
  3. rumia@marve:~/C_Hw$ ./try
  4. Fahrenheit to Centigrade
  5. 0 -17.8
  6. 20 -6.7
  7. 40 4.4
  8. 60 15.6
  9. 80 26.7
  10. 100 37.8
  11. 120 48.9
  12. 140 60.0
  13. 160 71.1
  14. 180 82.2
  15. 200 93.3
  16. 220 104.4
  17. 240 115.6
  18. 260 126.7
  19. 280 137.8
  20. 300 148.9

Report for modifying & testing

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" :

  1. 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 :)


F2C_2.c

Code for printing the table marked with "Comfortable!"

  1. #include <stdio.h>
  2. /*print Fahrenheit - Celsius table for fahr = 0,20,...,300;*/
  3. main() {
  4. float fahr,celsius;
  5. int lower,upper,step;
  6. lower = 0;
  7. upper = 300;
  8. step = 20;
  9. fahr = lower;
  10. printf("Fahrenheit to Centigrade\n");
  11. while (fahr <= upper) {
  12. celsius = (5.0/9.0)*(fahr - 32.0);
  13. if (celsius >= 15 && celsius <= 27 ) {
  14. printf("%7.0f%14.1f\t%s\n",fahr,celsius,"Comfortable!" );
  15. }
  16. else
  17. printf("%7.0f%14.1f\n",fahr,celsius );
  18. fahr = fahr + step;
  19. }
  20. }

Result for compiling & running

  1. rumia@marve:~$ cd C_Hw/
  2. rumia@marve:~/C_Hw$ gcc -o try F2C_2.c # to compile
  3. rumia@marve:~/C_Hw$ ./try # to test & run
  4. Fahrenheit to Centigrade
  5. 0 -17.8
  6. 20 -6.7
  7. 40 4.4
  8. 60 15.6 Comfortable!
  9. 80 26.7 Comfortable!
  10. 100 37.8
  11. 120 48.9
  12. 140 60.0
  13. 160 71.1
  14. 180 82.2
  15. 200 93.3
  16. 220 104.4
  17. 240 115.6
  18. 260 126.7
  19. 280 137.8
  20. 300 148.9

Report for modifying & testing

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 :

  1. if (celsius >= 15 && celsius <= 27 ) {
  2. printf("%7.0f%14.1f\t%s\n",fahr,celsius,"Comfortable!" );
  3. }
  4. else
  5. printf("%7.0f%14.1f\n",fahr,celsius );
  6. 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.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注