fork download
  1. //Key
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5. void problem1A()
  6. {
  7. // ให้เขียนโปรแกรมเพื่อคำนวณระยะเวลา (time) ที่ต้องใช้ในการเดินทางจากการขับรถตามความเร็ว (speed)
  8. // และระยะทาง (distance) ที่กำหนด
  9. // โดยสมมุติว่ารถที่ขับมีตัววัดความเร็วเป็นหน่วยไมล์ต่อชั่วโมง แต่ระยะทางในการเดินทางวัดเป็นหน่วยกิโลเมตร
  10. // สำหรับระยะทาง 1 ไมล์ จะมีค่าเท่ากับ 1.609 กิโลเมตร
  11.  
  12. // ให้โปรแกรมมีรูปแบบการทำงานดังตัวอย่างนี้
  13. // Enter speed (miles/hour): 90
  14. // Enter distance (kilometer): 445
  15. // Time to destination is 3.07 hour(s)
  16.  
  17. // ******* เริ่มต้น code ******* //
  18. // ************************** //
  19.  
  20. // ประกาศตัวแปร สำหรับเก็บค่าระยะทางและความเร็วเป็นจำนวนเต็ม
  21. // และเวลาที่ใช้ในการเดินทางเป็นจำนวนจริง (** 1 คะแนน **)
  22. // ******* เขียน code ที่นี่ ******* //
  23. int disrance, speed;
  24. float time;
  25. // รับค่าความเร็วเป็นหน่วยไมล์ต่อชั่วโมง (** 1 คะแนน **)
  26. // ******* เขียน code ที่นี่ ******* //
  27. printf("Enter speed (miles/hour): ");
  28. scanf("%d", &speed);
  29. // รับค่าระยะทางเป็นหน่วยกิโลเมตร (** 1.5 คะแนน **)
  30. // ******* เขียน code ที่นี่ ******* //
  31. printf("Enter distance (kilometer): ");
  32. scanf("%d", &disrance);
  33. // คำนวณเวลาที่ใช้ในการเดินทาง (** 1.5 คะแนน **)
  34. // ******* เขียน code ที่นี่ ******* //
  35. time = (float)(disrance / 1.609) / speed;
  36. // แสดงผลเวลาที่ใช้ในการเดินทางโดยให้มีเลขทศนิยม 3 หลัก (** 1 คะแนน **)
  37. // ******* เขียน code ที่นี่ ******* //
  38. printf("Time to destination is %.2f hour(s)", time);
  39. printf("\n");
  40. }
  41.  
  42. void problem2A()
  43. {
  44. // ให้เขียนโปรแกรมเพื่อคำนวณดอกเบี้ยเงินฝากสำหรับการฝากเงินไม่เกิน 1 ปี โดยมีอัตราดังนี้
  45. // ฝากไม่ถึง 3 เดือน ให้ดอกเบี่้ย 0.75%
  46. // ฝากครบ 3 เดือนขึ้นไป แต่ไม่ถึง 6 เดือน ให้ดอกเบี้ย 1.25%
  47. // ฝากครบ 6 เดือนขึ้นไป แต่ไม่ถึง 9 เดือน ให้ดอกเบี้ย 1.75%
  48. // ฝากครบ 9 เดือนขึ้นไป ให้ดอกเบี้ย 2%
  49.  
  50. // ให้โปรแกรมมีรูปแบบการทำงานดังตัวอย่างนี้
  51. // Enter deposit amount: 1500
  52. // Enter duration (1-12): 6
  53. // Your interest is 26.25 and your total balance is 1526.25
  54. // หรือ
  55. // Enter deposit amount: 1500
  56. // Enter duration (1-12): 13
  57. // Duration is not valid.
  58.  
  59. // ******* เริ่มต้น code ******* //
  60. // ************************** //
  61.  
  62. // ประกาศตัวแปร สำหรับเก็บค่าเงินฝาก (deposit) และระยะเวลาฝาก (duration) เป็นจำนวนเต็ม
  63. // และค่าดอกเบี้ย (interest) เป็นจำนวนจริง (** 0.25 คะแนน **)
  64. // ******* เขียน code ที่นี่ ******* //
  65. int deposit, duration;
  66. float interest;
  67. // รับค่าเงินฝากและระยะเวลา (** 0.75 คะแนน **)
  68. // ******* เขียน code ที่นี่ ******* //
  69. printf("Enter deposit amount: ");
  70. scanf("%d", &deposit);
  71. printf("Enter duration (1-12): ");
  72. scanf("%d", &duration);
  73. // ถ้าระยะเวลาน้อยกว่า 1 หรือมากกว่า 12 ให้แสดงข้อความ "Duration is not valid." (** 1 คะแนน **)
  74. // แต่ถ้าระยะเวลาอยู่ในช่วง 1-12 ก็ให้คำนวณหาดอกเบี้ย และแสดงผลค่าดอกเบี้ย (interest) ที่จะได้รับ
  75. // รวมทั้งค่ายอดเงินที่บวกดอกเบี้ยแล้วโดยให้มีเลขทศนิยม 2 หลัก ซึ่งจะมีประกอบด้วยการทำงานดังนี้
  76. // เช็คเงื่อนไขสำหรับแต่ละอัตรา (** 2 คะแนน **)
  77. // ในแต่ละเงื่อนไข คำนวณค่าดอกเบี่้ย (** 1 คะแนน **)
  78. // แสดงผลลัพธ์จากดอกเบี้ยที่คำนวณได้ (** 1 คะแนน **)
  79. // ******* เขียน code ที่นี่ ******* //
  80. if (duration >=1 && duration <= 12)
  81. {
  82. if (duration < 3) interest = (float)(deposit*0.75)/100;
  83. else if (duration < 6) interest = (float)(deposit*1.25) / 100;
  84. else if (duration < 9) interest = (float)(deposit*1.75) / 100;
  85. else if (duration >= 9) interest = (float)(deposit*2.00) / 100;
  86. printf("Your interest is %.2f and your total balance is %.2f",interest,interest+deposit);
  87. }
  88. else
  89. {
  90. printf("Duration is not valid.");
  91. }
  92. printf("\n");
  93. }
  94.  
  95. void problem3A()
  96. {
  97. // ให้เขียนโปรแกรมที่รับค่าตัวเลขจำนวนเต็ม 1 ตัว ทีมีค่าตั้งแต่ 2 ขึ้นไปแต่ไม่เกิน 15 แทนค่าความกว้างครึ่งหนึ่งของรูป
  98. // แล้วทำการแสดงผลเป็นรูปสามเหลี่ยมแบบตะแคงขวาตามความกว้างที่กำหนด ดังตัวอย่างต่อไปนี้
  99.  
  100. // Enter half of width: 4
  101. // *
  102. // *
  103. // *
  104. // *
  105. // *
  106. // *
  107. // *
  108. // *
  109.  
  110. // จากตัวอย่างข้างต้น
  111. // บรรทัดแรกจะแสดงช่องว่าง 0 ตำแหน่ง ตามด้วยดอกจัน
  112. // บรรทัดที่สองจะแสดงช่องว่าง 2 ตำแหน่ง ตามด้วยดอกจัน
  113. // บรรทัดที่สามจะแสดงช่องว่าง 4 ตำแหน่ง ตามด้วยดอกจัน
  114. // บรรทัดที่สี่จะแสดงช่องว่าง 6 ตำแหน่ง ตามด้วยดอกจัน
  115. // ...
  116.  
  117. // ******* เริ่มต้น code ******* //
  118. // ************************** //
  119.  
  120. // ประกาศค่าตัวแปรสำหรับเก็บค่าความกว้างครึ่งหนึ่ง (width) เป็นจำนวนเต็ม
  121. // และตัวแปรสำหรับวน loop (** 0.25 คะแนน **)
  122. // ******* เขียน code ที่นี่ ******* //
  123. int width, i, j;
  124. // รับค่าความกว้างที่แทนครึ่งหนึ่งของความกว้างของรูป (** 0.25 คะแนน **)
  125. // ******* เขียน code ที่นี่ ******* //
  126. printf("Enter half of width: ");
  127. scanf("%d", &width);
  128. // วาดครึ่งบนของสามเหลี่ยม (hint: ใช้ loop ซ้อนกัน 2 ชั้น) โดยมีการดำเนินการที่เกี่ยวข้องดังนี้
  129. // วน loop นอกสำหรับแต่ละบรรทัด (** 1.5 คะแนน **)
  130. // วน loop ในสำหรับแสดงช่องว่าง (** 1.5 คะแนน **)
  131. // แสดงค่าช่องว่างและดอกจัน (** 0.5 คะแนน **)
  132. // ******* เขียน code ที่นี่ ******* //
  133. for (i = width; i >0; i--)
  134. {
  135. for (j = i; j < width; j++)
  136. {
  137. printf(" ");
  138. }
  139. printf("*\n");
  140. }
  141.  
  142. // วาดครึ่งล่างของสามเหลี่ยม (ใช้หลักการเดียวกับครึ่งบน)
  143. // วน loop นอกสำหรับแต่ละบรรทัด (** 1 คะแนน **)
  144. // วน loop ในสำหรับแสดงช่องว่าง (** 0.5 คะแนน **)
  145. // แสดงค่าช่องว่างและดอกจัน (** 0.5 คะแนน **)
  146. // ******* เขียน code ที่นี่ ******* //
  147. for (i = 0; i < width; i++)
  148. {
  149. for (j = i; j < width - 1; j++)
  150. {
  151. printf(" ");
  152. }
  153. printf("*\n");
  154. }
  155. printf("\n");
  156. }
  157.  
  158. void problem4A()
  159. {
  160. // ให้เขียนโปรแกรมที่รับคำภาษาอังกฤษ 1 คำที่มีความยาวไม่เกิน 15 ตัวอักษร
  161. // จากนั้นให้แปลงข้อความดังกล่าวให้เป็นตัวอักษรตัวใหญ่ทั้งหมด โดยให้มีรูปแบบดังนี้
  162.  
  163. // Enter word: Elephant
  164. // Upper-case word is ELEPHANT
  165.  
  166. // สำหรับการแปลงค่าตัวอักษรตัวเล็กไปเป็นตัวใหญ่ให้ลบออกด้วย 32 เช่น 'b' - 32 จะได้ค่าเป็น 'B'
  167. // แต่จะต้องตรวจสอบก่อนว่าตัวอักษรนั้นเป็นตัวอักษรตัวเล็กหรือไม่ หากไม่ใช่ก็ไม่ต้องแปลงค่า (แสดงผลด้วยค่าเดิม)
  168. // โดยที่ตัวอักษรตัวเล็กจะมีค่าระหว่าง 97 ถึง 122 เช่น 'b' จะมีค่า 98
  169.  
  170. // ******* เริ่มต้น code ******* //
  171. // ************************** //
  172.  
  173. // ประกาศค่าตัวแปรสำหรับเก็บค่าคำ (word) และตัวแปรสำหรับวน loop (** 0.75 คะแนน **)
  174. // ******* เขียน code ที่นี่ ******* //
  175. char word[16];
  176. int i,len;
  177. // รับคำภาษาอังกฤษ (** 1 คะแนน **)
  178. // ******* เขียน code ที่นี่ ******* //
  179. printf("Enter word: ");
  180. scanf("%s", word);
  181. len = strlen(word);
  182. // แสดงข้อความ "Upper-case word is " (** 0.25 คะแนน **)
  183. // ******* เขียน code ที่นี่ ******* //
  184. printf("Upper-case word is ");
  185. // วน loop แปลงตัวอักษรแต่ละตัวให้เป็นตัวใหญ่ โดยมีการดำเนินการที่เกี่ยวข้องดังนี้
  186. // วน loop (** 1 คะแนน **)
  187. // ในแต่ละรอบ ตรวจสอบว่าเป็นตัวอักษรตัวเล็กหรือไม่ (** 1 คะแนน **)
  188. // ในแต่ละรอบ หากเป็นตัวอักษรตัวเล็กให้แปลงค่าตัวอักษรเป็นตัวใหญ่ (** 1 คะแนน **)
  189. // ในแต่ละรอบ แสดงค่าตัวอักษรผลลัพธ์ (** 1 คะแนน **)
  190. // ******* เขียน code ที่นี่ ******* //
  191. for (i = 0; i < len;i++)
  192. if (word[i] >= 97 && word[i] <= 122)
  193. {
  194. word[i] = word[i] - 32;
  195. printf("%c", word[i]);
  196. }
  197. else printf("%c", word[i]);
  198. printf("\n");
  199. }
  200.  
  201. void problem5A()
  202. {
  203. // เขียนโปรแกรมเพื่อใช้แปลงจำนวนเงินบาทไปเป็นจำนวนเงินในสกุลเงินต่างๆ ได้แก่ USD, EUR และ CNY
  204. // โดยที่ 1 USD = 32.68 บาท
  205. // 1 EUR = 44.94 บาท
  206. // 1 CNY = 5.41 บาท
  207.  
  208. // ให้โปรแกรมทำงานในรูปแบบดังนี้
  209. // Convert from Thai Baht to
  210. // 1. US Dollar (USD)
  211. // 2. Eurozone (EUR)
  212. // 3. Chinese Yuan (CNY)
  213. // Select 1, 2 or 3: 3
  214. // Enter money (Baht): 1000
  215. // 1000.00 Baht = 184.84 CNY
  216. // Enter 0 to exit or other number to convert again: 1
  217. // Convert from Thai Baht to
  218. // 1. US Dollar (USD)
  219. // 2. Eurozone (EUR)
  220. // 3. Chinese Yuan (CNY)
  221. // Select 1, 2 or 3:
  222. // ...
  223.  
  224. // ให้โปรแกรมแสดง menu ให้เลือก เพื่อแปลงเงินบาทไปเป็นเงินในสกุล USD, EUR หรือ CNY
  225. // โดยจะรับค่าเป็นค่า 1-3 จากนั้นให้รับค่าเงินบาท แล้วจึงทำการคำนวณค่าเงินและแสดงผลลัพธ์
  226. // จากนั้นให้โปรแกรมถามว่าจะทำการแปลงอีกหรือไม่ ถ้าใส่ตัวเลขใดๆ ที่ไม่ใช่ 0 ก็จะวนกลับไปเริ่มต้นใหม่
  227. // แต่ถ้าใส่ค่า 0 ก็จะจบการทำงานของโปแกรม
  228.  
  229. // ******* เริ่มต้น code ******* //
  230. // ************************** //
  231.  
  232. // ประกาศค่าตัวแปรที่เก็บค่า choice ที่เลือก (1-3) เป็นจำนวนเต็ม, ตัวแปรที่เก็บค่าเงินบาทเป็นจำนวนจริง,
  233. // และตัวแปรที่เก็บค่าสำหรับเช็คว่าจะจบโปรแกรม (exit) หรือไม่เป็นจำนวนเต็ม (** 0.25 คะแนน **)
  234. // ******* เขียน code ที่นี่ ******* //
  235. int choice, exit;
  236. float baht;
  237. // กำหนด loop เพื่อควบคุมการให้โปรแกรมสามารถแปลงค่าต่อได้เรื่อยๆ หากยังใส่ค่า exit ที่ไม่ใช่ 0
  238. // โดยให้เลือกใช้ loop ที่เหมาะสมจาก for, while หรือ do-while (** 1 คะแนน **)
  239. // สำหรับภายใน loop ให้มีการดำเนินการดังนี้
  240. // แสดงค่า menu (** 1 คะแนน **)
  241. // รับค่าตัวเลือกสกุลเงิน (1-3) (** 0.25 คะแนน **)
  242. // รับค่าเงินบาท (** 0.25 คะแนน **)
  243. // ใช้คำสั่ง switch-case หรือ if-else เพื่อแยกคำนวณตามสกุลเงิน (** 1 คะแนน **)
  244. // ในแต่ละกรณี คำนวณค่าเงินตามสกุลเงินที่เลือกพร้อมทั้งแสดงผลโดยให้แสดงทศนิยม 2 หลัก (** 2 คะแนน **)
  245. // รับค่าสำหรับเช็คว่าจะทำการแปลงค่าเงินต่อหรือออกจากโปรแกรม (** 0.25 คะแนน **)
  246. // ******* เขียน code ที่นี่ ******* //
  247. do
  248. {
  249. printf("Convert from Thai Baht to\n");
  250. printf("1. US Dollar (USD)\n");
  251. printf("2. Eurozone (EUR)\n");
  252. printf("3. Chinese Yuan (CNY)\n");
  253. printf("Select 1, 2 or 3: ");
  254. scanf("%d", &choice);
  255. printf("Enter money (Baht): ");
  256. scanf("%f", &baht);
  257. switch (choice)
  258. {
  259. case 1: {printf("%.2f Baht = %.2f USD\n", baht, baht/32.68); break; }
  260. case 2: {printf("%.2f Baht = %.2f EUR\n", baht, baht/44.94); break; }
  261. case 3: {printf("%.2f Baht = %.2f CNY\n", baht, baht/5.41); break; }
  262. }
  263. printf("Enter 0 to exit or other number to convert again: ");
  264. scanf("%d", &exit);
  265. } while (exit != 0);
  266. printf("\n");
  267. }
  268. void problem1B()
  269. {
  270. // ให้เขียนโปรแกรมเพื่อคำนวณความหนาแน่น (density) ของของเหลว ที่วัดจากน้ำหนัก (weight) เป็นกิโลกรัม
  271. // หารด้วยปริมาตร (volume) เป็นลูกบาศก์เมตร ซึ่งหน่วยของความหนาแน่นจะเป็น กิโลกรัมต่อลูกบาศก์เมตร
  272. // โดยสมมุติว่าภาชนะที่ใส่ของเหลวสามารถวัดหาปริมาตรได้ในหน่วยลูกบาศก์เมตร
  273. // แต่เครื่องชั่งของเหลวมีหน่วยเป็นปอนด์ ซึ่ง 1 ปอนด์ จะมีค่าเท่ากับ 0.45359 กิโลกรัม
  274.  
  275. // ให้โปรแกรมมีรูปแบบการทำงานดังตัวอย่างนี้
  276. // Enter weight (pound): 2204.62
  277. // Enter volume (cubic meter): 1.5
  278. // Density = 666.6624 kilograms/cubic meter
  279.  
  280. // ******* เริ่มต้น code ******* //
  281. // ************************** //
  282.  
  283. // ประกาศตัวแปร สำหรับเก็บค่าน้ำหนัก ปริมาตร และความหนาแน่นเป็นจำนวนจริง (** 1 คะแนน **)
  284. // ******* เขียน code ที่นี่ ******* //
  285. float density, weight, volume;
  286. // รับค่าน้ำหนักเป็นหน่วยปอนด์ (** 1.5 คะแนน **)
  287. // ******* เขียน code ที่นี่ ******* //
  288. printf("Enter weight (pound): ");
  289. scanf("%f", &weight);
  290. // รับค่าปริมาตรเป็นหน่วยลูกบาศก์เมตร (** 1.5 คะแนน **)
  291. // ******* เขียน code ที่นี่ ******* //
  292. printf("Enter volume (cubic meter): ");
  293. scanf("%f", &volume);
  294. // คำนวณความหนาแน่น (** 1 คะแนน **)
  295. // ******* เขียน code ที่นี่ ******* //
  296. density = (float)(weight*0.45359) / volume;
  297. // แสดงผลค่าความหนาแน่นให้มีเลขทศนิยม 4 หลัก (** 1 คะแนน **)
  298. // ******* เขียน code ที่นี่ ******* //
  299. printf("Density = %.4f kilograms/cubic meter", density);
  300. printf("\n");
  301. }
  302.  
  303. void problem2B()
  304. {
  305. // ให้เขียนโปรแกรมเพื่อคำนวณค่าเช่าเก็บสินค้าในโกดังตามปริมาตร (volume) และระยะเวลาเช่า (duration)
  306. // โดยมีอัตราที่แตกต่างกันดังนี้
  307. // ปริมาตรไม่ถึง 10 ลูกบาศก์เมตร ให้คิดค่าเช่า 500 บาทต่อลูกบาศก์เมตรต่อวัน
  308. // ปริมาตรตั้งแต่ 10 ลูกบาศก์เมตรขึ้นไป แต่ไม่ถึง 50 ลูกบาศก์เมตร ให้คิดค่าเช่า 450 บาทต่อลูกบาศก์เมตรต่อวัน
  309. // ปริมาตรตั้งแต่ 50 ลูกบาศก์เมตรขึ้นไป แต่ไม่ถึง 100 ลูกบาศก์เมตร ให้คิดค่าเช่า 400 บาทต่อลูกบาศก์เมตรต่อวัน
  310. // ปริมาตรตั้งแต่ 100 ลูกบาศก์เมตรขึ้นไป ให้คิดค่าเช่า 350 บาทต่อลูกบาศก์เมตรต่อวัน
  311. // สำหรับระยะเวลาเช่านั้น ให้มีระยะเวลาขึ้นต่ำ 15 วันขึ้นไปแต่ไม่เกิน 180 วัน
  312.  
  313. // ให้โปรแกรมมีรูปแบบการทำงานดังตัวอย่างนี้
  314. // Enter volume (cubic meter): 80
  315. // Enter duration (15-180 days): 120
  316. // Cost for inventory is 386400 Baht, by the rate 400 Baht/cubic meter/day
  317. // หรือ
  318. // Enter volume (cubic meter): 80
  319. // Enter duration (15-180 days): 10
  320. // Duration is not valid.
  321.  
  322. // ******* เริ่มต้น code ******* //
  323. // ************************** //
  324.  
  325. // ประกาศตัวแปรสำหรับเก็บค่าระยะเวลาเช่า (duration) และอัตราค่าเช่า (rate) เป็นจำนวนเต็ม
  326. // ตัวแปรสำหรับเก็บค่าปริมาตร (volume) เป็นจำนวนจริง (** 0.25 คะแนน **)
  327. // ******* เขียน code ที่นี่ ******* //
  328. int duration, rate;
  329. float volume;
  330. // รับค่าปริมาตรและระยะเวลาเช่า (** 0.75 คะแนน **)
  331. // ******* เขียน code ที่นี่ ******* //
  332. printf("Enter volume (cubic meter): ");
  333. scanf("%f", &volume);
  334. printf("Enter duration (15-180 days): ");
  335. scanf("%d", &duration);
  336. // ถ้าระยะเวลาอยู่ในช่วง 15-180 ก็ให้คำนวณค่าเช่าและแสดงผลค่าเช่าที่คำนวณได้ในลักษณะไม่มีเลขทศนิยม
  337. // โดยที่ประกอบด้วยการทำงานดังนี้
  338. // เช็คเงื่อนไขตามปริมาตรเพื่อหาอัตราค่าเช่า (rate) (** 2 คะแนน **)
  339. // กำหนดอัตราค่าเช่าสำหรับแต่ละเงื่อนไข (** 1 คะแนน **)
  340. // คำนวณค่าเช่าพร้อมทั้งแสดงผลลัพธ์ค่าเช่าในรูปแบบไม่มีทศนิยม (** 1 คะแนน **)
  341. // แต่ถ้าระยะเวลาน้อยกว่า 1 หรือมากกว่า 12 ให้แสดงข้อความ "Duration is not valid." (** 1 คะแนน **)
  342. // ******* เขียน code ที่นี่ ******* //
  343. if (duration >= 15 && duration <= 180)
  344. {
  345. if (volume < 10) rate = 500;
  346. else if (volume < 50) rate = 450;
  347. else if (volume < 100) rate = 400;
  348. else if (volume >= 100) rate = 350;
  349. printf("Cost for inventory is %d Baht, by the rate %d Baht / cubic meter / day", (int)volume*rate*duration, rate);
  350. }
  351. else
  352. {
  353. printf("Duration is not valid.\n");
  354. }
  355. printf("\n");
  356. }
  357.  
  358. void problem3B()
  359. {
  360. // ให้เขียนโปรแกรมที่รับค่าตัวเลขจำนวนเต็ม 1 ตัว ทีมีค่าตั้งแต่ 2 ขึ้นไปแต่ไม่เกิน 15 แทนค่าความกว้างครึ่งหนึ่งของรูป
  361. // แล้วทำการแสดงผลเป็นรูปตัววีแบบตะแคงขวาตามความกว้างที่กำหนด ดังตัวอย่างต่อไปนี้
  362.  
  363. // Enter half of width: 4
  364. // v
  365. // v
  366. // v
  367. // v
  368. // v
  369. // v
  370. // v
  371. // v
  372.  
  373. // จากตัวอย่างข้างต้น
  374. // บรรทัดแรกจะแสดงช่องว่าง 6 ตำแหน่ง ตามด้วยตัวอักษร v
  375. // บรรทัดที่สองจะแสดงช่องว่าง 4 ตำแหน่ง ตามด้วยตัวอักษร v
  376. // บรรทัดที่สามจะแสดงช่องว่าง 2 ตำแหน่ง ตามด้วยตัวอักษร v
  377. // บรรทัดที่สี่จะแสดงช่องว่าง 0 ตำแหน่ง ตามด้วยตัวอักษร v
  378. // ...
  379.  
  380. // ******* เริ่มต้น code ******* //
  381. // ************************** //
  382.  
  383. // ประกาศค่าตัวแปรสำหรับเก็บค่าความกว้างครึ่งหนึ่ง (width) เป็นจำนวนเต็ม
  384. // และตัวแปรสำหรับวน loop (** 0.25 คะแนน **)
  385. // ******* เขียน code ที่นี่ ******* //
  386. int width, i, j;
  387. // รับค่าความกว้างที่แทนครึ่งหนึ่งของความกว้างของรูป (** 0.25 คะแนน **)
  388. // ******* เขียน code ที่นี่ ******* //
  389. printf("Enter half of width: ");
  390. scanf("%d", &width);
  391. // วน loop วาดครึ่งบนของตัววี (hint: ใช้ loop ซ้อนกัน 2 ชั้น) โดยมีการดำเนินการที่เกี่ยวข้องดังนี้
  392. // วน loop นอกสำหรับแต่ละบรรทัด (** 1.5 คะแนน **)
  393. // วน loop ในสำหรับแสดงช่องว่าง (** 1.5 คะแนน **)
  394. // แสดงค่าช่องว่างและตัวอักษร v (** 0.5 คะแนน **)
  395. // ******* เขียน code ที่นี่ ******* //
  396. for (i = 0; i < width; i++)
  397. {
  398. for (j = i; j < width - 1; j++)
  399. {
  400. printf(" ");
  401. }
  402. printf("v\n");
  403. }
  404. // วน loop วาดครึ่งล่างของตัววี (ใช้หลักการเดียวกับครึ่งบน)
  405. // วน loop นอกสำหรับแต่ละบรรทัด (** 1 คะแนน **)
  406. // วน loop ในสำหรับแสดงช่องว่าง (** 0.5 คะแนน **)
  407. // แสดงค่าช่องว่างและตัวอักษร v (** 0.5 คะแนน **)
  408. // ******* เขียน code ที่นี่ ******* //
  409. for (i = width; i >0; i--)
  410. {
  411. for (j = i; j < width; j++)
  412. {
  413. printf(" ");
  414. }
  415. printf("v\n");
  416. }
  417. printf("\n");
  418. }
  419.  
  420. void problem4B()
  421. {
  422. // ให้เขียนโปรแกรมที่รับค่าตัวเลขฐานสอง 2 จำนวน ที่แต่ละจำนวนมีความยาวไม่เกิน 8 หลัก
  423. // โดยกำหนดให้ทั้ง 2 จำนวนมีความยาวเท่ากัน
  424. // แล้วทำการหาผลลัพธ์ของการทำ xor (exclusive or) แต่ละ bit เข้าด้วยกันเป็นคู่ๆ
  425. // สำหรับ xor จะมีความหมายดังนี้
  426. // 1 xor 1 = 1, 0 xor 0 = 0, 0 xor 1 = 1, 1 xor 0 = 0
  427.  
  428. // ให้โปรแกรมมีการทำงานดังตัวอย่างต่อไปนี้
  429. // Enter binary number 1 (1-8 digits): 101011
  430. // Enter binary number 2 (1-8 digits): 001101
  431. // 101011 xor 001101 = 001101
  432.  
  433. // ******* เริ่มต้น code ******* //
  434. // ************************** //
  435.  
  436. // ประกาศค่าตัวแปรสำหรับเก็บค่าจำนวนทั้ง 2 จำนวนในรูปแบบข้อความ
  437. // และตัวแปรสำหรับวน loop (** 0.75 คะแนน **)
  438. // ******* เขียน code ที่นี่ ******* //
  439. char bin[3][9];
  440. int i, len;
  441. // รับค่าตัวเลขฐานสองทั้ง 2 จำนวน (** 1 คะแนน **)
  442. // ******* เขียน code ที่นี่ ******* //
  443. for (i = 0; i < 2; i++)
  444. {
  445. printf("Enter binary number %d (1-8 digits): ", i + 1);
  446. scanf("%s", bin[i]);
  447. }
  448. len = strlen(bin[0]);
  449. // แสดงผล "จำนวนที่ 1 xor จำนวนที่ 2 = " โดยแทนค่าจำนวนที่ 1 และ 2 จากค่าที่รับมา (** 0.5 คะแนน **)
  450. // ******* เขียน code ที่นี่ ******* //
  451. printf("%s xor %s = ", bin[0], bin[1]);
  452. // หาค่าผลการทำ xor ของแต่ละ bit โดยมีการดำเนินการที่เกี่ยวข้องดังนี้
  453. // วน loop (** 1 คะแนน **)
  454. // ในแต่ละรอบ ตรวจสอบเงื่อนไขสำหรับค่าผลลัพธ์การทำ xor ของแต่ละคู่ bit (** 1.75 คะแนน **)
  455. // ในแต่ละเงื่อนไข แสดงค่าตัวเลขของ bit ผลลัพธ์ (** 1 คะแนน **)
  456. // ******* เขียน code ที่นี่ ******* //
  457. for (i = 0; i < len; i++)
  458. {
  459. if (bin[0][i] == bin[1][i]) printf("%c", bin[0][i]);
  460. else printf("%c", bin[1][i]);
  461. }
  462. printf("\n");
  463. }
  464.  
  465. void problem5B()
  466. {
  467. // เขียนโปรแกรมเพื่อใช้คำนวณค่าใช้จ่ายในการทาสีบนพื้นที่ตามขนาดที่กำหนด
  468. // โดยให้เลือกชนิดของสีซึ่งจะมีราคาต่างกัน ได้แก่
  469. // 1. TOA Super Shield ตร.เมตร ละ 180 บาท
  470. // 2. TOA Classic Shield ตร.เมตร ละ 150 บาท
  471. // 3. TOA 4 SEASONS ตร.เมตร ละ 95 บาท
  472.  
  473. // ให้โปรแกรมทำงานในรูปแบบดังตัวอย่างนี้
  474. // Compute cost of painting
  475. // 1. TOA Super Shield
  476. // 2. TOA Classic Shield
  477. // 3. TOA 4 SEASONS
  478. // Select 1, 2 or 3: 2
  479. // Enter area (square meter): 86.5
  480. // Cost = 12975 Baht, by the rate 150 Baht/square meter
  481. // Type 0 to exit or other number to compute again: 1
  482. // Compute cost of painting
  483. // 1. TOA Super Shield
  484. // 2. TOA Classic Shield
  485. // 3. TOA 4 SEASONS
  486. // Select 1, 2 or 3:
  487. // ...
  488.  
  489. // ให้โปรแกรมแสดง menu ให้เลือก เพื่อคำนวณค่าทาสี
  490. // โดยจะรับค่าเป็นค่า 1-3 จากนั้นให้รับค่าพื้นที่ แล้วจึงทำการคำนวณค่าทาสีและแสดงผลลัพธ์
  491. // จากนั้นให้โปรแกรมถามว่าจะทำการคำนวณซ้ำอีกหรือไม่ ถ้าใส่ตัวเลขใดๆ ที่ไม่ใช่ 0 ก็จะวนกลับไปเริ่มต้นใหม่
  492. // แต่ถ้าใส่ค่า 0 ก็จะจบการทำงานของโปแกรม
  493.  
  494. // ******* เริ่มต้น code ******* //
  495. // ************************** //
  496.  
  497. // ประกาศค่าตัวแปรที่เก็บค่า menu ที่เลือก (1-3) เป็นจำนวนเต็ม, ตัวแปรที่เก็บค่าพื้นที่เป็นจำนวนจริง,
  498. // ตัวแปรที่เก็บค่าอัตราค่าทาสีต่อ ตร.เมตร (rate) เป็นจำนวนเต็ม
  499. // และตัวแปรที่เก็บค่าสำหรับเช็คว่าจะจบโปรแกรม (exit) หรือไม่เป็นจำนวนเต็ม (** 0.25 คะแนน **)
  500. // ******* เขียน code ที่นี่ ******* //
  501. int menu, rate, exit;
  502. float area;
  503. // กำหนด loop เพื่อควบคุมให้โปรแกรมสามารถคำนวณราคาต่อได้เรื่อยๆ หากยังใส่ค่า exit ที่ไม่ใช่ 0
  504. // โดยให้เลือกใช้ loop ที่เหมาะสมจาก for, while หรือ do-while (** 1 คะแนน **)
  505. // สำหรับภายใน loop ให้มีการดำเนินการดังนี้
  506. // แสดงค่า menu (** 1 คะแนน **)
  507. // รับค่าตัวเลือกชนิดของสี (1-3) (** 0.25 คะแนน **)
  508. // รับค่าพื้นที่ (** 0.25 คะแนน **)
  509. // ใช้คำสั่ง switch-case หรือ if-else เพื่อแยกกำหนดอัตราค่าทาสีตามชนิดของสี (** 1 คะแนน **)
  510. // คำนวณค่าทาสีตามอัตราที่ได้พร้อมทั้งแสดงผลโดยไม่ต้องมีทศนิยม (กำหนดทศนิยมเป็น 0 หลัก) (** 2 คะแนน **)
  511. // รับค่าสำหรับเช็คว่าจะทำการคำนวณต่อหรือออกจากโปรแกรม (** 0.25 คะแนน **)
  512. // ******* เขียน code ที่นี่ ******* //
  513. do
  514. {
  515. printf("Compute cost of painting\n");
  516. printf("1. TOA Super Shield\n");
  517. printf("2. TOA Classic Shield\n");
  518. printf("3. TOA 4 SEASONS\n");
  519. printf("Select 1, 2 or 3: ");
  520. scanf("%d", &menu);
  521. printf("Enter area (square meter): ");
  522. scanf("%f", &area);
  523. switch (menu)
  524. {
  525. case 1: {rate = 180; break; }
  526. case 2: {rate = 150; break; }
  527. case 3: {rate = 95; break; }
  528. }
  529. printf("Cost = %.f Baht, by the rate %d Baht/square meter\n", rate*area, rate);
  530. printf("Type 0 to exit or other number to compute again: ");
  531. scanf("%d", &exit);
  532. } while (exit != 0);
  533. printf("\n");
  534. }
  535. void AllA()
  536. {
  537. // ค่าจำนวนจริงให้ใช้ค่าแบบ single precision
  538. //problem1A();
  539. //problem2A();
  540. //problem3A();
  541. //problem4A();
  542. problem5A();
  543. }
  544. void AllB()
  545. {
  546. // ค่าจำนวนจริงให้ใช้ค่าแบบ single precision
  547. //problem1B();
  548. //problem2B();
  549. //problem3B();
  550. //problem4B();
  551. //problem5B();
  552. }
  553. void main()
  554. {
  555. AllA();
  556. //AllB();
  557. }
  558.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:2: error: illegal character: '#'
