This question already has answers here:
Evaluating arithmetic expressions from string in C++ [duplicate]
(7 answers)
Closed 7 years ago.
i wonder if is possible, for example read a file with some content like:
a+b*c
and that my programm "create" a function to do this operation, and if i modify the file ( like a+b*c+2 ) the programm read this changes and updates what this function do. Well i dont have a solid backbround in the basis of C++ and i don't know if what i'm asking is just plain stupid. I need ( or something like this could be nice ) for my work in physics simulations, where the model is mainly definied by a equilibrium function ( and some other parameters ) so what i think is that could be good if i can make a programm to test this models without having to writte a special code for each one...
Thanks!
C++ is not interpreted code. So you can only compile hard coded expressions in source code. However you can evaluate an expression on your own. You can look at some solutions here
for sure it is "possible", actually that's what matlab, mapple or any other formal calculation software/lib do.
BTW, writing one may be quite simple if you just handle */+- basic operators, and may become more and more complex depending on what you want to use (sin cos, exp, log etc.)
Basic implementations reads of the input and build an internal tree with final values on leaves would look like soething like this in your case:
+
a *
b c
I'm sure you can find a lot of docs on it.
Related
I'm a daily user of R but very new to c++. I'm using Rstudio to explore a very large and complicated C++ code by using R and Shell tools like ctags, grep and the system function. To better understand the code, i would like to understand the call hierarchy by building a call tree of that code. Is there a simple way to do that in R, e.g. with Rcpp?
Ultimately, i would like to be able to follow a parameter by coloring/ extracting the parts of the code that contains it (and still seeing the call hierarchy between the routines the parameter is mentioned in)
FYI:
So far i have tried to use Vim cctree to get the call tree from the main(), and transferred the list result to R and then to a graph with igraph, but this method has a lot of intermediate steps that make it hard to repeat.
Also, from R, i'm using ctags to find all the routines and i'm thinking i could use grep to find the various occurences of the routine, detect where it is defined and where it is called, but i don't know exactly how to do that.
Sorry if the question is quite vague. I guess, in short, i'm hoping that there is already a way to explore C++ code in R.
Thanks!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am a beginner Clojure programmer.
In a Book, it is an advantage of the Clojure that the code that is not evaluated is data.
But I do not understand.
So, I want an example code and an explanation so I can understand.
I am Korean. For that reason, I would appreciate it if you could understand even if I wrote an awkward sentence or failed to keep my manners.
I think the question you are asking is: why is it an advantage for a programming language that its source code is available within the language as data (and in particular as structured data, not just as strings)?
The reason it is an advantage is simple: once you have access to a data structure representing the source of a program you can manipulate that structure: you can write programs that manipulate other programs.
A particularly good case is where the surface form of the language and the data structure which represents it is rather 'low-commitment': it doesn't encode much meaning of the program, just its syntactic form.
Then you can write programs in this low-commitment language which include constructs which don't yet exist in the language. And you write other programs which take these programs and turn them into other programs which only use constructs which already exist in the language.
And you can keep doing this. So you can start with a language which is whatever language you are given, and incrementally build it into a language which is the language you want.
Of course you can do this with almost any language: in almost any language that lets you read files into strings, parse those strings into some representation of a language, with extensions, and then process that representation into the original language which you then hand off to the compiler or interpreter.
But Lisp-family languages make this much easier for you:
they do the parsing-into-a-data-structure for you, so 'source code is data';
they have an intentionally low-commitment source form, leaving it up to you what given constructs mean;
they provide facilities to let you do this processing-of-source-code-into-other-source-code in a fairly painless way, by defining macros.
That's the advantage of having source code available as structured data in the language: it's a crucial part of the things you need to make implementing your own programming language (which is often really a programming jargon – a language which inherits all of a base language but adds something of its own on top of it) easy.
I think it is in "Mastering Clojure Macros" that I saw an example of "code is data" that made a lot of sense (at least to me).
Syntactically speaking this is valid Clojure code: it's a simple list of numbers and symbol.
(1 + 1)
But it is not a valid program! If you evaluate this in your REPL it will throw an error because 1 isn't a function.
When Clojure reads this text, it produces a list (the same list) and allows things such as a macro to receive it (unevaluated), transform it and send it back. Perhaps that macro could simply swap the first two elements and return (+ 1 1)?
This would not have been possible in JavaScript for example:
var a = + 1 1; // Syntax error
The engine would have exploded way before you could try anything!
This question already has answers here:
What is wrong with using goto? [duplicate]
(6 answers)
Closed 5 years ago.
I read in the book I am learning C++ from about loops and it taught be goto first, but near the end it said not to use it, as it was bad practice, and said it created 'spaghetti' code. I don't understand what that means and why it is bad. Could someone please explain?
Spaghetti is a code poorly structured and which makes it hard to update because of multiple undocumented and unexpected links or dependencies. You touch it in one place and you see other things get broken or modified in an unexpected way. Just like sticky spaghetti, you pull one end and you see a number of places start moving.
GOTO usually violates the principles of structured, procedural programming, hence the term, which suggests the tangled and arbitrary nature of the program flow.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there any kind of “expression class” (C++)
I want to make a class that holds a function that is defined at run time.
i.e.
function function1("x*sin(3.141*x)");
I want to do things like find roots and it would be better not having to rewrite the program each time.
edit: I am looking at lua.
This thing is certainly much easier in dynamic languages. For example, in Matlab you can evaluate strings using the eval command.
However, this is not impossible in C++. You might be able to make a nice solution with a combination of C++11 lambdas and a custom interpreter. The member function can take a lambda as an argument, and you can formulate the lambda by interpreting a string.
This answer might give you a good starting point for the interpreting part of the challenge.
Evaluate Mathematical Function from String
So i need to use two arrays in my program.
A is an array of datapoints (values x takes). and B is an array of functions of x.
Both are to be entered by the user(from the keyboard).
My problem is in reading in the elements of B. For example, A={1,2,3} and B={sin(x), x+5} where x is supposed to take in all values of A.
I am relatively new to c++ and am not sure of the best way to do this. I read somewhere about Parsing but it seemed a bit complicated.
Can it be done without parsing?
No, this is complicated. C++ is a statically-typed and non-reflective language: you cannot create actual C++ code at runtime.
The next-best thing to do is to write a small parser that matches the input against a list of recognized functions that you choose to provide. It's not that hard to do, but you'll need to write some code.
Boost contains some libraries to help you with this (Spirit), I believe, but it's also not terribly difficult to do by hand. You just need to lex the input into tokens and build up a parse tree.
Your best bet is to use a function parser library, such as FunctionParser. Other options include using a scripting language, such as Lua or Squirrel, and using a separate command line calculator application.
Parsing is a large and exciting field in its own, but writing your own parser would likely take longer than it is worth, unless you are doing this project specifically to become familiar with the subject.