1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*header files*/
#include <stdio.h>
#include <conio.h>
#include <cctype>
#include <string.h>
#include <stdlib.h>
#include <cstdlib>
/*constants*/
#define MAX_CHARACTER_COUNT 100
#define NUMBVER_OF_KNOWN_ELEMENTS 118
typedef struct elements_t{
int atomicNumber;
char name[MAX_CHARACTER_COUNT];
char symbole[MAX_CHARACTER_COUNT];
double atomicMass;
};
typedef struct sorter_t{
int location;
int valueAtLocation;
};
/*function prototypes*/
int yesNoValidation(char input[MAX_CHARACTER_COUNT]);
void printBlanks(int count);
void printInputError(char input[MAX_CHARACTER_COUNT]);
void shouldIclose(char input[MAX_CHARACTER_COUNT]);
void strToUpper(char string[MAX_CHARACTER_COUNT], int lenOfString);
/*main*/
int main()
{
/*variables*/
FILE *inStream;
elements_t input[NUMBVER_OF_KNOWN_ELEMENTS];
sorter_t mySorter;
int crapOut;
int eof = 1;
int subscript=0;
/*input*/
char fileName[] = "elementsTwo";
crapOut = fopen_s( &inStream, "elementsTwo.txt" , "r+");
if (inStream == NULL)
printf_s("Oh crap out on file one");
do{
eof = fscanf_s(inStream, "%d %s %s %lf", input[subscript].atomicNumber, input[subscript].name, input[subscript].symbole, input[subscript].atomicMass);
if (eof != EOF)
subscript++;
} while (eof != EOF);
fclose(inStream);
strcpy_s(fileName, "elementsOne.txt");
crapOut = fopen_s(&inStream, fileName, "r+");
if (inStream == NULL)
printf_s("Oh crap out on file two");
do{
eof = fscanf_s(inStream, "%d %s %s %lf", &input[subscript].atomicNumber, input[subscript].name, input[subscript].symbole, &input[subscript].atomicMass);
if (eof != EOF)
subscript++;
} while (eof != EOF);
fclose(inStream);
do
{
input[subscript].atomicNumber = NUMBVER_OF_KNOWN_ELEMENTS + 1;
subscript++;
} while (subscript<NUMBVER_OF_KNOWN_ELEMENTS);
/*calculations*/
elements_t output[NUMBVER_OF_KNOWN_ELEMENTS];
subscript = 0;
do{
mySorter.valueAtLocation = NUMBVER_OF_KNOWN_ELEMENTS + 1;
for (int i = 0; i < NUMBVER_OF_KNOWN_ELEMENTS; i++)
{
if (input[i].atomicNumber < mySorter.valueAtLocation)
mySorter.valueAtLocation = input[i].atomicNumber;
mySorter.location = i;
}
output[subscript].atomicNumber = input[mySorter.location].atomicNumber;
output[subscript].atomicMass = input[mySorter.location].atomicNumber;
strcpy_s(output[subscript].name, input[mySorter.location].name);
strcpy_s(output[subscript].symbole, input[mySorter.location].symbole);
subscript++;
} while (mySorter.valueAtLocation != NUMBVER_OF_KNOWN_ELEMENTS + 1);
/*output*/
strcpy_s(fileName, "elementsCmb.txt");
crapOut = fopen_s(&inStream, fileName, "wb");
printf_s("Atomic number | Name of Element | Symbol | Atomic Weight");
do{
fprintf_s(inStream, "%d %s %s %.2f\n", output[subscript].atomicNumber, output[subscript].name, output[subscript].symbole, output[subscript].atomicMass);
printf_s("%d %-17s %-8% %.2f\n", output[subscript].atomicNumber, output[subscript].name, output[subscript].symbole, output[subscript].atomicMass);
} while (output[subscript+1].atomicNumber !=NUMBVER_OF_KNOWN_ELEMENTS+1);
fclose(inStream);
/*close*/
/*shouldIclose("");*/
_getch();
return(0);
}
/*functions*/
int yesNoValidation(char prompt[MAX_CHARACTER_COUNT])
{
char userInput[MAX_CHARACTER_COUNT];
int returnValue = 0;
puts(prompt);
gets_s(userInput, MAX_CHARACTER_COUNT);
strToUpper(userInput, MAX_CHARACTER_COUNT);
if ((userInput[0] == 'Y') && (userInput[1] == '\0'))
{
strcpy_s(userInput, 4, "YES");
}
else if ((userInput[0] == 'N') && (userInput[1] == '\0'))
{
strcpy_s(userInput, 3, "NO");
}
if (0 == strcmp(userInput, "YES"))
{
returnValue = 1;
}
else if (0 == strcmp(userInput, "NO"))
{
returnValue = -1;
}
else
{
returnValue = 0;
printInputError(userInput);
}
return(returnValue);
}
void printBlanks(int count)
{
for (int j = 0; j < count; j++)
{
printf_s(" ");
};
}
void printInputError(char input[MAX_CHARACTER_COUNT])
{
printf("\nInput Error: I'm sorry I did not recognize what you told me, %s.\nCould you please try again.\n", input);
}
void shouldIclose(char prompt[MAX_CHARACTER_COUNT])
{
char userinput[MAX_CHARACTER_COUNT];
int yesNoCode = 0;
puts(prompt);
gets_s(userinput);
yesNoCode = yesNoValidation(userinput);
switch (yesNoCode)
{
case 1:
printf("\n\n");
main();
break;
case 2:
printf("\nAlright, have a great day then! Till next time!\n(: GOOD BYE :)");
break;
case 0:
printInputError(userinput);
shouldIclose(prompt);
break;
default:
printf_s("Closing error: please contact the supplier");
break;
}
}
void strToUpper(char string[MAX_CHARACTER_COUNT], int maxCharacterCount)
{
for (int i = 0; (string[i] != '\0') && (i<maxCharacterCount); i++)
{
string[i] = toupper(string[i]);
}
}
|