#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int c;
// ฟังก์ชันคำนวณน้ำหนักโมเลกุล
double calculate_molecular_weight( const char * formula) {
double molecular_weight = 0.0 ;
int i = 0 ;
while ( formula[ i] != '\0 ' ) {
double weight = 0.0 ;
// ตรวจสอบธาตุที่มีสัญลักษณ์ 1 ตัว
if ( formula[ i] == 'H' ) weight = 1.008 ;
else if ( formula[ i] == 'B' ) weight = 10.81 ;
else if ( formula[ i] == 'C' ) weight = 12.01 ;
else if ( formula[ i] == 'N' ) weight = 14.01 ;
else if ( formula[ i] == 'O' ) weight = 16.00 ;
else if ( formula[ i] == 'F' ) weight = 19.00 ;
else if ( formula[ i] == 'P' ) weight = 30.97 ;
else if ( formula[ i] == 'S' ) weight = 32.07 ;
else if ( formula[ i] == 'K' ) weight = 39.10 ;
else if ( formula[ i] == 'V' ) weight = 50.94 ;
// ตรวจสอบธาตุที่มี 2 ตัวอักษร
if ( i
+ 1 < strlen ( formula
) ) { // ตรวจสอบธาตุสองตัวที่มีการใช้ตัวอักษรทั้งสองตัว เช่น 'Na', 'Mg', 'Cl'
if ( formula[ i] == 'H' && formula[ i + 1 ] == 'e' ) { weight = 4.00 ; i++; }
else if ( formula[ i] == 'L' && formula[ i + 1 ] == 'i' ) { weight = 6.94 ; i++; }
else if ( formula[ i] == 'B' && formula[ i + 1 ] == 'e' ) { weight = 9.01 ; i++; }
else if ( formula[ i] == 'N' && formula[ i + 1 ] == 'e' ) { weight = 20.18 ; i++; }
else if ( formula[ i] == 'N' && formula[ i + 1 ] == 'a' ) { weight = 23.00 ; i++; }
else if ( formula[ i] == 'M' && formula[ i + 1 ] == 'g' ) { weight = 24.31 ; i++; }
else if ( formula[ i] == 'A' && formula[ i + 1 ] == 'l' ) { weight = 26.98 ; i++; }
else if ( formula[ i] == 'S' && formula[ i + 1 ] == 'i' ) { weight = 28.09 ; i++; }
else if ( formula[ i] == 'C' && formula[ i + 1 ] == 'l' ) { weight = 35.45 ; i++; }
else if ( formula[ i] == 'A' && formula[ i + 1 ] == 'r' ) { weight = 39.95 ; i++; }
else if ( formula[ i] == 'C' && formula[ i + 1 ] == 'a' ) { weight = 40.08 ; i++; }
else if ( formula[ i] == 'S' && formula[ i + 1 ] == 'c' ) { weight = 44.96 ; i++; }
else if ( formula[ i] == 'T' && formula[ i + 1 ] == 'i' ) { weight = 47.87 ; i++; }
else if ( formula[ i] == 'C' && formula[ i + 1 ] == 'r' ) { weight = 52.00 ; i++; }
else if ( formula[ i] == 'M' && formula[ i + 1 ] == 'n' ) { weight = 54.94 ; i++; }
else if ( formula[ i] == 'F' && formula[ i + 1 ] == 'e' ) { weight = 55.85 ; i++; }
else if ( formula[ i] == 'C' && formula[ i + 1 ] == 'o' ) { weight = 58.93 ; i++; }
else if ( formula[ i] == 'N' && formula[ i + 1 ] == 'i' ) { weight = 58.69 ; i++; }
else if ( formula[ i] == 'C' && formula[ i + 1 ] == 'u' ) { weight = 63.55 ; i++; }
else if ( formula[ i] == 'Z' && formula[ i + 1 ] == 'n' ) { weight = 65.39 ; i++; }
else continue ; // ถ้าธาตุไม่รู้จักให้ข้ามไป
}
// ถ้าธาตุไม่รู้จักให้ข้ามไป
if ( weight == 0.0 ) {
printf ( "\t Unknown element detected: %c%c\n " , formula
[ i
] , formula
[ i
+ 1 ] != '\0 ' ? formula
[ i
+ 1 ] : ' ' ) ; return - 1 ; // แสดงว่ามีธาตุที่ไม่รู้จัก
}
// ตรวจสอบจำนวนของอะตอม
int count = 0 ;
count = count * 10 + ( formula[ ++ i] - '0' ) ;
}
if ( count == 0 ) count = 1 ;
molecular_weight += weight * count;
i++;
}
return molecular_weight;
}
int main( ) {
float g, Mw, Mol, N, V;
char formula[ 100 ] ;
printf ( "\t Please select choice to Find Mol \n " ) ; printf ( "\t *****************\n " ) ; printf ( "\t 1.If want to find Mol from g/Mw #\n " ) ; printf ( "\t 2.If want to find Mol from N/(6.02*pow(10,23)) #\n " ) ; printf ( "\t 3.If want to find Mol from V/22.4 #\n " ) ;
switch ( c) {
case 1 :
printf ( "\t Please enter g : " ) ;
printf ( "\t Please enter chemical formula (e.g., H2O): " ) ;
double Mw = calculate_molecular_weight( formula) ;
if ( Mw == - 1 ) {
printf ( "\t Invalid formula or unknown element.\n " ) ; break ;
}
printf ( "\t Molecular weight of %s is %.2f g/mol\n " , formula
, Mw
) ; printf ( "\t Mol of Substance is %.2f / %.2f = %.2f\n " , g
, Mw
, g
/ Mw
) ; break ;
case 2 :
printf ( "\t Please enter N : " ) ; printf ( "\t Mol of Substance is %.2f / (6.02*pow(10,23)) = %.2f\n " , N
, N
/ ( 6.02 * pow ( 10 , 23 ) ) ) ; break ;
case 3 :
printf ( "\t Please enter V : " ) ; printf ( "\t Mol of Substance is %.2f / 22.4 = %.2f\n " , V
, V
/ 22.4 ) ; break ;
default :
printf ( "Please select 1-3 only\n " ) ; break ;
}
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPGN0eXBlLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgppbnQgYzsKCi8vIOC4n+C4seC4h+C4geC5jOC4iuC4seC4meC4hOC4s+C4meC4p+C4k+C4meC5ieC4s+C4q+C4meC4seC4geC5guC4oeC5gOC4peC4geC4uOC4pQpkb3VibGUgY2FsY3VsYXRlX21vbGVjdWxhcl93ZWlnaHQoY29uc3QgY2hhciAqZm9ybXVsYSkgewogICAgZG91YmxlIG1vbGVjdWxhcl93ZWlnaHQgPSAwLjA7CiAgICBpbnQgaSA9IDA7CiAgICB3aGlsZSAoZm9ybXVsYVtpXSAhPSAnXDAnKSB7CiAgICBkb3VibGUgd2VpZ2h0ID0gMC4wOwoKICAgICAgICAvLyDguJXguKPguKfguIjguKrguK3guJrguJjguLLguJXguLjguJfguLXguYjguKHguLXguKrguLHguI3guKXguLHguIHguKnguJPguYwgMSDguJXguLHguKcKICAgICAgICBpZiAoZm9ybXVsYVtpXSA9PSAnSCcpIHdlaWdodCA9IDEuMDA4OwogICAgICAgIGVsc2UgaWYgKGZvcm11bGFbaV0gPT0gJ0InKSB3ZWlnaHQgPSAxMC44MTsKICAgICAgICBlbHNlIGlmIChmb3JtdWxhW2ldID09ICdDJykgd2VpZ2h0ID0gMTIuMDE7CiAgICAgICAgZWxzZSBpZiAoZm9ybXVsYVtpXSA9PSAnTicpIHdlaWdodCA9IDE0LjAxOwogICAgICAgIGVsc2UgaWYgKGZvcm11bGFbaV0gPT0gJ08nKSB3ZWlnaHQgPSAxNi4wMDsKICAgICAgICBlbHNlIGlmIChmb3JtdWxhW2ldID09ICdGJykgd2VpZ2h0ID0gMTkuMDA7CiAgICAgICAgZWxzZSBpZiAoZm9ybXVsYVtpXSA9PSAnUCcpIHdlaWdodCA9IDMwLjk3OwogICAgICAgIGVsc2UgaWYgKGZvcm11bGFbaV0gPT0gJ1MnKSB3ZWlnaHQgPSAzMi4wNzsKICAgICAgICBlbHNlIGlmIChmb3JtdWxhW2ldID09ICdLJykgd2VpZ2h0ID0gMzkuMTA7CiAgICAgICAgZWxzZSBpZiAoZm9ybXVsYVtpXSA9PSAnVicpIHdlaWdodCA9IDUwLjk0OwoKICAgICAgICAvLyDguJXguKPguKfguIjguKrguK3guJrguJjguLLguJXguLjguJfguLXguYjguKHguLUgMiDguJXguLHguKfguK3guLHguIHguKnguKMKCiAgICAgICAgaWYgKGkgKyAxIDwgc3RybGVuKGZvcm11bGEpKSB7CiAgICAgICAgICAgIC8vIOC4leC4o+C4p+C4iOC4quC4reC4muC4mOC4suC4leC4uOC4quC4reC4h+C4leC4seC4p+C4l+C4teC5iOC4oeC4teC4geC4suC4o+C5g+C4iuC5ieC4leC4seC4p+C4reC4seC4geC4qeC4o+C4l+C4seC5ieC4h+C4quC4reC4h+C4leC4seC4pyDguYDguIrguYjguJkgJ05hJywgJ01nJywgJ0NsJwoKICAgICAgICAgICAgaWYgKGZvcm11bGFbaV0gPT0gJ0gnICYmIGZvcm11bGFbaSArIDF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AgICAgIGVsc2UgaWYgKGZvcm11bGFbaV0gPT0gJ1onICYmIGZvcm11bGFbaSArIDFdID09ICduJykgeyB3ZWlnaHQgPSA2NS4zOTsgaSsrOyB9CiAgICAgICAgICAgIGVsc2UgY29udGludWU7ICAvLyDguJbguYnguLLguJjguLLguJXguLjguYTguKHguYjguKPguLnguYnguIjguLHguIHguYPguKvguYnguILguYnguLLguKHguYTguJsKICAgICAgICB9CgogICAgICAgIC8vIOC4luC5ieC4suC4mOC4suC4leC4uOC5hOC4oeC5iOC4o+C4ueC5ieC4iOC4seC4geC5g+C4q+C5ieC4guC5ieC4suC4oeC5hOC4mwogICAgICAgIGlmICh3ZWlnaHQgPT0gMC4wKSB7CiAgICAgICAgICAgIHByaW50ZigiXHQgVW5rbm93biBlbGVtZW50IGRldGVjdGVkOiAlYyVjXG4iLCBmb3JtdWxhW2ldLCBmb3JtdWxhW2kgKyAxXSE9ICdcMCcgPyBmb3JtdWxhW2kgKyAxXSA6ICcgJyk7CiAgICAgICAgICAgIHJldHVybiAtMTsgIC8vIOC5geC4quC4lOC4h+C4p+C5iOC4suC4oeC4teC4mOC4suC4leC4uOC4l+C4teC5iOC5hOC4oeC5iOC4o+C4ueC5ieC4iOC4seC4gQogICAgICAgIH0KCiAgICAgICAgLy8g4LiV4Lij4Lin4LiI4Liq4Lit4Lia4LiI4Liz4LiZ4Lin4LiZ4LiC4Lit4LiH4Lit4Liw4LiV4Lit4LihCiAgICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgICB3aGlsZSAoaXNkaWdpdChmb3JtdWxhW2kgKyAxXSkpIHsKICAgICAgICAgICAgY291bnQgPSBjb3VudCAqIDEwICsgKGZvcm11bGFbKytpXSAtICcwJyk7CiAgICAgICAgfQogICAgICAgIGlmIChjb3VudCA9PSAwKSBjb3VudCA9IDE7CiAgICAgICAgbW9sZWN1bGFyX3dlaWdodCArPSB3ZWlnaHQgKiBjb3VudDsKICAgICAgICBpKys7CiAgICB9CgogICAgcmV0dXJuIG1vbGVjdWxhcl93ZWlnaHQ7Cn0KCmludCBtYWluKCkgewogICAgZmxvYXQgZywgTXcsIE1vbCwgTiwgVjsKICAgIGNoYXIgZm9ybXVsYVsxMDBdOwoKICAgIHByaW50ZigiXHQgUGxlYXNlIHNlbGVjdCBjaG9pY2UgdG8gRmluZCBNb2wgXG4iKTsKICAgIHByaW50ZigiXHQgKioqKioqKioqKioqKioqKipcbiIpOwogICAgcHJpbnRmKCJcdCAxLklmIHdhbnQgdG8gZmluZCBNb2wgZnJvbSBnL013ICNcbiIpOwogICAgcHJpbnRmKCJcdCAyLklmIHdhbnQgdG8gZmluZCBNb2wgZnJvbSBOLyg2LjAyKnBvdygxMCwyMykpICNcbiIpOwogICAgcHJpbnRmKCJcdCAzLklmIHdhbnQgdG8gZmluZCBNb2wgZnJvbSBWLzIyLjQgI1xuIik7CiAgICBwcmludGYoIlx0IFNlbGVjdCA6ICIpOwogICAgc2NhbmYoIiVkIiwgJmMpOwoKICAgIHN3aXRjaCAoYykgewogICAgY2FzZSAxOgoKICAgICAgICBwcmludGYoIlx0IFBsZWFzZSBlbnRlciBnIDogIik7CiAgICAgICAgc2NhbmYoIiVmIiwgJmcpOwoKICAgICAgICBwcmludGYoIlx0IFBsZWFzZSBlbnRlciBjaGVtaWNhbCBmb3JtdWxhIChlLmcuLCBIMk8pOiAiKTsKICAgICAgICBzY2FuZigiJXMiLCBmb3JtdWxhKTsKCiAgICAgICAgZG91YmxlIE13ID0gY2FsY3VsYXRlX21vbGVjdWxhcl93ZWlnaHQoZm9ybXVsYSk7CiAgICAgICAgaWYgKE13ID09IC0xKSB7CiAgICAgICAgICAgIHByaW50ZigiXHQgSW52YWxpZCBmb3JtdWxhIG9yIHVua25vd24gZWxlbWVudC5cbiIpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CgogICAgICAgIHByaW50ZigiXHQgTW9sZWN1bGFyIHdlaWdodCBvZiAlcyBpcyAlLjJmIGcvbW9sXG4iLCBmb3JtdWxhLCB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