Write a program to do fraction arithmetic. A sample run follows:
Welcome to the Fraction Arithmetic program.
-------------------------------------------
Your problems with fractions can be solved here. Enter a
fraction arithemetic problem (Example 2/5 - 4/7). 1/2 + 1/4
The answer is 6/8.
Your program should handle the operations of addition, subtraction, multiplication
and division. The answer does not have to be in lowest terms for this version of the program.
Plan your program so that it is modular. Have the input done in one module,
the output done in a second module, and the calculation done in a third.
Put each module in a separate source code file. The function main() should reside in
its own module and be a driver program. If possible, try to write the
program without using any global variables.
Header file
enum ops {add,sub,mult,divide};
struct fraction
{
int num;
int den;
};
void problem8_7main();
void problem8_7input(struct fraction *first, struct fraction *second, enum ops *operation);
void problem8_7calc(struct fraction first, struct fraction second, enum ops operation,struct fraction *result);
void problem8_7output(struct fraction result);
Input module
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
#include
#include "problem8_7.h"
void problem8_7input(struct fraction *first, struct fraction *second, enum ops *operation)
{
char Input[1024];
char *firsttoken = NULL;
char *opstoken = NULL;
char *secondtoken = NULL;
char *token = NULL;
printf("Welcome to the Fraction Arithmetic program.\n");
printf("-------------------------------------------\n");
printf("Your problems with fractions can be solved here.\n");
printf("Enter a fraction arithmetic problem (Example 2/5 - 4/7)\n");
fgets(Input,1024,stdin);
firsttoken = strtok(Input," \n");
opstoken = strtok(NULL ," \n");
secondtoken = strtok(NULL ," \n");
token=strtok(firsttoken,"/");
first->num=atoi(token);
token=strtok(NULL,"/");
first->den=atoi(token);
token=strtok(secondtoken,"/");
second->num=atoi(token);
token=strtok(NULL,"/");
second->den=atoi(token);
switch(opstoken[0])
{
case '+':
*operation=add;
break;
case '-':
*operation=sub;
break;
case '*':
*operation=mult;
break;
case '/':
*operation=divide;
break;
}
}
Output module
#include "problem8_7.h"
#include
void problem8_7output(struct fraction result)
{
printf("The result is %d/%d\n",result.num,result.den);
}
2006-11-13
09:24:31
·
1 answers
·
asked by
Anonymous