fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5.  
  6. int c;
  7.  
  8. // ฟังก์ชันคำนวณน้ำหนักโมเลกุล
  9. double calculate_molecular_weight(const char *formula) {
  10. double molecular_weight = 0.0;
  11. int i = 0;
  12. while (formula[i] != '\0') {
  13. double weight = 0.0;
  14.  
  15. // ตรวจสอบธาตุที่มีสัญลักษณ์ 1 ตัว
  16. if (formula[i] == 'H') weight = 1.008;
  17. else if (formula[i] == 'B') weight = 10.81;
  18. else if (formula[i] == 'C') weight = 12.01;
  19. else if (formula[i] == 'N') weight = 14.01;
  20. else if (formula[i] == 'O') weight = 16.00;
  21. else if (formula[i] == 'F') weight = 19.00;
  22. else if (formula[i] == 'P') weight = 30.97;
  23. else if (formula[i] == 'S') weight = 32.07;
  24. else if (formula[i] == 'K') weight = 39.10;
  25. else if (formula[i] == 'V') weight = 50.94;
  26.  
  27. // ตรวจสอบธาตุที่มี 2 ตัวอักษร
  28.  
  29. if (i + 1 < strlen(formula)) {
  30. // ตรวจสอบธาตุสองตัวที่มีการใช้ตัวอักษรทั้งสองตัว เช่น 'Na', 'Mg', 'Cl'
  31.  
  32. if (formula[i] == 'H' && formula[i + 1] == 'e') { weight = 4.00; i++; }
  33. else if (formula[i] == 'L' && formula[i + 1] == 'i') { weight = 6.94; i++; }
  34. else if (formula[i] == 'B' && formula[i + 1] == 'e') { weight = 9.01; i++; }
  35. else if (formula[i] == 'N' && formula[i + 1] == 'e') { weight = 20.18; i++; }
  36. else if (formula[i] == 'N' && formula[i + 1] == 'a') { weight = 23.00; i++; }
  37. else if (formula[i] == 'M' && formula[i + 1] == 'g') { weight = 24.31; i++; }
  38. else if (formula[i] == 'A' && formula[i + 1] == 'l') { weight = 26.98; i++; }
  39. else if (formula[i] == 'S' && formula[i + 1] == 'i') { weight = 28.09; i++; }
  40. else if (formula[i] == 'C' && formula[i + 1] == 'l') { weight = 35.45; i++; }
  41. else if (formula[i] == 'A' && formula[i + 1] == 'r') { weight = 39.95; i++; }
  42. else if (formula[i] == 'C' && formula[i + 1] == 'a') { weight = 40.08; i++; }
  43. else if (formula[i] == 'S' && formula[i + 1] == 'c') { weight = 44.96; i++; }
  44. else if (formula[i] == 'T' && formula[i + 1] == 'i') { weight = 47.87; i++; }
  45. else if (formula[i] == 'C' && formula[i + 1] == 'r') { weight = 52.00; i++; }
  46. else if (formula[i] == 'M' && formula[i + 1] == 'n') { weight = 54.94; i++; }
  47. else if (formula[i] == 'F' && formula[i + 1] == 'e') { weight = 55.85; i++; }
  48. else if (formula[i] == 'C' && formula[i + 1] == 'o') { weight = 58.93; i++; }
  49. else if (formula[i] == 'N' && formula[i + 1] == 'i') { weight = 58.69; i++; }
  50. else if (formula[i] == 'C' && formula[i + 1] == 'u') { weight = 63.55; i++; }
  51. else if (formula[i] == 'Z' && formula[i + 1] == 'n') { weight = 65.39; i++; }
  52. else continue; // ถ้าธาตุไม่รู้จักให้ข้ามไป
  53. }
  54.  
  55. // ถ้าธาตุไม่รู้จักให้ข้ามไป
  56. if (weight == 0.0) {
  57. printf("\t Unknown element detected: %c%c\n", formula[i], formula[i + 1]!= '\0' ? formula[i + 1] : ' ');
  58. return -1; // แสดงว่ามีธาตุที่ไม่รู้จัก
  59. }
  60.  
  61. // ตรวจสอบจำนวนของอะตอม
  62. int count = 0;
  63. while (isdigit(formula[i + 1])) {
  64. count = count * 10 + (formula[++i] - '0');
  65. }
  66. if (count == 0) count = 1;
  67. molecular_weight += weight * count;
  68. i++;
  69. }
  70.  
  71. return molecular_weight;
  72. }
  73.  
  74. int main() {
  75. float g, Mw, Mol, N, V;
  76. char formula[100];
  77.  
  78. printf("\t Please select choice to Find Mol \n");
  79. printf("\t *****************\n");
  80. printf("\t 1.If want to find Mol from g/Mw #\n");
  81. printf("\t 2.If want to find Mol from N/(6.02*pow(10,23)) #\n");
  82. printf("\t 3.If want to find Mol from V/22.4 #\n");
  83. printf("\t Select : ");
  84. scanf("%d", &c);
  85.  
  86. switch (c) {
  87. case 1:
  88.  
  89. printf("\t Please enter g : ");
  90. scanf("%f", &g);
  91.  
  92. printf("\t Please enter chemical formula (e.g., H2O): ");
  93. scanf("%s", formula);
  94.  
  95. double Mw = calculate_molecular_weight(formula);
  96. if (Mw == -1) {
  97. printf("\t Invalid formula or unknown element.\n");
  98. break;
  99. }
  100.  
  101. printf("\t Molecular weight of %s is %.2f g/mol\n", formula, Mw);
  102. printf("\t Mol of Substance is %.2f / %.2f = %.2f\n", g, Mw, g / Mw);
  103. break;
  104.  
  105. case 2:
  106. printf("\t Please enter N : ");
  107. scanf("%f", &N);
  108. printf("\t Mol of Substance is %.2f / (6.02*pow(10,23)) = %.2f\n", N, N / (6.02 * pow(10, 23)));
  109. break;
  110.  
  111. case 3:
  112. printf("\t Please enter V : ");
  113. scanf("%f", &V);
  114. printf("\t Mol of Substance is %.2f / 22.4 = %.2f\n", V, V / 22.4);
  115. break;
  116.  
  117. default:
  118. printf("Please select 1-3 only\n");
  119. break;
  120. }
  121.  
  122. return 0;
  123. }
  124.  
  125.  
  126.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
	 Please select choice to Find Mol 
	 *****************
	 1.If want to find Mol from g/Mw #
	 2.If want to find Mol from N/(6.02*pow(10,23)) #
	 3.If want to find Mol from V/22.4 #
	 Select : Please select 1-3 only