A C programozási nyelv
Dennis M Ritchie (1941 - 2011)
At&T 1969 - 1973
#include <stdio.h>
int main()
{
fprintf( stdout, "Hello world\n");
return 0;
}
#include <stdio.h>
int main()
{
int fahr;
for ( fahr = 0; fahr <= 200; fahr += 40 )
{
fprintf( stdout, "F = %d\tC = %d\n", fahr, 5/9*(fahr-32));
}
return 0;
}
#include <stdio.h>
#define LOWER 0
#define UPPER 200
#define STEP 40
int main()
{
int fahr;
for ( fahr = LOWER; fahr <= UPPER; fahr += STEP )
{
fprintf( stdout, "F = %d\tC = %d\n", fahr, 5/9*(fahr-32));
}
return 0;
}
#include <stdio.h>
#define LOWER 0
#define UPPER 200
#define STEP 40
int main()
{
int fahr;
for ( fahr = LOWER; fahr <= UPPER; fahr += STEP )
{
fprintf( stdout, "F = %d\tC = %d\n", fahr, 5./9*(fahr-32));
}
return 0;
}
#include <stdio.h>
#define LOWER 0
#define UPPER 200
#define STEP 40
int main()
{
int fahr;
for ( fahr = LOWER; fahr <= UPPER; fahr += STEP )
{
fprintf( stdout, "F = %d\tC = %f\n", fahr, 5./9*(fahr-32));
}
return 0;
}
#include <stdio.h>
int main()
{
int ch;
while ( (ch = getchar()) != EOF)
{
putchar(ch);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int capacity;
int sp;
double *v;
};
struct stack *make_stack(int init_size)
{
struct stack *s = (struct stack*) malloc(sizeof(struct stack));
s->capacity = init_size;
s->sp = 0;
s->v = (double *) malloc(init_size*sizeof(double));
return s;
}
void push(struct stack *s, double d)
{
s->v[s->sp] = d;
++s->sp;
}
double pop(struct stack *s)
{
return s->v[--s->sp];
}
int size(struct stack *s)
{
return s->sp;
}
void print(struct stack *s)
{
int i;
printf("[ ");
for(i = 0; i < s->sp; ++i)
{
printf("%f ", s->v[i]);
}
printf("]\n");
}
int main()
{
struct stack *d1 = make_stack(10);
push(d1, 3.14); push(d1, 4.14); push(d1, 5.14);
print(d1);
struct stack *d2 = d1;
print(d2);
while ( size(d1) > 0 )
printf("pop: %f\n", pop(d1));
print(d1);
return 0;
}
struct stack *copy_stack(struct stack *rhs)
{
int i;
struct stack *s = make_stack(rhs->capacity);
s->sp = rhs->sp;
for(i = 0; i < s->sp; ++i)
s->v[i] = rhs->v[i];
return s;
}
int main()
{
struct stack *d1 = make_stack(10);
push(d1, 3.14); push(d1, 4.14); push(d1, 5.14);
print(d1);
struct stack *d2 = copy_stack(d1);
print(d2);
while ( size(d1) > 0 )
printf("pop: %f\n", pop(d1));
print(d1);
print(d2);
return 0;
}
Bjarne Stroustrup (1950 - )
#ifndef DSTACK_H
#define DSTACK_H
#include <iostream>
class dstack
{
friend std::ostream &operator<<( std::ostream &os, dstack ds);
public:
dstack( int size = 128);
dstack( const dstack &other);
~dstack();
dstack& operator=( const dstack &other);
void push( double d);
double pop();
bool is_empty() const;
bool is_full() const;
private:
int capacity;
int sp;
double *v;
void copy( const dstack &other);
};
std::ostream &operator<<( std::ostream &os, dstack ds);
#endif
#include <iostream>
#include "dstack2.h"
dstack::dstack( int size)
{
v = new double[capacity = size];
sp = 0;
}
dstack::dstack( const dstack &other)
{
copy(other);
}
dstack::~dstack()
{
delete [] v;
}
dstack& dstack::operator=( const dstack &other)
{
if ( this != &other )
{
delete [] v;
copy(other);
}
return *this;
}
void dstack::copy( const dstack &other)
{
v = new double[capacity = other.capacity];
sp = other.sp;
for ( int i = 0 ; i < sp; ++i)
v[i] = other.v[i];
}
void dstack::push( double d)
{
TODO
if ( ! is_full() )
v[sp++] = d;
}
double dstack::pop()
{
TODO
return is_empty() ? 0.0 : v[--sp];
}
bool dstack::is_empty() const
{
return 0 == sp;
}
bool dstack::is_full() const
{
return capacity == sp;
}
std::ostream &operator<<( std::ostream &os, dstack ds)
{
os << "[ ";
for ( int i = 0; i < ds.sp-1; ++i )
os << ds.v[i] << ", ";
if ( ds.sp > 0 )
os << ds.v[ds.sp-1];
os << " ]";
return os;
}
#include <iostream>
#include "dstack2.h"
using namespace std;
int main()
{
dstack d1(10);
d1.push(3.14); d1.push(4.14); d1.push(5.14);
cout << d1 << endl;
dstack d2(d1);
while ( ! d1.is_empty() )
cout << "pop: " << d1.pop() << endl;
d1.push(6.14);
cout << d1 << endl;
cout << d2 << endl;
d1 = d2;
cout << d1 << endl;
return 0;
}
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
return 0;
}
#include <iostream>
using namespace std;
inline double fahr2cels( double f)
{
return 5./9*(f-32);
}
int main()
{
const int lower = 0;
const int upper = 200;
const int step = 40;
for ( int fahr = lower; fahr <= upper; fahr+=step )
{
cout << "F = " << fahr << '\t' << "C = " << fahr2cels(fahr) << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char ch;
while ( cin.get(ch) )
{
cout.put(ch);
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char ch;
while ( cin >> ch )
{
cout << ch;
}
return 0;
}
#include <iostream>
int i;
static int j;
extern int n;
namespace X
{
int i;
int f()
{
i = n;
return j;
}
void g();
}
namespace
{
int k;
}
void f()
{
int i;
static int k;
{
int j = i;
int i = j;
std::cout << i << j << k << X::i << ::k << ::i;
}
}
static void g()
{
++k;
}
void X::g()
{
++i;
}
A C++ objektumok az alábbi módon jöhetnek létre:
- globális, névtérbeli változó vagy statikus adattag
C - program eleje
D - program vége
- automatikus objektum (névvel rendelkezik)
C - deklaráció kiértékelődik
D - vezérlés elhagyja a deklaráló blokkot
- dinamikus objektum (szabad memória, free store)
C - new
D - delete
- nemstatikus adattag
C - tartalmazó objektum létrejön
D - tartalmazó objektum megsemmisül
- tömbelem
C - tömb létrejön
D - tömb megsemmisül
- lokális statikus objektum
C - deklaráció első alkalommal kiértékelődik
D - program vége
- temporális objektum
C - (rész)kifejezés kiértékelése
D - teljes kifejezés kiértékelése után
- union tag
nem lehet statikus adattag
nem lehet konstruktora ill. destruktora
void f()
{
int i = 2;
....
}
date d(2003,3,13);
static int i;
int main()
{
while ( ... )
{
if ( ... )
{
static int j = 6;
}
}
}
date *p = new date;
date *q = new date(*p);
date *s = new date[10];
delete p;
delete p;
delete s;
delete [] s;
date *p = (date*) malloc(sizeof(date));
free(p);
#include <iostream>
using namespace std;
char *answer( char *question);
int main()
{
cout << answer( "How are you? ") << endl;
return 0;
}
char *answer( char *question)
{
cout << question;
char buffer[80];
cin >> buffer;
return buffer;
}
#include <iostream>
using namespace std;
char *answer( char *question);
char buffer[80];
int main()
{
cout << answer( "How are you? ") << endl;
return 0;
}
char *answer( char *question)
{
cout << question;
cin.getline(buffer,80);
return buffer;
}
#include <iostream>
using namespace std;
char *answer( char *question);
int main()
{
cout << answer( "How are you? ") << endl;
return 0;
}
char *answer( char *question)
{
cout << question;
static char buffer[80];
cin.getline(buffer,80);
return buffer;
}
#include <iostream>
using namespace std;
char *answer( char *question);
int main()
{
cout << answer("Sure? ") << answer( "How are you? ") << endl;
return 0;
}
char *answer( char *question)
{
cout << question;
static char buffer[80];
cin.getline(buffer,80);
return buffer;
}
#include <iostream>
using namespace std;
char *answer( char *question);
int main()
{
cout << answer("Sure? ") << answer( "How are you? ") << endl;
return 0;
}
char *answer( char *question)
{
cout << question;
char *buffer = new char[80];
cin.getline(buffer,80);
return buffer;
}
#include <iostream>
using namespace std;
char *answer( const char *question, char *buffer, int size);
int main()
{
const int bufsize = 80;
char buffer1[bufsize], buffer2[bufsize];
cout << answer("Sure? ", buffer1, bufsize)
<< answer( "How are you? ", buffer2, bufsize) << endl;
return 0;
}
char *answer( const char *question, char *buffer, int size)
{
cout << question;
cin.getline(buffer,size);
return buffer;
}
#include <iostream>
#include <string>
using namespace std;
string answer( string question);
int main()
{
cout << answer("Sure? ") << answer( "How are you? ") << endl;
return 0;
}
string answer( string question)
{
cout << question;
string s;
getline(cin,s);
return s;
}
#include <iostream>
#include <string>
using namespace std;
string answer( string question);
int main()
{
cout << answer( "How are you? ") << endl;
cout << answer( "Sure? ") << endl;
return 0;
}
string answer( string question)
{
cout << question;
string s;
getline(cin,s);
return s;
}
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string answer( string question);
int main()
{
ostringstream os;
os << answer( "How are you? ");
os << ", ";
os << answer( "Sure? ");
cout << os.str() << endl;
return 0;
}
string answer( string question)
{
cout << question;
string s;
getline(cin,s);
return s;
}
#ifndef STACK_H
#define STACK_H
#include <iostream>
template <class T>
class stack
{
friend std::ostream &operator<< <> ( std::ostream &os, stack s);
public:
stack( int size = 128);
stack( const stack &rhs);
~stack();
stack& operator=( const stack &rhs);
void push( T d);
T pop();
bool is_empty() const;
bool is_full() const;
private:
int capacity;
int sp;
T *v;
void copy( const stack &other);
};
template <class T>
stack<T>::stack( int size)
{
v = new T[capacity = size];
sp = 0;
}
template <class T>
stack<T>::stack( const stack &other)
{
copy(other);
}
template <class T>
stack<T>::~stack()
{
delete [] v;
}
template <class T>
stack<T>& stack<T>::operator=( const stack &other)
{
if ( this != &other )
{
delete [] v;
copy(other);
}
return *this;
}
template <class T>
void stack<T>::copy( const stack &other)
{
v = new T[capacity = other.capacity];
sp = other.sp;
for ( int i = 0 ; i < sp; ++i)
v[i] = other.v[i];
}
template <class T>
void stack<T>::push( T d)
{
v[sp++] = d;
}
template <class T>
T stack<T>::pop()
{
v[--sp];
}
template <class T>
bool stack<T>::is_empty() const
{
return 0 == sp;
}
template <class T>
bool stack<T>::is_full() const
{
return capacity == sp;
}
template <class T>
std::ostream &operator<<( std::ostream &os, stack<T> ds)
{
os << "[ ";
for ( int i = 0; i < ds.sp-1; ++i )
os << ds.v[i] << ", ";
if ( ds.sp > 0 )
os << ds.v[ds.sp-1];
os << " ]";
return os;
}
#endif
#ifndef STACK_BOOL_H
#define STACK_BOOL_H
#include <iostream>
template <>
class stack<bool>
{
};
template <>
void stack<bool>::copy( const stack &other)
{
v = new T[capacity = other.capacity];
sp = other.sp;
memcpy(v, other.v, sizeof(T)*sp);
}
#endif
#include <iostream>
#include "stack.h"
#include "stack_bool.h"
#include "date.h"
using namespace std;
int main()
{
stack<int> d1(10);
d1.push(3);
d1.push(4);
d1.push(5);
cout << d1 << endl;
date d;
stack<date> d2(5);
d2.push(date(2000,6,23));
d2.push(d);
stack<bool> d3(2000);
cout << d1 << endl;
cout << d2 << endl;
cout << d3 << endl;
return 0;
}
int t[] = { 1, 3, 5, ... };
int *pi = find( t, t+sizeof(t)/sizeof(t[0]), 55);
if ( pi )
{
*pi = 56;
}
int *find( int *begin, int *end, int x)
{
while ( begin != end )
{
if ( *begin == x )
{
return begin;
}
++begin;
}
return 0;
}
template <typename T>
T *find( T *begin, T *end, const T& x)
{
while ( begin != end )
{
if ( *begin == x )
{
return begin;
}
++begin;
}
return 0;
}
template <typename It, typename T>
It find( It begin, It end, const T& x)
{
while ( begin != end )
{
if ( *begin == x )
{
return begin;
}
++begin;
}
return end;
}
int t[] = { 1, 3, 5, ... };
int t[] = { 1, 3, 5, ... };
int *pi = find( t, t+sizeof(t)/sizeof(t[0]), 55);
if ( pi )
{
*pi = 56;
}
vector<int> v;
v.push_back(1); v.push_back(3); v.push_back(5); ...
vector<int>::iterator vi = find( v.begin(), v.end(), 55);
if ( vi != v.end() )
{
*vi = 56;
}
list<double> l;
l.push_back(1.1); l.push_back(3.3); l.push_back(5.5); ...
list<double>::iterator li = find( l.begin(), l.end(), 55.55);
if ( li != l.end() )
{
*li = 56.66;
}
const int t[] = { 1, 3, 5, ... };
const int *pi = find( t, t+sizeof(t)/sizeof(t[0]), 55);
if ( pi )
{
cout << *pi;
}
vector<int> v;
v.push_back(1); v.push_back(3); v.push_back(5); ...
const list<double> cl( v.begin(), v.end());
list<double>::const_iterator cli = find( cl.begin(), cl.end(), 55.55);
if ( cli != cl.end() )
{
cout << *cli;
}
list<double> l; ...
list<double>::iterator li;
li = find( l.begin(), l.end(), 3.14);
li = find( li, l.end(), 3.14);
li = find( li, l.end(), 3.14);
li = find(find(find(l.begin(),l.end(),3.14),l.end(),3.14),l.end(),3.14);
template <typename It, typename Pred>
It find_if( It begin, It end, Pred p)
{
while ( begin != end )
{
if ( p(*begin) )
{
return begin;
}
++begin;
}
return end;
}
bool less55_3rd( int x)
{
static int cnt = 0;
if ( x < 55 )
++cnt;
return 3 == cnt;
}
vector<int> v; ...
vector<int>::iterator = find_if( v.begin(), v.end(), less55_3rd);
struct less55_3rd
{
less55_3rd() : cnt(0) { }
bool operator()(int x)
{
if ( x < 55 )
++cnt;
return 3 == cnt;
}
private:
int cnt;
};
vector<int> v; ...
less55_3rd pp;
vector<int>::iterator = find_if( v.begin(), v.end(), pp);
vector<int>::iterator = find_if( v.begin(), v.end(), less55_3rd());
template <typename T>
struct less_nth
{
less_nth( const T& t, int n) : t_(t), n_(n), cnt_(0) { }
bool operator()(const T& t)
{
if ( t < t_ )
++cnt;
return n_ == cnt;
}
private:
T t_;
int n_;
int cnt_;
};
vector<int> v; ...
vector<int>::iterator = find_if(v.begin(),v.end(),less_nth<int>(55,3));
http: