why does C = 2 shouldn't it be 0 A+1 = 1 and 1-B =0 how does it work
#include <iostream>
#include <string>
using namespace std;
#define A 0
#define B A+1
#define C 1-B
int main()
{
cout << C ;
}
Thanks to Lakshay Garg i understand what he means it just replaces the Macro with whatever i define so in the case of "#define C 1-B" the B will be replaced with A+1 which is 0+1
So in in my cout C= 1-0+1 Which is 2 again thanks for the help
Related
input: 5.7 4 b
output: 5 0.7 4
What is happening for this output to occur?
Also I notice that I am unable to input the last varble "char" when I use inputs that don't match their declared data type.
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main()
{
int whole;
double fractional;
char letter;
cin >> whole >> fractional >> letter;
cout << whole << " " << fractional << " " << letter << endl;
return 0;
}
at first cin expects an integer, so it only reads the integer part of the first value and leaves the rest. Your input is equivalent to this 5 0.7 4 b, so b is never read.
I'd like to build a rule that takes in a few parameters from a parsed line then sets a few as constant. Is that possible? An (invalid) example to illustrate what I'm trying to do is below. I think I'm using _r1 incorrectly here, but I'm not sure how to get at the right thing. Assume I don't want to just modify r before sending it into the parser.
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_plus.hpp>
#include <boost/spirit/include/qi_sequence.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/phoenix/bind/bind_function.hpp>
#include <string>
using namespace boost::spirit::qi;
struct Sample
{
int a;
int b;
};
BOOST_FUSION_ADAPT_STRUCT(Sample, a , b)
const rule<std::string::const_iterator, Sample()> AnythingAndOne = int_ >> eps[_r1.b = 1] >> eoi;
int main()
{
std::string z("3");
Sample r;
parse(z.begin(), z.end(), AnythingAndOne, r);
return 0;
}
Again, with reference to Boost Spirit: "Semantic actions are evil"? I'd avoid the semantic action.
You can directly synthesize a particular attribute value by using qi::attr:
Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
struct Sample {
int a;
int b;
};
BOOST_FUSION_ADAPT_STRUCT(Sample, a , b)
namespace qi = boost::spirit::qi;
int main()
{
std::string const z("3");
Sample r;
qi::rule<std::string::const_iterator, Sample()> AnythingAndOne
= qi::int_ >> qi::attr(1) >> qi::eoi;
if (parse(z.begin(), z.end(), AnythingAndOne, r))
std::cout << "Parsed: " << boost::fusion::as_vector(r) << "\n";
else
std::cout << "Parse failed\n";
}
Prints
Parsed: (3 1)
So I'm about to finish my course in university in C programming.
I want to get better at bit operations (such as creating masks) so I'll go to it:
#define BIT_I_SET(TYPE,I) ((TYPE)(1) << (I))
#define SET_BIT(NUM,I,TYPE) \
NUM |= BIT_I_SET(I,TYPE)
I am still at the early stages and learning the syntax at the moment, I have no idea why the compiler says there's error:
Severity Code Description Project File Line Suppression State
Error (active) E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type Project14
full program (yeah it's for synatx only):
#include <stdio.h>
#include <stdlib.h>
#define SHIFT(I,TYPE) ((TYPE)(1) << (I))
#define NEGATIVE(TYPE) (~(TYPE)(0))
#define BIT_I_SET(TYPE,I) ((TYPE)(1) << (I))
#define BIT_I_CLEAR(I,TYPE) (~((TYPE)(1)<< (I)))
#define MSB_SET(TYPE) ((TYPE)(1) << (sizeof(TYPE)*8-1)
#define SET_BIT(NUM,I,TYPE) \
NUM |= BIT_I_SET(I,TYPE)
void main()
{
unsigned char i, j;
int shift = 3;
i = 0;
j = 0;
SET_BIT(j, 2, unsigned char);
printf("%d\n",sizeof(j));
printf("%d",i);
system("pause>null");
}
change
NUM |= BIT_I_SET(I,TYPE)
to
NUM |= BIT_I_SET(TYPE, I)
You can run just the preprocessor stage of your compiler, which expand the macros
using the command:
gcc -E file.c
I would like to do the boolean matrix plus. How could I do it in Eigen?
My following example only gives a scalar +.
#include "Eigen/Dense"
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
Eigen::Matrix<bool, 4, 4> m;
m << 0,1,1,1,
1,0,1,0,
1,1,0,0,
1,1,1,0;
cout << m + m; //should be logical_and here
}
How could I use the logical_and here?
Eigen does not seem to provide specific functions to work on boolean matrices. However you can use the fact that booleans are converted to 0 (false) and 1 (true) reliably (see bool to int conversion). Noting that 0=0*0=0*1=1*0 and 1*1=1 it is obvious that multiplication of the booleans as integers is the same (up to type) as logical and. Therefore the following should work:
#include "Eigen/Dense"
#include <iostream>
using namespace std;
using namespace Eigen;
int main()
{
Eigen::Matrix<bool, 4, 4> m;
m << 0,1,1,1,
1,0,1,0,
1,1,0,0,
1,1,1,0;
Eigen::Matrix<bool, 4, 4> result = m.cwiseProduct(m);
cout << result;
}
I would like to ask if anybody knows why this is not working:
For example, let
SparseMatrix<int> A
and
SparseMatrix<std::complex<float> > B
I would like to do the following math:
B=i*A
As code:
std::complex<float> c;
c=1.0i;
B=A.cast<std::complex<float> >()*c;
or equivalent:
B=A.cast<std::complex<float> >()*1.0i;
I expect all real values of A to be imaginary in B but
there are only zeros as (0,0).
Example:
#include <Eigen/Sparse>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace Eigen;
using std::cout;
using std::endl;
int main(int argc, char *argv[]){
int rows=5, cols=5;
SparseMatrix<int> A(rows,cols);
A.setIdentity();
SparseMatrix<std::complex<float> > B;
std::complex<float> c;
c=1i;
B=A.cast<std::complex<float> >()*1.0i;
//B=A.cast<std::complex<float> >()*c;
cout << B << endl;
return 0;
}
compile with:
g++ [name].cpp -o [name]
What am I doing wrong?
Thanks a lot for any help!
You need to enable c++14 to get 1.0i working as expected. With GCC or clang, you need to add the -std=c++14 compiler option.
Then, you can simply do:
MatrixXd A = MatrixXd::Random(3,3);
MatrixXcd B;
B = A * 1.0i;
Same with a SparseMatrix.