1. define a class of rectangle:
class rectangle()
{
private: int w,h;
void setparameter(int w_, int h_)
{w=w_;h=h_;}
int area()
{ return w*h;}
int perimeter()
{ return 2*(w+h);}
};
void main()
{ int w,h;
rectangle c;
cin>>w>>h;
c.setparameter(w,h);
cout<<c.area()<<endl<<c.perimeter()<<endl;
return 0;
}
2. count matched brackets in a string:
#include <iostream>
#include <stack>
using namespace std;
int countbrackets(string s)
{
if (s.size()==0) return 0;
int n=0;
stack <char> cstack;
for (char c:s)
{ if (c=="(")
{cstack.push(c);}
else if(c==")")
{if (c==cstack.top())
{ cstack.pop();
n++;
}
}
}
return n;
}