C分段故障中的方程求解器

时间:2013-06-27 14:49:08

标签: c

#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

而退出

有人可以指出我哪里出错了。我非常感谢您对我的代码的任何其他帮助

2 个答案:

答案 0 :(得分:4)

这是一个问题:

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

只为这三个值提供了两个参数。

答案 1 :(得分:4)

scanf()中没有像question()这样的输入函数,所以如果t偶然不是'y'或'n',那么在超过堆栈大小之前会得到无限的递归...