• c++实践课(上课用)


    源程序:

    //编写一个程序,将从键盘输入的n个字符串保存在一个一维数组A中。在输入字符串之前,先输入n的值。
    //要求,数组A需要动态申请空间,程序运行结束前再释放掉。
    //再输出n字符串中最长和最短的串,计算平均字符中的长度。

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    int n, i, length = 0;
    cout << "输入几个字符串:";
    cin >> n;

    string* A = new string[n];
    string* p;
    p = A; //指针p指向字符串数组A

    cout << "请输入字符串:";
    for (i = 0; i < n; i++)
    {
    cin >> A[i];
    length = length + A[i].length(); //n个字符串的总长度
    }

    p = A; //让p重新指向字符串数组的首地址

    string max = A[0];
    string min = A[0];
    for (i = 1; i < n; i++)
    {
    if (p[i].length() > max.length())
    max = p[i];
    if (p[i].length() < min.length())
    min = p[i];
    }

    cout << "最长的字符串为:" << max << "\n" << "最短的字符串为:" << min << endl;
    cout << "字符串的平均长度为:" << length / n << endl;

    delete[]p;

    return 0;
    }

    --------------------------------------------------------------------------------------------------------------------

    //函数重载
    #include <iostream>
    using namespace std;

    int bigger(int x, int y)
    {
    if (x > y)
    return x;
    else
    return y;
    }

    float bigger(float x, float y)
    {
    if (x > y)
    return x;
    else
    return y;
    }

    double bigger(double x, double y)
    {
    if (x > y)
    return x;
    else
    return y;
    }

    int main()
    {
    int xI = 10, yI = 20;
    float xF = 30, yF = 40;
    double xD = 50, yD = 60;
    cout << bigger(xI, yI) << endl;
    return 0;
    }

    ------------------------------------------------------------------------

    //类内声音,类外定义
    #include <iostream>
    using namespace std;

    class A
    {
    public:
    int fun(double); //函数的声明
    int fun(int);
    };

    int A::fun(double x)
    {
    return (int)x / 2;
    }

    int A::fun(int x)
    {
    return x * 2;
    }

    int main()
    {
    A a;
    int s = a.fun(6.0) + a.fun(2);
    cout << s << endl;
    return 0;
    }

    -------------------------------------------------------------------------

    #include <iostream>
    using namespace std;

    int main()
    {
    int* p1;
    int** p2 = &p1;
    int b = 20;
    p1 = &b;
    cout << *(*p2) << endl;
    return 0;
    }

    ----------------------------------------------------------------------------

    #include<iostream>
    #include<string>
    using namespace std;

    class Test{
    int a, b;
    int getmin()
    {
    return (a < b ? a : b);
    }
    public:
    int c;
    void setValue(int x1, int x2, int x3){
    a = x1;
    b = x2;
    c = x3;
    }
    int GetMin();
    };
    int Test::GetMin(){
    int d = getmin();
    return (d = d < c ? d : c);
    }
    int main(){
    Test t1;
    t1.setValue(34, 6, 2);
    cout <<"最小值为:"<< t1.GetMin() << endl;
    system("pause");
    return 0;
    }

    --------------------------------------------------------------------------

    #include <iostream>
    using namespace std;

    class myComplex
    {
    private:
    double real, imag; //复数的实部和虚部
    public:
    myComplex(); //缺省的构造函数
    myComplex(double r, double i);
    friend myComplex addCom(myComplex c1, myComplex c2); //友元函数,复数相加
    friend void outCom(myComplex c); //输出函数
    };

    myComplex::myComplex()
    {
    real = 0;
    imag = 0;
    }
    myComplex::myComplex(double r, double i)
    {
    real = r;
    imag = i;
    }

    myComplex addCom(myComplex c1, myComplex c2)
    {
    return myComplex(c1.real + c2.real, c1.imag + c2.imag);
    }

    void outCom(myComplex c)
    {
    cout << "(" << c.real << "," << c.imag << ")";
    }

    int main()
    {
    myComplex c1(1, 2), c2(3, 4), result;
    result = addCom(c1, c2);
    outCom(c1);
    cout << "+";
    outCom(c2);
    cout << "=";
    outCom(result);
    cout << endl;
    return 0;
    }

    -----------------------------------------------------------------------------------------

    #include <iostream>
    using namespace std;

    class Point
    {
    private:
    double x, y; //横坐标,纵坐标
    friend class Line; //友元类
    public:
    Point(double i = 0,double j = 0)
    {
    x = i;
    y = j;
    }
    Point(Point& p) //复制的构造函数
    {
    x = p.x;
    y = p.y;
    }
    };

    class Line
    {
    private:
    Point p1, p2;
    public:
    Line(Point& xp1, Point& xp2) :p1(xp1), p2(xp2)
    {}
    double GetLength();
    };

    double Line::GetLength()
    {
    double dx = p2.x - p1.x;
    double dy = p2.y - p1.y;
    return sqrt(dx*dx+dy*dy);
    }
    int main()
    {
    Point p1, p2(6,8);
    Line L1(p1, p2);
    cout << L1.GetLength() << endl;
    return 1;
    }

    ---------------------------------------------------------------------

    #include <iostream>
    using namespace std;

    class Point
    {
    private:
    float x;
    public:
    void f(float a)
    {
    x = a;
    }
    void f()
    {
    x = 0;
    }
    friend float max(Point& a, Point& b); //程序填空
    };

    float max(Point& a, Point& b)
    {
    return (a.x > b.x) ? a.x : b.x;
    }
    int main()
    {
    Point a, b;
    a.f(2.2);
    b.f(3.3);
    cout << max(a, b) << endl; //程序填空
    return 0;
    }

    ----------------------------------------------------------------------------

    //下面程序定义了类Test,num的初值是5,程序创建对象t,且修改其num的值为10,使用show()函数
    //输出num的值,请将程序补充完整.
    #include <iostream>
    using namespace std;

    class Test
    {
    private:
    static int num; //静态变量,应该在类的外面,由类来初始化
    public:
    Test(int);
    void show();
    };

    int Test::num = 5; //程序填空
    Test::Test(int n)
    {
    num = n;
    }

    void Test::show()
    {
    cout << num<<endl;
    }

    int main()
    {
    Test t(10);
    t.show(); //程序填空
    return 0;
    }
     
     
     
  • 相关阅读:
    The Future of Middleware and the BizTalk Roadmap
    FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
    Strips illegal Xml characters
    luogu P2280 激光炸弹(二维前缀和)
    luogu P2704 炮兵阵地(经典状态压缩DP)
    SP1716 GSS3 Can you answer these queries III (线段树维护最大连续子段和)
    二分图判定、匹配问题
    C++语法综合 | 基于char*设计一个字符串类MyString
    luogu P1044 火车进出栈问题(Catalan数)
    C++设计模式 | 三种设计模式基础
  • 原文地址:https://www.cnblogs.com/duanqibo/p/16440147.html
Copyright © 2020-2023  润新知