111,089
社区成员




using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CSharp;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> ExpList = new List<string>()
{
"23 + 56 * 2",
"Math.Pow(2, 2)",
"Math.Pow(2, 0.5)",
"(3 + 5) * 2 - 7",
"1.0 / 3.0 + 7",
"((2 + 5) * 2 + (4 * 50)) * 30 - 1",
"-10 * 10 - 9",
"2 / 10.0 - 4",
"3.14 * 3.14 * 2",
"2 << 4"
};
ExpList.ForEach(i => Console.WriteLine(i + " = " + ExpCalc.Calc(i)));
string expwithvar = "(@a + @b) * 10";
double rexpwithvar = ExpCalc.Calc(expwithvar, new Tuple<string, string>("a", "10"), new Tuple<string, string>("b", "5"));
Console.WriteLine(rexpwithvar);
/*
output:
23 + 56 * 2 = 135
Math.Pow(2, 2) = 4
Math.Pow(2, 0.5) = 1.4142135623731
(3 + 5) * 2 - 7 = 9
1.0 / 3.0 + 7 = 7.33333333333333
((2 + 5) * 2 + (4 * 50)) * 30 - 1 = 6419
-10 * 10 - 9 = -109
2 / 10.0 - 4 = -3.8
3.14 * 3.14 * 2 = 19.7192
2 << 4 = 32
150
*/
}
}
class ExpCalc
{
private static Random rnd = new Random();
private static string RandomString(string CharList = "abcdefghijklmnopqrstuvwxyz", int length = 1)
{
string rndstr = "";
for (int i = 1; i <= length; i++)
{
rndstr += CharList.Substring(rnd.Next(0, CharList.Length), 1);
}
return rndstr;
}
private static string RenderText(string Template, Dictionary<string, string> Params)
{
string result = Template;
foreach (var item in Params)
{
result = result.Replace("@" + item.Key, item.Value);
}
return result;
}
public static double Calc(string exp)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters cps = new CompilerParameters();
cps.GenerateExecutable = false;
cps.GenerateInMemory = true;
string classSource = "using System;\n" +
"class @classname\n" +
"{\n" +
"\tpublic double Eval { get { return @exp; } } \n" +
"}";
Dictionary<string, string> renderparams = new Dictionary<string, string>();
string classname = RandomString(length: 10);
renderparams.Add("classname", classname);
renderparams.Add("exp", exp);
classSource = RenderText(classSource, renderparams);
CompilerResults result = provider.CompileAssemblyFromSource(cps, classSource);
Assembly assembly;
try
{
assembly = result.CompiledAssembly;
}
catch
{
throw new Exception("Invaild expression: " + exp);
}
object calcobj = assembly.CreateInstance(classname);
PropertyInfo pi = calcobj.GetType().GetProperty("Eval");
double returnvar = 0.0f;
returnvar = Convert.ToDouble(pi.GetValue(calcobj, null));
return returnvar;
}
public static double Calc(string exp, params Tuple<string, string>[] varlist)
{
double result = 0.00;
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (var i in varlist.AsEnumerable())
{
dict.Add(i.Item1, i.Item2);
}
exp = RenderText(exp, dict);
result = Calc(exp);
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;
namespace Caculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Caculate ca = new Caculate();
ca.RegType(textBox1.Text);
ca.Compile();
IA a = ca.Create_A();
textBox2.Text = a.f().ToString() ;
ca.UnloadAdm();
}
}
public interface IA
{
int f();
}
public class Caculate:MarshalByRefObject
{
AppDomain adm;
public void RegType(string str) // 动态注册一个类型
{
StreamWriter sw = new StreamWriter("a.cs");
sw.WriteLine("public class A :System.MarshalByRefObject, Caculator.IA { public int f() { try { return " + str + "; } catch { return int.MinValue; } } } ");
sw.Close();
}
// C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
// C:\Windows\System32\cmd.exe
//在C盘根目录下 out.txt查看是否编译成功
public void Compile()//编译 下面的字符串写你的CMD 和 编译器路径
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"C:\Windows\System32\cmd.exe";
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
info.Arguments = @"/c C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:library " + path + @"\a.cs /r:"+ Assembly.GetExecutingAssembly().Location+@" >E:\out.txt";
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(info);
p.WaitForExit();
}
public IA Create_A()//创建A的对象
{
adm = AppDomain.CreateDomain("abc", null, new AppDomainSetup());
return adm.CreateInstanceFromAndUnwrap("a.dll", "A") as IA;
//string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
//Assembly asm = Assembly.LoadFrom(path + @"\a.dll");
//IA a = asm.CreateInstance("A") as IA;
//return a;
}
public void UnloadAdm()
{
AppDomain.Unload(adm);
}
}
}
/*---------------------------------------
函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成
功能:
目前提供了10多个常用数学函数:
⑴正弦sin
⑵余弦cos
⑶正切tan
⑷开平方sqrt
⑸反正弦arcsin
⑹反余弦arccos
⑺反正切arctan
⑻常用对数lg
⑼自然对数ln
⑽e指数exp
⑾乘幂函数∧
用法:
如果要求2的32次幂,可以打入2^32<回车>
如果要求30度角的正切可键入tan(Pi/6)<回车>
注意不能打入:tan(30)<Enter>
如果要求1.23弧度的正弦,有几种方法都有效:
sin(1.23)<Enter>
sin 1.23 <Enter>
sin1.23 <Enter>
如果验证正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2 <Enter>或sin1.23^2+cos1.23^2 <Enter>
此外两函数表达式连在一起,自动理解为相乘如:sin1.23cos0.77+cos1.23sin0.77就等价于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)
当然你还可以依据三角变换,再用sin(1.23+0.77)也即sin2验证一下。
本计算器充分考虑了运算符的优先级因此诸如:2+3*4^2 实际上相当于:2+(3*(4*4))
另外函数名前面如果是数字,那么自动认为二者相乘.
同理,如果某数的右侧是左括号,则自动认为该数与括弧项之间隐含一乘号。
如:3sin1.2^2+5cos2.1^2 相当于3*sin2(1.2)+5*cos2(2.1)
又如:4(3-2(sqrt5-1)+ln2)+lg5 相当于4*(3-2*(√5 -1)+loge(2))+log10(5)
此外,本计算器提供了圆周率 Pi键入字母时不区分大小写,以方便使用。
----------------------------------------*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;
const char Tab=0x9;
const int DIGIT=1;
const int MAXLEN=16384;
char s[MAXLEN],*endss;
int pcs=15;
double fun(double x,char op[],int *iop) {
while (op[*iop-1]<32) //本行使得函数嵌套调用时不必加括号,如 arc sin(sin(1.234)) 只需键入arc sin sin 1.234<Enter>
switch (op[*iop-1]) {
case 7: x=sin(x); (*iop)--;break;
case 8: x=cos(x); (*iop)--;break;
case 9: x=tan(x); (*iop)--;break;
case 10: x=sqrt(x); (*iop)--;break;
case 11: x=asin(x); (*iop)--;break;
case 12: x=acos(x); (*iop)--;break;
case 13: x=atan(x); (*iop)--;break;
case 14: x=log10(x);(*iop)--;break;
case 15: x=log(x); (*iop)--;break;
case 16: x=exp(x); (*iop)--;break;
}
return x;
}
double calc(char *expr,char **addr) {
static deep; //递归深度
static char *fname[]={ "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp",NULL};
double ST[10]={0.0}; //数字栈
char op[10]={'+'}; //运算符栈
char c,*rexp,*pp,*pf;
int ist=1,iop=1,last;
if (!deep) {
pp=pf=expr;
do {
c = *pp++;
if (c!=' '&& c!=Tab)
*pf++ = c;
} while (c!='\0');
}
pp=expr;
if ((c=*pp)=='-'||c=='+') {
op[0] = c;
pp++;
}
last = !DIGIT;
while ((c=*pp)!='\0') {
if (c=='(') {//左圆括弧
deep++;
ST[ist++]=calc(++pp,addr);
deep--;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp = *addr;
last = DIGIT;
if (*pp == '('||isalpha(*pp) && strnicmp(pp,"Pi",2)) {//目的是:当右圆括弧的右恻为左圆括弧或函数名字时,默认其为乘法
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
else if (c==')') {//右圆括弧
pp++;
break;
} else if (isalpha(c)) {
if (!strnicmp(pp,"Pi",2)) {
if (last==DIGIT) {
cout<< "π左侧遇)" <<endl;exit(1);
}
ST[ist++]=3.14159265358979323846264338328;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp += 2;
last = DIGIT;
if (!strnicmp(pp,"Pi",2)) {
cout<< "两个π相连" <<endl;exit(2);
}
if (*pp=='(') {
cout<< "π右侧遇(" <<endl;exit(3);
}
} else {
for(int i=0; (pf=fname[i])!=NULL; i++)
if (!strnicmp(pp,pf,strlen(pf)))break;
if (pf!=NULL) {
op[iop++] = 07+i;
pp += strlen(pf);
} else {
cout<< "陌生函数名" <<endl;exit(4);
}
}
} else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='^') {
char cc;
if (last != DIGIT) {
cout<< "运算符粘连" <<endl;exit(5);
}
pp++;
if (c=='+'||c=='-') {
do {
cc = op[--iop];
--ist;
switch (cc) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
} while (iop);
op[iop++] = c;
} else if (c=='*'||c=='/') {
operate: cc = op[iop-1];
if (cc=='+'||cc=='-') {
op[iop++] = c;
} else {
--ist;
op[iop-1] = c;
switch (cc) {
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
} else {
cc = op[iop-1];
if (cc=='^') {
cout<< "乘幂符连用" <<endl;exit(6);
}
op[iop++] = c;
}
last = !DIGIT;
} else {
if (last == DIGIT) {
cout<< "两数字粘连" <<endl;exit(7);
}
ST[ist++]=strtod(pp,&rexp);
ST[ist-1]=fun(ST[ist-1],op,&iop);
if (pp == rexp) {
cout<< "非法字符" <<endl;exit(8);
}
pp = rexp;
last = DIGIT;
if (*pp == '('||isalpha(*pp)) {
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
}
*addr=pp;
if (iop>=ist) {
cout<< "表达式有误" <<endl;exit(9);
}
while (iop) {
--ist;
switch (op[--iop]) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
return ST[0];
}
int main(int argc,char **argv) {
if (argc<=1) {
if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页
cout << "计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;
while (1) {
cout << "请输入表达式:";
gets(s);
if (s[0]==0) break;//
cout << s <<"=";
cout << setprecision(15) << calc(s,&endss) << endl;
}
} else {
strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;
if (argc>=3) pcs=atoi(argv[2]);
if (pcs<0||15<pcs) pcs=15;
printf("%.*lf\n",pcs,calc(s,&endss));
}
return 0;
}
public static object Eval(string AExpression)
{
try
{
return new DataTable().Compute(AExpression, "");
}
catch
{
return null;
}
}
//先在项目中添加COM引用Microsoft Script Control 1.0
using MSScriptControl;
ScriptControl vScriptControl = new ScriptControl();
vScriptControl.Language = "JavaScript";
Text = vScriptControl.Eval("'Zswang 路过' + (1 + 2)").ToString();
[DllImport("user32")]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, int unused);
[DllImport("user32")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
public static extern bool AttachThreadInput(int nThreadId, int nThreadIdTo, bool bAttach);
[DllImport("user32")]
public static extern bool BringWindowToTop(IntPtr hWnd);
public static bool SetForegroundWindow(IntPtr window, bool force)
{
IntPtr windowForeground = GetForegroundWindow();
if (window == windowForeground || SetForegroundWindow(window))
{
return true;
}
if (force == false)
{
return false;
}
if (windowForeground == IntPtr.Zero)
{
return false;
}
if (!AttachThreadInput(Thread.CurrentThread.ManagedThreadId,
GetWindowThreadProcessId(windowForeground, 0), true))
{
return false;
}
SetForegroundWindow(window);
BringWindowToTop(window);
AttachThreadInput(Thread.CurrentThread.ManagedThreadId,
GetWindowThreadProcessId(windowForeground, 0), false);
return GetForegroundWindow() == window;
}
private void button1_Click(object sender, EventArgs e)
{
Process process = Process.Start("calc.exe");
while (process.MainWindowHandle == IntPtr.Zero) process.Refresh();
SetForegroundWindow(process.MainWindowHandle, true);
SendKeys.Send("20{+}{(}{(}3-6{)}/3{)}=");
}
#include <cctype>
#include <cassert>
#include <memory.h>
#include <iostream>
using namespace std;
template< class T >
class Tree
{
public:
class Node
{
public:
Node(const T& elem, Node* lc = NULL, Node* rc = NULL)
: m_elem( elem ), m_left( lc ), m_right( rc )
{
}
~Node()
{
if( m_left )
{
delete m_left;
m_left = NULL;
}
if( m_right )
{
delete m_right;
m_right = NULL;
}
}
Node& left(Node* lc) { m_left = lc; return *this; }
Node* left() const { return m_left; }
Node& right(Node* rc) { m_right = rc; return *this; }
Node* right() const { return m_right; }
Node& data(Node* elem) { m_elem = elem; return *this; }
T data() const { return m_elem; }
bool isLeaf() const { return !m_left && !m_right; }
private:
T m_elem;
Node* m_left;
Node* m_right;
};
Tree(typename Tree::Node* root = 0 )
: m_root( root )
{
}
~Tree()
{
if( m_root )
{
delete m_root;
m_root = NULL;
}
}
Node* root() const { return m_root; }
private:
Node* m_root;
};
typedef Tree< int > exp_tree;
typedef exp_tree::Node exp_node;
//////////////////////////////////////////////////////////////////
template< class T >
void printExpTree(typename Tree< T >::Node* node, ostream& o = cout)
{
bool leaf = node->isLeaf();
if( !leaf )
cout << "(";
if( node->left() )
printExpTree< T >( node->left(), o );
if( leaf )
o << node->data();
else
o << (char) node->data();
if( node->right() )
printExpTree< T >( node->right(), o );
if( !leaf )
cout << ")";
}
template< class T >
T calcExpTree(typename Tree< T >::Node* node)
{
if( node->isLeaf() )
return node->data();
T left = 0;
if( node->left() )
left = calcExpTree< T >( node->left() );
T right = 0;
if( node->right() )
right = calcExpTree< T >( node->right() );
switch( node->data() )
{
case '+':
return left + right;
case '-':
return left - right;
case '*':
return left * right;
case '/':
return left / right;
case '%':
return left % right;
default:
return 0;
}
}
bool get_operand(const char*& curr, int& result)
{
const char* first = curr;
while( isdigit( *curr ) )
{
++curr;
}
if( first == curr )
return false;
int bits = curr - first;
char* d = new char[ bits + 1 ];
memcpy( d, first, sizeof(char) * bits );
d[ bits ] = 0;
result = atoi( d );
delete [] d;
d = NULL;
return true;
}
bool is_correct_operator(char opr)
{
const char* oprators = "+-*/%";
return strchr( oprators, opr ) != NULL;
}
enum OperatorPriority {
PRIO_NONE,
PLUS_MINUS, // + -
MUL_DIV_MOD,
BRACKET, // (
};
OperatorPriority get_priority(char opr)
{
switch( opr )
{
case '(':
return BRACKET;
case '+':
case '-':
return PLUS_MINUS;
case '*':
case '/':
case '%':
return MUL_DIV_MOD;
default:
return PRIO_NONE;
}
}
/// \param curr 在当前符号所在位置检测后一个符号
/// \return 当前符号的优先级是否小于后一个
bool get_next_operator_prio(const char* curr)
{
const char curropr = *curr;
const char* c = curr + 1;
OperatorPriority p1, p2 = PRIO_NONE;
p1 = get_priority( curropr );
while( *c )
{
if( !isdigit( *c ) )
{
p2 = get_priority( *c );
break;
}
++c;
}
return p1 < p2;
}
exp_node* parse_exp(const char*& curr, bool no_more = false)
{
exp_node *opr = NULL, *lhs = NULL, *rhs = NULL;
// 左操作数
int left_operand;
if( get_operand( curr, left_operand ) )
{
lhs = new exp_node( left_operand );
}
else // 括号括起来的
{
lhs = parse_exp( ++curr );
++curr;
if( !*curr || no_more )
return lhs;
}
if( !*curr )
return lhs;
// 操作符
char oprtor = *curr;
// 假如出现无用的括号,如 ((1+1))
if( !is_correct_operator( oprtor ) )
return lhs;
// 右操作数
if( !get_next_operator_prio( curr ) )
{
int right_operand;
get_operand( ++curr, right_operand );
rhs = new exp_node( right_operand );
}
else
{
// 右值不要继续处理剩余表达式
bool bracket = *curr == '(';
rhs = parse_exp( ++curr, true );
if( bracket )
++curr;
}
opr = new exp_node( oprtor, lhs, rhs );
// 继续处理
if( *curr )
{
if( *curr != ')' )
{
char more_oprtor = *curr;
exp_node* more_right = parse_exp( ++curr );
exp_node* more_opr =
new exp_node( more_oprtor, opr, more_right );
return more_opr;
}
}
return opr;
}
void test(const char* exp, int correct)
{
Tree< int > et( parse_exp( exp ) );
printExpTree< int >( et.root() );
cout << endl;
cout << "[" << correct << "]"
<< calcExpTree< int >( et.root() ) << endl
<< "-------------------------------\n";
}
int main()
{
test( "(((((1+1)))))", 2 );
test( "1+((2*3))+(4+5)", 16 );
test( "(30*2)/(10+2*5)*(3+5)", 24 );
test( "(30*2)/5+(20+(2*5))*(1+1)", 72 );
test( "30*(2+5)+(20+3*6-6*8)", 200 );
test( "30*(2+5)", 210 );
test( "10+4/2", 12 );
test( "5*(1+2+7)", 50 );
test( "100*3", 300 );
return 0;
}