c++二级——大题


// proj1.cpp
#include <iostream>
using namespace std;

class MyClass {
public:
   MyClass(int len) 
   { 
      array = new int[len];
      arraySize = len;
      for(int i = 0; i < arraySize; i++)
         array[i] = i+1;
   }

   ~MyClass()
   {
// ERROR   **********found**********
       delete array[i];
   }

   void Print() const
   {
      for(int i = 0; i < arraySize; i++)
// ERROR   **********found**********
         cin << array[i] << ' ';

       cout << endl;
    }
private:
   int *array;
   int arraySize;
};
int main()
{
// ERROR   **********found**********
   MyClass obj;

   obj.Print();
   return 0;
}

修改:
// proj1.cpp
#include <iostream>
using namespace std;

 

class MyClass {
public:
   MyClass(int len) 
   { 
      array = new int[len];
      arraySize = len;
      for(int i = 0; i < arraySize; i++)
         array[i] = i+1;
   }

   ~MyClass()
   {
// ERROR   **********found**********
       delete [] array;
   }

   void Print() const
   {
      for(int i = 0; i < arraySize; i++)
// ERROR   **********found**********
         cout << array[i] << ' ';

       cout << endl;
    }
private:
   int *array;
   int arraySize;
};
int main()
{
// ERROR   **********found**********
   MyClass obj(10);

   obj.Print();
   return 0;
}

输出:


#include <iostream>
using namespace std;

class Array {
public:
  Array(unsigned int s)
  {
    size = s;
    num = 0;
    a = new int[s];
  }
    
  virtual ~Array() { delete[] a;    }
    
  virtual void add(int e)
  {
    if (num < size) {
      //**********found**********
      ___________
      num++;
    }
  }
    
  int get(unsigned int i) const
  {
    if (i < size)
      return a[i];
    return 0;
  }
    
protected:
  int *a;
  unsigned int size, num;
};

class SortedArray : public Array {
public:
  //**********found**********
  SortedArray(unsigned int s) : _______________ { }
    
  virtual void add(int e)
  {
    if (num >= size)
      return;
    
    int i=0, j;
    while (i < num) {
      if (e < a[i]) {
        for (j = num; j > i; j--) {
          //**********found**********
          _______________;
 
      }
        //**********found**********
        _______________;
 
      break;
      }
      i++;
    }
    
    if (i == num)
      a[i] = e;
    num++;
  }
};

void fun(Array& a)
{
  int i;
  for (i = 10; i >= 1; i--) {
    a.add(i);
  }
  for (i = 0; i < 10; i++) {
    cout << a.get(i) << ", ";
  }
  cout << endl;
}

int main()
{
  Array a(10);
  fun(a);
    
  SortedArray sa(10);
  fun(sa);
    
  return 0;
}                 

 修改
#include <iostream>
using namespace std;

class Array {
public:
  Array(unsigned int s)
  {
    size = s;
    num = 0;
    a = new int[s];
  }
    
  virtual ~Array() { delete[] a;    }
    
  virtual void add(int e)
  {
    if (num < size) {
      //**********found**********
      a[num] = e;
      num++;
    }
  }
    
  int get(unsigned int i) const
  {
    if (i < size)
      return a[i];
    return 0;
  }
    
protected:
  int *a;
  unsigned int size, num;
};

class SortedArray : public Array {
public:
  //**********found**********
  SortedArray(unsigned int s) : Array(s) { }
    
  virtual void add(int e)
  {
    if (num >= size)
      return;
    
    int i=0, j;
    while (i < num) {
      if (e < a[i]) {
        for (j = num; j > i; j--) {
          //**********found**********
          a[j] = a[j-1];
        }
        //**********found**********
        a[i] = e;
        break;
      }
      i++;
    }
    
    if (i == num)
      a[i] = e;
    num++;
  }
};

void fun(Array& a)
{
  int i;
  for (i = 10; i >= 1; i--) {
    a.add(i);
  }
  for (i = 0; i < 10; i++) {
    cout << a.get(i) << ", ";
  }
  cout << endl;
}

int main()
{
  Array a(10);
  fun(a);
    
  SortedArray sa(10);
  fun(sa);
    
  return 0;
}

