This program will reinforce our knowledge of “if”, “while”, “scanf”, “printf”, a
ID: 3885678 • Letter: T
Question
This program will reinforce our knowledge of “if”, “while”, “scanf”, “printf”, and other topics such as editing and compiling programs.
In Taxiland, the tax system is scaled as follows:
• If you are a government worker, you pay a flat tax of 15% of your income.
• Otherwise, you pay 10% on the first $5,000, then an additional 20% on the amount over $5,000.
• Unless, of course, you drive a taxi. In this case, you pay the standard taxi surcharge of $800, plus 1/3 of your taxi income. Uber has not made it there yet.
The Assignment! Your job, should you decide to accept it, is to save the country by providing an easy way to calculate the tax owed by a citizen. Your program should:
• Read the amount that the worker has earned. Print an error message and exit if the amount is <= $0.
• Read in the type of worker, either ‘G’ for government, ‘T’ for taxi driver, or ‘O’ for other. Print an error message and exit if the letter is none of these.
• The program should print out the amount in “check format” (but capitalization is not important). The program must use a function to do this printing.
Examples: Amount Prints as $123.45 one hundred twenty three dollars and forty five cents; $1,234.56 one thousand two hundred thirty four dollars and fifty six cents; $100.00 one hundred dollars and no cents
and this should work for any amount up to $99,999.99. Note that the cents are printed even if they are zero, so every answer should have “and”… in there.
• Include the C source code obviously but also a “Makefile”.
• The program should continue to run until end-of-file is indicated. You can force the end-of-file with control-D. By the way, “scanf” will return the number of values that were assigned, so this is easy to check. Here is an example execution of the program.
The data you type in is in italics: What is the amount of pay? 12345.67 What is the worker type? G One thousand eight hundred fifty one dollars and eighty five cents
What is the amount of pay? 65432.10 What is the worker type? O Twelve thousand five hundred eighty six dollars and forty two cents
What is the amount of pay? 34343.43 What is the worker type? T Twelve thousand two hundred forty seven dollars and eighty one cents
The first is 15% of $12345.67. The second is $500 on the first $5,000, plus 20% of $60,432.10. The last one is $800 + $34,343.43 / 3
I would suggest using the “scanf” function with “%d.%d” as the format string. Then combine the dollars and cents into one number, like 6543210 in the middle example above.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//Function to print a given number in words
void convert_to_words(char *number)
{
//Stores number of digits in given number
int length = strlen(number);
//If the length is zero display error message
if (length == 0)
{
fprintf(stderr, " Empty string ");
return;
}//End of if
//If the length is more than 4 display error message
if (length > 4)
{
fprintf(stderr, " Length more than 4 is not supported ");
return;
}//End of if
//The first string is not used, it is to make array indexing simple
char *single_digit[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
//The first string is not used, it is to make array indexing simple
char *two_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
//The first two string are not used, they are to make array indexing simple
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
char *tens_power[] = {"hundred", "thousand"};
//Used for debugging purpose only
//printf(" %s: ", num);
//For single digit number
if (length == 1)
{
printf("%s ", single_digit[*number - '0']);
return;
}//End of if
//Iterate while number is not ''
while (*number != '')
{
//For first two digits
if (length >= 3)
{
//Converts to number and checks for not zero
if (*number -'0' != 0)
{
printf("%s ", single_digit[*number - '0']);
//Here length can be 3 or 4
printf("%s ", tens_power[length-3]);
}//End of inner if
//Decrease the length by one
--length;
}//End of outer if
//For last 2 digits
else
{
//Need to explicitly handle 10-19. Sum of the two digits is used as index of "two_digits" array of strings
if (*number == '1')
{
int sum = *number - '0' + *(number + 1)- '0';
printf("%s ", two_digits[sum]);
return;
}//End of if
//Need to explicitly handle 20
else if (*number == '2' && *(number + 1) == '0')
{
printf("twenty ");
return;
}//End of else if
//Rest of the two digit numbers i.e., 21 to 99
else
{
//Converts to integer
int counter = *number - '0';
printf("%s ", counter? tens_multiple[counter]: "");
//Increase the number by one
++number;
if (*number != '0')
printf("%s ", single_digit[*number - '0']);
}//End of inner else
}//End of outer else
//Increase the number by one
++number;
}//End of while loop
}//End of function
//Main function
int main()
{
//To store amount
float amount;
//To store tax
float tax;
//To store integral and fractional part
int intgral, frac;
//To convert to character
char snum[5];
//To store worker type
char workerType;
//Loops till 0 is entered
do
{
//Accepts the amount
printf(" What is the amount of pay? ");
scanf("%f", &amount);
//Checks if the amount is zero or negative
if(amount <= 0)
{
//Display error message
printf(" Amount cannot be zero");
//Exit program
exit(0);
}//End of if
//Clears standard input device
fflush(stdin);
//Accepts the worker type
printf(" What is the worker type? ");
scanf("%c", &workerType);
//Checks if the worker type is G or g
if(workerType == 'G' || workerType == 'g')
{
//Calculates tax
tax = amount * 0.15;
}//End of if
//Checks if the worker type O or o
else if(workerType == 'O' || workerType == 'o')
{
//Checks if the amount is more than 5000
if(amount > 5000)
{
//Calculates tax for first 5000
tax = 500 + (amount - 5000) * .20;
}//End of if
//If less than 5000
else
//Calculates tax
tax = amount * .10;
}//End of else if
//Checks if the worker is T or t
else if(workerType == 'T' ||workerType == 't')
{
//Calculates tax
tax = 800 + amount / 3;
}//End of else
//Invalid worker type
else
{
printf(" Invalid Worker Type ");
continue;
}//End of else
//Displays amount and worker type
printf(" Amount: %.2f Worker: %c", amount, workerType);
printf(" Tax : %.2f ", tax);
//Extracts integral part
intgral = tax;
//Extracts fractional part
frac = (tax - intgral) * 100;
//Converts to character
itoa(intgral, snum, 10);
//Calls the method display word form
convert_to_words(snum);
printf("dollars and ");
itoa(frac, snum, 10);
convert_to_words(snum);
printf(" cents ");
}while(1);//End of while
}//End of main function
Sample Run:
What is the amount of pay? 12345.67
What is the worker type? G
Amount: 12345.67
Worker: G
Tax : 1851.85
one thousand eight hundred fifty one dollars and eighty five cents
What is the amount of pay? 55432.10
What is the worker type? O
Amount: 55432.10
Worker: O
Tax : 10586.42
Length more than 4 is not supported
dollars and forty one cents
What is the amount of pay? 45432.10
What is the worker type? O
Amount: 45432.10
Worker: O
Tax : 8586.42
eight thousand five hundred eighty six dollars and forty one cents
What is the amount of pay? 24343.43
What is the worker type? t
Amount: 24343.43
Worker: t
Tax : 8914.48
eight thousand nine hundred fourteen
dollars and forty seven cents
What is the amount of pay? 12362.23
What is the worker type? m
Invalid Worker Type
What is the amount of pay? 0
Amount cannot be zero
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.