gpt4 book ai didi

c - 如何让我的脚本循环并根据命令退出?

转载 作者:行者123 更新时间:2023-11-30 18:53:00 24 4
gpt4 key购买 nike

我有一个基本脚本,但我不知道如何让它循环并选择通过关键字“退出”结束脚本。

 #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;
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);
int fac = gcd(add, add1);
add /=fac;
add1 /=fac;
printf("\The sum of your fractions is: %d/%d",add,add1);
sub=(a*d-b*c);
sub1=(b*d);
int red = gcd(sub, sub1);
sub /=red;
sub1 /=red;
printf("\nThe difference of your fractions is: %d/%d",sub,sub1);
mul=(a*c);
mul1=(b*d);
int red1 = gcd(mul, mul1);
mul /=red1;
mul1 /=red1;
printf("\nThe product of your fractions is: %d/%d",mul,mul1);
dd=(a*d);
dd1=(b*c);
int red2 = gcd(dd, dd1);
dd /=red2;
dd1 /=red2;
printf("\nThe quotient of your fractions is: %d/%d",dd,dd1);
}

这个想法是让用户能够持续尝试该功能,即使在给出一组结果之后也是如此。话虽如此,用户应该能够在第一个分子问题中输入“quit”,脚本就会结束。有人可以帮我解决这个问题吗?

更新4:

#include<stdio.h>
#include<math.h>
#include<string.h>
#define LINE_SIZE 100
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;
char line[LINE_SIZE];

printf("%s: ", prompt);
if ( fgets(line, LINE_SIZE, stdin) == NULL )
{

exit(0);
}

if (strncmp(line, "quit", 4) == 0 )
{
exit(0);
}

if ( sscanf(line, "%d", &res) != 1 )
{
}

return res;
}
void computeAndPrint()
{
main();
{
int add,sub,mul,dd;
int add1,sub1,mul1,dd1;
int a,b,c,d;
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);
int fac = gcd(add, add1);
add /=fac;
add1 /=fac;
printf("\The sum of your fractions is: %d/%d",add,add1);
sub=(a*d-b*c);
sub1=(b*d);
int red = gcd(sub, sub1);
sub /=red;
sub1 /=red;
printf("\nThe difference of your fractions is: %d/%d",sub,sub1);
mul=(a*c);
mul1=(b*d);
int red1 = gcd(mul, mul1);
mul /=red1;
mul1 /=red1;
printf("\nThe product of your fractions is: %d/%d",mul,mul1);
dd=(a*d);
dd1=(b*c);
int red2 = gcd(dd, dd1);
dd /=red2;
dd1 /=red2;
printf("\nThe quotient of your fractions is: %d/%d",dd,dd1);
printf("\n");
}

int main();
{
while ( 1 )
{
computeAndPrint();
}
return 0;
}

最佳答案

当您可以选择输入数字或字符串quit时,最好使用fgets,后跟strcmp sscanf

// Make LINE_SIZE as large as you need to
#define LINE_SIZE 100

int input(char* prompt) {
int res;
char line[LINE_SIZE];

printf("%s: ", prompt);
if ( fgets(line, LINE_SIZE, stdin) == NULL )
{
// There is no more input
exit(0);
}

if (strncmp(line, "quit", 4) == 0 )
{
// The user entered quit
exit(0);
}

// Expect to see a number
if ( sscanf(line, "%d", &res) != 1 )
{
// Deal with the error
}

return res;
}

更新,回应OP的评论

  1. main 的核心移至辅助函数。
  2. main中使用while循环。在 while 循环中,调用辅助函数。

void computeAndPrint()
{
int add,sub,mul,dd;
int add1,sub1,mul1,dd1;
int a,b,c,d;

printf("\n");
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);
int fac = gcd(add, add1);
add /=fac;
add1 /=fac;
printf("\nThe sum of your fractions is: %d/%d",add,add1);

sub=(a*d-b*c);
sub1=(b*d);
int red = gcd(sub, sub1);
sub /=red;
sub1 /=red;
printf("\nThe difference of your fractions is: %d/%d",sub,sub1);

mul=(a*c);
mul1=(b*d);
int red1 = gcd(mul, mul1);
mul /=red1;
mul1 /=red1;
printf("\nThe product of your fractions is: %d/%d",mul,mul1);

dd=(a*d);
dd1=(b*c);
int red2 = gcd(dd, dd1);
dd /=red2;
dd1 /=red2;
printf("\nThe quotient of your fractions is: %d/%d",dd,dd1);

printf("\n");
}

int main()
{
while ( 1 )
{
computeAndPrint();
}
return 0;
}

关于c - 如何让我的脚本循环并根据命令退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com