输出:


// proj3.cpp
#include<iostream>
using std::ostream;
using std::cout;
using std::endl;

class MyVector{    //表示二维向量的类
    double x;      //X坐标值
    double y;      //Y坐标值
public:
    MyVector(double i=0.0 , double j=0.0);                //构造函数
    MyVector operator+( MyVector j);                      //重载运算符+
    friend MyVector operator-( MyVector i, MyVector j);   //重载运算符-
    friend ostream& operator<<( ostream& os, MyVector v); //重载运算符<<
};
//**1** **********found**********
___________________(double i , double j): x(i),y(j){}

MyVector MyVector::operator+( MyVector j) {
    return  MyVector(x+j.x, y+j.y);
}

MyVector operator-( MyVector i, MyVector j)
{//**2** **********found**********
    return  MyVector(__________________);
}

ostream& operator<<( ostream& os, MyVector v){
    os << '(' << v.x << ',' << v.y << ')' ;          //输出向量v的坐标
    return os;
}

int main()

    MyVector v1(2,3), v2(4,5), v3;
//**3** **********found**********
    v3=___________;
    cout<<v3<<endl;
    return 0;
}

修改:
// proj3.cpp
#include<iostream>
using std::ostream;
using std::cout;
using std::endl;

class MyVector{    //表示二维向量的类
    double x;      //X坐标值
    double y;      //Y坐标值
public:
    MyVector(double i=0.0 , double j=0.0);                //构造函数
    MyVector operator+( MyVector j);                      //重载运算符+
    friend MyVector operator-( MyVector i, MyVector j);   //重载运算符-
    friend ostream& operator<<( ostream& os, MyVector v); //重载运算符<<
};
//**1** **********found**********
MyVector::MyVector(double i , double j): x(i),y(j){}

MyVector MyVector::operator+( MyVector j) {
    return  MyVector(x+j.x, y+j.y);
}

MyVector operator-( MyVector i, MyVector j)
{//**2** **********found**********
    return  MyVector(i.x - j.x, i.y - j.y);
}

ostream& operator<<( ostream& os, MyVector v){
    os << '(' << v.x << ',' << v.y << ')' ;          //输出向量v的坐标
    return os;
}

int main()

    MyVector v1(2,3), v2(4,5), v3;
//**3** **********found**********
    v3=v1 + v2;
    cout<<v3<<endl;
    return 0;
}

// proj1.cpp
#include <iostream>
using namespace std;

class MyClass {
public:
// ERROR  **********found********** 
    void MyClass(int i)
    { value = i; cout << "Constructor called." << endl; }

    int Max(int x, int y) { return x>y ? x : y; }     // 求两个整数的最大值

// ERROR  **********found**********                  
    int Max(int x, int y, int z = 0)       // 求三个整数的最大值
    {
        if (x > y)
            return x>z ? x : z;
        else
            return y>z ? y : z;
    }

    int GetValue() const { return value; }

    ~MyClass() { cout << "Destructor called." << endl; }

private:
    int value;
};

int main()
{
    MyClass obj(10);

// ERROR  **********found**********
    cout<< "The value is "<< value()<< endl;
    cout << "Max number is " << obj.Max(10,20) << endl;
    return 0;
}

 修改:


// proj1.cpp
#include <iostream>
using namespace std;

class MyClass {
public:
// ERROR  **********found********** 
    MyClass(int i)
    { value = i; cout << "Constructor called." << endl; }

    int Max(int x, int y) { return x>y ? x : y; }     // 求两个整数的最大值

// ERROR  **********found**********                  
    int Max(int x, int y, int z)       // 求三个整数的最大值
    {
        if (x > y)
            return x>z ? x : z;
        else
            return y>z ? y : z;
    }

    int GetValue() const { return value; }

    ~MyClass() { cout << "Destructor called." << endl; }

private:
    int value;
};

int main()
{
    MyClass obj(10);

// ERROR  **********found**********
    cout<< "The value is "<< obj.GetValue()<< endl;
    cout << "Max number is " << obj.Max(10,20) << endl;
    return 0;
}

输出:

