2013-12-11 116 views
1

這個主程序應該要求用戶輸入一些數字並將它們存儲到動態數組中。然後該數組應該以直線形式輸出其內容,而不是結束行命令,其間用逗號隔開。我無法弄清楚如何啓動程序。抽象類輸出問題

如果你們能幫助我找到辦法做到這一點,我會永遠感謝!

這裏是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; 
} 
+0

你試過了嗎?int main(void){'?它通常是我最好的出發地。 –

+0

我知道使用int main()來啓動,但int main(void)會做什麼? – RyuKaze78

+0

'void'告訴編譯器你的程序不期望來自操作系統的任何命令行參數。 –

回答

0
#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); 
} 
0

你已經擁有你所需要的一切。嘗試以下內容

#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; 
} 
+0

中讀取它們如何退出do while循環? – RyuKaze78

+1

@ ldicus34:這就是'break'所做的。 – Beta

+0

是的。你試過了嗎?一切都很好? – felknight