0

所以我找到了以下程序,我试图在 C++ 中完成相同的结果。

请注意,这不是我写的,只是试图理解它并用 C++ 重写。

它的作用: 它在游戏 RuneScape 中搜索一个项目并返回它的当前价格。从表面上看,他正在使用正则表达式来查找价格。

来源: http: //forum.swiftirc.net/viewtopic.php?f=20 &t=21868

编码:

using System;
using System.Collections;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace pricelookupapp
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the item name to lookup prices for");
        string item = Console.ReadLine();
        try
        {
            WebClient price = new WebClient();
            Stream pricestream = price.OpenRead("http://itemdb-rs.runescape.com/results.ws?query=" + item + "&sortby=name&order=1");
            StreamReader priceread = new StreamReader(pricestream);
            Console.WriteLine();
            Console.WriteLine("Price results for " + item + ":");
            string sockread = null, sockline = null;
            int a = 1;
            while ((sockread = priceread.ReadLine()) != null)
            {
                 if (Regex.IsMatch(sockread, "(.*) items matched the search term: '<i>(.*)</i>'.<br>"))
                {
                    string[] results = sockread.Split(new string[] { " " }, StringSplitOptions.None);
                    Console.WriteLine(results[0] + " Results Found");
                }
                if (Regex.IsMatch(sockread, "<td><a href=\"(.*)\"> (.*)</a></td>"))
                {
                    sockread = nohtml(sockread);
                    sockline = a + " Name: " + sockread.Trim();
                }
                if (Regex.IsMatch(sockread, "<td>(.*)</td>"))
                {
                    if ((!sockread.Contains("<img src") && (!sockread.Contains("<a href")) && (!sockread.Contains("<span class"))))
                    {
                        sockread = nohtml(sockread);
                        sockline = sockline + " Price: " + sockread.Trim();
                    }
                }
                if (Regex.IsMatch(sockread, "<td><span class=\"(.*)\">(.*)</span></td>"))
                {
                    string[] match = Regex.Split(sockread, "<span class=\"(.*)\">");
                    string status = match[1], change = nohtml(sockread);
                    sockline = sockline + " Status: " + status + " Change: " + change;
                }
                if (Regex.IsMatch(sockread, "<img src=\"(.*)\" alt=\"(.*)\" title=\"(.*)"))
                {
                    string[] match1 = Regex.Split(sockread, "title=\"(.*)\">");
                    string[] aa = match1[1].Split(new string[] { " " }, StringSplitOptions.None);
                    string type = aa[0];
                    type = type.Remove(type.Length - 1);
                    type = type.Replace("Members", "P2P");
                    type = type.Replace("Free", "F2P");
                    sockline = sockline + " Type: " + type;
                    Console.WriteLine(sockline);
                    a++;
                }
            }
            Console.Read();
            pricestream.Close();
        }
        catch (IOException e)
        {
            Console.WriteLine(e.GetType() + ": " + e.Message);
            Console.Read();
        }
    }

    public static string nohtml(string html)
    {
        return Regex.Replace(html, "<.*?>", "");
    }
}
 }

我尝试过的: 我已经搜索了几个小时,通常尝试用 C++ 重写这段代码,我也尝试过使用 Curl - 不走运。我用 C++ 做的任何事情都会变得冗长而丑陋,通常不起作用。

关于查看哪些库或是否有人愿意翻译它/提供片段的任何建议,我会很高兴!

4

0 回答 0