//proj2.cpp
#include <iostream>
using namespace std;
const int Size=5;
class Stack;
class Item
{
public:
//********found********
  Item(const int& val):__________ {}
                //构造函数 对item进行初始化
private:
  int item;
  Item* next;
  friend class Stack;
};
class Stack
{
public:
  Stack():top(NULL) {}
  ~Stack();
  int Pop();
  void Push(const int&);
private:
  Item *top;
};
Stack::~Stack()
{
  Item *p=top,*q;
  while(p!=NULL)
  {
    q=p->next;
//********found********
    __________;
            //释放p所指向的结点
    p=q;
  }
}
int Stack::Pop()
{
  Item* temp;
  int ret;
//********found********
  __________;
            //使temp指向栈顶结点
  ret=top->item;
  top=top->next;
  delete temp;
  return ret;
}
void Stack::Push(const int& val)
{
  Item* temp=new Item(val);
//********found********
  __________;
            //使新结点的next指针指向栈顶数据
  top=temp;
}
int main()
{
  Stack s;
  int i;
  for( i=1;i<Size;i++)
    s.Push(i);
  cout<<"The element of stack are: ";
  for(i=1;i<Size;i++)
    cout<<s.Pop()<<'\t';
  return 0;
}

修改:

 //proj2.cpp
#include <iostream>
using namespace std;
const int Size=5;
class Stack;
class Item
{
public:
//********found********
  Item(const int& val):item(val) {}
                //构造函数 对item进行初始化
private:
  int item;
  Item* next;
  friend class Stack;
};
class Stack
{
public:
  Stack():top(NULL) {}
  ~Stack();
  int Pop();
  void Push(const int&);
private:
  Item *top;
};
Stack::~Stack()
{
  Item *p=top,*q;
  while(p!=NULL)
  {
    q=p->next;
//********found********
    delete [] p;
            //释放p所指向的结点
    p=q;
  }
}
int Stack::Pop()
{
  Item* temp;
  int ret;
//********found********
  temp = top;
            //使temp指向栈顶结点
  ret=top->item;
  top=top->next;
  delete temp;
  return ret;
}
void Stack::Push(const int& val)
{
  Item* temp=new Item(val);
//********found********
  temp->next = top;
            //使新结点的next指针指向栈顶数据
  top=temp;
}
int main()
{
  Stack s;
  int i;
  for( i=1;i<Size;i++)
    s.Push(i);
  cout<<"The element of stack are: ";
  for(i=1;i<Size;i++)
    cout<<s.Pop()<<'\t';
  return 0;


// proj3.cpp
#include<iostream>
using namespace std;
class MyPoint{   //表示平面坐标系中的点的类
  double x;
  double y;
public:
  MyPoint (double x,double y){this->x=x;this->y=y;}
  double getX()const{ return x;}
  double getY()const{ return y;}
  void show()const{ cout<<'('<<x<<','<<y<<')';}
};
class MyRectangle{         //表示矩形的类
  MyPoint up_left;         //矩形的左上角顶点
  MyPoint down_right;      //矩形的右下角顶点
public:
  MyRectangle(MyPoint upleft,MyPoint downright);
  MyPoint getUpLeft()const{ return up_left;}         //返回左上角坐标
  MyPoint getDownRight()const{ return down_right;}   //返回右下角坐标
  MyPoint getUpRight()const;                         //返回右上角坐标
  MyPoint getDownLeft()const;                        //返回左下角坐标
  double area()const;                                //返回矩形的面积
};
//**1** **********found**********
MyRectangle::MyRectangle(_____________________): 
                     up_left(p1),down_right(p2){}

 

MyPoint MyRectangle::getUpRight()const
{
  return MyPoint(down_right.getX(),up_left.getY());
}

MyPoint MyRectangle::getDownLeft()const
{//**2** **********found**********
  return MyPoint(___________________________);
}
//**3** **********found**********
___________________area()const
{
  return (getUpLeft().getX()-getDownRight().getX())*
         (getDownRight().getY()-getUpLeft().getY());
}

int main( )
{
  MyRectangle r(MyPoint(0,2),MyPoint(2,0));
  r.getUpLeft().show();
  r.getUpRight().show();
  r.getDownRight().show();
  r.getDownLeft().show();
  cout<<r.area()<<endl;
  return 0;
}

修改:

 

// proj3.cpp
#include<iostream>
using namespace std;
class MyPoint{   //表示平面坐标系中的点的类
  double x;
  double y;
public:
  MyPoint (double x,double y){this->x=x;this->y=y;}
  double getX()const{ return x;}
  double getY()const{ return y;}
  void show()const{ cout<<'('<<x<<','<<y<<')';}
};
class MyRectangle{         //表示矩形的类
  MyPoint up_left;         //矩形的左上角顶点
  MyPoint down_right;      //矩形的右下角顶点
public:
  MyRectangle(MyPoint upleft,MyPoint downright);
  MyPoint getUpLeft()const{ return up_left;}         //返回左上角坐标
  MyPoint getDownRight()const{ return down_right;}   //返回右下角坐标
  MyPoint getUpRight()const;                         //返回右上角坐标
  MyPoint getDownLeft()const;                        //返回左下角坐标
  double area()const;                                //返回矩形的面积
};
//**1** **********found**********
MyRectangle::MyRectangle(MyPoint p1,MyPoint p2): 
                     up_left(p1),down_right(p2){}

MyPoint MyRectangle::getUpRight()const
{
  return MyPoint(down_right.getX(),up_left.getY());
}

MyPoint MyRectangle::getDownLeft()const
{//**2** **********found**********
  return MyPoint(up_left.getX(),up_left.getY());
}
//**3** **********found**********
double MyRectangle::area()const
{
  return (getUpLeft().getX()-getDownRight().getX())*
         (getDownRight().getY()-getUpLeft().getY());
}

int main( )
{
  MyRectangle r(MyPoint(0,2),MyPoint(2,0));//MyPoint(0,2) -> 左上角顶点 MyPoint(2,0) -> 右上角顶点
  r.getUpLeft().show();
  r.getUpRight().show();
  r.getDownRight().show();
  r.getDownLeft().show();
  cout<<r.area()<<endl;
  return 0;
}

// proj1.cpp
#include <iostream>
using namespace std;

class MyClass {
    int value;
public:
// ERROR  ********found********
  void MyClass(int val) : value(val) {}
  int GetValue() const { return value; }
  void SetValue(int val);
};

// ERROR  ********found********
inline void SetValue(int val) { value = val; }

int main()
{
  MyClass obj(0);
  obj.SetValue(10);
// ERROR  ********found******** 下列语句功能是输出obj的成员value的值
  cout << "The value is " << obj.value << endl;
  return 0;
}

修改:


// proj1.cpp
#include <iostream>
using namespace std;

class MyClass {
    int value;
public:
// ERROR  ********found********
  MyClass(int val) : value(val) {}
  int GetValue() const { return value; }
  void SetValue(int val);
};

// ERROR  ********found********
inline void MyClass::SetValue(int val) { value = val; }

int main()
{
  MyClass obj(0);
  obj.SetValue(10);
// ERROR  ********found******** 下列语句功能是输出obj的成员value的值
  cout << "The value is " << obj.GetValue() << endl;
  return 0;
}

 

 // proj2.cpp
#include <iostream>
using namespace std;
class Stack {
public:
  virtual void push(char c) = 0;
  virtual char pop() = 0;
};

class ArrayStack : public Stack {
  char * p;
  int maxSize;
  int top;
public:
  ArrayStack(int s)
  {
    top = 0;
    maxSize = s;
//********found********
    p = __________;
  }
  ~ArrayStack()
  {
//********found********
    __________;
  }
  void push(char c)
  {
    if (top == maxSize) {
      cerr << "Overflow!\n";
      return;
    }
//********found********
    __________;
    top++;
  }
  char pop()
  {
    if (top == 0) {
      cerr << "Underflow!\n";
      return '\0';
    }
    top--;
//********found********
    __________;
  }
};
void f(Stack& sRef)
{
  char ch[] = {'a', 'b', 'c'};
  cout << ch[0] << ", " << ch[1] << ", " << ch[2] << endl;
  sRef.push(ch[0]); sRef.push(ch[1]); sRef.push(ch[2]);
  cout << sRef.pop() << ", ";
  cout << sRef.pop() << ", ";
  cout << sRef.pop() << endl;
}
int main()
{
  ArrayStack as(10);
  f(as);
  return 0;
}
修改:


// proj2.cpp
#include <iostream>
using namespace std;
class Stack {
public:
  virtual void push(char c) = 0;
  virtual char pop() = 0;
};

class ArrayStack : public Stack {
  char * p;
  int maxSize;
  int top;
public:
  ArrayStack(int s)
  {
    top = 0;
    maxSize = s;
//********found********
    p = new char[s];
  }
  ~ArrayStack()
  {
//********found********
    delete [] p;
  }
  void push(char c)
  {
    if (top == maxSize) {
      cerr << "Overflow!\n";
      return;
    }
//********found********
    p[top] = c;
    top++;
  }
  char pop()
  {
    if (top == 0) {
      cerr << "Underflow!\n";
      return '\0';
    }
    top--;
//********found********
    return p[top];
  }
};
void f(Stack& sRef)
{
  char ch[] = {'a', 'b', 'c'};
  cout << ch[0] << ", " << ch[1] << ", " << ch[2] << endl;
  sRef.push(ch[0]); sRef.push(ch[1]); sRef.push(ch[2]);
  cout << sRef.pop() << ", ";
  cout << sRef.pop() << ", ";
  cout << sRef.pop() << endl;
}
int main()
{
  ArrayStack as(10);
  f(as);
  return 0;
}

 

// proj3.cpp
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

class doc
{
private:
  char *str;  //文本字符串首地址
  int length; //文本字符个数
public:
//构造函数,读取文件内容,用于初始化新对象。filename是文件名字符串首地址。
  doc(char *filename);
  void reverse(); //将字符序列反转
  ~doc();
  void writeToFile(char *filename);
};

doc::doc(char *filename)
{
  ifstream myFile(filename);
  int len=1001,tmp;
  str=new char[len];
  length=0;
  while((tmp=myFile.get())!=EOF)
  {
      str[length++]=tmp;
  }
  str[length]='\0';
  myFile.close();
}

void doc::reverse(){
// 将数组str中的length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个
// 字符交换……
//*************333***********         
___________________________ 
//*************666***********
}

 

doc::~doc()
{
  delete [] str;
}

void doc::writeToFile(char *filename)
{
  ofstream outFile(filename);
  outFile<<str;
  outFile.close();
}

void main()
{
  doc myDoc("in.dat");
  myDoc.reverse();
  myDoc.writeToFile("out.dat");
}

// proj3.cpp
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

class doc
{
private:
  char *str;  //文本字符串首地址
  int length; //文本字符个数
public:
//构造函数,读取文件内容,用于初始化新对象。filename是文件名字符串首地址。
  doc(char *filename);
  void reverse(); //将字符序列反转
  ~doc();
  void writeToFile(char *filename);
};

doc::doc(char *filename)
{
  ifstream myFile(filename);
  int len=1001,tmp;
  str=new char[len];
  length=0;
  while((tmp=myFile.get())!=EOF)
  {
      str[length++]=tmp;
  }
  str[length]='\0';
  myFile.close();
}

void doc::reverse(){
// 将数组str中的length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个
// 字符交换……
//*************333***********         
int i, j;      
for(i = 0, j = length-1; i<j; i++, j--)      
{           
    char temp = str[i];           
    str[i] = str[j];           
    str[j] = temp;      
}      

//*************666***********
}

doc::~doc()
{
  delete [] str;
}

void doc::writeToFile(char *filename)
{
  ofstream outFile(filename);
  outFile<<str;
  outFile.close();
}

void main()
{
  doc myDoc("in.dat");
  myDoc.reverse();
  myDoc.writeToFile("out.dat");
}

 // proj1.cpp
#include <iostream>
using namespace std;
class Point {
public:
// ERROR  ********found********
  Point(double x, double y) _x(x), _y(y) {}
  double GetX() const { return _x; }
  double GetY() const { return _y; }
// ERROR  ********found********
  void Move(double xOff, double yOff) const
  { _x += xOff;  _y += yOff; }
protected:
  double _x, _y;
};

int main()
{
  Point pt(1.5, 2.5);
  pt.Move(2.5, 1.5);
// ERROR  ********found********  以下语句输出pt成员_x和_y的值
  cout << '(' << pt._x << ',' << pt._y << ')' << endl;
  return 0;
}


// proj1.cpp
#include <iostream>
using namespace std;
class Point {
public:
// ERROR  ********found********
  Point(double x, double y):_x(x), _y(y) {}

// Point(double x, double y) { _x = x; _y = y; }
  double GetX() const { return _x; }
  double GetY() const { return _y; }
// ERROR  ********found********
  void Move(double xOff, double yOff) 
  {  _x += xOff;  _y += yOff; }
protected:
  double _x, _y;
};

int main()
{
  Point pt(1.5, 2.5);
  pt.Move(2.5, 1.5);
// ERROR  ********found********  以下语句输出pt成员_x和_y的值
  cout << '(' << pt.GetX() << ',' << pt.GetY() << ')' << endl;
  return 0;
}

// proj2.cpp
#include <iostream>
#include <cstring>
using namespace std;

class Base1 {
public:
//********found********  下列语句需要声明纯虚函数Show
  __________;
};

class Base2 {
protected:
  char * _p;
  Base2(const char *s)
  {
    _p = new char[strlen(s)+1];
//********found********  下列语句将形参指向的字符串常量复制到该类的字符数组中
    __________;
  }
  ~Base2() { delete [] _p; }
};

//********found********  Derived类公有继承Base1,私有继承Base2类
class Derived : __________ {
public:
//********found********  以下构造函数调用Base2类构造函数
  Derived(const char *s) : __________
  { }
  void Show()
  { cout << _p << endl; }
};

int main()
{
  Base1 *pb = new Derived("I'm a derived class.");
  pb->Show();
  delete pb;
  return 0;
}

 // proj2.cpp
#include <iostream>
#include <cstring>
using namespace std;

class Base1 {
public:
    //********found********  下列语句需要声明纯虚函数Show
    virtual void Show() = 0;
};

class Base2 {
protected:
    char * _p;
    Base2(const char *s)
    {
        _p = new char[strlen(s) + 1];
        //********found********  下列语句将形参指向的字符串常量复制到该类的字符数组中
        strcpy(_p, s);
    }
    ~Base2() { delete[] _p; }
};

//********found********  Derived类公有继承Base1,私有继承Base2类
class Derived : public Base1, private Base2 {
public:
    //********found********  以下构造函数调用Base2类构造函数
    Derived(const char *s) : Base2(s)
    { }
    void Show()
    {
        cout << _p << endl;
    }
};

int main()
{
    Base1 *pb = new Derived("I'm a derived class.");
    pb->Show();
    delete pb;
    return 0;
}


// proj3.cpp
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

class intArray
{
private:
  int *array;  //整数序列首地址
  int length;  //序列中的整数个数
public:
  //构造函数,从文件中读取数据用于初始化新对象。参数是文件名。
  intArray(char *filename);
  void sort();  //对整数序列按非递减排序
  ~intArray();
  void writeToFile(char *filename);
};

intArray::intArray(char *filename)
{
  ifstream myFile(filename);
  int len=300;
  array=new int[len];
  length=0;
  while(myFile>>array[length++]);
  length--;
  myFile.close();
}

void intArray::sort(){
//*************333***********

_____________________
//**********666*********
}

intArray::~intArray()
{
  delete [] array;
}

void intArray::writeToFile(char *filename)
{
  int step=0;
  ofstream outFile(filename);
  for(int i=0; i<length; i=i+step)
  {
    outFile<<array[i]<<endl;
    step++;
  }
  outFile.close();
}

void main()
{
  intArray myArray("in.dat");
  myArray.sort();
  myArray.writeToFile("out.dat");
}

// proj3.cpp
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

class intArray
{
private:
  int *array;  //整数序列首地址
  int length;  //序列中的整数个数
public:
  //构造函数,从文件中读取数据用于初始化新对象。参数是文件名。
  intArray(char *filename);
  void sort();  //对整数序列按非递减排序
  ~intArray();
  void writeToFile(char *filename);
};

intArray::intArray(char *filename)
{
  ifstream myFile(filename);
  int len=300;
  array=new int[len];
  length=0;
  while(myFile>>array[length++]);
  length--;
  myFile.close();
}

void intArray::sort(){
//*************333***********

    for(int i = 0;i < length;++i)
        for(int j = i;j<length;++j)
            if (array[i] > array[j])
            {
                int temp;
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
    for (int a = 0; a < length; ++a)
    {
        cout << array[a] << "";
    }

//**********666*********
}

intArray::~intArray()
{
  delete [] array;
}

void intArray::writeToFile(char *filename)
{
  int step=0;
  ofstream outFile(filename);
  for(int i=0; i<length; i=i+step)
  {
    outFile<<array[i]<<endl;
    step++;
  }
  outFile.close();
}

void main()
{
  intArray myArray("in.dat");
  myArray.sort();
  myArray.writeToFile("out.dat");
}

 

 

 // proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
  int _i;
  friend void Increment(MyClass& f);
public:
  const int NUM;
// ERROR  ********found********
 
MyClass(int i=0) { NUM = 0;
    _i = i;
  }
  int GetValue() const { return _i; }
};

// ERROR  ********found********
void Increment() { f._i++; }


int main()
{
  MyClass obj;
// ERROR  ********found********

  MyClass::Increment(obj);
 
  cout << "NUM = " << obj.NUM << endl
       << "Value = " << obj.GetValue() << endl;
  return 0;
}

// proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
  int _i;
  friend void Increment(MyClass& f);
public:
  const int NUM;
// ERROR  ********found********
  MyClass(int i=0) : NUM(0){ 
    _i = i;
  }
  int GetValue() const { return _i; }
};

