如果我想再次运行C程序,或者输入错误,我该如何一键重启C程序



我正在编写一个计算函数根的c程序,有时我会输入错误的数字(比如3.333而不是33.333(,如果我可以从顶部重新启动/重新加载,我不想运行或退出程序。这也是一种多次运行程序的方法。一举两得。我是葡萄牙人,很抱歉英语不太好。

#include <stdio.h>
#include <math.h>
float main()
{

char let = 'a';
int b;
float x0, erro, gx, x1,dif, gxlinha, absgxlinha;
int it,val,i, exp, numero, e;
it=0;
int num = 0;
printf("Valor de x0 = ");
scanf("%f", &x0);
printf("Valor do erro = ");
scanf("%f", &erro);
printf("Qual o valor do expoente maior =  ");
scanf("%d", &exp);
printf("n");
val = (exp + 1);

printf("funcao do tipo:  nn    ");
e = exp - 1;
numero = exp;
for(b=0;b<=e;b++)
{
printf("%cx^%d + ",let,numero);
let++;
numero--;
}
printf("%cx^%d",let,numero);
printf("nn");

float a[val];
printf("Introduza os %d numeros da funcao, de acordo com os valores pedidos: n",val); // ex.: ax^2 + bx^1 + cx^0 itroduzir pela ordem c, b, a
printf("n");
for( i=0; i<val; i++ )
{
printf ("%c = ",let);
let--;
scanf("%f", &a[i] );
gx = gx + (a[i])*(pow((x0),num));
num++;
}
num = 1;
for (i=1; i<val; i++)
{
gxlinha = gxlinha + ((a[i]*num)*(pow((x0),num)));
num ++;
}
absgxlinha = (sqrt(pow(gxlinha,2)));
if ( absgxlinha < 1)
{
printf ("ng(x0)' < 1 logo a funcao tem pelo menos uma raizn");
x1=gx;
dif = (sqrt(pow((x1-x0),2)));
printf("n| Iter.| Val de x0| g(x0)=x1 |   x1-x0  |n|      |          |          |          |n|  %d   | %f | %f | %f |  ",it,x0,x1,dif);
while (sqrt(pow((x1-x0),2))>=erro)
{
x0 = x1;
gx = 0;
num = 0;
for( i=0; i<val; i++ )
{
gx = gx + (a[i])*(pow((x0),num));
num++;
}
x1=gx;
dif = (sqrt(pow((x1-x0),2)));
it++;
printf(">= %fn|  %d   | %f | %f | %f |  ",erro,it,x0,x1,dif);
}

printf("<  %f nn",erro);
printf("A raiz com erro inferior a %f = %f",erro,x1);
printf("nnnn");
}
else
{
printf ("ng(x0)' > 1 logo a funcao nao tem raizesn");
}

}

的一些输入在哪里

0.8,0.000005,3,0.333333,0,-0.333333

正如其他人已经说过的,您需要一个循环,可能在一个专用函数内部
这只是一个例子:

while(1)
{
float x;
scanf(" %f", &x);
if (x < 0) // or any other condtion you want
break;
// your code
}

您需要一个循环。例如:

for (;;) {
if (// some condition) {
break;
} else {
//your code
}
}

最新更新