#include <stdio.h>
^
Main.java:2: error: class, interface, or enum expected
#include <stdio.h>
         ^
Main.java:3: error: illegal character: '#'
#include <string.h>
^
Main.java:4: error: illegal character: '#'
#include <math.h>
^
Main.java:24: error: class, interface, or enum expected
	float time;
	^
Main.java:27: error: class, interface, or enum expected
	printf("Enter speed (miles/hour): ");
	^
Main.java:28: error: class, interface, or enum expected
	scanf("%d", &speed);
	^
Main.java:31: error: class, interface, or enum expected
	printf("Enter distance (kilometer): ");
	^
Main.java:32: error: class, interface, or enum expected
	scanf("%d", &disrance);
	^
Main.java:35: error: class, interface, or enum expected
	time = (float)(disrance / 1.609) / speed;
	^
Main.java:38: error: class, interface, or enum expected
	printf("Time to destination is %.2f hour(s)", time);
	^
Main.java:39: error: class, interface, or enum expected
	printf("\n");
	^
Main.java:40: error: class, interface, or enum expected
}
^
Main.java:66: error: class, interface, or enum expected
	float interest;
	^
Main.java:69: error: class, interface, or enum expected
	printf("Enter deposit amount: ");
	^
Main.java:70: error: class, interface, or enum expected
	scanf("%d", &deposit);
	^
Main.java:71: error: class, interface, or enum expected
	printf("Enter duration (1-12): ");
	^
Main.java:72: error: class, interface, or enum expected
	scanf("%d", &duration);
	^
Main.java:80: error: class, interface, or enum expected
	if (duration >=1 && duration <= 12)
	^
Main.java:83: error: class, interface, or enum expected
		else if (duration < 6) interest = (float)(deposit*1.25) / 100;
		^
Main.java:84: error: class, interface, or enum expected
		else if (duration < 9) interest = (float)(deposit*1.75) / 100;
		^
Main.java:85: error: class, interface, or enum expected
		else if (duration >= 9) interest = (float)(deposit*2.00) / 100;
		^
Main.java:86: error: class, interface, or enum expected
		printf("Your interest is %.2f and your total balance is %.2f",interest,interest+deposit);
		^
Main.java:87: error: class, interface, or enum expected
	}	
	^
Main.java:91: error: class, interface, or enum expected
	}
	^
Main.java:93: error: class, interface, or enum expected
}
^
Main.java:126: error: class, interface, or enum expected
	printf("Enter half of width: ");
	^
Main.java:127: error: class, interface, or enum expected
	scanf("%d", &width);
	^
Main.java:133: error: class, interface, or enum expected
	for (i = width; i >0; i--)
	^