// ERROR  ********found********
void Increment(MyClass& f) { f._i++; }


int main()
{
  MyClass obj;
// ERROR  ********found********
  Increment(obj);
 
  cout << "NUM = " << obj.NUM << endl
       << "Value = " << obj.GetValue() << endl;
  return 0;
}

 


// proj2.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class Score {
public:
  Score(double *s, int n) : _s(s), _n(n) {}
  double GetScore(int i) const { return _s[i]; }
  void Sort();
private:
  double *_s;
  int _n;
};

void Score::Sort()
{
//********found********
    for (int i = 0; i < _n-1;
______)
//********found********
      for (int j =
_______; j > i; j--)
      if ( _s[j] < _s[j-1] )
      {                       // 交换 _s[j] 和 _s[j-1]
        double t = _s[j];
//********found********
        
____________;
//********found********
        
____________;
      }
}

int main()
{
  const int NUM = 10;
  double s[NUM];
  srand(time(0));
  for (int i=0; i<NUM; i++)
    s[i] = double(rand())/RAND_MAX * 100;

  Score ss(s, NUM);
  ss.Sort();
  for (int j=0; j<NUM; j++)
    cout << ss.GetScore(j) << endl;
  return 0;
}

 


// proj2.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class Score {
public:
  Score(double *s, int n) : _s(s), _n(n) {}
  double GetScore(int i) const { return _s[i]; }
  void Sort();
private:
  double *_s;
  int _n;
};

void Score::Sort()
{
//********found********
    for (int i = 0; i < _n-1; i++)
//********found********
      for (int j =_n-1; j > i; j--)
      if ( _s[j] < _s[j-1] )
      {                       // 交换 _s[j] 和 _s[j-1]
        double t = _s[j];
//********found********
        _s[j] = _s[j-1];
//********found********
        _s[j-1] = t;
      }
}

int main()
{
  const int NUM = 10;
  double s[NUM];
  srand(time(0));
  for (int i=0; i<NUM; i++)
    s[i] = double(rand())/RAND_MAX * 100;

  Score ss(s, NUM);
  ss.Sort();
  for (int j=0; j<NUM; j++)
    cout << ss.GetScore(j) << endl;
  return 0;
}

#include <iostream>
using namespace std;

class ValArray {
  int* v;        
  int size;
public:
  ValArray(const int* p, int n) : size(n)
  {
    v = new int[size];
    for (int i = 0; i < size; i++)
      v[i] = p[i];
  }
  ValArray(const ValArray& other);
  ~ValArray() { delete [] v; }
  void setElement(int i, int val)
  {
      v[i]=val;
  }
  int *getVarray() const{return v;}
  int getSize() const{return size;}
  void print(ostream& out) const 
  {
    out << '{';
    for (int i = 0; i < size-1; i++)
      out << v[i] << ", ";
      out << v[size-1] << '}';
  }
};

