用户输入,而不是内部输入编辑脚本 [英] Edit script for user input rather than internal input

查看:187
本文介绍了用户输入,而不是内部输入编辑脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本,它工作正常。我的问题是,怎样才能让我的成绩在减少简化的分数显示?此外,在这一点上我定义的价值观我自己,有没有办法让脚本要求用户输入不同的分数的分子和分母用?

张贴的脚本创造了奇迹,我的问候对提出和执行的操作,所以我对此非常感激。它没有降低分数,我还是我失去了一些东西?

 #包括LT&;&stdio.h中GT;
 #包括LT&;&MATH.H GT;
 #包括LT&;&string.h中GT; INT GCD(int类型的,INT B){
    而(0 = B!){INT读=%A; A = B; B = - [R; }
    返回;
}
 INT输入(字符*提示){
    中期业绩;
    的printf(%s:时,提示);
    scanf函数(%d个,&安培; RES);
    返回水库;
}
 主要()
 {
    INT增加,SUB,MUL,DD;
    INT ADD1,SUB1,MUL1,DD1;
    一个诠释,B,C,D;
    INT FAC = GCD(添加,ADD1);
    A =输入(请输入分子为你的第一个公式:);
    B =输入(请输入分母为你的第一个公式:);
    C =输入(请输入你的第二个公式中的分子:);
    D =输入(请输入分母你的第二个公式:);
    添加=(A * D + B * C);
    ADD1 =(B * D);
    添加/ = FAC;
    ADD1 / = FAC;
    的printf(\\你的分数之和为:%D /%D,添加ADD1);
    分=(A * D-B * C);
    SUB1 =(B * D);
    的printf(你的分数\\ n此不同的是:%D /%D,子,SUB1);
    MUL =(A * C);
    MUL1 =(B * D);
    的printf(你的分数\\ n该产品是:%D /%D,MUL MUL1);
    DD =(A * D);
    DD1 =(B * C);
    的printf(你的分数\\ n该比值为:%D /%D,DD,DD1);
 }


解决方案

查找欧几里德算法。这是更快的计算两个数的最大公约数比尝试所有可能的素因子

  INT GCD(int类型的,INT B){
    回报(0 == B)?答:GCD(B,A%B);
}

  INT GCD(int类型的,INT B){
    而(0 = B!){INT R = A%B; A = B; B = - [R; }
    返回;
}

然后应用,例如用于总和,如

  FAC = GCD(添加,ADD1);
添加/ = FAC;
ADD1 / = FAC;

获得数量减少。


要您的其他问题,使用的atoi 函数参数字符串转换成数字或使用 scanf函数函数提示输入。请注意,没有输入验证是由C函数完成的。

 的#include ...INT GCD(int类型的,INT B){...}INT输入(字符*提示){
    中期业绩;
    的printf(%s:时,提示);
    scanf函数(%d个,&安培; RES);
    返回水库;
}诠释主(){
...
诠释A,B,C,D;
A =输入(对数);
B =输入(号B);
...

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);
 }

解决方案

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");
...

这篇关于用户输入,而不是内部输入编辑脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