#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Point;
class UPoint;
class Handle;
class Point
{
public:
Point():xval(0),yval(0){}
Point (int x,int y):xval(x),yval(y){}
int x()const{return xval;}
int y()const{return yval;}
Point & x(int xv){xval=xv;return *this;}
Point & y(int yv){yval=yv;return *this;}
protected:
private:
int xval;
int yval;
};
class UPoint
{
public:
protected:
private:
friend class Handle;
Point p;
int u;
UPoint():u(1){}
UPoint(int x,int y):p(x,y),u(1){}
UPoint(const Point&p0):p(p0),u(1){}
};
class Handle
{
public:
Handle();
Handle(int ,int);
Handle(const Point &);
Handle(const Handle&);
Handle &operator=(const Handle&);
~Handle();
int x()const;
int y()const;
Handle & x(int);
Handle & y(int);
protected:
private:
UPoint *up;
};
Handle::Handle():up(new UPoint)
{};
Handle::Handle(int x,int y):up(new UPoint(x,y))
{} ;
Handle::~Handle()
{
if (--up==0)
{
delete up;
}
};
Handle::Handle(const Handle& h):up(h.up)
{
++up->u;
};
Handle& Handle::operator=(const Handle & h)
{
++h.up->u;
if (--up==0)
{
delete up;
}
up=h.up;
return *this;
} ;
int Handle::x()const{return up->p.x();}
int Handle::y()const{return up->p.y();}
Handle & Handle::x(int x0)
{
up->p.x(x0);
return *this;
};
Handle & Handle::y(int y0)
{
up->p.y(y0);
return *this;
};
//
class Expr_Node;
class Expr;
class Binary_node;
class Unary_node;
class Int_node;
ostream & operator<<(ostream & o,const Expr& t);
class Expr_Node
{
public:
protected:
~Expr_Node(){}
virtual void print(ostream &)const =0;
virtual int eval()const=0;
Expr_Node ():use(1){}
private:
friend ostream & operator<<(ostream & o,const Expr &t);
friend class Expr;
int use;
};
class Expr
{
public:
Expr(int );
Expr(const string & ,Expr t);
Expr(const string & ,Expr l,Expr r);
Expr(Expr &t){ p = t.p; ++p->use; }
Expr & operator=(const Expr &t);
~Expr(){
if (--p->use==0)
{
delete p;
}
}
int eval()const{ return p->eval(); }
protected:
private:
friend ostream & operator<<(ostream & ,const Expr&);
Expr_Node * p;
};
class Int_node:public Expr_Node
{
public:
protected:
private:
friend class Expr;
int n;
Int_node (int k):n(k){}
void print(ostream & o)const {o<<n;}
int eval()const{return n;}
};
class Unary_node:public Expr_Node
{
friend class Expr;
string op;
Expr opnd;
Unary_node(const string &a,Expr b):op(a),opnd(b){}
void print(ostream & o)const{o<<"("<<op<<opnd<<")";}
int eval()const
{
if (op=="-")
{
return -opnd.eval();
}
throw " error ,bad operator "+ op+" in Unary_node";
}
};
class Binary_node:public Expr_Node
{
friend class Expr;
string op;
Expr left;
Expr right;
Binary_node (const string &a,Expr b, Expr c):op(a),left(b),right(c){}
void print(ostream & o) const
{
o<<"("<<left<<op<<right<<")";
}
int eval()const;
};
int Binary_node::eval()const
{
int op1=left.eval();
int op2=right.eval();
if (op=="-"){return op1-op2;}
if (op=="+"){return op1+op2;}
if (op=="*"){return op1*op2;}
if (op=="/"&&op2!=0){return op1/op2;}
throw " error ,bad operator "+ op+" in Binary_node";
} ;
Expr::Expr(int n)
{
p=new Int_node(n);
}
Expr::Expr(const string &op,Expr t)
{
p=new Unary_node(op,t);
}
Expr::Expr(const string & op,Expr left,Expr right)
{
p=new Binary_node(op,left,right);
}
Expr & Expr::operator=(const Expr &t)
{
t.p->use++;
if (--p->use==0)
{
delete p;
}
p=t.p;
return *this;
} ;
ostream & operator<<(ostream & o,const Expr& t)
{
t.p->print(o);
return o;
};
///
int main()
{
Handle h(10,10);
for (int i=0;i<10;i++)
{
h.x(i);
h.y(i);
}
system("pause");
Expr t=Expr("*",Expr("-",5),Expr("+",3,4));
cout<<t<<" = "<<t.eval()<<endl;
system("pause");
}
C语言查找数组里数字重复次数
#include<stdio.h>
#include <iostream>
int count_number( int* arr_beg, int* arr_end , int num );
using namespace std;
int main()
{
int myarray[] = {4, 3, 7, 4, 8, 7, 9, 4, 3, 6, 4, 3, 7, 4, 8, 7, 9, 4, 3, 6};
printf( "输入你想查询的数:" );
int number = 0;
cin >> number;
int count = 0;
int size = sizeof( myarray ) / sizeof( *myarray );
for ( int i = 0; i < size; i++ ) {
if ( number == myarray[i] ) {
count++;
}
}
if ( count != 0 ) {
printf( "共出现了 %d 次数字 %d \n", count , number );
}
else {
printf( "一次都没出现!\n" );
}
// 使用函数调用查找数字
count = count_number( myarray, myarray + size , 3 );
if ( count != 0 ) {
printf( "共出现了 %d 次数字 %d \n", count , 3 );
}
else {
printf( "一次都没出现!\n" );
}
return 0;
}
int count_number( int* arr_beg, int* arr_end , int num )
{
int count = 0;
while ( arr_beg != arr_end ) {
if ( *arr_beg == num ) {
count++;
}
arr_beg++;
}
return count;
}