|
Sure, but as Maxx points out it's necessary complexity - just by the smell of it I don't think you can avoid the iterative approach. The program needs to know what "*" means and how to do it. It also needs to know what "+" means and how to do it, etc. ad infinitum.
Besides, the operator class I gave you was what, 6 lines long? It won't take you long at all!
Having said that, you could do something fancy and add attributes to the Operator classes that specify what token identifies that operation. You could then use reflection to, once again, loop over all the Operator classes in the Assembly and find the one with the matching token.
This way you avoid a big set of if/else or switch/case statements, but it's going to be slower. Admittedly more fun, IMO, but definitely slower. In fact, as a practical solution I think that's crazy (but interesting).
You could reduce the number of classes by changing IOperator into a single delegate:
delegate double OperateDelegate(double left, double right);
Then you could have a Dictionary/Hashtable mapping tokens ("*", "+", "sin" etc.) to delegates, which you could declare nice and compactly, like so:
var operatorLookup = new Dictionary<string, OperateDelegate>();
operatorLookup.Add("*", ((left, right) => left * right));
operatorLoojup.Add("+", ((left, right) => left + right));
var operatorLookup = new Dictionary<string, OperateDelegate>
{
{ "*", ((left, right) => left * right) },
{ "+", ((left, right) => left + right) }
};
It's a trade-off of file-size vs. readability, which it is up to you as the developer to decide which one is important.
|
|
|
|
|
I think you are using anonymous method in C sharp, but unfortunately I'm using JAVA. I'm asking questions here because, in JAVA section nobody replies . Anyways, I appreciate your help. Thank you.
|
|
|
|
|
Haha! Just goes to show you how similar the two languages are! Good luck.
|
|
|
|
|
Hi,
Also, I've to consider priority of operators , like '*' is greater than '+' and '+' is equal to '-' and so on. For this I've made an abstract class Operator with field int priority in it. Here is the link[^] for my class diagram so far. Do you think this approach has be further enhanced?
SK
|
|
|
|
|
GlobX's solution moves the problem (i.e. searching 30 operators each time) out of the way, but I don't think there's any way of actually avoiding it.
However, you could use a Dictionary<string, ioperator=""> which you populate with all of the valid operators and their associated tokens, then use TryGetValue to return the IOperator instance to use.
Of course, behind the scenes, the Dictionary still has to look through all the keys...
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
Replace the Dictionary with a Hashtable?
|
|
|
|
|
Yes. Hash table look up is O(1) for less keys and I've only 30 operators. This is good idea.
Thanks
|
|
|
|
|
Indeed, making iOperator and then searching operator with hash table can bring worst case to O(1) complexity.
Thanks
|
|
|
|
|
Shivamkalra wrote: reading articles on code project
Including mine[^]?
|
|
|
|
|
You can use CodeDom to create source code at runtime, compile and run it. If your numerical expressions can be evaluated in a programming language then I guess this might be the easiest way. This article[^] might give you some clues.
|
|
|
|
|
This will cheating. LOL. But nice article.
|
|
|
|
|
I've used a much simpler approach to do this. The basic idea is you build a binary tree that represents your expression, where you have an interior node for each operator and its left and right sons are the operands.
1. Replace each token in your expression with a node that has left/right pointers, so it can be in a binary tree. Each number, operator, and parenthesis is a separate node. Initially the nodes are in a linear list, in the same order they appear in the expression.
2. We process the list, moving operand nodes to the left/right sons of operator nodes (or removing nodes) at each step, until we're left with a single node in the list: The root of the binary tree.
3. There are three operations:
a) If you have a single node enclosed by parentheses, eliminate the parentheses: (N) => N
b) Find the region most deeply nested in parentheses. Go through the nodes, moving the operands around the highest-precedence operators to the left/right sons: A + B * C + D => A + [* node with sons B and C] + D (Hard to draw a tree here...)
c) Go through this region again, this time doing the same operation to the next-lower-precedence operators:
[+ node]
/ \
[+ node] D
/ \
A [* node]
/ \
B C
When done, only the root [+ node] will remain in the list. To evaluate, call the Eval() method on the root. Eval returns the value of the node's operand, or for an operator, it returns the operator applied to the Eval() of its sons.
It works, and it's way simpler than the standard approach.
|
|
|
|
|
Why don't you use a switch instead of an if/else chain?
|
|
|
|
|
Hi,
I'm writing an application where I need administrator authority to create the database. I do not want to run as an administrator the entire time, just to create the file. How do I switch into Administrator mode and switch back? I know the User will get a prompt when trying to do this as I have seen other applications requesting administrator authority.
Thank you,
Glenn
|
|
|
|
|
Unless something has changed that I don't know about, you cannot switch back and forth. It's the entire process runs with admin priv's or nothing.
|
|
|
|
|
As Dave said - AFAIK you can't. You can create separate process to do the DB stuff and start that with elevated privileges though.
Google "start new process with elevated privileges" and you will find many results.
|
|
|
|
|
I prefer to include an empty copy of the database with the app.
|
|
|
|
|
how can i get usage cpu performance from wmi based on c# ?
many solution just write about sql script or the other.
|
|
|
|
|
when i use PerformanceCounter,
it can get a information cpu performance. but it just cpu own.
i.e, case of invoke progressbar, it can display cpu usage rate.
it just one thread. but i wanna get a cpu,network and memory usage rate.
so i make them in one thread. but always cpu usage rate 0% in my project.
i don't understand it.
following my code.
------------------------
PerformanceCounter p = new PerformanceCounter();
p.CategoryName = "Processor";
p.CounterName = "% Processor Time";
p.InstanceName = "_Total";
... progressbar invoke ...
float usage = p.NextValue();
progressBar1.Value = ((int)usage);
label1.Text = "CPU usage : " + ((int)usage).ToString() + "%";
.....
it can display useage rate...
i write this code in my project, but case on my project. it can't display performance.
|
|
|
|
|
Hello,
Have you tried this:
ManagementObject processor = new ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
processor.Get();
ulong value = (ulong)processor.Properties["PercentProcessorTime"].Value;
Valery.
|
|
|
|
|
I'm going to be writing a distributed app soon... a GUI will submit jobs to a server and the server will hand out jobs to up to 300 worker PCs.
Is there anything built in to .NET or some API available from Microsoft?
Seems like a lot of detail stuff (the 3 pieces communicating, updating status, etc) would have already been written?
I know there is Windows Azure / Cloud computing, but the boss doesn't want to use somebody elses cloud. Does Windows Azure let you build your own cloud? etc.
Basically looking for something like that.
Did a google search on some keywords, but found almost nothing.
|
|
|
|
|
Seems like a pretty standard operation except the worker PCs should request a job rather than have a job pushed to them.
Server creates a queue of jobs to be done, each pc grabs a job as it becomes free, fairly straght forward.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hmm... whats the advantage of pulling vs pushing? seems like just moving status management from the server piece to the client itself. How should worker status / heart beat be handled?
Anyways, I didn't think it was exceptionally difficult, I just thought to get it ROBUST and STABLE would take a lot of detail work and I'd rather just re-use something if possible .
|
|
|
|
|
Is there any reason the server needs to know the status of the worker beyond the pass/fail result of the process.
And yes I'll be interested to see if a framework comes out of the question.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
We need to know if a client is hung or crashed or had a hardware failure, etc. We aren't going to RDP or ping 300 machines .
|
|
|
|