Visual C++ statement with mixture of operators priority - visual-c++

I have this problem using Visual Studio 2017:
int x = 2, y = -3;
cout << ((--x + y && y++ - x) || x-- + (--y + x++)) << endl;
cout << "x = " << x << ", y = " << y << endl << endl;
It always shows that the result of the whole statement is 1 (which is OK) and that x = 1 and y = -2.
What I don't understand is how can y by equal to -2? In my calculation it should be -3!
I referenced to this link: https://msdn.microsoft.com/en-us/library/126fe14k.aspx

The right-hand side of the || operator is not performed because the left-hand side expression && operator evaluates to true.
So only y++ has taken place, leaving it as -2.

I'm not an expert but here are my views:
cout << ((--x + y && y++ - x) || x-- + (--y + x++)) << endl;
((--x + y && y++ - x) resolves to -4 which is non zero and hence is true. So x-- + (--y + x++)) don't have to be checked and no need to execute that code.
in the expression (--x + y && y++ - x) there is a --x so that will result in a decrement in x and y++ will result in an increment in y, ie. y=-3+1.

Related

c++ implementation of quadratic equation, throws an error when implemented on a school testing website

I am trying to solve a very simple task tested by my univeristy's code checker. The code is about a c++ implementation of the equation of the quadratic equation. The code won't work for all cases, I am supplying my code, and if there is some comment, hint, please help me with it.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int a, b, c, delta;
double x1, x2, x;
cin >> a >> b >> c;
delta = ((b*b) - (4 * a * c));
if (a == 0) {
if (b != 0) {
x = (-(c*1.0) / (b));
cout << 1 << " " << x;
}
else
cout << "-1";
}
else
if (delta > 0)
{
x1 = ((-(b + sqrt(delta))*1.0) / (2 * a));
x2 = ((-(b - sqrt(delta))*1.0) / (2 * a));
cout << 2 << " " << x1 << " " << x2;
}
else if (delta == 0)
{
x = (-(b*1.0) / (2 * a));
cout << 1 << " " << x;
}
else
cout << "-1";
return 0;
}

in c++ how do I calculate tool path length?

I am dealing with generation of tool path where composed many points in three dimension and I am using CNC machine to generate them. One of the things that I want to calculate is tool path length which is defined the total length of path. So I tried this:
1.6760 3.7901 6.1955
1.2788 4.1872 5.3681
0.2832 5.1828 3.2939
0.1835 5.2173 3.0576
0.1097 5.1205 2.8292
0.0815 4.9185 2.6699
0.0812 4.8728 2.6491
0.0810 4.8270 2.6288
0.0807 4.7810 2.6089
The points are like these.
// math.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::istream;
using std::ifstream;
using std::operator>>;
using std::operator<<;
struct point
{
float x ;
float y ;
float z ;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
float sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
}
}
else
cout << "Unable to open file";
pointfile.close();
vector<line>::iterator iter = side.begin();
line temp, closest = *iter;
float minimumDistance = closest.sqDistance(), distance = 0.0;
system("PAUSE");
return 0;
}
-I expect the distance between point and its next point.
-the total length of this line.
What about something like this - suppose you want distance between points and not from start (line 93 with comment):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>
#include <conio.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::istream;
using std::ifstream;
using std::operator>>;
using std::operator<<;
struct point
{
float x ;
float y ;
float z ;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
float sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
ray.start = ray.next; // set start to last end (?)
}
}
else
cout << "Unable to open file";
pointfile.close();
vector<line>::iterator iter = side.begin();
line temp, closest = *iter;
float minimumDistance = closest.sqDistance(), distance, sumDistance = 0.0;
cout << "Line coords" << endl << "distance, Sum of distances, minimum" << endl;
while(iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumDistance += distance;
if(minimumDistance > distance) minimumDistance = distance;
cout << closest << endl
<< distance << " | " << sumDistance << " | " << minimumDistance << endl;
sumDistance += distance;
iter++;
}
getch();
return 0;
}
Sorry for the bug - sum line was there twice - line after cout was an error, also noticed some precision warning - mixed double/float, so switched to double everywhere, now main loop looks like this:
vector<line>::iterator iter = side.begin();
line closest = *iter;
double distance, sumOfDistances = 0.0;
cout << "Line coords" << endl << "distance | Sum of distances" << endl;
while (iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumOfDistances += distance;
cout << closest << endl << distance << " | " << sumOfDistances << endl; // step info output
iter++;
}
Here complete, a bit shorter version including simple results.txt file output.
You can remove 2 lines with comment info output:
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
using namespace std;
struct point
{
float x;
float y;
float z;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
double sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
ray.start = ray.next; // set start to last end (?)
}
}
else
cout << "Unable to open file";
pointfile.close();
ofstream results("results.txt");
vector<line>::iterator iter = side.begin();
line closest = *iter;
double distance, sumOfDistances = 0.0;
cout << "Line coords" << endl << "distance | Sum of distances" << endl;
while (iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumOfDistances += distance;
results << distance << endl;
cout << closest << endl << distance << " | " << sumOfDistances << endl; // info output
iter++;
}
results << sumOfDistances << " << Sum" << endl;
results.close();
cout << "Complete path distance: " << sumOfDistances << endl; // info output
getch();
return 0;
}

Program loop bug

Thanks for reading my problem. My loop code is not working as expected. The console output must not display negative numbers but it still does display them.
While (x > 0 && Y > 0)
{
--x;
--y;
cout << x << endl;
cout << y << endl;
}
I'm still a beginner in c++.
'Y' and 'y' are different. Perhaps you want to write it this way:
while (x > 0 && y > 0)
{
// First print the value and then decrement it
cout << x << endl;
cout << y << endl;
--x;
--y;
}

How to add decimals to switch?

I've been assigned a homework and I seem to have worked most of its coding. The only problem I'm facing is that the professor used what seems to be variables double for x and y when I'm sure we can only use int, long, and char when using switch (We are restricted to use switch and not if/else loop).
I think I am supposed to use setprecision(3) or static_cast(double)< > since we studied those, but I am not sure how to add them in the code.
This is the output required:
Welcome to my Calculator!
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 1
Enter first number: 3.899999
Enter second number: 4.000001
3.900 + 4.000 = 7.900
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 2
Enter first number: 2.34
Enter second number: 76.44422
2.340 * 76.444 = 178.879
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 3
Enter first number: 2.342
Enter second number: 1.321
2.342 / 1.321 = 1.773
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 4
Enter first number: -3411
Enter second number: 2454
-3411.000 - 2454.000 = -5865.000
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 5
Enter first integer: 10
Enter second integer: 3
10 % 3 = 1
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 6
Wrong input! Try again!
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 56
Wrong input! Try again!
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: 5
Enter first integer: 6
Enter second integer: 5
6 % 5 = 1
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Modula
Enter operation number, or -1 to quit: -1
Goodbye!!
This is my code:
#include <iostream>
using namespace std;
#include <iomanip>
int main()
{
//variable introduction
int op_num; //number of the operation
int x; // value of first integer
int y; // value of second integer
int sum; //answer for addition
int mul; //answer for multiplication
int div; //answer for division
int sub; //answer for subtraction
int mod; //answer for modula
//initialization phase
cout << "Welcome to my Calculator!\n\n";
cout << "1. Addition\n"
<< "2. Multiplication\n"
<< "3. Division\n"
<< "4. Subtraction\n"
<< "5. Modula\n" << endl;
cout << "Enter operation number, or -1 to quit: ";
cin >> op_num;
//processing phase
while (op_num != -1)
{
cout << "\nEnter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;
sum = x + y; //addition equation
mul = x * y; //multiplication equation
div = x / y; //division equation
sub = x - y; //subtraction equation
mod = x % y; //modula equation
switch (op_num)
{
case 1: cout << "\n" << x << " + "
<< y << " = " << sum << endl;
break;
case 2: cout << "\n" << x << " * "
<< y << " = " << mul << endl;
break;
case 3: cout << "\n" << x << " / "
<< y << " = " << div << endl;
break;
case 4: cout << "\n" << x << " - "
<< y << " = " << sub << endl;
break;
case 5: cout << "\n" << x << " % "
<< y << " = " << mod << endl;
break;
default: cout << "Wrong input! Try again!\n";
break;
}
cout << "1. Addition\n"
<< "2. Multiplication\n"
<< "3. Division\n"
<< "4. Subtraction\n"
<< "5. Modula\n" << endl;
cout << "Enter operation number, or -1 to quit: ";
cin >> op_num;
}
cout << "Goodbye!!" << endl;
return 0;
}
If there's anything I missed or a better way to sum up the coding, please do share!
From your code op_num is the variable used for the switch but not x or y. So it does it matter if the variable x and y are doubles? The answer is no. Also you included all your equations outside the switch. In the requirements, you don't have to calculate mul = x * y; div = x / y; sub = x - y;mod = x % y; when the user enters op_num as 1. So move these equations to their respective cases in the switch. That would solve all your bugs.

Extract X and Y Coordinates from String

I'm having a problem with extracting b2vec2 coordinates from a CCString these are from cocos2dx and box2d.
I have tried using strtk but i could not get it to work
Any help would be great.
Thanks
The Layout of the string is "x,y x,y x,y"
i want to put the x and y's into an array of b2vec2
string s = "12,4 4,5 6,3";
istringstream is(s);
while (is.good())
{
int x, y;
char comma;
is >> x >> comma >> y;
cout << x << ", " << y << endl;
}

Resources