/***************************************************
F = m a Program
Write a program that solves for the unknown variable
in the F = m a equation. The user enters two
variable = value pairs (e.g "m = 10.0 F = 3.5") and
the program writes the unknown variable and its
value ("a = .35" in our example).
***************************************************/
#include "si204.h"
/* Note: this is the straightfoward but very TEDIOUS
* way to solve this problem. */
int main() {
fputs("The F = m a Program\n", stdout);
// Read first variable/value pair
char var1;
char c;
double val1;
fputs("Enter <var> = <value> : ", stdout);
var1 = readchar(stdin); // the variable
c = readchar(stdin); // the equals sign
val1 = readnum(stdin); // the value
// Read second variable/value pair
char var2;
double val2;
fputs("Enter <var> = <value> : ", stdout);
var2 = readchar(stdin); // the variable
c = readchar(stdin); // the equals sign
val2 = readnum(stdin); // the value
// Decide which variable is missing & solve
if (var1 == 'F') {
// var1 is F
if (var2 == 'm') {
fputs("a = ", stdout);
writenum(val1 / val2, stdout);
fputs("\n", stdout);
} else {
fputs("m = ", stdout);
writenum(val1 / val2, stdout);
fputs("\n", stdout);
}
} else {
if (var1 == 'm') {
// var1 is m
if (var2 == 'a') {
fputs("F = ", stdout);
writenum(val1 * val2, stdout);
fputs("\n", stdout);
} else {
fputs("a = ", stdout);
writenum(val2 / val1, stdout);
fputs("\n", stdout);
}
} else {
// var1 is a
if (var2 == 'm') {
fputs("F = ", stdout);
writenum(val1 * val2, stdout);
fputs("\n", stdout);
} else {
fputs("m = ", stdout);
writenum(val2 / val1, stdout);
fputs("\n", stdout);
}
}
}
return 0;
}