Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C PROGRAMMING ONLY PLEASE To recover scanner.c and scanner.h: wget troll.cs.ua.e

ID: 3673872 • Letter: C

Question

C PROGRAMMING ONLY PLEASE

To recover scanner.c and scanner.h:   wget troll.cs.ua.edu/ACP-C/scanner.c wget troll.cs.ua.edu/ACP-C/scanner.h

Project Overviewi Now that you are in college, you have to manage more things on your own. For example, you have to figure out what to wear every day. Since you also do your own laundry, you are constantly asking yourself Can I wear this or do I need to wash it? If you look at the diagram on the next page, we've developed a flowchart that allows a person to determine whether he/she should wear or wash the article of clothing under consideration. The flowchart asks a series of questions and expects specific answers to each question. For example, the first question you will ask the user is What are you considering? and the user will always give you one of three possible answers - shirt, pants, or other For this project, you can assume that the user: Will always enter lower-case answers (or legal, non-negative numbers) to the questions Will always enter a single-word answer to the questions The complete set of possible inputs that you could see with this program is shown below shirt, pants, other, t-shirt, nice, sweatshirt, jeans, socks, fine, marginal, bad, none, small, lots * * As an example of the execution of this program, consider the two instances shown below. Program prompts are shown in blue and the user's input is shown in red. What are you considering? shirt What kind? t-shirt How does it smell? fine Are there any stains on it? none You can wear this What are you considering? pants What kind? jeans How many times have you worn them? 7 How does it smell? bad You need to wash this We recommend writing functions for the basic questions that get asked on a regular basis. These four functions are: What Kind? and Smell Test and Stains Test and Times Worn. Each of these functions would ask the user a question, read the user's input, and the return a value to the calling routine. The exact wording of the question is not important; you can phrase it as you want as long as the user knows what the expected answer should be.

Explanation / Answer

Solution: See the code below. Source files mentioned by you - scanner.h and scanner.c were not accessible through wget as there was no response from the URL given by you. So, I have used printf() and scanf() functions for I/O. You can modify the code at respective places to use I/O as per your scanner files.

------------------------------------------------------------------

/*
============================================================================
This programs helps a person determine whether he/she should wear or wash
the article of clothing under consideration.
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//enum types of various values
typedef enum {
   FINE, MARGINAL, BAD
} SMELLS;
typedef enum {
   TSHIRT, NICE, SWEATSHIRT
} SHIRT_TYPES;
typedef enum {
   JEANS, OTHERPANT
} PANT_TYPES;
typedef enum {
   SHOCKS, OTHER
} OTHER_TYPES;
typedef enum {
   NONE, SMALL, LOTS
} STAINS;
typedef enum {
   SHIRT, PANTS, OTHERS
} WEAR_TYPES;

/**
* Ask the user - "What kind?" for shirt type
* returns - answer to the question.
* Valid answers are "t-shirt", "nice", "sweatshirt".
*/
SHIRT_TYPES ask_shirt_type() {
   char kind[12];
   SHIRT_TYPES types;
   printf("What kind?(t-shirt,nice,sweatshirt)");
   scanf("%s", kind);
   if (strcmp(kind, "t-shirt") == 0)
       types = TSHIRT;
   else if (strcmp(kind, "nice") == 0)
       types = NICE;
   else if (strcmp(kind, "sweatshirt") == 0)
       types = SWEATSHIRT;
   return types;
}

/**
* Ask the user - "What kind?" for pant type
* Valid answers are "jeans", "other".
*/
PANT_TYPES ask_pant_type() {
   char kind[12];
   PANT_TYPES types;
   printf("What kind?(jeans,other)");
   scanf("%s", kind);
   if (strcmp(kind, "jeans") == 0)
       types = JEANS;
   else if (strcmp(kind, "other") == 0)
       types = OTHERPANT;
   return types;
}

/**
* Ask the user - "What kind?" for other type
* Valid answers are "shocks", "other".
*/
PANT_TYPES ask_other_type() {
   char kind[12];
   OTHER_TYPES types;
   printf("What kind?(shocks,other)");
   scanf("%s", kind);
   if (strcmp(kind, "shocks") == 0)
       types = SHOCKS;
   else if (strcmp(kind, "other") == 0)
       types = OTHER;
   return types;
}

/**
* Performs smell test and returns the value.
* Valid smell values are "fine", "marginal", "bad".
*/
SMELLS perform_smell_test() {
   char smell[10];
   SMELLS types;
   printf("How does it smell? (fine, marginal, bad)");
   scanf("%s", smell);
   if (strcmp(smell, "fine") == 0)
       types = FINE;
   else if (strcmp(smell, "marginal") == 0)
       types = MARGINAL;
   else if (strcmp(smell, "bad") == 0)
       types = BAD;
   return types;
}

/**
* Performs stains test and returns the value.
* Valid stain values are "none", "small", "lots"
*/
STAINS perform_stain_test() {
   char stain[8];
   STAINS types;
   printf("Are there any stains on it?(none,small,lots)");
   scanf("%s", stain);
   if (strcmp(stain, "none") == 0)
       types = NONE;
   else if (strcmp(stain, "small") == 0)
       types = SMALL;
   else if (strcmp(stain, "lots") == 0)
       types = LOTS;
   return types;
}

/**
* Checks how many times a cloth is worn and returns the value.
*/
int check_time_worn() {
   int times = 0;
   printf("How many times you worn them?");
   scanf("%d", &times);
   return times;
}

/**
* Ask the user about wear type.
* Valid types are "shirt", "pants", "other".
*/
WEAR_TYPES ask_wear_type() {
   char wear[10];
   WEAR_TYPES types;
   printf("What are you considering (shirt, pant or other?");
   scanf("%s", wear);
   if (strcmp(wear, "shirt") == 0)
       types = SHIRT;
   else if (strcmp(wear, "pants") == 0)
       types = PANTS;
   else if (strcmp(wear, "other") == 0)
       types = OTHERS;
   return types;
}

int main(void) {
   WEAR_TYPES wear_type = ask_wear_type();
   SHIRT_TYPES shirt_type = -1;
   PANT_TYPES pant_type = -1;
   OTHER_TYPES other_type = -1;
   SMELLS smell = -1;
   STAINS stain = -1;
   int times = -1;
   switch (wear_type) {
   case SHIRT:
       shirt_type = ask_shirt_type();
       switch (shirt_type) {
       case TSHIRT:
           smell = perform_smell_test();
           if (smell == FINE || smell == MARGINAL) {
               stain = perform_stain_test();
               if (stain == NONE || stain == SMALL) {
                   printf("You can wear this. ");
               } else if (stain == LOTS) {
                   printf("You need to wash this ");
               }
           } else if (smell == BAD) {
               printf("You need to wash this ");
           }
           break;
       case NICE:
           stain = perform_stain_test();
           if (stain == NONE) {
               printf("You can wear this. ");
           } else if (stain == SMALL || stain == LOTS) {
               printf("You need to wash this ");
           }
           break;
       case SWEATSHIRT:
           times = check_time_worn();
           if (times < 3) {
               stain = perform_stain_test();
               if (stain == NONE || stain == SMALL) {
                   printf("You can wear this. ");
               } else if (stain == LOTS) {
                   printf("You need to wash this ");
               }
           } else {
               smell = perform_smell_test();
               if (smell == FINE) {
                   printf("You can wear this. ");
               } else if (smell == MARGINAL || smell == BAD) {
                   printf("You need to wash this ");
               }
           }
           break;
       }
       break;
   case PANTS:
       pant_type = ask_pant_type();
       switch (pant_type) {
       case JEANS:
           times = check_time_worn();
           if (times < 5) {
               printf("You can wear this. ");
           } else {
               smell = perform_smell_test();
               if (smell == FINE || smell == MARGINAL) {
                   stain = perform_stain_test();
                   if (stain == NONE || stain == SMALL) {
                       printf("You can wear this. ");
                   } else if (stain == LOTS) {
                       printf("You need to wash this ");
                   }

               } else if (smell == BAD) {
                   printf("You need to wash this ");
               }
           }
           break;
       case OTHERPANT:
           stain = perform_stain_test();
           if (stain == NONE) {
               printf("You can wear this. ");
           } else if (stain == SMALL || stain == LOTS) {
               printf("You need to wash this ");
           }
           break;
       }
       break;
   case OTHERS:
       other_type = ask_other_type();
       switch (other_type) {
       case SHOCKS:
           if (times == 0) {
               printf("You can wear this. ");
           } else if (times >= 1) {
               if (smell == FINE) {
                   printf("You can wear this. ");
               } else if (smell == MARGINAL || smell == BAD) {
                   printf("You need to wash this ");
               }
           }
           break;
       case OTHER:
           if (times == 1) {
               if (stain == NONE) {
                   printf("You can wear this. ");
               } else if (stain == SMALL || stain == LOTS) {
                   printf("You need to wash this ");
               }
           } else if (times > 1) {
               if (smell == FINE || smell == MARGINAL) {
                   printf("You can wear this. ");
               } else if (smell == BAD) {
                   printf("You need to wash this ");
               }
           }
           break;
       }
       break;
   }
   return 0;
}

-----------------------------------------------------------------------------