Edit script for user input rather than internal input

允我心安 提交于 2019-12-13 08:31:17

问题


I have a simple script and it works fine. My question is, how can I get my results to display in a reduced simplified fraction? Also, at this point I am defining the values myself, is there a way to have the script ask the user to input the numerator and denominator for the different fractions used?

The posted script worked wonders for me in regards towards asking and performing the operations, so I am very thankful for that. It did not reduce the fractions, am I still missing something?

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

 int gcd(int a, int b) {
    while(0!=b) { int r = a % b; a=b; b=r; }
    return a;
}
 int input(char* prompt) {
    int res;
    printf("%s: ", prompt);
    scanf("%d", &res);
    return res;
}
 main()
 {
    int add,sub,mul,dd;
    int add1,sub1,mul1,dd1;
    int a,b,c,d;
    int fac = gcd(add, add1);
    a=input("Please enter the numerator for your first equation:");
    b=input("Please enter the denominator for your first equation:");
    c=input("Please enter the numerator for your second equation:");
    d=input("Please enter the denominator for your second equation:");
    add=(a*d+b*c);
    add1=(b*d);
    add /=fac;
    add1/=fac;
    printf("\The sum of your fractions is: %d/%d",add,add1);
    sub=(a*d-b*c);
    sub1=(b*d);
    printf("\nThe difference of your fractions is: %d/%d",sub,sub1);
    mul=(a*c);
    mul1=(b*d);
    printf("\nThe product of your fractions is: %d/%d",mul,mul1);
    dd=(a*d);
    dd1=(b*c);
    printf("\nThe quotient of your fractions is: %d/%d",dd,dd1);
 }

回答1:


for each possible factor from 2 to min (numerator, denominator)
    while this factor evenly divides both numerator and denominator
        numerator   /= factor;
        denominator /= factor;

This will do it. Actually, you could go up to min (sqrt (numerator), sqrt (denominator)), and that would work too.

Since you need it several times, best put it into a function and call it on each of your results.




回答2:


Look up the Euclidean algorithm. This is faster to compute the gcd of two numbers than trying out all possible prime factors.

int gcd(int a, int b) {
    return  (0==b) ? a : gcd(b, a % b);
}

or

int gcd(int a, int b) {
    while(0!=b) { int r = a % b; a=b; b=r; }
    return a;
}

Then apply, for instance for the sum, as

fac = gcd(add, add1); 
add /=fac; 
add1/=fac;

to get the reduced numbers.


To your other question, use the atoi function to transform argument string into numbers or use the scanf function to prompt for input. Be aware that no input verification is done by the C functions.

#include ...

int gcd(int a, int b) { ... }

int input(char* prompt) {
    int res;
    printf("%s: ", prompt);
    scanf("%d", &res);
    return res;
}

int main() {
...
int a,b,c,d;
a = input("Number for a");
b = input("Number for b");
...


来源:https://stackoverflow.com/questions/33787851/edit-script-for-user-input-rather-than-internal-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!