2000行之C++基础练习

#include<iostream>
using namespace std;
enum GameResult{WIN,LOSE,TIE,CANCEL};
int main(){
    GameResult result;
    enum GameResult omit = CANCEL;
    for(int count = WIN;count <= CANCEL;count++){
        result = GameResult(count);
        if(result = omit)
            cout<<"The game was cancelled"<<endl;
        else{
            cout<<"The game was played"<<endl;
            if(result == WIN)
                cout<<"and we won!";
            if(result == LOSE)
                cout<<"and we lost";
            cout<<endl;
        }
    }
    return 0;
}
#include<iostream>
using namespace std;
class clock{
public:
    void setTime(int newH = 0, int newM = 0, int newS = 0);
    void showTime();
private:
    int hour,minute,second;
};
void clock::setTime(int newH, int newM, int newS){
    hour = newH;
    minute = newM;
    second = newS;
}
inline void clock::showTime(){
    cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main(){
    clock myclock;
    cout<<"First time set and output"<<endl;
    myclock.setTime();
    myclock.showTime();
    return 0;
}
#include<iostream>
using namespace std;
const float PI = 3.141593;
const float F = 35;
const float C = 20;
class circle1{
public:
    circle1(float r);
    float circumference();
    float area();
private:
    float radius;
};
circle1::circle1(float r){
    radius = r;
}
float circle1::circumference(){
    return 2*PI*radius;
}
float circle1::area(){
    return PI*radius*radius;
}
int main(){
    float radius;
    cout<<"Enter the radius of the pool:"<<endl;
    cin>>radius;
    circle1 pool(radius);
    circle1 poolR(radius + 3);
    float fenceCost = poolR.circumference()*F;
    cout<<fenceCost<<endl;
    float concreteCost = (poolR.area() - pool.area())*C;
    cout<<concreteCost<<endl;
    return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
class point{
public:
    point(int xx = 0, int yy = 0){
        x = xx;
        y = yy;
    }
    point(point &p);
    int getX(){return x;}
    int getY(){return y;}
private:
    int x,y;
};
point::point(point &p){
    x = p.x;
    y = p.y;
    cout<<"copy constructor of point"<<endl;
}
class line{
public:
    line(point xp1, point xp2);
    line(line &l);
    double getLen(){return len;}
private:
    point p1,p2;
    double len;
};
line::line(point xp1, point xp2):p1(xp1),p2(xp2){
    cout<<"c of line"<<endl;
    double x = static_cast<double>(p1.getX() - p2.getX());
    double y = static_cast<double>(p1.getY() - p2.getY());
    len = sqrt(x*x + y*y);
}
line::line(line &l):p1(l.p1),p2(l.p2){
    cout<<"copy c of line"<<endl;
    len = l.len;
}
int main(){
    point myp1(1,1),myp2(4,5);
    line line1(myp1,myp2);
    line line2(line1);
    cout<<line1.getLen()<<line2.getLen()<<endl;
    return 0;
}
#include<iostream>
using namespace std;
 class point {
 public:
     point (int x = 0, int y = 0):x(x),y(y){
        count++;
     }
     point (point &p){
        x = p.x;
        y = p.y;
        count++;
     }

     ~point(){count--;}
     int getX(){
        return x;
     }
     int getY(){return y;}
     void showCount(){cout<<count<<endl;}
 private:
    int x,y;
    static int count;
 };
 int point::count = 0;
 int main(){
    point a(4,5);
    cout<<a.getX()<<a.getY()<<endl;
    a.showCount();
    point b(a);
    b.showCount();
    return 0;
 }

#include<iostream>
using namespace std;
 class point {
 public:
     point (int x = 0, int y = 0):x(x),y(y){
        count++;
     }
     point (point &p){
        x = p.x;
        y = p.y;
        count++;
     }

     ~point(){count--;}
     int getX(){
        return x;
     }
     int getY(){return y;}
     static void showCount(){cout<<count<<endl;}
 private:
    int x,y;
    static int count;
 };
 int point::count = 0;
 int main(){
    point a(4,5);
    cout<<a.getX()<<a.getY()<<endl;
    point::showCount();
    point b(a);
    point::showCount();
    return 0;
 }
时间: 2024-09-29 20:36:54

2000行之C++基础练习的相关文章

天才黑客!17岁打脸乔布斯,20岁搞疯索尼,26岁叫板特斯拉,写2000行代码市值8000万美金

文/创日报 今天说一个屌到飞起的黑客小哥! 扒他的时候创哥就觉得无比的欢快, 因为他的人生用三个字就能概括: “爽!爽!爽!” 不但颜值出众,智商更是卓绝! 89年出生,却把互联网巨头玩弄于股掌间, 17岁仅靠一把螺丝刀,破解了iPhone核心基带, 让iPhone变成了全网通. ▼ 无数人削尖脑袋都挤不进去的 “Google,SpaceX,Facebook” 人家拍拍屁股混了一圈, 说不干就不干! 因为觉得人生太无聊, 顺手破解了索尼的游戏机, 逼的索尼将他告上法庭, 还引发了世!界!级!黑客

程序员写 2000 行 if else?领导:这个锅我不背

前言 知乎上有小伙伴提了这么一个问题,如何看待陕西省普通话水平测试成绩查询系统?查询系统前端代码就直接给出了身份账号,姓名,证书编号,如果信息是真的,就泄露了这么多考生的信息,白给那种.为什么会发生这样的事情?事情的始末是什么? 证据 很多机智的小伙伴都打开了网址一探究竟,小编也不敢怠慢赶紧瞅瞅这牛逼的网站到底长什么样子. 看着的确有模有样,一股80年代的复古风格,赶紧拿出 F12 神器看一遍究竟哪位程序员写出如此神奇的逻辑代码. 点开层层结构,找到 <script>,卧槽还有这等神逻辑,本地

行测(基础篇)之汉语语法与阅读习惯梳理

一.词性和语法结构 1.词性 实词(具有实际意义的词):名词.动词.形容词.数词.量词.代词. 虚词(没有具体意义的词):副词(很.非常).连词.介词(在.于).助词(的).叹词.拟声词. 2.语法结构 主语(状语)谓语 (补语) (定语) 宾语    傅园慧使出洪荒之力.(主谓宾)    "傅园慧(真的)使出(了)(自己的)洪荒之力" 主语:名词.代词 谓语:动词.形容词 宾语:名词.代词 定语:形容词.名词.代词.数量词 状语:副词.形容词 补语:形容词.动词 3.例题 二.易混淆

2000行之Qt简化写字板

//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> namespace Ui { class MainWindow; } class QLineEdit; class QDialog; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(Q

2000行之widget练习之二

//mydialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> namespace Ui { class MyDialog; } class MyDialog : public QDialog { Q_OBJECT public: explicit MyDialog(QWidget *parent = 0); ~MyDialog(); private slots: void on_pushButton_clicked(

2000行之mother、father、child

//child.h #ifndef CHILD_H #define CHILD_H #include <string> using namespace std; class Mother; class Father; class Child { public: Mother *mama; Father *baba; Child(); string name; void answer(); void callParent(); }; #endif // CHILD_H //child.cpp #

2000行之句柄

#include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK); return 0; } #include <windows.h> const char g_

2000行之宏中#和##的区别

#include<stdio.h> #define Fun(a,b) a##b int main() { x='H'; y='W'; printf("%s",Fun(x,y)); return; } #include<stdio.h> #define Var(x) var##x int main() { int Var(1)=1,Var(2)=2,Var(3)=3; printf("var1=%d\n",var1); printf("

2000行之widget练习

//mywidget.h #ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> #include <QWizard> namespace Ui { class MyWidget; } class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); ~MyWidget(); private slots: vo