Main.java:133: error: class, interface, or enum expected
	for (i = width; i >0; i--)
	                ^
Main.java:133: error: class, interface, or enum expected
	for (i = width; i >0; i--)
	                      ^
Main.java:135: error: class, interface, or enum expected
		for (j = i; j < width; j++)
		            ^
Main.java:135: error: class, interface, or enum expected
		for (j = i; j < width; j++)
		                       ^
Main.java:138: error: class, interface, or enum expected
		}
		^
Main.java:140: error: class, interface, or enum expected
	}
	^
Main.java:147: error: class, interface, or enum expected
	for (i = 0; i < width; i++)
	            ^
Main.java:147: error: class, interface, or enum expected
	for (i = 0; i < width; i++)
	                       ^
Main.java:149: error: class, interface, or enum expected
		for (j = i; j < width - 1; j++)
		            ^
Main.java:149: error: class, interface, or enum expected
		for (j = i; j < width - 1; j++)
		                           ^
Main.java:152: error: class, interface, or enum expected
		}
		^
Main.java:154: error: class, interface, or enum expected
	}
	^
Main.java:156: error: class, interface, or enum expected
}
^
Main.java:176: error: class, interface, or enum expected
	int i,len;
	^
Main.java:179: error: class, interface, or enum expected
	printf("Enter word: ");
	^
