弦到不带Sstream或增强词汇铸件的串转换

String to double conversion without sstream or boost lexical cast

本文关键字:转换 词汇 增强 Sstream      更新时间:2023-10-16

嘿,我对我的代码有疑问。这是我们要做的:"要求用户读取文件。该文件的格式与网站上的" items.txt"相同。始终是具有名称和价格的物品列表,然后是一些食谱。如果是物品不存在,制作商品的唯一方法是直接购买。制作一个读取全部的程序然后说,这些物品和食谱可以通过制造每个物品可以赚多少钱。如果商品没有食谱,您会购买该商品,然后以相同的价格转售它,并获利0.如果一个物品确实有食谱,您将购买材料来制造此物品并减去此费用从最终产品的价格。每个项目只有零或一个食谱。这些项目将始终首先列出。名称项目将始终是一个单词(使用_加入通常多个单词的名称)。你可以假设将少于50个项目,每个食谱将使用少于50个其他项目来创建一个最终产品。"

这是我们使用

的项目1.txt
Item: Wood 2.5
Item: Metal 5.5
Item: Cat 900
Item: Spear 50.7
Recipe: Spear = Wood + Wood + Metal ;

我有我认为可以工作的东西,但我无法获得一定的工作。我正在尝试使用STOD,但显然我学校的计算机不支持它。我还尝试了Boost Lexical Cast,这也行不通。

它说" stod:在此范围中没有声明。

这是我的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <sstream> 
using namespace std;
string nextstring(string str, int start_index);
int split(string str, string a[], int max_size);
int main()
{   
    ifstream in_stream;
    string fileName;
    cout << "Enter the file name : ";
    cin >> fileName;
    in_stream.open(fileName.c_str());
    //error checking
    if (in_stream.fail())
    {
        cout << "File could not be opened." << endl;
        exit(1);
    }
    string items[50];
    double items_value[50];
    string recipe[50];
    string rname = recipe[0];
    double profit = 0;
    int j = 0;
    string lines;
    int number_of_lines = 0;
    while(getline(in_stream, lines))
    {  
        if(lines.substr(0,5) == "Item:")
            {   
                int beginning = lines.find_first_of(' ') + 1;
                int next_space = lines.find(" ", beginning);
                items_value[j] = stod(lines.substr(next_space));
                items[j] = lines.substr(beginning,lines.find_first_of(' ', beginning) - beginning);
                j++;
            }
        if(lines.substr(0,7) == "Recipe:")
        {           
            int max_size = lines.length();
            int cnt = split(lines,recipe,max_size);
            double profit1 = 0;
            double profit2 = 0;
            for(int j = 3; j < cnt; j++)
            {
                for(int i = 0; i < 4; i++)
                    {
                        if((recipe[j] == items[i]) && (recipe[j] != "+")&& (recipe[j] != ";"))
                        {
                        cout << "Making " << items[i] << ", " << "profit = 0" << endl;          
                        profit1 += items_value[i];                      
                        }
                        if(recipe[1] != items[i])
                        {
                        profit2 = 0;
                        }
                    }
            }
            for(int i = 0; i < cnt; i++)
            {
                if((recipe[1] == items[i]))
                {
                    profit = items_value[i];
                    cout << "Making " << items[i] << ", " << "profit = ";
                }
            }
            cout << profit - profit1 << endl;

        }   
    }
    in_stream.close();
    return 0; 
}
string nextstring(string str, int start_index)
{
    int y =0;
    y = str.find(' ',start_index);
    y = y-start_index;
    str = str.substr(start_index,y);    
    return str;
}
int split(string str, string a[], int max_size)
{
    int i;
    int num = 0;
    for (i=0; i<max_size; i++)
    {
        a[i] = nextstring(str,num);
        num = num + a[i].length() + 1;
        if(num >= str.length())
        {
            i++;
            break;
        }
    }
    return i;
} 

第一步是从本世纪获得一个不错的编译器;)stod自C 11以来就可以使用,这实际上意味着它可能在此之前几年可用。

如果您无法使用stod