page 1 page 2
page 3 page 4 page 5 page 6 page 7 page 8
you've seen variables, functions, ridiculously offensive classes. what's left? tons of stuff,
even i don't know it all. i'll finish it with some tidbits we might've missed, or wouldn't
want to.
code:
#include <iostream>
#include "stuff.h"
#define pi 3.14
bool isprime(int n)
{
if (n < 4)
{
if (n < 0)
{
std::cout << "fuck your negative numbers";
return 0;
}
return true;
}
else
{
for (int i = 1; i <= n/2; i++)
{
if (n % 2 == 0)
return false;
else if (n/i == i)
return false;
}
return true;
}
}
ooh, booleans.
bool isprime(int n)
you declare a bool just like a function, the stuff inside it determines the true or false value.
return true; return false;
the return value for the bool is going to be true or false in the end. if n is less than 4 (2, 3) it is prime.
0, 1, and any negative numbers, my coding can't deal with theories.
n % 2
the percent symbol means modulo. it deals with the remainder when dividing n by 2. in this case, if the
remainder is 0 (any number that divides by 2 evenly is an even number, and immediately is not prime) it returns false.
#include "stuff.h"
now we'll tackle the stuff on top. notice the difference between this and iostream? this is including a local
file which is in the same directory. if the header file you want is not in your compiler's include directory,
it's probably one you've made and you need to use quotes instead. now you need the .h extension (or .hpp).
#define pi 3.14
#define makes a constant variable for use anywhere in your program. this is a c-style way of doing things (like
array of char instead of string) so const int/double/float/whatever is better to use in c++. #define isn't
limited to numbers or strings, though, so one nifty thing you can do is replace pieces of actual code with shorter ones.
code:
#define print cout<<
#define double d
#define forever for(;;)
int main(int argc, char *argv[])
{
d pi = 3.14;
forever
print pi;
}
cool fucking beans. one new thing i added to throw you off was a pair of arguments for main(). dun dun dun.
this is the actual standard for main(), whether you use the arguments or not. they are intended for command
line use, argc is the amount of arguments supplied and argv is, get this, an array of arrays of char.
argv stores the actual arguments. let's give an example.
code:
int main(int argc, char *argv[])
{
cout << "argc: " << argc;
for (int i = 0; i < argc; i++)
cout << "argv[i]: " << argv[i] << "\n";
}
and the output is...
shell:
[peaceofpi@slackware]$ example.exe arg1 arg2
argc: 3
argv[0]: example.exe
argv[1]: arg1
argv[2]: arg2
and that's about all i can stand to teach you, i hope you've learned a thing or two. tell me if you did,
getting email is always nice. peaceofpi@gmail.com
a large portion of this was written after 10 p.m., so there might be a piece of code that's not
entirely correct. there are a few fragments, simply because typing c++ source in html is fucking annoying.
page 7 (vulgar) was admittedly the entire reason i wrote this, i didn't realize it'd be the only page that had
that kind of offensive potential.
page 1 page 2
page 3 page 4
page 5 page 6 page 7 page 8