Main.java:180: error: class, interface, or enum expected
	scanf("%s", word);
	^
Main.java:181: error: class, interface, or enum expected
	len = strlen(word);
	^
Main.java:184: error: class, interface, or enum expected
	printf("Upper-case word is ");
	^
Main.java:191: error: class, interface, or enum expected
	for (i = 0; i < len;i++)
	^
Main.java:191: error: class, interface, or enum expected
	for (i = 0; i < len;i++)
	            ^
Main.java:191: error: class, interface, or enum expected
	for (i = 0; i < len;i++)
	                    ^
Main.java:195: error: class, interface, or enum expected
		printf("%c", word[i]);
		^
Main.java:196: error: class, interface, or enum expected
	}
	^
Main.java:198: error: class, interface, or enum expected
	printf("\n");
	^
Main.java:199: error: class, interface, or enum expected
}
^
Main.java:236: error: class, interface, or enum expected
	float baht;
	^
Main.java:247: error: class, interface, or enum expected
	do
	^
Main.java:250: error: class, interface, or enum expected
		printf("1. US Dollar (USD)\n");
		^
Main.java:251: error: class, interface, or enum expected
		printf("2. Eurozone (EUR)\n");
		^
Main.java:252: error: class, interface, or enum expected
		printf("3. Chinese Yuan (CNY)\n");
		^
