#include <stdio.h>
int main()
{
printf("choose number");
c();
}
c()
{
printf("1. ax+b=0\n\n");
printf("2. ax+by+c=0\n dx+ey+f=0\n\n");
int n;
scanf("%d", &n);
if (n > 3)
wrong();
if (n == 1)
formula1();
if (n == 2)
formula2();
if (n == 3)
;
formula3();
}
wrong()
{
printf("Please choose a number between 1 and 3.\n\n");
c();
}
formula1()
{
printf("ax+b=0\n");
printf("Enter your values for a and b respectively, seperated by commas\n");
float a, b, x;
scanf("%f,%f,%f", &a, &b);
x = -b/a;
printf("x=-b/a\n");
printf("=>x=%f", x);
question();
}
formula2()
{
printf("ax+by+c=0\n\ndx+ey+f=0\n");
printf(
"Enter your values for a, b, c, d ,e and f respectively, seperated by commas\n");
float a, b, c, d, e, f, x, y;
scanf("%f,%f,%f,%f,%f,%f", &a, &b, &c, &d, &e, &f);
x = ((f * b) - (c * e))/((a * e) - (d * b));
y = ((c * d) - (f * a))/((e * a) - (d * b));
printf("=>x=%f", x);
printf("\n\n");
printf("=>y=%f", y);
question();
}
question()
{
char t;
printf("\n\nanother equation?\ny/n?\n");
if (t == 'y')
{
printf("\n\n\n\n\n");
c();
}
else if (t != 'n')
question();
}
我有這段代碼,它簡單地解決了3個方程。 當你選擇任何選擇,似乎多次運行問題方法,然後退出由於segmentation fault: 11
C分段故障中的方程求解器
有人可以請指出我要去哪裏錯了。任何其他幫助,我的代碼,將不勝感激
'的scanf(「% f,%f,%f「,&a,&b);'你在這裏做什麼?在這裏檢查'x = -b/a;''a!= 0'很重要 – Maroun
請格式化(indent )你的代碼有點 – Cyrille
你可能希望在(-Wall on gcc)上編譯所有警告,修正代碼,直到沒有更多的警告出現,然後使用valgrind運行程序並修復代碼,直到沒有更多的警告出現,最後你可以自由地回來問問題(如果仍然有必要的話) – alk