本文介绍了如何从等式中获得系数作为输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过从用户那里获取方程而不是仅采用系数来解决方程我如何从方程中收集系数



我有什么试过:



i wanted to solve equations by taking the equation from user instead of taking only coefficient how do i collect the coefficents from the equation

What I have tried:

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string s;
   cin>>s;
   for(int i=0;i<strlen(s);i++)
   {
       if()
   }
    return 0;
}

推荐答案

#include <iostream>
using namespace std;

int main()
{

 double a,b;

 cout << "the equation is 'ax+b = 0'" << endl;
 cout << "please enter the value of 'a'" << endl;
 cin >> a;
 cout << "now, please enter the value of 'b'" << endl;
 cin >> b;

 if ( a != 0.0)
   cout << "the value of 'x' is " << (-b/a) << endl;
 else
   cout << "sorry, unable to find out the value of 'x'" << endl;

}





[update]

如果您需要从用户接受单个字符串,例如



[update]
If you need to accept from the user a single string like, for instance

23.75 x + 10.18 = 0



您必须构建(简单)解析器才能正式

  • 验证等式
  • 提取值 a = 23.75 b = 10.18

  • 这篇关于如何从等式中获得系数作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 01:48