Main.java:253: error: class, interface, or enum expected
		printf("Select 1, 2 or 3: ");
		^
Main.java:254: error: class, interface, or enum expected
		scanf("%d", &choice);
		^
Main.java:255: error: class, interface, or enum expected
		printf("Enter money (Baht): ");
		^
Main.java:256: error: class, interface, or enum expected
		scanf("%f", &baht);
		^
Main.java:257: error: class, interface, or enum expected
		switch (choice)
		^
Main.java:259: error: class, interface, or enum expected
		case 1: {printf("%.2f Baht = %.2f USD\n", baht, baht/32.68); break; }
		                                                             ^
Main.java:259: error: class, interface, or enum expected
		case 1: {printf("%.2f Baht = %.2f USD\n", baht, baht/32.68); break; }
		                                                                    ^
Main.java:260: error: class, interface, or enum expected
		case 2: {printf("%.2f Baht = %.2f EUR\n", baht, baht/44.94); break; }
		                                                             ^
Main.java:260: error: class, interface, or enum expected
		case 2: {printf("%.2f Baht = %.2f EUR\n", baht, baht/44.94); break; }
		                                                                    ^
Main.java:261: error: class, interface, or enum expected
		case 3: {printf("%.2f Baht = %.2f CNY\n", baht, baht/5.41); break; }
		                                                            ^
