Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//make not output visuals from comnd promp, have a sperate window
//make the program ask how many perifers are around it, get rid of tetrahedral/groups
//allow mutiple view latter on, i.e. orbitals, lewis structure, etc
//in order to be able to show isomers, you put all of lone pairs of same atoms = to each other,
//which ever is greater you switch the extras between the same atom
//*********If electrons are left over after filling up all lone pairs it still shows everything is fine. Move "finished making model" line model after it checks that //all electrons are used (=0)
//******** also if it goes under for some reason it fills the lone pairs no matter what
char charselect[2];
int select;
char tempElement[3];
char centerelement[3];
char pelement1[3]; //periferal element 1
char pelement2[3]; //periferal element 2
char pelement3[3]; //periferal element 3
char pelement4[3]; //periferal element 4
int i; //counter
int charge; //input charge of molecule
char chargestr[3];
int VEC; //# of valance electrons in element
int VE1;
int VE2;
int VE3;
int VE4;
int totalVE; //total valance electrons
int VE; //VE avaliable to work with in model
int VEtemp;
int compare; //used to compare two strings, if = 0, then they are the same string. i.e. C=C or Cl=Cl
//max bonds & lonepairs an atom can have
int maxC;
int max1;
int max2;
int max3;
int max4;
int temp_max;
//LONE PAIRS set to empty as default
char a1[3] = " ";
char a2[3] = " ";
char a3[3] = " ";
char b1[3] = " ";
char b2[3] = " ";
char b3[3] = " ";
char c1[3] = " ";
char c2[3] = " ";
char c3[3] = " ";
char d1[3] = " ";
char d2[3] = " ";
char d3[3] = " ";
/*********************************************************************************/
/***************************Declaration of Functions******************************/
/*********************************************************************************/
/*********************************************************************************/
/*********************************************************************************/
int calc_valance(char* tempElement);
int calc_max_valance(char* tempElement);
int put_lone_pairs(int max1, int max2, int max3, int max4, int VE);
/*********************************************************************************/
/*********************************************************************************/
/*******************************Start of program**********************************/
/*********************************************************************************/
/*********************************************************************************/
/*********************************************************************************/
/*********************************************************************************/
int main()
{
printf("What is the structure of the Lewis Structure?\n\n");
printf("1 - Tetrahedral\n\n");
printf("Input corresponding number: ");
scanf("%s", charselect);
select = atoi(charselect);
if (select == 1)
{
printf("You selected Tetrahedral.\n\n");
printf("What is the center Element? ");
scanf("%s", tempElement);
strcpy (centerelement, tempElement);
calc_valance(tempElement);
VEC = VEtemp;
calc_max_valance(tempElement);
maxC = temp_max;
printf("What is the 1st periferal element? ");
scanf("%s", tempElement);
strcpy (pelement1, tempElement);
calc_valance(tempElement);
VE1 = VEtemp;
calc_max_valance(tempElement);
max1 = temp_max;
printf("What is the 2nd periferal element? ");
scanf("%s", tempElement);
strcpy (pelement2, tempElement);
calc_valance(tempElement);
VE2 = VEtemp;
calc_max_valance(tempElement);
max2 = temp_max;
printf("What is the 3rd periferal element? ");
scanf("%s", tempElement);
strcpy (pelement3, tempElement);
calc_valance(tempElement);
VE3 = VEtemp;
calc_max_valance(tempElement);
max3 = temp_max;
printf("What is the 4th periferal element? ");
scanf("%s", tempElement);
strcpy (pelement4, tempElement);
calc_valance(tempElement);
VE4 = VEtemp;
calc_max_valance(tempElement);
max4 = temp_max;
printf("What is the charge of the molecule?");
scanf("%d", &charge);
printf("\nThe charge of the molecule is %d", charge);
totalVE = VEC + VE1 + VE2 + VE3 + VE4;
VE = totalVE - charge; //calculates valence electrons left with charge given
printf("\n\nTotal available Electrongs available after charge = %d\n\n", VE);
//Put single bonds on periferals
VE = VE - 8;
maxC = maxC - 8;
max1 = max1 - 2;
max2 = max2 - 2;
max3 = max3 -2;
max4 = max4 -2;
//no way to check for error yet for max
printf("After putting single bonds on all connections, %d electrons remain to be used\n\n", VE);
//need to make it so that it does one lone pair at a time for each periferal,
//instead of dumping it all on one, and when filled go to the next one
//later on put charges on atoms. i.e. (+) to replace a missing pair of electrons
VE = put_lone_pairs(max1, max2, max3, max4, VE);
//for some reason doesn't return the negated VE
printf("~ = .. = Lone Pair\n\n");
printf(" %s\n", a2);
printf(" %s%s%s\n", a1, pelement1, a3);
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" %s\n", centerelement);
printf(" /|\\\n");
printf(" / | \\\n");
printf(" / | \\\n");
printf(" / | \\\n");
printf(" / | \\\n");
printf(" / | \\\n");
printf(" %s%s%s %s%s%s %s%s%s\n", b2, pelement2, b3, c2, pelement3, c3, d2, pelement4, d3);
printf(" %s %s %s\n\n", b1, c1, d1);
//make it check if it used all electrons. If it is less than 0 or more than 0 it should err
if(VE>0)
printf("Error! There are %d Valance Electons left to be used! Model is incorrect!\n", VE);
else if (VE<0)
printf("Error! There were more electrons used than available! Model is incorrect!\n");
else
printf("Finished making model.\n");
} //end of tetrahedral (if select = 1)
else //if incorrect input for shape of molecule was chosen, error occurs
{
printf("Error, no selection has been made.");
}
return(0);
} //end main
int calc_valance(char* tempElement)
{
//maybe not put an error check twice bc it owuld show twice, also i do not think it shows at all
if(strcmp(tempElement, "H") == 0)
VEtemp = 1;
else if(strcmp(tempElement, "B") == 0)
VEtemp = 3;
else if(strcmp(tempElement, "C") == 0)
VEtemp = 4;
else if(strcmp(tempElement, "N") == 0)
VEtemp = 5;
else if(strcmp(tempElement, "P") == 0)
VEtemp = 5;
else if(strcmp(tempElement, "O") == 0)
VEtemp = 6;
else if(strcmp(tempElement, "S") == 0)
VEtemp = 6;
else if(strcmp(tempElement, "Se") == 0)
VEtemp = 6;
else if(strcmp(tempElement, "F") == 0)
VEtemp = 7;
else if(strcmp(tempElement, "Cl") == 0)
VEtemp = 7;
else if(strcmp(tempElement, "Br") == 0)
VEtemp = 7;
else if(strcmp(tempElement, "I") == 0)
VEtemp = 7;
else
printf("Error, Element not found in database.");
return(VEtemp);
}//end calc_valance
int put_lone_pairs(int max1, int max2, int max3, int max4, int VE)
{
//int tempMax1 = max1;
//int tempMax2 = max2;
//int tempMax3 = max3;
//int tempMax4 = max4;
//int tempVE = VE;
if (VE>=0 && max1>=2 || max2>=2 || max3>=2 || max4>=2)
{
while(VE>0)
{
compare = strcmp(a1," ");
if (compare==0)
{
strcpy (a1,"..");
max1 = max1 - 2;
VE = VE - 2;
printf("VE = %d, max1 = %d\n", VE, max1);
continue;
}
compare = strcmp(b1," ");
if (compare==0)
{
strcpy (b1,"~");
max2 = max2 - 2;
VE = VE - 2;
printf("VE = %d, max2 = %d\n", VE, max2);
continue;
}
compare = strcmp(c1," ");
if (compare==0)
{
strcpy (c1,"~");
max3 = max3 - 2;
VE = VE -2;
printf("VE = %d, max4 = %d\n", VE, max3);
continue;
}
compare = strcmp(d1," ");
if (compare==0)
{
strcpy (d1,"~");
max4 = max4 - 2;
VE = VE -2;
printf("VE = %d, max4 = %d\n", VE, max4);
continue;
}
compare = strcmp(a2," ");
if (compare==0)
{
strcpy (a2,"~");
max1 = max1 - 2;
VE = VE -2;
printf("VE = %d, max1 = %d\n", VE, max1);
continue;
}
compare = strcmp(b2," ");
if (compare==0)
{
strcpy (b2,"..");
max2 = max2 - 2;
VE = VE -2;
printf("VE = %d, max2 = %d\n", VE, max2);
continue;
}
compare = strcmp(c2," ");
if (compare==0)
{
strcpy (c2,"..");
max3 = max3 - 2;
VE = VE -2;
printf("VE = %d, max3 = %d\n", VE, max3);
continue;
}
compare = strcmp(d2," ");
if (compare==0)
{
strcpy (d2,"..");
max4 = max4 - 2;
VE = VE -2;
printf("VE = %d, max4 = %d\n", VE, max4);
continue;
}
compare = strcmp(a3," ");
if (compare==0)
{
strcpy (a3,"..");
max1 = max1 - 2;
VE = VE -2;
printf("VE = %d, max1 = %d\n", VE, max1);
continue;
}
compare = strcmp(b3," ");
if (compare==0)
{
strcpy (b3,"..");
max2 = max2 - 2;
VE = VE -2;
printf("VE = %d, max2 = %d\n", VE, max2);
continue;
}
compare = strcmp(c3," ");
if (compare==0)
{
strcpy (c3,"..");
max3 = max3 - 2;
VE = VE -2;
printf("VE = %d, max3 = %d\n", VE, max3);
continue;
}
compare = strcmp(d3," ");
if (compare==0)
{
strcpy (d3,"..");
max4 = max4 - 2;
VE = VE -2;
printf("VE = %d, max4 = %d\n", VE, max4);
continue;
}
}
}
printf("After putting lone pairs, %d electrons remain\n\n", VE);
return(VE);
}//end put_lone_pairs
int calc_max_valance(char* tempElement)
{
if(strcmp(tempElement, "H") == 0)
temp_max = 2;
else if (strcmp(tempElement, "B") == 0)
temp_max = 8;
else if (strcmp(tempElement, "C") == 0)
temp_max = 8;
else if (strcmp(tempElement, "N") == 0)
temp_max = 8;
else if (strcmp(tempElement, "P") == 0)
temp_max = 8;
else if (strcmp(tempElement, "O") == 0)
temp_max = 8;
else if (strcmp(tempElement, "S") == 0)
temp_max = 8;
else if (strcmp(tempElement, "Se") == 0)
temp_max = 8;
else if (strcmp(tempElement, "F") == 0)
temp_max = 8;
else if (strcmp(tempElement, "Cl") == 0)
temp_max = 8;
else if (strcmp(tempElement, "Br") == 0)
temp_max = 8;
else if (strcmp(tempElement, "I") == 0)
temp_max = 8;
else if (strcmp(tempElement, "F") == 0)
temp_max = 8;
else
printf("Error, Element not found in database.");
return(temp_max);
}