Advertisement
Guest User

Helloworld but in comic sans

a guest
Feb 22nd, 2018
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.29 KB | None | 0 0
  1. /**********************************************************************
  2. p1xmi708.c  by Holguin, Lorenzo
  3. (change the previous line for your abc123 ID and your name)
  4. Purpose:
  5.     This program reads team data to show current rankings.
  6. Command Line Arguments:
  7.     p1  -t  teamFileName  
  8. Input:  
  9.     Stream input file which contains many teams. There are two different
  10.     kinds of lines of data for each team:
  11.     - Team Identification Information:
  12.         o One line per team (separated by spaces)
  13.             szTeamId  iWins  iLosses  dFeeAmount  dPaidAmount  
  14.              6s       d      d        lf          lf
  15.     - Team Contact Information:
  16.         o One line per team (separated by commas)
  17.             szTeamNm  szEmail szPhone szContactName
  18.             12s       30s     13s     20s
  19.         o Although szTeamNm is a maximum of 12 characters, it may
  20.           contain spaces; therefore, you cannot simply use %13s.  
  21.           For szFullName, you will have to use a bracket format
  22.           code using %[^,].  The next two values are also terminated by
  23.           commas.
  24.         o For szContactName, it contains spaces and is terminated by
  25.           a new line character.  You will have to use a bracket format code
  26.           using %[^\n]
  27.  
  28. Results:
  29.     Print the list of teams as shown below.
  30.     Examples:
  31.         Id     Team Name    Wins Loss  Fee Amt   Paid Amt
  32.                         Contact Name         Phone         Email
  33.         UTSA01 Armadillos      8    0    150.00     80.00
  34.                         Jean E Us            (210)555-1111 utsa@xyz.com
  35.         COM001 Comm Eagles     7    1    150.00     75.00
  36.                         Mae King             (210)555-2222 maeking@xyz.com
  37.         SOUTH1 Slam Dunk       5    3    120.00     75.00
  38.                         Jerry Tall           (210)555-3333 slamdunk@gmail.com
  39.         ALHGHT Cake Eaters     4    4    175.00    100.00
  40.                         E Z Street           (210)555-6666 sliverspoon@xyz.com
  41.         UNKN01 Org New Blk     1    7    150.00     50.00
  42.                         Bob Wire             (210)555-1234 bobwire@xyz.com
  43.         NEWB01 River Rats      0    8    120.00     75.00
  44.                         Rock D Boat          (210)555-4444 riverrat@xyz.com
  45.         UNKN02 Hackers         3    5    150.00     75.00
  46.                         Tom E Gunn           (210)555-5555 cyber@gmail.com
  47.  
  48.  
  49. Returns:
  50.     0  normal
  51.     -1 invalid command line syntax
  52.     -2 show usage only
  53.     -3 error during processing, see stderr for more information
  54. Notes:
  55.     p1 -?  will provide the usage information.  In some shells,
  56.                 you will have to type reserve -\?
  57.  
  58. **********************************************************************/
  59. // If compiling using visual studio, tell the compiler not to give its warnings
  60. // about the safety of scanf and printf
  61. #define _CRT_SECURE_NO_WARNINGS 1
  62.  
  63. #include <stdio.h>
  64. #include <string.h>
  65. #include <stdlib.h>
  66. #include "cs1713p2.h"
  67. FILE *pFileTeam;               // stream Input for Team data
  68. FILE *pFileGame;              // stream input for games
  69.  
  70. void processCommandSwitches(int argc, char *argv[], char **ppszTeamFileName, char **ppszGameFileName);
  71.  
  72. int main(int argc, char *argv[])
  73. {
  74.    
  75.     char *pszTeamFileName = NULL;
  76.     char *pszGameFileName = NULL;
  77.     Team teamM[MAX_TEAMS];
  78.     int iteamcnt;
  79.    
  80.     // Process the command switches
  81.     processCommandSwitches(argc, argv,  &pszTeamFileName, &pszGameFileName);
  82.    
  83.     // open the Team stream data file
  84.     if (pszTeamFileName == NULL)
  85.         exitError(ERR_MISSING_SWITCH, "-t");
  86.    
  87.     pFileTeam = fopen(pszTeamFileName, "r");
  88.     if (pFileTeam == NULL)
  89.         exitError(ERR_TEAM_FILENAME, pszTeamFileName);
  90.        
  91.        // open the Game stream data file
  92.     if (pszGameFileName == NULL)
  93.         exitError(ERR_MISSING_SWITCH, "-g");
  94.    
  95.     pFileGame = fopen(pszGameFileName, "r");
  96.     if (pFileGame == NULL)
  97.         exitError(ERR_TEAM_FILENAME, pszGameFileName);
  98.    
  99.     // get the Teams Data
  100.     iteamcnt = getTeams(teamM);
  101.    
  102.     //print team info
  103.     printTeams("original information", teamM, iteamcnt);
  104.    
  105.     //print heading for games
  106.     printf("%-7s%-8s%8s%7s\n", "Team 1", "Score 1", "Score 2", "Team 2");
  107.    
  108.     //process GameFile
  109.     processGameFile(teamM, iteamcnt);
  110.    
  111.     //reprint team info
  112.     printTeams("Updated Information",teamM, iteamcnt);
  113.    
  114.     fclose(pFileGame);
  115.     fclose(pFileTeam);
  116.     printf("\n");    // included so that you can put a breakpoint on this line
  117.     return 0;
  118. }
  119.  
  120. /****************************************getTeams************************************
  121. void getTeams()
  122. Parameters:
  123.     O Team teamM[] array of teams
  124. returns:
  125.     count of the number of teams
  126. purpose:
  127.     Get team info and places it into an array
  128.  
  129. ************************************************************************************/
  130. int getTeams(Team teamM[])
  131. {
  132.     //setting all variables I'll need
  133.     char szDataBuffer[100]; //my understanding is this is a buffer
  134.     int iScanfcnt;
  135.    
  136.     int iTeamcnt = 0;
  137.    
  138.     //reads from file until EOF
  139.     while(fgets(szDataBuffer, 100, pFileTeam) != NULL)
  140.     {
  141.         iScanfcnt = sscanf(szDataBuffer, "%6s %d %d %lf %lf\n", teamM[iTeamcnt].szTeamId,
  142.                                     &teamM[iTeamcnt].iWins,
  143.                                     &teamM[iTeamcnt].iLosses,
  144.                                     &teamM[iTeamcnt].dFeeAmount,
  145.                                     &teamM[iTeamcnt].dPaidAmount);
  146.         if (iScanfcnt < 5){
  147.             exitError("Bad Team Identification", szDataBuffer);
  148.         }
  149.  
  150.         //reads second line of information then breaks back into larger loop
  151.         while(fgets(szDataBuffer, 100, pFileTeam) != NULL)
  152.         {  
  153.             iScanfcnt = sscanf(szDataBuffer, "%[^,],%[^,],%[^,],%[^\n]",teamM[iTeamcnt].szTeamName,
  154.                                             teamM[iTeamcnt].szEmailAddr,
  155.                                             teamM[iTeamcnt].szPhone,
  156.                                             teamM[iTeamcnt].szContactname);
  157.             if(iScanfcnt < 4){
  158.                 exitError("Bad Team Identification", szDataBuffer);
  159.             }
  160.            
  161.             break;  //breaks out so it won't continue to read for the 4 on next line.
  162.         }
  163.         iTeamcnt +=1; //increment count so that it can be returned
  164.     }
  165.     return iTeamcnt;
  166.    
  167. }
  168.  
  169. /******************** processCommandSwitches *****************************
  170. void processCommandSwitches(int argc, char *argv[], char **ppszTeamFileName)
  171. Purpose:
  172.     Checks the syntax of command line arguments and returns the filenames.
  173.     If any switches are unknown, it exits with an error.
  174. Parameters:
  175.     I   int argc                    Count of command line arguments
  176.     I   char *argv[]                Array of command line arguments
  177.     O   char **ppszTeamFileName     Team file name
  178. Notes:
  179.     If a -? switch is passed, the usage is printed and the program exits
  180.     with USAGE_ONLY.
  181.     If a syntax error is encountered (e.g., unknown switch), the program
  182.     prints a message to stderr and exits with ERR_COMMAND_LINE_SYNTAX.
  183. **************************************************************************/
  184. void processCommandSwitches(int argc, char *argv[], char **ppszTeamFileName, char **ppszGameFileName)
  185. {
  186.     int i;
  187.     // Examine each command argument (except argument 0 which is the executable name)
  188.     for (i = 1; i < argc; i++)
  189.     {
  190.         // check for a switch
  191.         if (argv[i][0] != '-')
  192.             exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]);
  193.         // determine which switch it is
  194.         switch (argv[i][1])
  195.         {
  196.             case 'g':                   // Game File Name
  197.                 if (++i >= argc)
  198.                     exitUsage(i, ERR_MISSING_ARGUMENT, argv[i - 1]);
  199.                 else
  200.                     *ppszGameFileName = argv[i];
  201.                 break;
  202.             case 't':                   // Team File Name
  203.                 if (++i >= argc)
  204.                     exitUsage(i, ERR_MISSING_ARGUMENT, argv[i - 1]);
  205.                 else
  206.                     *ppszTeamFileName = argv[i];
  207.                 break;
  208.             case '?':
  209.                 exitUsage(USAGE_ONLY, "", "");
  210.                 break;
  211.             default:
  212.                 exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]);
  213.         }
  214.     }
  215. }
  216.  
  217. /*****************processGameFile****************
  218. void processGameFile(Team teamM[], int iTeamcnt)
  219. Purpose:
  220.     reads information from game file until EOF
  221.     passes information line by line to processGame
  222. Parameters
  223.     I Team teamM[]  Team structure with all teams
  224.     I int iTeamCnt  count of teams in team structure
  225.    
  226. *************************************************/
  227. void processGameFile(Team teamM[], int iTeamCnt)
  228. {
  229.     char szDataBuffer[100];
  230.     Game game;
  231.     int iScanfcnt;
  232.     //reads game file until EOF
  233.     while(fgets(szDataBuffer, 100, pFileGame) != NULL)
  234.         {  
  235.             iScanfcnt = sscanf(szDataBuffer, "%6s %6s %d %d",game.szTeamId1,
  236.                                             game.szTeamId2,
  237.                                             &game.iScore1,
  238.                                             &game.iScore2);
  239.             if(iScanfcnt < 4){
  240.                 exitError("Bad Game File", szDataBuffer);
  241.             }
  242.         processGame(game, teamM, iTeamCnt);
  243. }
  244.  
  245. }
  246.  
  247. /*****************processGame********************
  248.  void processGame(Game game,  int iTeamCnt)
  249.  Purpose:
  250.     Processes game info and checks to see if teams exist, adds wins and losses to teams
  251.     Prints out game information
  252. Parameters:
  253.     I Game game     game structure
  254.     I Team teamM[]  Team structure
  255.     I int iTeamCnt  count of teams in team structure
  256.  ************************************************/
  257. void processGame(Game game, Team teamM[], int iTeamCnt)
  258. {
  259.     int iT1Index;
  260.     int iT2Index;
  261.    
  262.     printf("%-7s%-8d%8d%7s ",game.szTeamId1,
  263.                                 game.iScore1,
  264.                                 game.iScore2,
  265.                                 game.szTeamId2);
  266.    
  267.     iT1Index = findTeam(teamM,iTeamCnt, game.szTeamId1);
  268.     iT2Index = findTeam(teamM,iTeamCnt, game.szTeamId2);
  269.    
  270.     //check to see if teams exist
  271.     if(iT1Index == -1)
  272.         printf(WARN_TEAM_NOT_FOUND, game.szTeamId1);
  273.    
  274.     if(iT2Index == -1)
  275.         printf(WARN_TEAM_NOT_FOUND, game.szTeamId2);
  276.    
  277.     if(iT1Index == iT2Index){
  278.         printf(WARN_SAME_TEAM);
  279.         }
  280.      
  281.    
  282.     if(game.iScore1 > game.iScore2){
  283.         teamM[iT1Index].iWins +=1;
  284.         teamM[iT2Index].iLosses +=1;
  285.     }else if(game.iScore2 > game.iScore1){
  286.         teamM[iT2Index].iWins +=1;
  287.         teamM[iT1Index].iLosses +=1;
  288.     }else if(game.iScore1 == game.iScore2){
  289.         printf(WARN_GAME_WAS_TIE);
  290.     }
  291.    
  292.    
  293.        
  294.     printf("\n");
  295.    
  296. }
  297.  
  298. /*****************findTeam***********************
  299.  int findTeam(Team teamM[], int iTeamCnt, char szTeamId[])
  300.  Purpose:
  301.     compares name of teamIDs within the team structure and game structure
  302. Parameters:
  303.     I Team teamM[]      team structure
  304.     I int iTeamCnt      count of teams in team structure
  305.     I szTeamId[]        string of team ID to compare with team structure
  306. Returns:
  307.     any positive integer as the index of Team
  308.     -1 if nothing is found
  309.  ************************************************/
  310. int findTeam(Team teamM[], int iTeamCnt, char szTeamId[])
  311. {
  312.     int i = 0;
  313.    
  314.     while(i<iTeamCnt){
  315.         if(strcmp(teamM[i].szTeamId,szTeamId) != 0){
  316.         i++;
  317.         }else{
  318.          return i;    
  319.         }
  320.     }
  321.     return -1;
  322. }
  323.  
  324. /******************** exitError *****************************
  325.     void exitError(char *pszMessage, char *pszDiagnosticInfo)
  326. Purpose:
  327.     Prints an error message and diagnostic to stderr.  Exits with
  328.     ERROR_PROCESSING.
  329. Parameters:
  330.     I char *pszMessage              error message to print
  331.     I char *pszDiagnosticInfo       supplemental diagnostic information
  332. Notes:
  333.     This routine causes the program to exit.
  334. **************************************************************************/
  335. void exitError(char *pszMessage, char *pszDiagnosticInfo)
  336. {
  337.     fprintf(stderr, "Error: %s %s\n"
  338.         , pszMessage
  339.         , pszDiagnosticInfo);
  340.     exit(ERROR_PROCESSING);
  341. }
  342. /******************** exitUsage *****************************
  343.     void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo)
  344. Purpose:
  345.     If this is an argument error (iArg >= 0), it prints a formatted message
  346.     showing which argument was in error, the specified message, and
  347.     supplemental diagnostic information.  It also shows the usage. It exits
  348.     with ERR_COMMAND_LINE_SYNTAX.
  349.  
  350.     If this is just asking for usage (iArg will be -1), the usage is shown.
  351.     It exits with USAGE_ONLY.
  352. Parameters:
  353.     I int iArg                      command argument subscript
  354.     I char *pszMessage              error message to print
  355.     I char *pszDiagnosticInfo       supplemental diagnostic information
  356. Notes:
  357.     This routine causes the program to exit.
  358. **************************************************************************/
  359. void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo)
  360. {
  361.     if (iArg >= 0)
  362.         fprintf(stderr, "Error: bad argument #%d.  %s %s\n"
  363.             , iArg
  364.             , pszMessage
  365.             , pszDiagnosticInfo);
  366.     fprintf(stderr, "p1 -t TeamFileName\n");
  367.     if (iArg >= 0)
  368.         exit(-1);
  369.     else
  370.         exit(-2);
  371. }
  372. /******************** printTeams *****************************
  373.    void printTeams(char szHeading[], Team teamM[], int iTeamCnt)
  374. Purpose:
  375.     Prints team information.
  376. Parameters:
  377.     i char szHeading[]  string describing why the teams are printed
  378.                         (e.g., "Original Team Information")
  379.     i Team teamM[]      array of teams
  380.     i int iTeamCnt      number of teams
  381. Notes:
  382.     Prints two lines of text per team.
  383. **************************************************************************/
  384. void printTeams(char szHeading[], Team teamM[], int iTeamCnt)
  385. {
  386.     int i;          // local counter
  387.     // Print the heading
  388.     printf("%s\n", szHeading);
  389.     printf("  %-6s %-12s %4s %4s  %-8s  %-8s\n"
  390.         , "Id", "Team Name", "Wins", "Loss", "Fee Amt", "Paid Amt");
  391.     printf("\t\t  %-20s %-13s %s\n"
  392.         , "Contact Name", "Phone", "Email");
  393.     // iterate through the teamM array
  394.     for (i = 0; i < iTeamCnt; i++)
  395.     {
  396.         printf("  %-6s %-12s %4d %4d  %8.2lf  %8.2lf\n"
  397.             , teamM[i].szTeamId
  398.             , teamM[i].szTeamName
  399.             , teamM[i].iWins
  400.             , teamM[i].iLosses
  401.             , teamM[i].dFeeAmount
  402.             , teamM[i].dPaidAmount);
  403.         printf("\t\t  %-20s %-13s %s\n"
  404.             , teamM[i].szContactname
  405.             , teamM[i].szPhone
  406.             , teamM[i].szEmailAddr);
  407.     }
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement