Skip to content

Latest commit

 

History

History
592 lines (469 loc) · 10.9 KB

電一乙110910204吳晨知第六次作業.md

File metadata and controls

592 lines (469 loc) · 10.9 KB

程式設計

第九章

電子工程系一年乙班

110910204
吳晨知
教授:李金譚

目錄

筆記

暫存區問題

這是C語言對於暫存器、記憶體、緩衝區相對於其他更高階的程式語言有更複雜與需要了解的記憶體管理,由於這次的嘗試才了解到C語言對於記憶體的控制是有很多需要注意的。

以下是這次經過上網找到的相關詳細資料,測試結果也會寫在這,一樣,建議各位有時間還是自己操作一次最為佳。

作業

9.4 a~i

程式碼:

#include <stdio.h>
#include <stdlib.h>


int main(void) {
  puts("\n(a)\n");
  printf("%15s\n", "123456789012345");
  printf("%.8d\n", 40000);

  int x;
  puts("\n(b)\n");
  printf("輸入十六進制數值(ex. 0x123 or 123): ");
  scanf("%x", &x);
  printf("%#x\n", x);

  puts("\n(c)\n");
  printf("%d\n", 200);
  printf("%+d\n", 200);

  puts("\n(d)\n");
  printf("%#x\n", 256);

  puts("\n(e)\n");
  char s[15];
  printf("%s", "Enter string: ");
  scanf(" %15[^p]", s);
  printf("The input was \"%s\"\n", s);
  scanf("%[^\n]", s);

  puts("\n(f)\n");
  printf("%09g\n", 1.234);

  puts("\n(g)\n");
  int hour = 0;
  int minute = 0;
  int second = 0;
  printf("%s", "Enter a date in the from hh:mm:ss : ");
  scanf(" %d%*c%d%*c%d", &hour, &minute, &second);
  printf("hour = %.2d minute = %.2d second = %.2d\n", hour, minute, second);

  puts("\n(h)\n");
  printf("%s", "Please input \"characters\": ");
  scanf(" %*1[\"]%15[a-zA-Z0-9]%*1[\"]", s);
  printf("%s\n", s);
  scanf("%[^\n]", s);

  puts("\n(i)\n");
  hour = 0;
  minute = 0;
  second = 0;
  printf("%s", "Enter a date in the from hh:mm:ss : ");
  scanf(" %d:%d:%d", &hour, &minute, &second);
  printf("hour = %.2d minute = %.2d second = %.2d\n", hour, minute, second);

}

執行結果:


(a)

123456789012345
00040000

(b)

輸入十六進制數值(ex. 0x123 or 123): 1234
0x1234

(c)

200
+200

(d)

0x100

(e)

Enter string: asdfoqwerppppppppp
The input was "asdfoqwer"

(f)

00001.234

(g)

Enter a date in the from hh:mm:ss : 11:22:33
hour = 11 minute = 22 second = 33

(h)

Please input "characters": "characters"
characters

(i)

Enter a date in the from hh:mm:ss : 12:34:56
hour = 12 minute = 34 second = 56

9.5 a~h

程式碼:

#include <stdio.h>
#include <stdlib.h>


int main(void) {
  puts("\n(a)\n");
  printf("%-10d\n", 10000);

  puts("\n(b)\n");
  printf("%s\n", "This is a string");

  puts("\n(c)\n");
  printf("%*.*f\n", 8, 3, 1024.987654);

  puts("\n(d)\n");
  printf("%#o\n%#X\n%#e\n", 017, 0x17, 1008.83689);

  puts("\n(e)\n");
  printf("% d\n%+d\n", 1000000, 1000000);

  puts("\n(f)\n");
  printf("%10.2E\n", 444.93738);

  puts("\n(g)\n");
  printf("%10.2g\n", 444.93738);

  puts("\n(h)\n");
  printf("%f\n", 10.987);

}

執行結果:


(a)

10000     

(b)

This is a string

(c)

1024.988

(d)

017
0X17
1.008837e+03

(e)

 1000000
+1000000

(f)

  4.45E+02

(g)

   4.4e+02

(h)

10.987000

Fix

b

原本:

printf("%c\n", "This is a string");

會出現format '%c' expects argument of type 'int', but argument 2 has type 'const char*' [-Wformat=]的警告訊息。表示字串應該使用%s的資料型態。

修正後:

printf("%s\n", "This is a string");

c

原本:

printf("%*.*1f\n", 8, 3, 1024.987654);

會出現 too many arguments for format [-Wformat-extra-args]unknown conversion type character '1' in format [-Wformat=] 警告訊息。錯誤的多在f前面加了控制浮點數小數點位數,小數點位數可以由第三個參數3進行控制。

修正後:

printf("%*.*f\n", 8, 3, 1024.987654);

d

原本:

printf("%#o\n%#X\n%#e\n", 17, 17, 1008.83689);

上述敘述不會產生任何警告與錯誤,但看起來作者想要輸出8進位、16進位與十進位浮點數的資料型態,因此將第二個17與第三個參數17改為0170x17,使資料正確顯示。

修正後:

printf("%#o\n%#X\n%#e\n", 017, 0x17, 1008.83689);

e

原本:

printf("% ld\n%+ld\n", 1000000, 1000000);

此會產生format '%ld' expects argument of type 'long int', but argument 2 has type 'int' [-Wformat=]警告訊息。因為輸入的參數值兩者都不大到需要使用longlong long型態資料,因此修正使用%d的正負整數型態。

修正後:

printf("% d\n%+d\n", 1000000, 1000000);

h

原本:

printf("%d\n", 10.987);

此會產生format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]警告訊息。因為這個是浮點數資料型態,不應該使用整數資料型態去格式化。

修正後:

printf("%f\n", 10.987);

9.7 %d 和 %i 的差異

程式碼:

#include <stdio.h>
#include <stdlib.h>


int main(void) {
  int x;
  int y;

  for(size_t i = 0; i < 4; ++i) {
    printf("%s", "Please enter the same number as the two numbers: ");
    scanf("%i %d", &x, &y);
    printf("\n%d %d\n\n", x, y);
  }
}

執行結果:

Please enter the same number as the two numbers: 10 10

10 10

Please enter the same number as the two numbers: -10 -10

-10 -10

Please enter the same number as the two numbers: 010 010

8 10

Please enter the same number as the two numbers: 0x10 0x10

16 0

9.10 溫度轉換

編寫一個程式,將整數活是溫度從0到212度轉換為精確度2位的浮點數攝氏溫度。

請使用下列公式進行計算:

celsius = 5.0 / 9.0 * (fahrenheit - 32);

輸出應印出在兩個靠右對齊的列中,每列10個字元,而且攝氏溫度應加上正號或負號。

程式碼:

#include <stdio.h>
#include <stdlib.h>


int main(void) {
  float celsius;

  printf("%10s%10s\n", "Fahrenheit", "Celsius");
  for(float fahrenheit = 0; fahrenheit <= 212; ++fahrenheit) {
    celsius = 5.0 / 9.0 * (fahrenheit - 32);

    printf("%+10.3f%+10.3f\n", fahrenheit, celsius);

  }
}

執行結果:

Fahrenheit   Celsius
    +0.000   -17.778
    +1.000   -17.222
    +2.000   -16.667
    +3.000   -16.111
    +4.000   -15.556
    +5.000   -15.000
    +6.000   -14.444
    +7.000   -13.889
    +8.000   -13.333
    +9.000   -12.778
   +10.000   -12.222
   +11.000   -11.667
   +12.000   -11.111
   +13.000   -10.556
   +14.000   -10.000
   +15.000    -9.444
   +16.000    -8.889
   +17.000    -8.333
   +18.000    -7.778
   +19.000    -7.222
   +20.000    -6.667
   +21.000    -6.111
   +22.000    -5.556
   +23.000    -5.000
   +24.000    -4.444
   +25.000    -3.889
   +26.000    -3.333
   +27.000    -2.778
   +28.000    -2.222
   +29.000    -1.667
   +30.000    -1.111
   +31.000    -0.556
   +32.000    +0.000
   +33.000    +0.556
   +34.000    +1.111
   +35.000    +1.667
   +36.000    +2.222
   +37.000    +2.778
   +38.000    +3.333
   +39.000    +3.889
   +40.000    +4.444
   +41.000    +5.000
   +42.000    +5.556
   +43.000    +6.111
   +44.000    +6.667
   +45.000    +7.222
   +46.000    +7.778
   +47.000    +8.333
   +48.000    +8.889
   +49.000    +9.444
   +50.000   +10.000
   +51.000   +10.556
   +52.000   +11.111
   +53.000   +11.667
   +54.000   +12.222
   +55.000   +12.778
   +56.000   +13.333
   +57.000   +13.889
   +58.000   +14.444
   +59.000   +15.000
   +60.000   +15.556
   +61.000   +16.111
   +62.000   +16.667
   +63.000   +17.222
   +64.000   +17.778
   +65.000   +18.333
   +66.000   +18.889
   +67.000   +19.444
   +68.000   +20.000
   +69.000   +20.556
   +70.000   +21.111
   +71.000   +21.667
   +72.000   +22.222
   +73.000   +22.778
   +74.000   +23.333
   +75.000   +23.889
   +76.000   +24.444
   +77.000   +25.000
   +78.000   +25.556
   +79.000   +26.111
   +80.000   +26.667
   +81.000   +27.222
   +82.000   +27.778
   +83.000   +28.333
   +84.000   +28.889
   +85.000   +29.444
   +86.000   +30.000
   +87.000   +30.556
   +88.000   +31.111
   +89.000   +31.667
   +90.000   +32.222
   +91.000   +32.778
   +92.000   +33.333
   +93.000   +33.889
   +94.000   +34.444
   +95.000   +35.000
   +96.000   +35.556
   +97.000   +36.111
   +98.000   +36.667
   +99.000   +37.222
  +100.000   +37.778
  +101.000   +38.333
  +102.000   +38.889
  +103.000   +39.444
  +104.000   +40.000
  +105.000   +40.556
  +106.000   +41.111
  +107.000   +41.667
  +108.000   +42.222
  +109.000   +42.778
  +110.000   +43.333
  +111.000   +43.889
  +112.000   +44.444
  +113.000   +45.000
  +114.000   +45.556
  +115.000   +46.111
  +116.000   +46.667
  +117.000   +47.222
  +118.000   +47.778
  +119.000   +48.333
  +120.000   +48.889
  +121.000   +49.444
  +122.000   +50.000
  +123.000   +50.556
  +124.000   +51.111
  +125.000   +51.667
  +126.000   +52.222
  +127.000   +52.778
  +128.000   +53.333
  +129.000   +53.889
  +130.000   +54.444
  +131.000   +55.000
  +132.000   +55.556
  +133.000   +56.111
  +134.000   +56.667
  +135.000   +57.222
  +136.000   +57.778
  +137.000   +58.333
  +138.000   +58.889
  +139.000   +59.444
  +140.000   +60.000
  +141.000   +60.556
  +142.000   +61.111
  +143.000   +61.667
  +144.000   +62.222
  +145.000   +62.778
  +146.000   +63.333
  +147.000   +63.889
  +148.000   +64.444
  +149.000   +65.000
  +150.000   +65.556
  +151.000   +66.111
  +152.000   +66.667
  +153.000   +67.222
  +154.000   +67.778
  +155.000   +68.333
  +156.000   +68.889
  +157.000   +69.444
  +158.000   +70.000
  +159.000   +70.556
  +160.000   +71.111
  +161.000   +71.667
  +162.000   +72.222
  +163.000   +72.778
  +164.000   +73.333
  +165.000   +73.889
  +166.000   +74.444
  +167.000   +75.000
  +168.000   +75.556
  +169.000   +76.111
  +170.000   +76.667
  +171.000   +77.222
  +172.000   +77.778
  +173.000   +78.333
  +174.000   +78.889
  +175.000   +79.444
  +176.000   +80.000
  +177.000   +80.556
  +178.000   +81.111
  +179.000   +81.667
  +180.000   +82.222
  +181.000   +82.778
  +182.000   +83.333
  +183.000   +83.889
  +184.000   +84.444
  +185.000   +85.000
  +186.000   +85.556
  +187.000   +86.111
  +188.000   +86.667
  +189.000   +87.222
  +190.000   +87.778
  +191.000   +88.333
  +192.000   +88.889
  +193.000   +89.444
  +194.000   +90.000
  +195.000   +90.556
  +196.000   +91.111
  +197.000   +91.667
  +198.000   +92.222
  +199.000   +92.778
  +200.000   +93.333
  +201.000   +93.889
  +202.000   +94.444
  +203.000   +95.000
  +204.000   +95.556
  +205.000   +96.111
  +206.000   +96.667
  +207.000   +97.222
  +208.000   +97.778
  +209.000   +98.333
  +210.000   +98.889
  +211.000   +99.444
  +212.000  +100.000

參考資料

http://squall.cs.ntou.edu.tw/cprog/practices/scanfCommonTraps.pdf