void writeToFile(const char *);

cpp

#include "ValArray.h"
#include<fstream>

ValArray::ValArray(const ValArray& other)
{
//********333********
————————————
//********666********
}

int main()
{
  const int a[] = { 1, 2, 3, 4, 5 };
  ValArray v1(a, 5);
  ValArray v2(v1);

  for (int i=0; i<5; i++)
      v2.setElement(i,2);

  cout << "ValArray v1 = ";
  v1.print(cout);
  cout << endl;
  cout << "ValArray v2 = ";
  v2.print(cout);
  cout << endl;

  writeToFile("");
  return 0;
}

void writeToFile(const char *path)
{
    

    char filename[30];
    strcpy(filename,path);
    strcat(filename,"out.dat");
    ofstream fout(filename);

    const int a[] = {38, 42, 49, 49, 30, 19, 13, 14, 42, 1, 18, 4, 33, 2, 0};
    ValArray v1(a, 15);
    ValArray v2(v1);
    v2.print(fout);

    const int b[] = {-6, 42, 49, 49, 30, 11, 13, 14, 42, 1, 4, 4, 33, 2, 0};
    ValArray v3(b, 15);
    ValArray v4(v3);
    v4.print(fout);

    const int c[] = {21, 11, 28, 26, 31, 8, 25, 48, 34, 46, 9, 16, 8, 49, 22};
    ValArray v5(c, 15);
    ValArray v6(v5);
    v6.print(fout);

    const int d[] = {-6, 11, 28, 26, 31, 11, 25, 48, 34, 46, 4, 16, 8, 49, 22};
    ValArray v7(d, 15);
    ValArray v8(v7);
    v8.print(fout);

    fout.close();
}

