首页 > 代码库 > 贴一下我写过的c++程序代码

贴一下我写过的c++程序代码

5258

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class X{
    public:
    const static double PI;
};
const double X::PI=acos(-1.0);
int main()
{
 cout<<setiosflags(ios::fixed)<<setprecision(2)<<X::PI<<endl;
 return 0;
}

1178

#include<iostream>
#include<string>
using namespace  std;
int main(){
 string c;
 int count=0;
 while (cin >> c)
 { count++; }
 cout << count << endl;
 
 return 0;
}

1001
#include <iostream.h>
int main()
 {
   int a,b;
   cin>>a>>b;
   cout<<a+b<<endl;
 return 0;
}  

1174

#include<iostream>
#include<string>
using namespace  std;
int main(){
 string s, c;
 getline(cin, s);
 cin >> c;
 int pos = 0;
 while ((pos=s.find(c, pos) )>= 0)
  s.erase(pos, c.length());
   cout << s << endl;

return 0;}

1283使用优先队列来一波

#include <stdio.h>
#include <queue>
using namespace std;
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        int n;
        scanf("%d",&n);
        priority_queue<int,vector<int>,greater<int> > qu;
        for(int i=0; i<n; i++) {
            int a;
            scanf("%d",&a);
            qu.push(a);
        }
        printf("%d",qu.top());
        for(int i=1; i<n; i++) {
            qu.pop();
            printf(" %d",qu.top());
        }
        printf("\n");
    }

    return 0;
}

1090使用sort来一波,使用c++写起来好麻烦,不过这个更安全

#include <bits/stdc++.h>
using namespace std;
int cmp(int a,int b){
return fabs(a)>fabs(b);}
int main()
{int n,j,i,t;
while(cin>>n,n)
{vector<int>a;
for(i=0;i<n;i++){
int p;
cin>>p;
a.push_back(p);}
sort(a.begin(),a.end(),cmp);
vector<int> :: iterator it;
it=a.begin();
cout<<*it;
it++;
for(;it!=a.end();it++)
cout<<" "<<*it;
cout<<endl;
}
return 0;}

1214就是个简单的操作

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
 vector<int>a;
 string s;
 int i,j,x,y,;
 while(getline(cin,s))
 {
  if(s=="clear")
  {
   a.clear();
  }
  else if(s=="delete")
  {
   cin>>x;
   cout<<a[x-1]<<endl;
   a.erase(a.begin()+x-1);
  }
  else if(s=="insert")
  {
   cin>>j;
   while(j--)
   {
    cin>>x>>y;
    a.insert(a.begin()+x-1,y);
   }
  }
 
  else if(s=="getelem")
  {
   cin>>x;
   cout<<a[x-1]<<endl;
  } 
  else if(s=="exit")
  return 0;
 }
}

1171字符串倒置,直接放进函数就ok

#include <bits/stdc++.h>
using namespace std;
int main()
{string s;
getline(cin,s);
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;}

3016设计一个简单的类

#include <iostream>
#include <cmath>
class CPoint
{
private:
 double x;
 double y;
public:
 void setXY(double x, double y);
 double returnx() { return x; }
 double returny() { return y; }
};
class Circle
{
private:
 double r;
public:
 CPoint center;
 double Dist(Circle p);
 double setR(double r);
 double relation(double d, Circle c2);
};
double Circle::Dist(Circle p)
{

 double d;
 d = sqrt((this->center.returnx() - p.center.returnx())*(this->center.returnx() - p.center.returnx()) + (this->center.returny() - p.center.returny())*(this->center.returny() - p.center.returny()));
 return d;

}
double Circle::setR(double r)
{
 this->r = r;
 return 0;
}
double Circle::relation(double l, Circle b)
{
 if (l > r + b.r)
  return 2;
 else if (l == r + b.r || l == fabs(r - b.r))
  return 1;
 else if (l<r + b.r&&l>fabs(r - b.r))
  return 4;
 else
  return 3;
}
void CPoint::setXY(double x, double y)
{
 this->x = x;
 this->y = y;
}
using namespace std;
int main()
{
 double x1, x2, y1, y2, r1, r2;
 Circle c1, c2;
 while (cin >> x1 >> y1 >> r1)
 {
  cin >> x2 >> y2 >> r2;
  c1.center.setXY(x1, y1);
  c2.center.setXY(x2, y2);
  c1.setR(r1);
  c2.setR(r2);
  double d = c1.Dist(c2);
  double back;
  back = c1.relation(d, c2);
  cout << back << endl;

 }
}

3846

 #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class CTriangle{
 private:
 int a,b,c;
 public:
    void Init(int a,int b,int c);
 double Area();
};
void CTriangle::Init(int a,int b,int c){
  this->a=a;
  this->b=b;
  this->c=c;
 }
double CTriangle::Area(){
  double  l=(a+b+c)/2.0;
        return sqrt(l*(l-a)*(l-b)*(l-c));
}

5232写构造函数析构函数什么的

#include <iostream>
using namespace std;
class X{
 public:
 X(){  
 puts("Constructor");
 }
 ~X(){  
 puts("Destructor");
 }
};
int main()
{
    X x[3];

    return 0;
}

#include<iostream>
using namespace std;
class X{
 private:
 int b;
 public:
 X(int a){
  b=a;
  printf("Constructor %d\n",b);
 }
 X(X &Y){
  b=Y.b;
  printf("Copy Constructor %d\n",b);
 }
    ~X(){
  puts("Destructor");
 }


};

 5233

 #include <iostream>
using namespace std;
class X{
 public:
 X(){  
 puts("Constructor");
 }
 ~X(){  
 puts("Destructor");
 }
};

 5234

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class CPoint{
 public:
 double x,y;
 CPoint(double x,double y){
  this->x=x;
  this->y=y;
 }
 double Dist(CPoint p){
 double d;
 d=sqrt((this->x - p.x)*(this->x - p.x) + (this->y - p.y)*(this->y - p.y));
 return d;} 
};

5236

#include<iostream>
using namespace std;
class X{
 private:
 int b;
 public:
 X(int a){
  b=a;
  printf("Constructor %d\n",b);
 }
 X(X &Y){
  b=Y.b;
  printf("Copy Constructor %d\n",b);
 }
    ~X(){
  puts("Destructor");
 }


};

5251

#include <iostream>
using namespace std;
class Point{
 public:
 int x,y;
 Point(int x,int y){
  this->x=x;
  this->y=y;
 }
};
class Circle{
   private:int x,y,r;
   public:Circle(Point a,int r){
       this->r=r;
       this->x=a.x;
       this->y=a.y;
   }
   int Contain(Point a){
       if((a.x-x)*(a.x-x)+(a.y-y)*(a.y-y)<r*r)
       return 1;
       else return 0;
   }
};

贴一下我写过的c++程序代码