I'm a beginner in C++ and am trying to find if there is a function that allows to "scan" an input in order to detect the type of equation entered.
Basically, what I'm trying to do is enter something like (a-b)^2 and the program would expand it to a^2-2*a*b+b^2 or enter ax^2+by+c and the program would give me a(x-α)+β.
What I have previously done is simply ask for what type of equation is going to be entered and then ask specifically what is each factor (like this) :
int a, a2;
int b, b2;
int inbetween;
cout << "Equation of type (ax+by)^2.\nPlease enter the factor of X : ";
cin >> a;
cout << "Please enter the factor of Y : ";
cin >> b;
a2 = a*a; //pow(a,2)
b2 = b*b; //pow(b,2)
inbetween = 2*a*b;
cout << "(" << a << "x + " << b << "y" << ")^2 = " << a2 << "x + " << inbetween << "xy + " << b2 << "y\n";
// (ax + by)^2 = a^2x + 2*a*b*xy + b^2y
But this is exactly what I do not want to do.
I'd like to know if there is a way to expand the input for any type of equation that the coded program supports.
I hope I am being clear enough, english isn't my main language I see that what I'm trying to communicate isn't really clear.
Your question is clear enough (it's actually a very interesting question IMO).
Yes, this is possible, but as far as I know there is no easy way of doing this ; you would have to program this yourself. I think your program is fine the way it is.
There is a whole area of mathematics called language theory which studies the sort of problem you just asked. To give you an idea of the solution of this sort of problem, the best way of solving this is writing your own grammar. You would have to elaborate the grammar rules formally before writing the parser, and implement the rules using regular expressions (then you would have two problems ;D ).
From the looks of your problem, this would require so much programming that, say, 95% of your code would be to decide what sort of expression it is and extract the values, and the remaining 5% would be just to expand or factorize. An excessive effort for a simple program, if you ask me.
I think Anthony D. has summed up the answer nicely, basically you're looking for a lexical analyzer and parser. If you are interested in learning more, it's worth directing you (and others that might find your question) towards the Yacc documentation. Although much of the documentation is Yacc specific, it is well written and detailed enough that it will help you reason through the different challenges and complexities of this problem. Plus, there are plenty of Yacc calculator examples that you can use as a reference if you eventually want to build a calculator.
Related
Good day,
I am currently in the learning process of CGAL while being relatively new to C++. For my current project I need to use Minkowski sums and then do additional operations on the boundary of it.
However, before I do these additional operations I need to get a better understanding of the output of offset_polygon_2(), the exact Minkowski offset computation.
Question 1: What is the Syntax of the output for .outer_boundary?
From what I understand so far, it outputs a list of a conic circles defined here. I would also imagine you would need some kind of arc-angle range for each of these concic circles and origin point, correct? An example of the output goes something like this:
89 {-1*x^2 + -1*y^2 + 0*xy + 1400*x + 0*y + -489975} : (705,0) --ccw--> (700,5) {0*x^2 + 0*y^2 + 0*xy + 0*x + 0*y + 0} : (700,5) --l--> (699.97,5)...
Question 2: How do you use CGAL::draw() for the above?
I have the following code, but I am unsure of what else needs to be done before it can be drawn.
Offset_polygon_with_holes_2 offset = CGAL::offset_polygon_2(P, 5, traits);
double secs = timer.time();
std::cout << "The offset polygon has " << offset.outer_boundary().size()
<< " vertices, " << offset.number_of_holes() << " holes."
<< std::endl;
std::cout << "Offset computation took " << secs << " seconds." << std::endl;
Question 3: What other operations can be done on the "offset"?
So in the example code for Minkowski sums (also see above) offset.outer_boundary() is done, is there a list of other operations that can be done? Note: I do not think "operations" is the correct term here, please correct me.
I think that is all I have for now, thanks!
OK, to start off I am a complete beginner in a computer science class. I could ask my teacher, but I don't have time to do so. So, expect some really dumb errors that I can't see and I am using eclipse.
here's my code:
#include <iostream>
using namespace std;
int something()
{
int big = 1000;//largest number is 1000
int small = 1;//smallest number is 1
//so, best guess is to go in the middle
int c;//my guesses
int inequality;//used to write if statements
for (int a = 0; a <= 10; a++)
{
cout << "Think about a number between 1-1000" << endl;//what console tells you
c = (big - small) / 2;//my guess will be the midpoint of the two numbers
while (big > small)//while the highest number is ALWAYS greater than the lowest number
{
cout << "Is your number less than, equal to, or greater than my guess? 1-less,2-equal,3-greater" << c << endl;
cin >> inequality;//you tell me whether my guess is too high, low, or equal
if (inequality == 1)//if you say it is too low...
{
small = c;//the smallest number is now my last guess
c = c - (big - small) / 2;//so, I'll take the midpoint of the CURRENT biggest and smallest number
}
else if (inequality == 2)//if you say it is equal...
{
cout << "Yay, I guessed your number." << endl;//cool.
}
else if (inequality == 3)//if you say it is too high...
{
big = c;//biggest number is now my guess
c = c + (big - small) / 2;//so, I'll take the midpoint of the CURRENT biggest and smallest number
}
}
}
system("pause");
return 0;//returns something in int main function
}
int main()
{
something();//so I can actually do code.
}
So my problem:
If I enter 1 after the console enters the first guess, I get 499, which is fine. After the second guess (where I enter 1), I get 249, which is also fine. However, the third guess after I enter 1 gets a random 681, so could someone help me?
It would be most appreciated if you did not rewrite the entire code for me, otherwise that is really suspicious when I turn it in. I am struggling because I do not have very good computer science background, so to improve, I need ideas mostly. Lastly, any way to make my code look a LITTLE more professional would be appreciated :)
Also, my for loop may be a bit off, I'm not sure.
When you calculate next number you need to change range
So you have first
small guess big
+---------+----------+
if user says too small, then the answer is above the guess, so in the range
big - guess and that is what you need to cut in half so instead of
c = c - (big - small)/2
guess = (big - guess) / 2 + guess
if user says too big then the answer is between guess and small
guess = (guess - small) / 2 + small
Try removing the c+ and c - terms from your midpoint calculations.
Edit: Also, try swapping the small = c and big = c statements in the two conditionals.
Your comments are mostly incorrect and that was my source of confusion.
I'm quite new to c++ and I was not sure how I could output the 5th integer from an 8 integer input? Here is what I have so far:
using namespace std;
void main()
{
int SID, i, x ;
cout << "Pleas enter 8 digits :";
cin >> SID;
cout << "The 5th digit is : " << x << endl;
for (SID = i; i < 10; i++);
if (x % 2 = 0) { cout << "It is even number."; }
else { cout << "It is odd number."; }
As you can see I want to use x as the 5th integer but I am not sure how to code that. I know that using modulus division will determine if the 5th integer is even/odd. I know that using a loop here is mandatory and decided to use a for loop but I feel I did not input it correctly.
Just quickly, becuase I am getting the feeling you are very new to this... I think you need to rethink your question and answer...
Few things to try and think about:
Do you want the user to enter a number (int) with 8 digits? If so then think of what operations you need to perform on 12345678 to get the 5th number.
Do you want the user to enter 8 numbers? If so you will need to store each one of them... If you know where you store them, you know where to go looking for the 5th one. No?
Using a for loop could be useful, but what for? I am not sure if you know what are you trying to use it for? Why did you chose i<10?
Maybe make sure you understand what loops do and how to use them.
Overall you should probably break the problem down and ask specific questions. I don't think this is the place for us to solve your entire problem. Also, I have a feeling that once you break your question down to more specific things the answers would already be on StackOverflow somewhere.
Also as owacoder mentioned it might be a good idea to place the cin >> SID inside a loop.
I am starting to write a command line converter and the only concern I have is user input (rest wont be hard). The program will have few commands like (convert 2 m to km), so when the user enters that, the program will output the converted value. My question is, what is the best way to parse user input and determine the command that user entered?. Should I divide user input into array of words and then pass to a function, so it can do something or there is another way?
I have written a few types of "simple parsers" (and several more advanced ones). From what you describe, if the commands are "convert 2 m to km", then you would simply need to split things on spaces.
Of course, if you allow "convert2mtokm" and "convert 2m to km" it gets a bit more difficult to deal with. Sticking to a "strict rule of there has to be a(t least one) space between words" makes life a lot easier.
At that point, you will have a vector<string> cmd that can be dealt with. So for example:
if (cmd[0] == "convert")
{
convert(cmd);
}
...
void convert(vector<string> cmd)
{
double dist = stod(cmd[1]);
string unit_from = cmd[2];
string unit_to = cmd[4];
if(cmd[3] != "to")
{
... print some error ...
}
double factor = unit_conversion(unit_from, unit_to);
cout << "that becomes " << dist * factor << " in " << unit_to << endl;
}
If you only have a few commands, it will be best to just strtok(input, ' '), which just splits up a string into an array of words in the command (assuming your command words are all separated by spaces). Then you can do some simple if/switch checks to see which command the user entered. For a larger number of commands (where some may be similar), you will probably need to implement or at least write out a DFA (deterministic finite automata).
An array of structures will be fine. The structure may be like this:
struct cmd
{
char **usrcmd;
void (*fc)();
};
Then you just have to iterate the array and compare the user input and the usrcmd[0] field (I assume the command is the first word).
However this solution is not the best way to go if you have a lot of user commands to handle.
I am stuck trying to figure out a way using C++ to solve a situation where the user inputs a value using cin and then have the computer solve for a way to get the value of cin, given that the format is given. a super fast example is written below.. yes i know there is a lot of code missing... but the concept is there..
int x;
int y;
int w;
int x = 30 < w < 50;
int y = 60 < w < 90;
cin >> input;
x + y = input;
cout << x;
cout << y;
Naturally though x + y cant be on the lvalue on the right. so i cant just write x + y = input.. so how would i have it solve x + y = input? Additionally I want x and y to be between the numbers listed, which limits the numbers between those inputs.. however in my actual coding i did this with a function.
has school even started yet? no its not homework. im teaching myself C++.. – Sean Holt 1 min ago edit
No im just trying to figure a way of having the computer solve for x/y of an input value if x and y are between specified values in a function
It looks like you think that C++ is going to solve equations for you. It won't. C++ is an imperative style language that is based around the concept of you telling it exactly what to do.
You will have to figure out how to solve for x and y so that you can make an algorithm. This algorithm is then what you make your program from.
There exists other languages in which you can in a sense describe what you want and have the compiler or runtime figure out how to get it for you. C++ is not one of them.
Different ways to solve your particular problem would be to set up an equation system and solving that. Or do brute force approach and iterate through the values of x and y in order to find out which values match.
It looks like you have a 'mathematical' problem here: a couple of values constrained by equations, and you want 'the computer' to find all possible values that fit into the constraints (equations). Am I right?
While some computer programs can certainly do that, the C++ language is not designed for this purpose. The role of the C++ is to give you a way of giving instructions to the processor, like "store this value in memory" or "add these two numbers". But there is no way of saying "solve this mathematical problem".
What you need is some equation solver. But I am not familiar with any. There are tools like Matlab or Mathematica. But I do not think they are free.
If you want to solve the math problem algorithmically, here is a brute force idea in pseudocode:
Input a number.
for each value x between 30 and 50
for each value y between 60 and 90
if x+y equals the number
print x and y
Now you can take a good book or tutorial and code in C++. Look for the for and if keywords (algorithmic concepts of iteration and selection) in your teaching material. Have fun!
This case can be solved trivially by interval arithmetic. C++ code that solves your "sum of two interval-constrained variables problem" is given below.
int min_x = 30, max_x = 50;
int min_y = 60, max_y = 90;
// solutions exist in this interval
int min_z = min_x + min_y, max_z = max_x + max_y;
cin >> input;
// solutions possible?
if (input >= min_z && input <= max_z)
{
// determine solution interval for x (y is dependent)
cout
<< "Solution:\n"
<< "x in [" << min(max( input - max_y , min_x),max_x)
<< ";" << min(max( input - min_y , min_x),max_x) << "], "
<< "y = " << input << " - x" << endl;
}
else
{
cout << "No solution." << endl;
}
Computers are "basically stupid" and if they do smart things it is the software.
Using a general purpose programming language like C++ requires you (or at least
the libraries you eventually use) to be very specific on how exactly to solve
a problem based on the simple arithmetic means of the bare computer.
Although the programming language won't magically and somehow do things
for you, algorithms exist to solve many mathematical standard problems such
as e.g. systems of equations. Numerical Recipes in C++ covers a variety of
algorithms and their C++ implementations.