Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- CIS 26B: Homework 1B
- ********************
- NAME:
- IDE (compiler):
- *************************************************************************
- Review: Functions, Structures, Pointers, Arrays, Sorting, Data Files,
- and Dynamic Allocation of Memory
- Create and process a dynamically allocated array of structures
- Save the output as a comment at the end of the program!
- */
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #ifdef _MSC_VER
- #include <crtdbg.h> // needed to check for memory leaks (Windows only!)
- #endif
- typedef struct
- {
- char* name; // dynamically allocated
- char identity[5]; //not dynamically allocated
- int performance[6]; // no more than 6
- } SALESPERSON;
- typedef struct
- {
- int size; // the number of salespeople in the list
- SALESPERSON* salesperson; //dynamically allocated
- } LIST;
- FILE *dataFile;
- char filename[50]; //no more than 50
- void printBeginningMessage(void);
- void requestInputFile(char filename[]);
- void readDataFile(const char filename[]);
- LIST *createEmptyList(int numberSalespersons);
- void addItemToList(LIST *list, char identity[], char *name, int performance[], int numWeeks);
- void printList(LIST *list, int numWeeks);
- char* allocateString(char *inString);
- void searchManager(LIST *list, int numWeeks);
- void writeOutput(LIST *list, int numWeeks, char filename[]);
- void printTableHeaderEquals(const FILE *filePointer, int numWeeks, int maxLength, int addTotal);
- // const char FILENAME[] = "sales_2.txt"
- int main(void)
- {
- int numWeeks;
- numWeeks = 4; //for testing purposes
- printBeginningMessage();
- requestInputFile(filename);
- printf("%s", filename);
- if (strcmp(filename, "") == 0) {
- strcpy(filename, "sales.txt"); //Default filename
- }
- printf("Filename to be used: %s\n", filename);
- readDataFile(filename);
- #ifdef _MSC_VER
- printf(_CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");
- #endif
- return 0;
- }
- void printBeginningMessage(void) {
- printf("Welcome to CIS 26B Homework 1 B, a program which creates and processes arrays of structures.\n");
- }
- void requestInputFile(char filename[]) {
- printf("Please enter the input file: ");
- fgets(filename, 10000, stdin);
- filename[strlen(filename) - 1] = '\0';
- printf(filename);
- }
- void readDataFile(const char filename[]) {
- dataFile = fopen(filename, "r");
- if (dataFile == NULL) {
- printf("Error opening %s for reading!", filename);
- exit(1);
- }
- // analyze the data file
- // read the number of salespersons & the number of weeks
- int numberSalespersons;
- int numberWeeks;
- fscanf(dataFile, "%d %d", &numberSalespersons, &numberWeeks);
- // printf("%d %d", numberSalespersons, numberWeeks);
- // create the list
- LIST* list = createEmptyList(numberSalespersons);
- // read the lines, line by line
- char line[700];
- char tempIdentity[5];
- char tempName[500];
- int tempPerformanceArray[6];
- int mode = 0;
- int times = 0;
- char* numberPtr;
- //Assemble the scanf string
- char percentS[] = " %s";
- char format[18] = "%s";
- for (int i = 1; i < numberWeeks; i++) {
- strcat(format, percentS);
- }
- printList(list, 1);
- // "JOM2 Johnson, Mary; 32 30 33 29"
- int k = 0;
- while fscanf(dataFile, "%4s, %[^;];") {
- }
- while (fgets(line, 100, dataFile)) {
- if (k == 0) {
- k = 1;
- continue;
- }
- printf("LINE %s\n", line);
- for (int i = 0; i < strlen(line); i++) {
- switch (mode) {
- case 0:
- // reading ID
- tempIdentity[i] = line[i];
- times++;
- if (times == 4) {
- mode = 1;
- }
- break;
- case 1:
- mode = 2;
- break;
- case 2:
- tempName[i - 5] = line[i]; //last name
- if (line[i] == ' ') {
- mode = 3;
- }
- break;
- case 3:
- if (line[i] == ';') {
- mode = 4;
- }
- else {
- tempName[i - 5] = line[i];
- }
- break;
- case 4:
- mode = 5;
- break;
- case 5:
- // "32 30 33 29"
- numberPtr = &line[i];
- // extract
- // printf("NUMBERPTR %s", numberPtr);
- mode = 6;
- }
- }
- // printf("Identity: %s\n", tempIdentity);
- // printf("Name: %s\n", tempName);
- for (int i = 0; i < numberWeeks; i++) {
- sscanf(numberPtr, "%d", &tempPerformanceArray[i]);
- // printf("%d %d\n", i, tempPerformanceArray[i]);
- numberPtr = numberPtr + 3;
- }
- // printf("\n\n\n\n");
- //add item to list
- addItemToList(list, tempIdentity, tempName, tempPerformanceArray, numberWeeks);
- printf("%s\n", tempName);
- mode = 0;
- times = 0;
- numberPtr = NULL;
- //We don't need to wipe the identity because it's always 4 chars long
- memset(tempName, 0, sizeof(tempName)); // wipe the name, since not all names are the same length
- }
- printList(list, numberWeeks);
- writeOutput(list, numberWeeks, filename);
- searchManager(list, numberWeeks);
- if (fclose(dataFile) == EOF) {
- printf("Error closing %s!", filename);
- exit(1);
- }
- }
- LIST *createEmptyList(int numberSalespersons) {
- LIST *list;
- // allocate the header of the list
- list = (LIST *)malloc(sizeof(LIST));
- if (!list)
- printf("Error allocating the header of the list!\n"), exit(111);
- list->salesperson = (SALESPERSON *)calloc(numberSalespersons, sizeof(SALESPERSON));
- if (!list) {
- printf("Error allocating the list of salespersons.\n"), exit(112);
- }
- list->size = numberSalespersons;
- return list;
- }
- void writeOutput(LIST *list, int numWeeks, char* filename) {
- char tempOutFilename[53];
- strncpy(tempOutFilename, filename, strlen(filename) - 4);
- tempOutFilename[strlen(filename) - 4] = '\0';
- strcat(tempOutFilename, "Out.txt");
- printf(tempOutFilename);
- FILE * outFile;
- outFile = fopen(tempOutFilename, "w");
- fprintf(outFile, "===========================\nABC Manufacturing Company\n===========================\n");
- fprintf(outFile, "*** Sales Table ***");
- // Calculate the max salesperson length
- //int maxLength = 0;
- //for (int i = 0; i < list->size; i++) {
- // printf("%s", list->salesperson[i].name);
- // if (strlen(list->salesperson[i].name) > maxLength) {
- // maxLength = strlen(list->salesperson[i].name);
- // }
- //}
- //printTableHeaderEquals(outFile, numWeeks, maxLength, 1);
- //fclose(outFile);
- }
- void printTableHeaderEquals(const FILE *filePointer, int numWeeks, int maxLength, int addTotal) {
- for (int i = 0; i < maxLength; i++) {
- fprintf(filePointer, "=");
- }
- for (int i = 0; i < numWeeks; i++) {
- fprintf(filePointer, " ======");
- }
- if (addTotal) {
- fprintf(filePointer, " ============");
- }
- fprintf(filePointer, "\n");
- }
- void addItemToList(LIST *list, char identity[], char *name, int performance[], int numWeeks) {
- for (int i = 0; i < list->size; i++) {
- if (strcmp(list->salesperson[i].name, "") == 0) {
- strcpy(list->salesperson[i].name, name);
- printf("%s\n", list->salesperson[i].name);
- strcpy(list->salesperson[i].identity, identity);
- printf("Identity: %4s", list->salesperson[i].identity);
- for (int j = 0; j < numWeeks; j++) {
- // printf("%d", performance[j]);
- list->salesperson[i].performance[j] = performance[j];
- }
- for (int j = 0; j < numWeeks; j++) {
- // printf("T %d ", list->salesperson[i].performance[j]);
- }
- printf("\n");
- break;
- }
- else {
- }
- }
- }
- void searchManager(LIST *list, int numWeeks) {
- char query[5];
- char tempQuery[50];
- while (1) {
- printf("Enter ID: ");
- scanf("%s", tempQuery);
- if (strlen(tempQuery) != 4) {
- printf("ID wrong length!\n");
- continue;
- }
- strncpy(query, tempQuery, 4);
- if (strcmp(query, "QUIT") == 0) {
- printf("*** Quit message received; exiting!\n");
- break;
- }
- for (int i = 0; i < list->size; i++) {
- if (strcmp(query, list->salesperson[i].identity) == 0) {
- printf("ID: %4s\n", list->salesperson[i].identity);
- printf("Name: %s\n", list->salesperson[i].name);
- printf("Performance:");
- for (int j = 0; j < numWeeks; j++) {
- printf(" %d", list->salesperson[i].performance[j]);
- }
- printf("\n");
- }
- }
- }
- }
- void printList(LIST *list, int numWeeks) {
- for (int i = 0; i < list->size; i++) {
- if (strcmp(list->salesperson[i].name, "") != 0) {
- printf("ID: %4s \n", list->salesperson[i].identity);
- printf("Name: %s\n", list->salesperson[i].name);
- printf("Performance:");
- for (int j = 0; j < numWeeks; j++) {
- printf(" %d", list->salesperson[i].performance[j]);
- }
- printf("\n");
- }
- }
- }
- char* allocateString(char *inString)
- {
- // Returns a pointer to a dynamically allocated string.
- char *outString;
- int stringSize;
- stringSize = strlen(inString) + 1;
- outString = (char *)calloc(stringSize, sizeof(char));
- if (outString == NULL)
- printf("ERROR, not enough memory!!!\a\n"), exit(103);
- strcpy(outString, inString);
- return outString;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement