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

📄 screenscraping.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Tutorial 19: ScreenScraping.cpp
// Search an HTML code string for an item and display its price
// converted from euros to American dollars.
#include <iostream> // required to perform C++ stream I/O
#include <iomanip>  // required for parameterized stream manipulators
#include <string>   // required to access string functions
#include <cstdlib>  // required for the atof function

using namespace std; // for accessing C++ Standard Library members

// function prototypes
void displayMenu( string & );
double getRate();
double getPrice( string &, string &, double );

// function main begins program execution
int main ()
{
   // define variables
   string searchText; // stores the search string

   // assign HTML code to htmlText
   string htmlText = "<HTML><BODY><TABLE>"
      "<TR><TD>Antique Rocking Chair</TD>"
      "<TD>&euro;82.67</TD></TR>"
      "<TR><TD>Silver Teapot</TD>"
      "<TD>&euro;64.55</TD></TR>"
      "<TR><TD>Gold Pocket Watch</TD>"
      "<TD>&euro;128.83</TD></TR>"
      "</TABLE></BODY></HTML>";

   cout << "\nThe HTML source is: \n" << htmlText << endl;
   
   // display menu and retrieve appropriate search text
   displayMenu( searchText );

   // repeat while the user has selected a valid menu option
   while ( !searchText.empty() )
   {
      // retrieve exchange rate from the user
      double rate = getRate();

      // search the source file for the item's price and return
      // the value converted to dollars
      double price = getPrice( htmlText, searchText, rate );

      // display price
      cout << fixed << setprecision( 2 );
      cout << "\nPrice is: $" << price << endl;

      // display menu and retrieve appropriate search text
      displayMenu( searchText );
   } // end while

   return 0; // indicate that program ended successfully

} // end function main

// prompt the user for a search item and return the appropriate
// search string
void displayMenu( string &searchText )
{
   // define local variable to store user selection from the menu
   int selection = 0;

   while ( selection < 1 || selection > 4 )
   {
      // display the menu
      cout << "\nWhich item would you like to search for?\n";
      cout << "1 - Antique Rocking Chair" << endl;
      cout << "2 - Silver Teapot" << endl;
      cout << "3 - Gold Pocket Watch" << endl;
      cout << "4 - Exit the application" << endl;
      cout << "Enter selection: ";
      cin >> selection; // retrieve the selection

      // assign the appropriate search string to searchText
      switch ( selection )
      {
      case 1:
         searchText = "Antique Rocking Chair";
         break;

      case 2:
         searchText = "Silver Teapot";
         break;

      case 3:
         searchText = "Gold Pocket Watch";
         break;

      case 4:
         searchText = "";
         cout << endl;
         break;

      default: // if invalid option was selected
         searchText = "";
         cout << "Error: Please enter a valid selection."
            << endl;
      } // end switch

   } // end while

} // end function displayMenu

// get currency conversion rate
double getRate()
{
   double rate; // stores the conversion rate

   // prompt the user for and input the conversion rate
   cout << "Enter conversion rate: ";
   cin >> rate; // get the rate

   return rate; // return a copy of the rate

} // end function getRate

// find the price for the selected item
double getPrice( string &htmlText, string &searchText,
                double conversionRate )
{
   // search for location of item and price
   int itemLocation = htmlText.find( searchText );
   int priceBegin = htmlText.find( "&euro;", itemLocation ) + 6;
   int priceEnd = htmlText.find( "</TD>", priceBegin );   

   // store the price in the string priceText
   string priceText =
      htmlText.substr( priceBegin, priceEnd - priceBegin);

   // convert the text to a floating-point value
   double price = atof( priceText.c_str() );
   
   // convert price from euros to dollars
   return price * conversionRate;

} // end function getPrice


/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
* Pearson Education, Inc. All Rights Reserved.                           **
* DISCLAIMER: The authors and publisher of this book have used their     *
* best efforts in preparing the book. These efforts include the          *
* development, research, and testing of the theories and programs        *
* to determine their effectiveness. The authors and publisher make       *
* no warranty of any kind, expressed or implied, with regard to these    *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or       *
* consequential damages in connection with, or arising out of, the       *
* furnishing, performance, or use of these programs.                     *
**************************************************************************/

⌨️ 快捷键说明

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