Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.78 KB | None | 0 0
  1. /***
  2. CIS 26B: Homework 1B
  3. ********************
  4. NAME:
  5. IDE (compiler):
  6. *************************************************************************
  7.  
  8. Review: Functions, Structures, Pointers, Arrays, Sorting, Data Files,
  9. and Dynamic Allocation of Memory
  10.  
  11. Create and process a dynamically allocated array of structures
  12. Save the output as a comment at the end of the program!
  13. */
  14. #define _CRT_SECURE_NO_WARNINGS
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #ifdef _MSC_VER
  20. #include <crtdbg.h>  // needed to check for memory leaks (Windows only!)
  21. #endif
  22.  
  23. typedef struct
  24. {
  25.     char* name; // dynamically allocated
  26.     char identity[5]; //not dynamically allocated
  27.     int performance[6];  // no more than 6
  28. } SALESPERSON;
  29.  
  30. typedef struct
  31. {
  32.     int size;     // the number of salespeople in the list
  33.     SALESPERSON* salesperson;  //dynamically allocated
  34. } LIST;
  35.  
  36. FILE *dataFile;
  37. char filename[50]; //no more than 50
  38.  
  39. void printBeginningMessage(void);
  40. void requestInputFile(char filename[]);
  41. void readDataFile(const char filename[]);
  42. LIST *createEmptyList(int numberSalespersons);
  43. void addItemToList(LIST *list, char identity[], char *name, int performance[], int numWeeks);
  44. void printList(LIST *list, int numWeeks);
  45. char* allocateString(char *inString);
  46. void searchManager(LIST *list, int numWeeks);
  47. void writeOutput(LIST *list, int numWeeks, char filename[]);
  48. void printTableHeaderEquals(const FILE *filePointer, int numWeeks, int maxLength, int addTotal);
  49.  
  50. // const char FILENAME[] = "sales_2.txt"
  51.  
  52. int main(void)
  53. {
  54.     int numWeeks;
  55.     numWeeks = 4;   //for testing purposes
  56.     printBeginningMessage();
  57.     requestInputFile(filename);
  58.     printf("%s", filename);
  59.     if (strcmp(filename, "") == 0) {
  60.         strcpy(filename, "sales.txt"); //Default filename
  61.     }
  62.     printf("Filename to be used: %s\n", filename);
  63.     readDataFile(filename);
  64.  
  65.  
  66.  
  67.  
  68. #ifdef _MSC_VER
  69.     printf(_CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");
  70. #endif
  71.  
  72.     return 0;
  73. }
  74.  
  75. void printBeginningMessage(void) {
  76.     printf("Welcome to CIS 26B Homework 1 B, a program which creates and processes arrays of structures.\n");
  77. }
  78.  
  79. void requestInputFile(char filename[]) {
  80.     printf("Please enter the input file: ");
  81.     fgets(filename, 10000, stdin);
  82.     filename[strlen(filename) - 1] = '\0';
  83.     printf(filename);
  84. }
  85.  
  86. void readDataFile(const char filename[]) {
  87.     dataFile = fopen(filename, "r");
  88.     if (dataFile == NULL) {
  89.         printf("Error opening %s for reading!", filename);
  90.         exit(1);
  91.     }
  92.  
  93.     // analyze the data file
  94.  
  95.     // read the number of salespersons & the number of weeks
  96.  
  97.     int numberSalespersons;
  98.     int numberWeeks;
  99.     fscanf(dataFile, "%d %d", &numberSalespersons, &numberWeeks);
  100.  
  101.     //    printf("%d %d", numberSalespersons, numberWeeks);
  102.  
  103.  
  104.  
  105.     // create the list
  106.  
  107.     LIST* list = createEmptyList(numberSalespersons);
  108.  
  109.  
  110.  
  111.  
  112.     // read the lines, line by line
  113.  
  114.     char line[700];
  115.     char tempIdentity[5];
  116.     char tempName[500];
  117.     int tempPerformanceArray[6];
  118.     int mode = 0;
  119.     int times = 0;
  120.     char* numberPtr;
  121.  
  122.     //Assemble the scanf string
  123.  
  124.     char percentS[] = " %s";
  125.     char format[18] = "%s";
  126.     for (int i = 1; i < numberWeeks; i++) {
  127.         strcat(format, percentS);
  128.     }
  129.  
  130.     printList(list, 1);
  131.  
  132.     // "JOM2 Johnson, Mary; 32 30 33 29"
  133.     int k = 0;
  134.     while fscanf(dataFile, "%4s, %[^;];") {
  135.        
  136.     }
  137.     while (fgets(line, 100, dataFile)) {
  138.         if (k == 0) {
  139.             k = 1;
  140.             continue;
  141.         }
  142.         printf("LINE %s\n", line);
  143.        
  144.         for (int i = 0; i < strlen(line); i++) {
  145.             switch (mode) {
  146.             case 0:
  147.                 // reading ID
  148.                 tempIdentity[i] = line[i];
  149.                 times++;
  150.                 if (times == 4) {
  151.                     mode = 1;
  152.                 }
  153.                 break;
  154.             case 1:
  155.                 mode = 2;
  156.                 break;
  157.             case 2:
  158.                 tempName[i - 5] = line[i]; //last name
  159.                 if (line[i] == ' ') {
  160.                     mode = 3;
  161.                 }
  162.                 break;
  163.             case 3:
  164.                 if (line[i] == ';') {
  165.                     mode = 4;
  166.                 }
  167.                 else {
  168.                     tempName[i - 5] = line[i];
  169.                 }
  170.                 break;
  171.             case 4:
  172.                 mode = 5;
  173.                 break;
  174.             case 5:
  175.                 // "32 30 33 29"
  176.                 numberPtr = &line[i];
  177.                 // extract
  178.                 //                    printf("NUMBERPTR %s", numberPtr);
  179.                 mode = 6;
  180.             }
  181.         }
  182.         //        printf("Identity: %s\n", tempIdentity);
  183.         //        printf("Name: %s\n", tempName);
  184.         for (int i = 0; i < numberWeeks; i++) {
  185.             sscanf(numberPtr, "%d", &tempPerformanceArray[i]);
  186.             //            printf("%d %d\n", i, tempPerformanceArray[i]);
  187.             numberPtr = numberPtr + 3;
  188.         }
  189.         //        printf("\n\n\n\n");
  190.         //add item to list
  191.         addItemToList(list, tempIdentity, tempName, tempPerformanceArray, numberWeeks);
  192.         printf("%s\n", tempName);
  193.         mode = 0;
  194.         times = 0;
  195.         numberPtr = NULL;
  196.  
  197.         //We don't need to wipe the identity because it's always 4 chars long
  198.         memset(tempName, 0, sizeof(tempName)); // wipe the name, since not all names are the same length
  199.  
  200.     }
  201.  
  202.     printList(list, numberWeeks);
  203.  
  204.     writeOutput(list, numberWeeks, filename);
  205.  
  206.     searchManager(list, numberWeeks);
  207.  
  208.     if (fclose(dataFile) == EOF) {
  209.         printf("Error closing %s!", filename);
  210.         exit(1);
  211.     }
  212. }
  213.  
  214. LIST *createEmptyList(int numberSalespersons) {
  215.     LIST *list;
  216.  
  217.     // allocate the header of the list
  218.     list = (LIST *)malloc(sizeof(LIST));
  219.     if (!list)
  220.         printf("Error allocating the header of the list!\n"), exit(111);
  221.  
  222.  
  223.     list->salesperson = (SALESPERSON *)calloc(numberSalespersons, sizeof(SALESPERSON));
  224.     if (!list) {
  225.         printf("Error allocating the list of salespersons.\n"), exit(112);
  226.     }
  227.  
  228.     list->size = numberSalespersons;
  229.  
  230.     return list;
  231. }
  232.  
  233. void writeOutput(LIST *list, int numWeeks, char* filename) {
  234.     char tempOutFilename[53];
  235.     strncpy(tempOutFilename, filename, strlen(filename) - 4);
  236.     tempOutFilename[strlen(filename) - 4] = '\0';
  237.     strcat(tempOutFilename, "Out.txt");
  238.     printf(tempOutFilename);
  239.     FILE * outFile;
  240.     outFile = fopen(tempOutFilename, "w");
  241.     fprintf(outFile, "===========================\nABC Manufacturing Company\n===========================\n");
  242.     fprintf(outFile, "*** Sales Table ***");
  243.     // Calculate the max salesperson length
  244.     //int maxLength = 0;
  245.     //for (int i = 0; i < list->size; i++) {
  246.     //  printf("%s", list->salesperson[i].name);
  247.     //  if (strlen(list->salesperson[i].name) > maxLength) {
  248.     //      maxLength = strlen(list->salesperson[i].name);
  249.     //  }
  250.     //}
  251.     //printTableHeaderEquals(outFile, numWeeks, maxLength, 1);
  252.     //fclose(outFile);
  253. }
  254.  
  255.  
  256.  
  257. void printTableHeaderEquals(const FILE *filePointer, int numWeeks, int maxLength, int addTotal) {
  258.     for (int i = 0; i < maxLength; i++) {
  259.         fprintf(filePointer, "=");
  260.     }
  261.  
  262.     for (int i = 0; i < numWeeks; i++) {
  263.         fprintf(filePointer, " ======");
  264.     }
  265.  
  266.     if (addTotal) {
  267.         fprintf(filePointer, " ============");
  268.     }
  269.    
  270.     fprintf(filePointer, "\n");
  271. }
  272.    
  273.  
  274. void addItemToList(LIST *list, char identity[], char *name, int performance[], int numWeeks) {
  275.     for (int i = 0; i < list->size; i++) {
  276.         if (strcmp(list->salesperson[i].name, "") == 0) {
  277.             strcpy(list->salesperson[i].name, name);
  278.             printf("%s\n", list->salesperson[i].name);
  279.             strcpy(list->salesperson[i].identity, identity);
  280.             printf("Identity: %4s", list->salesperson[i].identity);
  281.             for (int j = 0; j < numWeeks; j++) {
  282.                 //                printf("%d", performance[j]);
  283.                 list->salesperson[i].performance[j] = performance[j];
  284.             }
  285.             for (int j = 0; j < numWeeks; j++) {
  286.                 //                printf("T %d ", list->salesperson[i].performance[j]);
  287.             }
  288.             printf("\n");
  289.             break;
  290.         }
  291.         else {
  292.  
  293.         }
  294.     }
  295. }
  296.  
  297. void searchManager(LIST *list, int numWeeks) {
  298.     char query[5];
  299.     char tempQuery[50];
  300.     while (1) {
  301.         printf("Enter ID: ");
  302.         scanf("%s", tempQuery);
  303.         if (strlen(tempQuery) != 4) {
  304.             printf("ID wrong length!\n");
  305.             continue;
  306.         }
  307.         strncpy(query, tempQuery, 4);
  308.         if (strcmp(query, "QUIT") == 0) {
  309.             printf("*** Quit message received; exiting!\n");
  310.             break;
  311.         }
  312.         for (int i = 0; i < list->size; i++) {
  313.             if (strcmp(query, list->salesperson[i].identity) == 0) {
  314.                 printf("ID: %4s\n", list->salesperson[i].identity);
  315.                 printf("Name: %s\n", list->salesperson[i].name);
  316.                 printf("Performance:");
  317.                 for (int j = 0; j < numWeeks; j++) {
  318.                     printf(" %d", list->salesperson[i].performance[j]);
  319.                 }
  320.                 printf("\n");
  321.             }
  322.         }
  323.     }
  324. }
  325.  
  326. void printList(LIST *list, int numWeeks) {
  327.     for (int i = 0; i < list->size; i++) {
  328.         if (strcmp(list->salesperson[i].name, "") != 0) {
  329.             printf("ID: %4s \n", list->salesperson[i].identity);
  330.             printf("Name: %s\n", list->salesperson[i].name);
  331.             printf("Performance:");
  332.             for (int j = 0; j < numWeeks; j++) {
  333.                 printf(" %d", list->salesperson[i].performance[j]);
  334.             }
  335.             printf("\n");
  336.         }
  337.     }
  338. }
  339.  
  340.  
  341. char* allocateString(char *inString)
  342. {
  343.     // Returns a pointer to a dynamically allocated string.
  344.     char *outString;
  345.     int   stringSize;
  346.  
  347.     stringSize = strlen(inString) + 1;
  348.     outString = (char *)calloc(stringSize, sizeof(char));
  349.     if (outString == NULL)
  350.         printf("ERROR, not enough memory!!!\a\n"), exit(103);
  351.     strcpy(outString, inString);
  352.  
  353.     return outString;
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement