HEAD:source/_posts/2011-11-14-c-note-1.html --- layout: post title: C++ note 1 categories: - C++ - Programming tags: - C++ - HOWTO - Note - Programming published: true comments: true ---
可读性差,只留着自己用
=======
>>>>>>> d80cd8fa3e1fb5461144707ba04f7385ec6726a7:category/c-plus-plus/atom.xml
[cpp]
// my first program in C++
/* my second program in C++
with more comments */
#include
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
[/cpp]
Name | Description | Size* | Range* |
---|---|---|---|
char |
Character or small integer. | 1byte | signed: -128 to 127 unsigned: 0 to 255 |
short int (short ) |
Short Integer. | 2bytes | signed: -32768 to 32767 unsigned: 0 to 65535 |
int |
Integer. | 4bytes | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
long int (long ) |
Long integer. | 4bytes | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
bool |
Boolean value. It can take one of two values: true or false. | 1byte | true or false |
float |
Floating point number. | 4bytes | +/- 3.4e +/- 38 (~7 digits) |
double |
Double precision floating point number. | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
long double |
Long double precision floating point number. | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t |
Wide character. | 2 or 4 bytes | 1 wide character |
\n |
newline |
\r |
carriage return |
\t |
tab |
\v |
vertical tab |
\b |
backspace |
\f |
form feed (page feed) |
\a |
alert (beep) |
\' |
single quote (’) |
\" |
double quote (”) |
\? |
question mark (?) |
\\ |
backslash (\) |
[cpp]
// In order to use a variable in C++
// we must first declare it specifying which data type we want it to be.
int a, b, c;
// Signed types can represent both positive and negative values
// whereas unsigned types can only represent positive values (and zero).
unsigned short int NumberOfSisters;
signed int MyAccountBalance;
int a = 0;
int a (0);
string mystring = "This is a string";
string mystring ("This is a string");
// String literals can extend to more than a single line of code by
// putting a backslash sign (\) at the end of each unfinished line.
"string expressed in \
two lines"
//does not require a semicolon (;)
#define PI 3.14159
<<<<<<< HEAD:source/_posts/2011-11-14-c-note-1.html
#define NEWLINE '\n'
const int pathwidth = 100;
const char tabulator = ‘\t’;
const int pathwidth = 100;
const char tabulator = ‘\t’;
// +, -, *, /, %
// +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=
// ++, –
// ==, !=, >, <, >=, <=
7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.
5>3 ? a : b // returns the value of a, since 5 is greater than 3.
a>b ? a : b // returns whichever is greater, a or b.
a = (b=3, b+2);
// Would first assign the value 3 to b, and then assign b+2 to variable a.
// variable a would contain the value 5 while variable b would contain value 3.
// Bitwise Operators ( &, |, ^, ~, <<, >> )
int i;
float f = 3.14;
i = (int) f;
i = int ( f );
// returns the size in bytes of that type or object:
a = sizeof (char);
cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;
// Hello, I am 24 years old and my zipcode is 90064
cout << "First sentence.\n";
cout << "First sentence." << endl;
// Standard Input (cin).
int age;
cin >> age;
// to request more than one datum input from the user:
cin >> a >> b;
// In order to get entire lines, we can use the function getline,
// which is the more recommendable way to get user input with cin
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystr;
cout << "What’s your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
// The standard header file <sstream> defines a class called stringstrea
// that allows a string-based object to be treated as a stream.
// This way we can perform extraction or insertion operations from/to strings,
// which is especially useful to convert strings to numerical values and vice versa.
// For example, if we want to extract an integer from a string we can write:
string mystr ("1204");
int myint;
stringstream(mystr) >> myint;
// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystr;
float price=0;
int quantity=0;
cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}
if (x == 100)
cout << "x is 100";
if (x == 100)
{
cout << "x is ";
cout << x;
}
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
while (n>0) {
cout << n << ", ";
–n;
}
do {
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
} while (n != 0);
for (int n=10; n>0; n–) {
cout << n << ", ";
}
for ( n=0, i=100 ; n!=i ; n++, i– )
=======
for (int n=10; n>0; n–) {
cout << n << ", ";
}
for ( n=0, i=100 ; n!=i ; n++, i– )
>>>>>>> d80cd8fa3e1fb5461144707ba04f7385ec6726a7:category/c-plus-plus/atom.xml
{
// whatever here…
}
for (n=10; n>0; n–)
=======
for (n=10; n>0; n–)
>>>>>>> d80cd8fa3e1fb5461144707ba04f7385ec6726a7:category/c-plus-plus/atom.xml
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
for (int n=10; n>0; n–) {
=======
for (int n=10; n>0; n–) {
>>>>>>> d80cd8fa3e1fb5461144707ba04f7385ec6726a7:category/c-plus-plus/atom.xml
if (n==5) continue;
cout << n << ", ";
}
int n=10;
loop:
cout << n << ", ";
n–;
if (n>0) goto loop;
// exit is a function defined in the cstdlib library.
// to terminate the current program with a specific exit code.
void exit (int exitcode);
// Functions
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
// void function example
void printmessage ()
{
cout << "I’m a function!";
}
int main ()
{
printmessage ();
// printmessage; wrong!!!
return 0;
}
[/cpp]
<<<<<<< HEAD:source/_posts/2011-11-14-c-note-1.html ======= ]]>“` cpp hello.cpp
// my first program in C++
using namespace std;
int main () { cout « “Hello World!”; return 0; }
“`
compile:
g++ -o hello hello.cpp]]>