【问题标题】:Abstract Class Output problems抽象类输出问题
【发布时间】:2013-12-11 02:56:56
【问题描述】:

这个主程序应该要求用户输入一些数字并将它们存储到一个动态数组中。然后应该以直线输出数组的内容,没有结束行命令,中间有逗号。我不知道如何启动程序。

如果你们能帮我找到方法,我将永远感激不尽!

这里是 ListType.h:

#ifndef LISTTYPE_H_INCLUDED
#define LISTTYPE_H_INCLUDED
#include <iostream>

class ListType {
public:
 ListType(size_t=10);
 virtual ~ListType();
 virtual bool insert(int)=0;
 virtual bool erase();
 virtual bool erase(int)=0;
 virtual bool find(int) const=0;
 size_t size() const;
 bool empty() const;
 bool full() const;
 void output(std::ostream& out) const;
 friend std::ostream& operator << (std::ostream&, const ListType&);
protected:
 int *items;
 size_t capacity;
 size_t count;
};

#endif // LISTTYPE_H_INCLUDED

这里是 UListType.h:

#ifndef ULISTTYPE_H_INCLUDED
#define ULISTTYPE_H_INCLUDED
#include <iostream>

class UListType: public ListType {
public:
 UListType(size_t=10);
 bool insert(int);
 bool erase(int);
 bool find(int) const;
};


#endif // ULISTTYPE_H_INCLUDED

这里是 OListType.h:

#ifndef OLISTTYPE_H_INCLUDED
#define OLISTTYPE_H_INCLUDED
#include <iostream>

class OListType: public ListType {
public:
 OListType(size_t=10);
 bool insert(int);
 bool erase(int);
 bool find(int) const;
};


#endif // OLISTTYPE_H_INCLUDED

这里是 ListType.cpp:

#include "ListType.h"

ListType::ListType (size_t a) {
 capacity = a;
 count = 0;
 items = new int [capacity];
}

ListType::~ListType() {
 delete [] items;
}

bool ListType::erase() {
 count = 0;
 return 0;
}

size_t ListType::size() const {
 return (count);
}

bool ListType::empty() const {
 return (count == 0);
}

bool ListType::full() const {
 return (count == capacity);
}

void ListType::output(std::ostream& out) const {
 for (int i = 0; i < count; i++) {
      if (i > 0) {
        out << ", ";
      }
      out << items[i];
 }
}

std::ostream& operator << (std::ostream& out, const ListType& my_list) {
 my_list.output(out);
   return out;
}

这里是 UListType.cpp

#include "ListType.h"
#include "UListType.h"

UListType::UListType (size_t c): ListType(c) {}

bool UListType::insert(int item) {
 if (full()) {
    int *newitems;
    capacity *=2;
    newitems = new int[capacity];
    for (size_t i =0; i < count; ++i){
        newitems[i] = items[i];
    }
    delete [] items;
    items = newitems;
 }
 items[count++] = item;
 return true;
}

bool UListType::erase(int item) {
 bool result = false;
 size_t i=0;
 while ( i < count && items [i] != item) {
     ++i;
}
 if (i < count) {
    items[i] = items[-- count];
    result = true;
 }
 return result;
}

bool UListType::find(int item) const {
 size_t i = 0;
 while (i < count && items [i] != item) {
     ++i;
 }
 return i;
}

这里是 OListType.cpp

#include "ListType.h"
#include "OListType.h"

OListType::OListType(size_t c): ListType(c) {}

bool OListType::insert(int item) {
 size_t i = count;
 if (full()) {
    int *newitems;
    capacity *=2;
    newitems = new int[capacity];
    while (i > 0 && items[i-1] > item){
        newitems[i] = items[i];
    }
    delete [] items;
    items = newitems;
 }
 items[count++] = item;
 return true;
}

bool OListType::erase(int item) {
 bool found=false;
 size_t i=0, j= count-1, mid;
 while (i <= j && !(found)){
    mid = (i + j)/2;
    if (item < items [mid])
        j = mid - 1;
    else if (item > items [mid])
        i = mid + 1;
    found = items [mid] == item;
}
if (found) {
    for (i = mid; i < count - 1; ++i) {
        items [i] = items [i +1];
    }
     --count;
 }
 return found;
}

bool OListType::find (int item) const {
bool found=false;
 size_t i=0, j= count-1, mid;
 while (i <= j && !(found)){
    mid = (i + j)/2;
    if (item < items [mid])
        j = mid - 1;
    else if (item > items [mid])
        i = mid + 1;
    found = items [mid] == item;
 }
 return found;
}

【问题讨论】:

  • 你试过int main(void) {吗?它通常是我最好的起点。
  • 我知道使用 int main() 开始,但是 int main(void) 是做什么的?
  • void 告诉编译器您的程序不需要操作系统提供任何命令行参数。
  • @Thomas Matthews 这对我有什么帮助?抱歉,我对此有点陌生
  • @ThomasMatthews:重新“告诉编译器”,仅在 C 中。不在 C++ 中。在 C++ 中,这只是不必要的冗长。

标签: c++ abstract-class virtual-functions dynamic-arrays


【解决方案1】:
#include "ListType.h"
#include "UListType.h"

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
  UListType UL;

  cout << "How many numbers do you want to put it?" << endl;
  int n;
  cin >> n;
  cout << "All right, enter " << n << " numbers:" << endl;
  int x;
  for(int k=0; k<n; ++k)
  {
    cin >> x;
    // do something with x
  }
  return(0);
}

【讨论】:

    【解决方案2】:

    您已经拥有所需的一切。试试下面的

    #include <iostream>
    #include "OListType.h"
    
    using namespace std;
    int main()
    {
        OListType list;
        int n;
        do
        {
            cout << "Add a number [Y/n]?";
            char a;
            cin >> a;
            if (a != 'n')
            {
                cin >> n;
                list.insert(n);
            }
            else
            {
                list.output(cout);
                break;
            }
        }while (1);
    
        return 0;
    }
    

    【讨论】:

    • 如何退出 do while 循环?
    • @ldicus34:这就是break 所做的。
    • 是的。你试过了吗?一切都好吗?
    猜你喜欢
    • 2015-04-06
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多