
C#的.纯加减 一元一次方程 求解
无括号.只有加和减x变量只出现一次的方程如x+1=62+x=6-2+36+3=x+2求x的值用C#写.....
无括号.只有 加 和 减
x变量只出现一次 的方程
如
x+1=6
2+x=6-2+3
6+3=x+2
求x的值
用C#写.. 展开
x变量只出现一次 的方程
如
x+1=6
2+x=6-2+3
6+3=x+2
求x的值
用C#写.. 展开
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestFun
{
class Program
{
static void Main(string[] args)
{
bool flag = true;
while (flag)
{
try
{
Console.WriteLine("请输入方程:");
string fun = Console.ReadLine();//读入方程
string left;//带x的表达式字符串
string right;//不带x的表达式字符串
string[] lrTemp = fun.Split('=');//用等号分割方程为两部分
if (lrTemp[0].Contains('x') || lrTemp[0].Contains('X'))//将带x的一边表达式为left,不带x的一边表达式为right
{
left = lrTemp[0];
right = lrTemp[1];
}
else
{
left = lrTemp[1];
right = lrTemp[0];
}
//在两项表达式前面加上'+'号,为了处理方便
if (left.IndexOfAny(new char[] { '+', '-' }) != 0)
{
left = '+' + left;
}
if (right.IndexOfAny(new char[] { '+', '-' }) != 0)
{
right = '+' + right;
}
//将带x项拆分成,两项leftx和left2:一项为‘+x’或者‘-x’;另一项为数值加减表达式
int t=left.IndexOf('x');//x在left串中位置
string leftx = left.Substring( t- 1, 2);
string left2 = left.Replace(leftx, "");
//求出带x表达式除去x项的值
double leftValue = StringValue(left2);
//求出不带x表达式值
double rightValue = StringValue(right);
//求出+x或者-x的值
double res = rightValue - leftValue;
//根据x前面正负号求出最后结果
if (leftx.IndexOf('-') == 0)
{
res = -res;
}
Console.WriteLine("解为:x="+res);
flag = false;
}
catch
{
Console.WriteLine("您的输入有误:请重新输入!");
}
}
}
//计算数值表达式的值的函数
static double StringValue(string str)
{
string temp = "";//临时变量
double res=0;
char operate='+';
foreach (char c in str)
{
if (c != '+' && c != '-')
{
temp += c;
}
else
{
if (temp != "")
{
res += double.Parse(operate + temp);
temp = "";
}
operate = c;
}
}
res += double.Parse(operate + temp);
return res;
}
}
}
经过我测试,应该没有问题。
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestFun
{
class Program
{
static void Main(string[] args)
{
bool flag = true;
while (flag)
{
try
{
Console.WriteLine("请输入方程:");
string fun = Console.ReadLine();//读入方程
string left;//带x的表达式字符串
string right;//不带x的表达式字符串
string[] lrTemp = fun.Split('=');//用等号分割方程为两部分
if (lrTemp[0].Contains('x') || lrTemp[0].Contains('X'))//将带x的一边表达式为left,不带x的一边表达式为right
{
left = lrTemp[0];
right = lrTemp[1];
}
else
{
left = lrTemp[1];
right = lrTemp[0];
}
//在两项表达式前面加上'+'号,为了处理方便
if (left.IndexOfAny(new char[] { '+', '-' }) != 0)
{
left = '+' + left;
}
if (right.IndexOfAny(new char[] { '+', '-' }) != 0)
{
right = '+' + right;
}
//将带x项拆分成,两项leftx和left2:一项为‘+x’或者‘-x’;另一项为数值加减表达式
int t=left.IndexOf('x');//x在left串中位置
string leftx = left.Substring( t- 1, 2);
string left2 = left.Replace(leftx, "");
//求出带x表达式除去x项的值
double leftValue = StringValue(left2);
//求出不带x表达式值
double rightValue = StringValue(right);
//求出+x或者-x的值
double res = rightValue - leftValue;
//根据x前面正负号求出最后结果
if (leftx.IndexOf('-') == 0)
{
res = -res;
}
Console.WriteLine("解为:x="+res);
flag = false;
}
catch
{
Console.WriteLine("您的输入有误:请重新输入!");
}
}
}
//计算数值表达式的值的函数
static double StringValue(string str)
{
string temp = "";//临时变量
double res=0;
char operate='+';
foreach (char c in str)
{
if (c != '+' && c != '-')
{
temp += c;
}
else
{
if (temp != "")
{
res += double.Parse(operate + temp);
temp = "";
}
operate = c;
}
}
res += double.Parse(operate + temp);
return res;
}
}
}
经过我测试,应该没有问题。
追问
谢谢cockermu
x--2=6
出错.解得x=8
正解是:x=4
展开全部
//因为赋值的变量X只能在左边
//所以我们可以这样求解:
//右边的整数移到左边即可,如果是X在右边就同时换边就行了
int x;
x = 6 - 1;
MessageBox.Show(x.ToString());
x = 6 - 2 + 3 - 2;
MessageBox.Show(x.ToString());
x = 6 + 3 - 2;
MessageBox.Show(x.ToString());
希望我的答复能令你满意
//所以我们可以这样求解:
//右边的整数移到左边即可,如果是X在右边就同时换边就行了
int x;
x = 6 - 1;
MessageBox.Show(x.ToString());
x = 6 - 2 + 3 - 2;
MessageBox.Show(x.ToString());
x = 6 + 3 - 2;
MessageBox.Show(x.ToString());
希望我的答复能令你满意
追问
如何用C#自动左右换边..
这样只需要输入方程式 就可以知道答案
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String str= Console.ReadLine();
String[] strlist = str.Split('=');
double left = 0;
double right = 0;
String x = null;
foreach (String li in strlist)
{
if (li.IndexOf("x") > -1)
{
double data = 0;
int index = 1;
int befindex = 0;
char[] inof = { '+', '-' };
while (li.IndexOfAny(inof, index) > -1)
{
if (li.IndexOfAny(inof, index) > -1)
{
String datastr = li.Substring(index - 1, li.IndexOfAny(inof, index) - befindex);
if (datastr.IndexOf("x") == -1)
{
data = double.Parse(datastr);
right += data;
}
else
{
x = datastr;
}
}
befindex = li.IndexOfAny(inof, index);
index = li.IndexOfAny(inof, index) + 1;
}
if (li.Substring(index - 1).IndexOf("x") == -1)
right += double.Parse(li.Substring(index - 1));
else
x = li.Substring(index - 1);
}
else
{
double data = 0;
int index = 1;
int befindex = 0;
char[] inof={'+','-'};
while (li.IndexOfAny(inof, index) > -1)
{
if (li.IndexOfAny(inof, index)>-1)
{
data = double.Parse(li.Substring(index - 1, li.IndexOfAny(inof, index) - befindex));
left += data;
}
befindex = li.IndexOfAny(inof, index);
index = li.IndexOfAny(inof, index)+1;
}
left += double.Parse(li.Substring(index - 1));
}
}
if (x.IndexOf("-") == -1)
{
Console.WriteLine(left - right);
}
else
{
Console.WriteLine(right - left);
}
Console.ReadLine();
一种不太好的方法。
String[] strlist = str.Split('=');
double left = 0;
double right = 0;
String x = null;
foreach (String li in strlist)
{
if (li.IndexOf("x") > -1)
{
double data = 0;
int index = 1;
int befindex = 0;
char[] inof = { '+', '-' };
while (li.IndexOfAny(inof, index) > -1)
{
if (li.IndexOfAny(inof, index) > -1)
{
String datastr = li.Substring(index - 1, li.IndexOfAny(inof, index) - befindex);
if (datastr.IndexOf("x") == -1)
{
data = double.Parse(datastr);
right += data;
}
else
{
x = datastr;
}
}
befindex = li.IndexOfAny(inof, index);
index = li.IndexOfAny(inof, index) + 1;
}
if (li.Substring(index - 1).IndexOf("x") == -1)
right += double.Parse(li.Substring(index - 1));
else
x = li.Substring(index - 1);
}
else
{
double data = 0;
int index = 1;
int befindex = 0;
char[] inof={'+','-'};
while (li.IndexOfAny(inof, index) > -1)
{
if (li.IndexOfAny(inof, index)>-1)
{
data = double.Parse(li.Substring(index - 1, li.IndexOfAny(inof, index) - befindex));
left += data;
}
befindex = li.IndexOfAny(inof, index);
index = li.IndexOfAny(inof, index)+1;
}
left += double.Parse(li.Substring(index - 1));
}
}
if (x.IndexOf("-") == -1)
{
Console.WriteLine(left - right);
}
else
{
Console.WriteLine(right - left);
}
Console.ReadLine();
一种不太好的方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//因为赋值的变量X只能在左边
//所以我们可以这样求解:
//右边的整数移到左边即可,如果是X在右边就同时换边就行了
int x;
x = 6 - 1;
MessageBox.Show(x.ToString());
x = 6 - 2 + 3 - 2;
MessageBox.Show(x.ToString());
x = 6 + 3 - 2;
MessageBox.Show(x.ToString());
//所以我们可以这样求解:
//右边的整数移到左边即可,如果是X在右边就同时换边就行了
int x;
x = 6 - 1;
MessageBox.Show(x.ToString());
x = 6 - 2 + 3 - 2;
MessageBox.Show(x.ToString());
x = 6 + 3 - 2;
MessageBox.Show(x.ToString());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询