gpt4 book ai didi

C++ Linked List -- 读取文件并根据数字对其进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:53 25 4
gpt4 key购买 nike

我正在读取文件。它由以下内容逐行填充:

e 2
b 1
a 3
h 5
c 4

如果我按数字对这些进行排序,我会得到“海滩”一词。我需要这样做,但规模更大。我目前有两个错误。我读到的行中有一个空格和一个数字 [ ""6 ],我的程序不会将它们附加/排序到我的链表中,因此这会导致段错误,因为我的 for 打印循环是为了循环通过所有 53 个单词。

对于为什么这些空格没有出现在我的链接列表中,是否有任何修复?此外,大约 44/53 的单词使用我的 addInOrder 函数正确找到了它们的位置,但还有一些单词没有,这有什么原因吗?

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <ctype.h>

using namespace std;

struct ListNode
{
string letter;
string num;
ListNode *next;
};

void append(ListNode *&h, string l, string n);
void addInOrder(ListNode *&h, string l, string n);
void printList(ListNode *h, int &lengthOfFile);
void deleteNode(ListNode *&h, string l, string n);
void deleteList(ListNode *&h);

int main()
{
string letter;
string num;
string lines;
int lengthOfFile = 0;
const string FILENAME = "file link";
ifstream inFile(FILENAME);

/*if (inFile)
{
while (getline( inFile, lines ))
{
lengthOfFile++;
cout << "Hello...1" << endl;
}
}*/
ListNode *head = nullptr;

if (inFile)
{
string line;
for (int lineNum = 1; getline(inFile, line); lineNum++)
{
stringstream ss(line);
string word;

for (int wordNum = 1; ss >> word; wordNum++)
{

if (wordNum == 1)
{
char c = word[0];

if (isalpha(c))
{
cout << "letter: " << c << endl;
letter = c;
}
else if (word == "!" or word == ".")
{
cout << "letter: " << word << endl;
letter = word;
}
else if (word != "!" or word != ".")
{
cout << "letter: " << " " << endl;
cout << "number: " << word << endl;
letter = " ";
num = word;
lengthOfFile++;
}
}
if (wordNum == 2)
{
cout << "number: " << word;
num = word;
lengthOfFile++;
}
if (wordNum == 2)
{
cout << endl;
append(head, letter, num);
//cout << "letter: " << letter << " and num: " << num << endl;
cout << endl;
addInOrder(head, letter, num);
//cout << "letter: " << letter << " and num: " << num << endl;
cout << endl;
cout << endl;
cout << endl;
}
}
cout << endl;
}
inFile.close();
}

cout << "lengthOfFile++;: " << lengthOfFile << endl;

printList(head, lengthOfFile);
}

void append(ListNode *&h, string l, string n)
{
// create a new ListNode and set data and next
ListNode *newNode;
newNode = new ListNode;
newNode->letter = l;
//cout << "l: " << l << endl;
newNode->num = n;
//cout << "n: " << n << endl;
newNode->next = nullptr;
}

void addInOrder(ListNode *&h, string l, string n)
{
// create a new ListNode and set data
ListNode *newNode;
newNode = new ListNode;
newNode->letter = l;
//cout << "l: " << l << endl;
newNode->num = n;
//cout << "n: " << n << endl;
newNode->next = nullptr;

// if list is empty, assign head to new ListNode; otherwise,
// find where to add in (non-descending) order
if (h == nullptr)
{
h = newNode;
newNode->next = nullptr;
}
else
{
ListNode *prev = nullptr;
ListNode *curr = h;

// find location to add
while (curr != nullptr && curr->num < n)
{
prev = curr;
curr = curr->next;
}

// if prev is nullptr, then we're adding to the beginning
// of the list; else, adding to end or between two nodes
if (prev == nullptr)
{
h = newNode;
newNode->next = curr;
}
else
{
prev->next = newNode;
newNode->next = curr;
}
}
}

void printList(ListNode *h, int &lengthOfFile)
{
ListNode * ptr = h;

// loop through and print data
for(int i = 0; i < lengthOfFile; i++)
{
cout << ptr->letter << " ";
cout << ptr->num << " ";
cout << endl;
ptr = ptr->next;
}
}

最佳答案

1)问题出在以下行:

ss >> word

运算符 >> 正在消耗空白并寻找有效(非空白)字符。

从流中获取单个字符 ss.get()应该使用。

2) 如果要与字 rune 字进行比较,请使用单撇号 '。但也记得从 line 中提取一个字符。

关于C++ Linked List -- 读取文件并根据数字对其进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com