Main.java:261: error: class, interface, or enum expected
		case 3: {printf("%.2f Baht = %.2f CNY\n", baht, baht/5.41); break; }
		                                                                   ^
Main.java:264: error: class, interface, or enum expected
		scanf("%d", &exit);
		^
Main.java:265: error: class, interface, or enum expected
	} while (exit != 0);
	^
Main.java:266: error: class, interface, or enum expected
	printf("\n");
	^
Main.java:267: error: class, interface, or enum expected
}
^
Main.java:288: error: class, interface, or enum expected
	printf("Enter weight (pound): ");
	^
Main.java:289: error: class, interface, or enum expected
	scanf("%f", &weight);
	^
Main.java:292: error: class, interface, or enum expected
	printf("Enter volume (cubic meter): ");
	^
Main.java:293: error: class, interface, or enum expected
	scanf("%f", &volume);
	^
Main.java:296: error: class, interface, or enum expected
	density = (float)(weight*0.45359) / volume;
	^
Main.java:299: error: class, interface, or enum expected
	printf("Density = %.4f kilograms/cubic meter", density);
	^
Main.java:300: error: class, interface, or enum expected
	printf("\n");
	^
Main.java:301: error: class, interface, or enum expected
}
^
Main.java:329: error: class, interface, or enum expected
	float volume;
	^
