YXTS122 2016-10-04 03:26 采纳率: 100%
浏览 1204
已采纳

c++基础不好,谁帮我改改

#include<iostream>
using namespace std;
class Array
{
private:
int *a; int l;
public:
Array()
{
a=NULL;
l=0;
}
Array(int x)
{
int c=0;
a=new int[x];
while (c<x)
{
cin>>a[c];
cin.get();
c++;
}
l=x;
}
~Array()
{
delete []a;
}
void datain(int f)
{
a[l-1]=f;
}
friend int big(Array& s,Array& t)
{
if (s.l>=t.l)
return s.l;
else
return t.l;
}
void operator=(Array &p)
{
l=p.l;
a=new int[l];
for (int j=0;j<l;j++)
a[j]=p.a[j];
}

 Array(Array &p)
 {
    l=p.l;
    a=new int[l];
    for (int j=0;j<l;j++)
         a[j]=p.a[j];
 }

 friend Array operator+(Array& s,Array& t)
 {
    Array r;
    int BIG=big(s,t);
    r.l=BIG;
    r.a=new int[l];
    for (int i=0;i<BIG;i++)
       r.a[i]=s.a[i]+t.a[i];
    return r;
 }
 int average()
 {
    int all=0,i;
    for (i=0;i<l;i++)
        all+=a[i];
    return all/l;
 }
 void show()
 {
    for (int i=0;i<l;i++)
        cout<<a[i]<<" ";
 }

};
int main()
{
Array A1(3);
A1.show();
cout<<endl;
A1.datain(6);
A1.show();
cout<<endl;
cout<<A1.average()<<endl;
Array B(3);
Array C;
C=A1+B;
C.show();
return 0;
}图片

  • 写回答

3条回答 默认 最新

  • YXTS122 2016-10-05 01:44
    关注

    #include<iostream>
    using namespace std;
    class Array
    {
    private:
    int *a; int l;
    public:
    Array()
    {
    a=NULL;
    l=0;
    }
    Array(int x)
    {
    int c=0;
    a=new int[x];
    while (c<x)
    {
    cin>>a[c];
    cin.get();
    c++;
    }
    l=x;
    }
    ~Array()
    {
    delete []a;
    }
    void datain(int f)
    {
    a[l-1]=f;
    }
    friend int big(Array& s,Array& t)
    {
    if (s.l>=t.l)
    return s.l;
    else
    return t.l;
    }
    Array& operator=(const Array &p)
    {
    l=p.l;
    a=new int[l];
    for (int j=0;j<l;j++)
    a[j]=p.a[j];
    return *this;
    }

     Array(Array &p)
     {
        l=p.l;
        a=new int[l];
        for (int j=0;j<l;j++)
             a[j]=p.a[j];
     }
    
     friend Array operator+(Array& s,Array& t)
     {
        Array r;
        int BIG=big(s,t);
        r.l=BIG;
        r.a=new int[BIG];
        for (int i=0;i<BIG;i++)
           r.a[i]=s.a[i]+t.a[i];
        return r;
     }
     int average()
     {
        int all=0,i;
        for (i=0;i<l;i++)
            all+=a[i];
        return all/l;
     }
     void show()
     {
        for (int i=0;i<l;i++)
            cout<<a[i]<<" ";
     }
    

    };
    int main()
    {
    Array A1(3);
    A1.show();
    cout<<endl;
    A1.datain(6);
    A1.show();
    cout<<endl;
    cout<<A1.average()<<endl;
    Array B(3);
    Array C;
    C=A1+B;
    C.show();
    return 0;
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型