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 %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;string.h%26gt;
#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("------------------------------...
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-%26gt;num=atoi(token);
token=strtok(NULL,"/");
first-%26gt;den=atoi(token);
token=strtok(secondtoken,"/");
second-%26gt;num=atoi(token);
token=strtok(NULL,"/");
second-%26gt;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 %26lt;stdio.h%26gt;
void problem8_7output(struct fraction result)
{
printf("The result is %d/%d\n",result.num,result.den);
}
C program problem.......?
What is your question? If you are having a specific problem we can try to help you. I don't think you are going to get anyone to do this for you. Not for 10 points.
spring flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment