⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 articlecitations.cpp

📁 C++高级编程这本书所附的源代码
💻 CPP
字号:
#include "ArticleCitations.h"#include <iostream>#include <fstream>#include <string>#include <stdexcept>using namespace std;ArticleCitations::ArticleCitations(const string& fileName){  // All we have to do is read the file  readFile(fileName);}ArticleCitations::ArticleCitations(const ArticleCitations& src){  // Copy the article name, author, etc.  mArticle = src.mArticle;  // Copy the number of citations  mNumCitations = src.mNumCitations;  // Allocate an array of the correct size  mCitations = new string[mNumCitations];  // Copy each element of the array  for (int i = 0; i < mNumCitations; i++) {    mCitations[i] = src.mCitations[i];  }}ArticleCitations& ArticleCitations::operator=(const ArticleCitations& rhs){  // Check for self-assignment  if (this == &rhs) {    return (*this);  }  // Free the old memory  delete [] mCitations;  // Copy the article name, author, etc.  mArticle = rhs.mArticle;  // Copy the number of citatoins  mNumCitations = rhs.mNumCitations;  // Allocate a new array of the correct size  mCitations = new string[mNumCitations];  // Copy each citation  for (int i = 0; i < mNumCitations; i++) {    mCitations[i] = rhs.mCitations[i];  }  return (*this);}ArticleCitations::~ArticleCitations(){  delete [] mCitations;}void printStreamState(const istream& istr){  if (istr.good()) {    cout << "stream state is good\n";  }  if (istr.bad()) {    cout << "stream state is bad\n";  }  if (istr.fail()) {    cout << "stream state is fail\n";  }  if (istr.eof()) {    cout << "stream state is eof\n";  }}void ArticleCitations::readFile(const string& fileName){  // open the file and check for failure  ifstream istr(fileName.c_str());  if (istr.fail()) {    throw invalid_argument("Unable to open file\n");  }  // read the article author, title, etc. line.  getline(istr, mArticle);  // skip the whitespace before the citations start  istr >> ws;  int count = 0;  // save the current position so we can return to it  int citationsStart = istr.tellg();  // First count the number of citations  cout << "readFile(): counting number of citations\n";  while (!istr.eof()) {    string temp;    getline(istr, temp);    // skip whitespace before the next entry    istr >> ws;    printStreamState(istr);    cout << "Citation " << count << ": " << temp << endl;    count++;  }  cout << "Found " << count << " citations\n" << endl;  cout << "readFile(): reading citations\n";  if (count != 0) {    // allocate an array of strings to store the citations    mCitations = new string[count];    mNumCitations = count;    // seek back to the start of the citations    istr.seekg(citationsStart);    // read each citation and store it in the new array    for (count = 0; count < mNumCitations; count++) {      string temp;      getline(istr, temp);      printStreamState(istr);      cout << temp << endl;      mCitations[count] = temp;    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -