贴一下我写过的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;
   }
};

时间: 2024-08-08 13:58:19

贴一下我写过的c++程序代码的相关文章

如何阅读他人的程序代码

近日,在互联网上游荡,偶然发现一篇曾经的文章,是关于如何阅读他人程序代码的,阅后颇为受益,于是乎重新整理了一下格式,将此文转载如下: 如何阅读他人的程序代码 文/王建兴   作者简介: 王建兴,清华大学资讯工程系的博士研究生,研究兴趣包括计算机网络.点对点网络.分布式网络管理.以及行动式代理人,专长则是Internet应用系统的开发.曾参与过的开发项目性质十分广泛而且不同,从ERP.PC Game到P2P网络电话都在他的涉猎范围之内. 一.读懂程序代码,使心法皆为我所用 程序代码是别人写的,只有

debian下烧写stm32f429I discovery裸机程序

需要安装openocd软件.如果已安装默认的openocd,需要先卸载系统默认的openocd(默认版本是0.5.0,版本太低),然后再安装. 在安装前需要安装libusb库文件: sudo apt-get install libusb-1.0-0-dev libusb-1.0-0 然后执行安装: git clone git://git.code.sf.net/p/openocd/code openocd cd openocd ./bootstrap ./configure --prefix=/

Python写的网络爬虫程序(很简单)

Python写的网络爬虫程序(很简单) 这是我的一位同学传给我的一个小的网页爬虫程序,觉得挺有意思的,和大家分享一下.不过有一点需要注意,要用python2.3,如果用python3.4会有些问题出现. python程序如下: import re,urllib strTxt="" x=1 ff=open("wangzhi.txt","r") for line in ff.readlines(): f=open(str(x)+".txt&

好久没写关于论坛的程序了

最近发现有个很牛逼的论坛,里面有个插件可以直接让支付宝即时付款,具体怎么实现的没看明白,好像是叫一品楼吧,有个收款的地方用的支付宝,但是可以直接选择自动提交付款人信息和付款的金额到支付宝的即时支付界面,好像很高端的样子,还可以提交银行卡号哦,真心很牛逼.求高人解答 好久没写关于论坛的程序了,布布扣,bubuko.com

幸福村站——成都传智播客程序员写出你的烧烤代码

又是一个阳光明媚,风和日丽之天,如果作为程序员的你还在键盘上苦苦的想着下一串代码该怎么写的话,那你就弱爆了.俗语说得好,学习要劳逸结合,写代码更是需要清晰的思维,在传智播客Java基础班开班一个月后,班主任决定带着这群"猿猴们"去传说中的"幸福村"放松放松,我们也跟着一起去感受程序员们的烧烤代码的幸福吧! 带着好奇的心理走进了"幸福梅林站",一个又一个的农家乐园开始浮现在我们眼前,那里朴素的民风和美丽的风景让我们暂时忘却了学习上的烦恼和城市里的喧

4年前的随笔---写出高质量程序的要点

从1990年開始敲代码.到如今已经快20年了.总结出写出高质量程序的几个要点: - 1.開始写之前思路越清晰完整越好. - 2.写的过程中代码一定要规范一致,这种代码便于维护和改动.这个规范一致性包括名称.格式.算法等.- 3.发现一处错误,马上回忆有没有可能其他地方具有相同的错误(假设你遵循第2条.就能非常快找到). - 4.多用ASSERT,在我的代码里面,这条语句至少占领了程序总量的1/10. - 5.每写完一段后至少重复看3遍.非常多BUG是非常难用调试器找出来的. - 通过遵循以上规则

读文章《写给未来的程序媛》有感

写给未来的程序媛 原文链接地址:http://kb.cnblogs.com/page/556770/ 原文中:对程序员的工作,环境,和生活进行了描述,读完感触很深! 1,对于成功的描述,,,成功的三要素,1,坚持:  2,不要脸:  3,坚持不要脸. 确实,不管干啥,都需要积累,都要坚持,,而不要脸,是说你要不耻下问,不要太在乎颜面(当然指是在你自己尝试解决多次也解决不了的情况),而坚持不要脸,是说的一种境界,是形容我们要怀虚若谷,要谦逊,今天你不会,明天你就会了,下次再碰到类似的情况,你就可以

隐藏你写的程序代码

[ 转自http://blog.csdn.net/zhongguomao/article/details/9111135 ] 如何隐藏你写的程序代码 不管是出于什么样的原因, 你偶尔会想到把自己写的程序代码隐藏, 当然, 这些代码一定不是项目中用到的, 比如你自己写的小程序 SAP提供隐藏代码功能, 但是需要注意的是, 代码一旦被隐藏, 则终生无法恢复, so, 在做这个隐藏代码的动作前, 一定要备份好你自己的代码, 切记切记. 1. 撰写插入隐藏码的程序 *&-----------------

怎样写出一个递归程序

作为小白,我看到递归程序只是能看懂,但是自己写不出来,我知道要有一个临界条件(这个并不难找),但我不知道怎么演进,这让我十分头疼,因此找到了一篇个人认为写的不错的文章如下,根据我对递归的理解和疑问对原文做了一些标注,欢迎各位大佬,写下自己对递归的理解,本小白感激不尽. 如何写一个递归程序 总是听到大大们说递归递归的,自己写程序的时候却用不到递归.其中的原因,一个是害怕写递归,另一个就是不知道什么时候用递归.这篇文章就浅析一下,希望看完之后不再害怕递归,这就是本文最大的目的. 递归到底有什么意义?