C语言解方程

早年作品,不是今天发现,恐怕要永远丢了。
解方程的种类包括:

  1. 多元一次
    1.1 二元一次
    1.2 三元一次
    1.3 四元一次
  2. 一元二次
  3. 一元高次
  4. 进制转换

转载请注明出处,联系我: [email protected]
本人热衷于数据库技术及算法的研究,志同道合之士, 欢迎探讨

#include
#include
main()
{
code_fun();
}

code_fun()
{long int code=123,accept_code,j;
for(j=0;j<3;j++)
    {
     printf("Qing shu ru mi ma:");
     scanf("%ld",&accept_code);
     if(j<2)
       {
        if(code==accept_code)
        {clrscr();menu();break;}
        else
        {clrscr();printf("Mi ma cuo wu!Ni hai you %d ji hui.\n\n",2-j);}
       }
     else
       {
       printf("\nDui bu qi,Ni yi jing shu ru cuo wu 3 ci,An ren yi jian tui chu.\n");
       getch();
       tui_chu();
       }
    }
}

tui_chu()
{
}

menu()
{
int variable_1;
printf("\n\n\n\n\n\n\n               1: Duo yuan yi ci fang cheng zu.\n");
printf("               2: Yi yuan er ci fang cheng.\n");
printf("               3: Yi yuan gao ci fang cheng.\n");
printf("               4: Jin zhi zhuan huan.\n");
printf("               5: Lian xi wo.\n");
printf("               6: Tui chu.\n");
printf("               Qing shu ru:");
scanf("%d",&variable_1);
 if(variable_1>0&&variable_1<7)
   {
     switch(variable_1)
     {
     case 1:duo_yuan_yi_ci();break;
     case 2:yi_yuan_er_ci();break;
     case 3:yi_yuan_gao_ci();break;
     case 4:jin_zhi_zhuan_huan();break;
     case 5:contact_me();break;
     case 6:break;
     }
   }
  else
   {
   clrscr();
   printf("Shu ru cuo wu,Qing chong xin shu ru.");
   menu();
   }

}

     duo_yuan_yi_ci()/*属于menu()*/
     {
     int variable_2;
     clrscr();
     printf("\n\n\n\n\n\n\n                1: Er yuan yi ci.\n");
     printf("                2: San yuan yi ci.\n");
     printf("                3: Si yuan yi ci.\n");
     printf("                4: Hui zhu cai dan.\n");
     printf("                Qing shu ru:");
     scanf("%d",&variable_2);
     switch(variable_2)
     {
        case 1:er_yuan_yi_ci();break;
        case 2:san_yuan_yi_ci();break;
        case 3:si_yuan_yi_ci();break;
        case 4:{clrscr();menu();}
       }

     }

     er_yuan_yi_ci()/*属于duo_yuan_yi_ci()*/
     {float a1,b1,c1,a2,b2,c2;
     clrscr();
     printf("Fang cheng mo shi wei: a1*x+b1*y=c1\n");
     printf("                       a2*x+b2*y=c2\n");

     printf("\n\nQing shu ru a1:");
     scanf("%f",&a1);
     printf("Qing shu ru b1:");
     scanf("%f",&b1);
     printf("Qing shu ru c1:");
     scanf("%f",&c1);

     printf("\n");
     printf("Qing shu ru a2:");
     scanf("%f",&a2);
     printf("Qing shu ru b2:");
     scanf("%f",&b2);
     printf("Qing shu ru c2:");
     scanf("%f",&c2);

     if(a1/a2!=b1/b2)
     {
     printf("\n");
     printf("x=%f\n",-((b1*c2-b2*c1)/(a1*b2-a2*b1)));
     printf("y=%f\n",(a1*c2-a2*c1)/(a1*b2-a2*b1));
     }
     else
     {
     printf("\n");
     printf("a1/a2=b1/b2,fang cheng wu li.");
     }

     printf("\n");
     printf("Fang cheng wu li,An ren yi jian hui zhu cai dan.");
     getch();
     clrscr();
     menu();
     }

     san_yuan_yi_ci()/*属于duo_yuan_yi_ci()*/
     {
     float a1,b1,c1,d1,   a2,b2,c2,d2,  a3,b3,c3,d3,   x,y,z;
     clrscr();

     /*标注模式*/
     printf("Fang cheng mo shi wei:a1*x+b1*y+c1*z=d1\n");
     printf("                      a2*x+b2*y+c2*z=d2\n");
     printf("                      a3*x+b3*y+c3*z=d3\n");

     printf("Qing shu ru a1:");
     scanf("%f",&a1);
     printf("Qing shu ru b1:");
     scanf("%f",&b1);
     printf("Qing shu ru c1:");
     scanf("%f",&c1);
     printf("Qing shu ru d1:");
     scanf("%f",&d1);printf("\n");

     printf("Qing shu ru a2:");
     scanf("%f",&a2);
     printf("Qing shu ru b2:");
     scanf("%f",&b2);
     printf("Qing shu ru c2:");
     scanf("%f",&c2);
     printf("Qing shu ru d2:");
     scanf("%f",&d2);printf("\n");

     printf("Qing shu ru a3:");
     scanf("%f",&a3);
     printf("Qing shu ru b3:");
     scanf("%f",&b3);
     printf("Qing shu ru c3:");
     scanf("%f",&c3);
     printf("Qing shu ru d3:");
     scanf("%f",&d3);printf("\n");

     y=((d1*a2-d2*a1)*(c3*a2-c2*a3)-(d3*a2-d2*a3)*(c1*a2-c2*a1))/((b1*a2-b2*a1)*(c3*a2-c2*a3)-(b3*a2-b2*a3)*(c1*a2-c2*a1));
     z=((d1*a2-d2*a1)*(b2*a3-b3*a2)-(d2*a3-d3*a2)*(b1*a2-b2*a1))/((c1*a2-c2*a1)*(b2*a3-b3*a2)-(c2*a3-c3*a2)*(b1*a2-b2*a1));
     x=(d1-c1*z-b1*y)/a1;
     printf("x=%f\n",x);
     printf("y=%f\n",y);
     printf("z=%f\n",z);
     printf("An ren yi jian hui zhu cai dan.");
     getch();
     clrscr();
     menu();
     }

     si_yuan_yi_ci()/*属于duo_yuan_yi_ci()*/
     {
     float a1=1,b1=1,c1=1,d1=1,e1=10,  a2=1,b2=2,c2=1,d2=1,e2=12,  a3=1,b3=1,c3=2,d3=1,e3=13,  a4=1,b4=1,c4=1,d4=2,e4=14,  x,y,z,w,  jian_ce_1,jian_ce_2;
     jian_ce_1=(((e1*d2-e2*d1)*(a2*d3-a3*d2)-(e2*d3-e3*d2)*(a1*d2-a2*d1))*((b2*d3-b3*d2)*(a3*d4-a4*d3)-(b3*d4-b4*d3)*(a2*d3-a3*d2))-((e2*d3-e3*d2)*(a3*d4-a4*d3)-(e3*d4-e4*d3)*(a2*d3-a3*d2))*((b1*d2-b2*d1)*(a2*d3-a3*d2)-(b2*d3-b3*d2)*(a1*d2-a2*d1)));
     jian_ce_2=(((c1*d2-c2*d1)*(a2*d3-a3*d2)-(c2*d3-c3*d2)*(a1*d2-a2*d1))*((b2*d3-b3*d2)*(a3*d4-a4*d3)-(b3*d4-b4*d3)*(a2*d3-a3*d2))-((c2*d3-c3*d2)*(a3*d4-a4*d3)-(c3*d4-c4*d3)*(a2*d3-a3*d2))*((b1*d2-b2*d1)*(a2*d3-a3*d2)-(b2*d3-b3*d2)*(a1*d2-a2*d1)));
     printf("%f,%f",jian_ce_1,jian_ce_2);
     getch();
     z=(((e1*d2-e2*d1)*(a2*d3-a3*d2)-(e2*d3-e3*d2)*(a1*d2-a2*d1))*((b2*d3-b3*d2)*(a3*d4-a4*d3)-(b3*d4-b4*d3)*(a2*d3-a3*d2))-((e2*d3-e3*d2)*(a3*d4-a4*d3)-(e3*d4-e4*d3)*(a2*d3-a3*d2))*((b1*d2-b2*d1)*(a2*d3-a3*d2)-(b2*d3-b3*d2)*(a1*d2-a2*d1)))/(((c1*d2-c2*d1)*(a2*d3-a3*d2)-(c2*d3-c3*d2)*(a1*d2-a2*d1))*((b2*d3-b3*d2)*(a3*d4-a4*d3)-(b3*d4-b4*d3)*(a2*d3-a3*d2))-((c2*d3-c3*d2)*(a3*d4-a4*d3)-(c3*d4-c4*d3)*(a2*d3-a3*d2))*((b1*d2-b2*d1)*(a2*d3-a3*d2)-(b2*d3-b3*d2)*(a1*d2-a2*d1)));
     printf("%f",z);
     getch();
     }

yi_yuan_er_ci()/*属于menu*/
{
float a,b,c,d,x1,x2;/*d设计为判别式*/
clrscr();
printf("Fang cheng mo shi wei: a*x*x+b*x+c=0\n");
printf("\n\nQing shu ru a:");
scanf("%f",&a);
printf("Qing shu ru b:");
scanf("%f",&b);
printf("Qing shu ru c:");
scanf("%f",&c);
d=b*b-4*a*c;
printf("\n");
printf("\nfen xi jie guo wei:");
if(d==0)/*注意:C里面的等号为“==”*/
  {printf("\n\nx1=x2=%f\n",(-b)/(2*a));
  printf("zhong zhou wei: x=%f\n",(-b)/(2*a));
printf("ding dian wei:(%f,%f)\n",(-b)/(2*a),(4*a*c-b*b)/(4*a));}
else if(d>0)
  {printf("\n\nx1=%f\nx2=%f\n",((-b)+sqrt(d))/(2*a),((-b)-sqrt(d))/(2*a));
  printf("zhong zhou wei: x=%f\n",(-b)/(2*a));
  printf("ding dian wei:(%f,%f)\n",(-b)/(2*a),(4*a*c-b*b)/(4*a));}
else
  {printf("\n\nfang cheng wu shi gen.\n");
  printf("zhong zhou wei: x=%f\n",(-b)/(2*a));
  printf("ding dian wei:(%f, %f)\n",(-b)/(2*a),(4*a*c-b*b)/(4*a));}

printf("Press any key to back menu.");
getch();
clrscr();
menu();
}

     yi_yuan_gao_ci()
     {
     int veriable_9;
     clrscr();
     printf("\n\n\n\n\n\n\n");
     printf("                 1.yi yuan san ci.\n");
     printf("                 2.yi yuan si ci.\n");
     printf("                 3.Back menu.\n");
     printf("                 Qing shu ru:");
     scanf("%d",&veriable_9);
     if(veriable_9>0&&veriable_9<4)
       {
     switch(veriable_9)
          {
     case 1:yi_yuan_san_ci();break;
     case 2:yi_yuan_si_ci();break;
     case 3:{clrscr();menu();}break;
           }
        }
     else
        {
        printf("shu ru cuo wu,an ren yi jian chong xin xuan ze.");
        getch();
        clrscr();
        yi_yuan_gao_ci();
        }
     }

     float f(float a9,float b9,float c9,float d9, float x)/*属于yi_yuan_gao_ci()*/
     {
     float y;
     y=a9*x*x*x+b9*x*x+c9*x+d9;
     return(y);
     }

     yi_yuan_san_ci()/*属于menu()*/
     {
     float x1,x2,x3,a8,b8,c8,d8;
     clrscr();

     printf("fang cheng mo shi wei:a*x*x*x+b*x*x+c*x+d=0\n");

     printf("qing shu ru a: ");
     scanf("%f",&a8);

     printf("qing shu ru b: ");
     scanf("%f",&b8);

     printf("qing shu ru c: ");
     scanf("%f",&c8);

     printf("qing shu ru d: ");
     scanf("%f",&d8);

     printf("Please input an area that include one root(like 1,2):");
     scanf("%f,%f",&x1,&x2);
     if(f(a8,b8,c8,d8,x1)*f(a8,b8,c8,d8,x2)>0)
       {printf("yi xia shi bu neng dei dao gen de san zhong ke neng de qing kuang:\n");
        printf("1.shu ru de qu jian bu bao han gen.\n");
        printf("2.shu ru de qu jian bao han liang ge yi shang de gen.\n");
        printf("3.zai shu ru de qu jian shang qu xian yu x zhou xiang qie.\n");
        printf("Press any key to input again.");
        getch();
       yi_yuan_gao_ci();}
     else
     {
     do
      {
      x3=(x1+x2)/2;
      if(f(a8,b8,c8,d8,x3)*f(a8,b8,c8,d8,x1)>0)
        x1=x3;
      else
        x2=x3;
      }
     while(fabs(f(a8,b8,c8,d8,x3))>=0.000001);/*注意写头文件“#include”*/
     printf("yi ge gen shi: %f",x3);
     printf("\n");
     printf("An ren yi jian hui zhu cai dan.");
     getch();
     clrscr();
     menu();
     }
     }
     float f1(float a,float b,float c,float d,float e, float x)/*属于yi_yuan_gao_ci()*/
     {
     float y;
     y=a*x*x*x*x+b*x*x*x+c*x*x+d*x+e;
     return(y);
     }

     yi_yuan_si_ci()/*属于menu()*/
     {
     float x1,x2,x3,a,b,c,d,e;
     clrscr();

     printf("fang cheng mo shi wei:a*x*x*x*x+b*x*x*x+c*x*x+d*x+e=0\n");

     printf("qing shu ru a: ");
     scanf("%f",&a);

     printf("qing shu ru b: ");
     scanf("%f",&b);

     printf("qing shu ru c: ");
     scanf("%f",&c);

     printf("qing shu ru d: ");
     scanf("%f",&d);

     printf("qing shu ru e: ");
     scanf("%f",&e);

     printf("Please input an area that include one root(like 1,2):");
     scanf("%f,%f",&x1,&x2);
     if(f1(a,b,c,d,e,x1)*f1(a,b,c,d,e,x2)>0)
       {printf("yi xia shi bu neng dei dao gen de san zhong ke neng de qing kuang:\n");
        printf("1.shu ru de qu jian bu bao han gen.\n");
        printf("2.shu ru de qu jian bao han liang ge yi shang de gen.\n");
        printf("3.zai shu ru de qu jian shang qu xian yu x zhou xiang qie.\n");
        printf("Press any key to input again.");
        getch();
       yi_yuan_gao_ci();}
     else
     {
     do
      {
      x3=(x1+x2)/2;
      if(f1(a,b,c,d,e,x3)*f1(a,b,c,d,e,x1)>0)
        x1=x3;
      else
        x2=x3;
      }
     while(fabs(f1(a,b,c,d,e,x3))>=0.0000001);/*注意写头文件“#include”*/
     printf("one root is %f",x3);

     printf("\n");
     printf("Press any key to back menu.");
     getch();
     clrscr();
     menu();
     }
     }
     
/*任意进制到十进制*/
unsigned long fun1()
{
unsigned long s=0;/*yjz=yuan jin zhi的首字母,即是原来的数是多少进制*/
int i,yjz,veriable_3,a[8];
printf("Qing shu ru yao zhuan huan de shu de jin zhi:\n");
scanf("%d",&yjz);/*注意地址引用符&*/
if(yjz>1&&yjz<11)
{
printf("Qing shu ru ni de shu ju de 8 wei xing shi\n");
for(i=0;i<=7;i++)
    {printf("di %d wei: ",8-i);
    scanf("%d",&a[i]);
    }
for(i=0;i<=7;i++)
    s=s+a[i]*(unsigned long)pow(yjz,7-i);
return(s);
}
else
{
printf("Bao qian,zhi neng zai 2-10 jin zhi zhi jian jin xing zhuan huan.\n\n");
printf("Press any key to continue.");
getch();
clrscr();
fun1();
}
}
/*十进制到任意进制*/
void fun2(unsigned long m)
{int i,ys[8],hjz;/*ys=yu shu,hjz=hou mian de jin zhi*/
printf("qing shu ru yao dei dao de jin zhi:\n");
scanf("%d",&hjz);
for(i=0;i<=7;i++)
    {
    ys[i]=m%hjz;
    m=m/hjz;
    }
for (i=7;i>=0;i--)
     printf("%2d",ys[i]);
}

/*子主函数*/
jin_zhi_zhuan_huan()/*仅限于正数*/
{unsigned long sj;/*sj=shi jin zhi*/
clrscr();
sj=fun1();
printf("shi jin zhi wei:%ld\n",sj);
fun2(sj);
printf("\nPress any key to back menu.");
getch();
clrscr();
menu();
}


contact_me()
{
clrscr();
printf("E-mail:[email protected]\n");
printf("\n");
printf("    QQ:407-959-978\n");
printf("\n");
printf("An ren yi jian hui zhu cai dan.");
getch();
clrscr();
menu();
}














你可能感兴趣的:(C语言解方程)