Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

equation solver in C segmentation fault

#include <stdio.h>

int main()
{
  printf("choose number");
  c();
}

c()
{
  printf("1. ax+b=0

");
  printf("2. ax+by+c=0
   dx+ey+f=0

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

");
  c();
}

formula1()
{
  printf("ax+b=0
");
  printf("Enter your values for a and b respectively, seperated by commas
");
  float a, b, x;
  scanf("%f,%f,%f", &a, &b);
  x = -b / a;
  printf("x=-b/a
");
  printf("=>x=%f", x);
  question();
}

formula2()
{
  printf("ax+by+c=0

dx+ey+f=0
");
  printf(
      "Enter your values for a, b, c, d ,e and f respectively, seperated by commas
");
  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("

");
  printf("=>y=%f", y);
  question();
}

question()
{
  char t;
  printf("

another equation?
y/n?
");
  if (t == 'y')
  {
    printf("




");
    c();
  }
  else if (t != 'n')
    question();
}

I have this code, which in short solves 3 equations. When you select any choice it seems to run the question method multiple times then quits due to a segmentation fault: 11

Could someone please point out where I am going wrong. Any other help with my code would be greatly appreciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is one problem:

scanf("%f,%f,%f",&a, &b);

Only two arguments are supplied for the three values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...