Main.java:332: error: class, interface, or enum expected
	printf("Enter volume (cubic meter): ");
	^
Main.java:333: error: class, interface, or enum expected
	scanf("%f", &volume);
	^
Main.java:334: error: class, interface, or enum expected
	printf("Enter duration (15-180 days): ");
	^
Main.java:335: error: class, interface, or enum expected
	scanf("%d", &duration);
	^
Main.java:343: error: class, interface, or enum expected
	if (duration >= 15 && duration <= 180)
	^
Main.java:346: error: class, interface, or enum expected
		else if (volume < 50) rate = 450;
		^
Main.java:347: error: class, interface, or enum expected
		else if (volume < 100) rate = 400;
		^
Main.java:348: error: class, interface, or enum expected
		else if (volume >= 100) rate = 350;
		^
Main.java:349: error: class, interface, or enum expected
		printf("Cost for inventory is %d Baht, by the rate %d Baht / cubic meter / day", (int)volume*rate*duration, rate);
		^
Main.java:350: error: class, interface, or enum expected
	}
	^
Main.java:354: error: class, interface, or enum expected
	}
	^
Main.java:356: error: class, interface, or enum expected
}
^
Main.java:389: error: class, interface, or enum expected
	printf("Enter half of width: ");
	^
Main.java:390: error: class, interface, or enum expected
	scanf("%d", &width);
	^
Main.java:396: error: class, interface, or enum expected
	for (i = 0; i < width; i++)
	^
Main.java:396: error: class, interface, or enum expected
	for (i = 0; i < width; i++)
	            ^
Main.java:396: error: class, interface, or enum expected
	for (i = 0; i < width; i++)
	                       ^
100 errors
stdout
Standard output is empty