#include "ValArray.h"
#include<fstream>

ValArray::ValArray(const ValArray& other)
{
//********333********
    size = other.size;
    v = new int[other.size];
    for (int i = 0; i < size; ++i)
        v[i] = other.v[i];

//********666********
}

int main()
{
  const int a[] = { 1, 2, 3, 4, 5 };
  ValArray v1(a, 5);
  ValArray v2(v1);

  for (int i=0; i<5; i++)
      v2.setElement(i,2);

  cout << "ValArray v1 = ";
  v1.print(cout);
  cout << endl;
  cout << "ValArray v2 = ";
  v2.print(cout);
  cout << endl;

  writeToFile("");
  return 0;
}

void writeToFile(const char *path)
{
    

    char filename[30];
    strcpy(filename,path);
    strcat(filename,"out.dat");
    ofstream fout(filename);

    const int a[] = {38, 42, 49, 49, 30, 19, 13, 14, 42, 1, 18, 4, 33, 2, 0};
    ValArray v1(a, 15);
    ValArray v2(v1);
    v2.print(fout);

    const int b[] = {-6, 42, 49, 49, 30, 11, 13, 14, 42, 1, 4, 4, 33, 2, 0};
    ValArray v3(b, 15);
    ValArray v4(v3);
    v4.print(fout);

    const int c[] = {21, 11, 28, 26, 31, 8, 25, 48, 34, 46, 9, 16, 8, 49, 22};
    ValArray v5(c, 15);
    ValArray v6(v5);
    v6.print(fout);

    const int d[] = {-6, 11, 28, 26, 31, 11, 25, 48, 34, 46, 4, 16, 8, 49, 22};
    ValArray v7(d, 15);
    ValArray v8(v7);
    v8.print(fout);

    fout.close();
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙有肥鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值