使用getline解析一个字符串并将其放入链接列表c ++中

时间:2018-02-28 18:22:07

标签: c++ getline singly-linked-list stringstream

我的任务是用c ++编写经典的“购物车”链表项目。我的每个节点都有一个itemname,一个itemcost和一个nextc。我使用一个名为insertNode(字符串名称,双倍成本)的函数,按字母顺序构建列表。输入是一个文本文件,其中第一行是预算,之后的每一行都是“Itemname,itemcost”的形式。我使用getline()和stringstream来读取文件的行,并检查它是字符串还是数字。它运作良好,直到我用一个两个单词的项目(例如:“冰淇淋,4.50”)。 Visual Studios告诉我,它会引发“读取访问冲突”异常,。我没有运气搜索我的问题,因为它是如此具体(在字符串解析中保留空格+从文件中读取字符串+将其放入链表)。如果你们都能给我任何指示(+任何一般的编码技巧),我们将不胜感激!这是我在这项任务中留下的最后一件事。抱歉格式不佳,我在这里有点新鲜!

示例输入(在.txt文件中):

15.00
苹果,1.50
西兰花,2.00
酸奶,4.00
巧克力,3.80
纸巾,2.20
牛奶,1.70
冰淇淋,4.50

main.cpp中:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="foo1"></div>
<div class="foo1"></div>
<div class="foo1"></div>
<div class="foo1"></div>
<div class="hidden"></div>
<div class="hidden"></div>
<div class="hidden"></div>
<div class="foo1"></div>
<div class="foo1"></div>
<div class="foo1"></div>

List.h:

//  main.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <string>
#include "List.h"

bool isNum(string);
using namespace std;

int main(int argc, char * argv[]) {

string infilename = ("1.txt");
string outfilename = ("output.txt");

ifstream infile;

List list;
double budget = 0;
string iname;
double iprice = 0;

infile.open(infilename.c_str());

if (infile.fail()) {    //check for file failure (did not open)
    exit(1);
}

string temp;
getline(infile, temp);  //grab the budget, catch exceptions
try {
    budget = stod(temp);
    if (budget < 0)
        throw - 3;
}
catch (int e) {
    return -1;
}
catch (...) {
    cerr << "No budget given. Exiting";
    return -1;
}

string line = "";
string tempdata;
bool proceed = true;
double tempcost = 0;

while (!infile.eof()){
    getline(infile, line);
    istringstream ss;   //create ss object
    ss.str(line);       //converts string to stringstream
    if (isNum(line)) {  //is line a number (price)?
        proceed = false;    //yes, is the end of the line
    }

    if (proceed) {          //no, "import" name & cost using stringstream
        getline(ss, line, ',');
        getline(ss, tempdata);
        bool duplicated = list.isDuplicate(line);   //checks if name is already included in the list

        try {
            if (!duplicated && stod(tempdata) > 0) {        //make sure the item doesn't already exist & the price is a positive number
                tempcost = stod(tempdata);
                list.insertNode(line, tempcost);    //everything looks great, add it to the list
            }
        }
        catch (...) { /* price is not a number, item is already in list*/
        }
    }
    proceed = true;

}
infile.close();

cout << "unaltered, sorted list: " << endl;
list.displayList();
cout << endl;

return 0;
}

bool isNum(string s) {
int n = s.length();
char char_array[50];
strcpy_s(char_array, s.c_str());

for (int i = 0; i < n; i++) {
    if (!isdigit(char_array[i])) {
        return false;
        break;
    }
    return true;
}

List.cpp:

// List.h
// hw1
#ifndef LIST_H
#define LIST_H


#include <cstdlib>
#include <string>
using namespace std;


class List {
private:

typedef struct Node {   //creates node architecture
    double itemcost;
    string itemname;
    Node* next;
}* ptr;

ptr head;
ptr current;
ptr temp;
ptr tail;
int count;


public:             // where functions go
List();
void insertNode(string addName, double addCost);
bool isEmpty();
bool isDuplicate(string name);
void displayList();

};


#endif /* LIST_H */

0 个答案:

没有答案