将txt文件数据分配给链表中的struct节点 [英] Assign txt file data to struct node in linked list

查看:88
本文介绍了将txt文件数据分配给链表中的struct节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我从来没有使用过fstream,也从未在程序中打开过文件和文件.我的老师仅给出了几行代码,用于打开,读取和关闭文本文件.我应该将数据从文本文件中取出,并将其放入链表中的单独节点中,然后继续执行其他操作,这并不重要,因为我知道该怎么做.我的问题是我不知道如何将这些值分配给struct值.

Ok so I have never worked with fstream before or opened and read and files in a program. My instructor just gave a few lines of code that open, read, and close a text file. I'm supposed to take the data out of the text file and put it into separate nodes in a linked list and then go on to do other things with it which is not important because I know how to do it. My problem is that I don't know how to a assign these values to the struct values.

txt文件如下所示:

The txt file looks like this:

克拉克·肯特55000 2500 0.07

Clark Kent 55000 2500 0.07

路易斯大街56000 1500 0.06

Lois Lane 56000 1500 0.06

托尼·史塔克34000 2000 0.05

Tony Stark 34000 2000 0.05

...

我创建了一个名为Employee的结构,然后创建了基本的插入函数,因此可以将新节点添加到列表中.现在如何将这些名称和数字放入我的结构中.

I have created a structure called Employee and then basic insert functions so I can add new nodes to the list. Now how do I get these names and numbers into my structure.

这是我的代码:

#include <fstream>
#include <iostream>
using namespace std;

struct Employee
{
    string firstN;
    string lastN;
    float salary;
    float bonus;
    float deduction;

    Employee *link;
};

typedef Employee* EmployPtr;
void insertAtHead( EmployPtr&, string, string, float, float,float );
void insert( EmployPtr&, string, string, float, float,float );

int main()
{
    // Open file
    fstream in( "payroll.txt", ios::in );

    // Read and prints lines
    string first, last;
    float salary, bonus, deduction;

    while( in >> first >> last >> salary >> bonus >> deduction)
    {
        cout << "First, last, salary, bonus, ded: " << first << ", " << last << ", " << salary << ", " << bonus << ", " << deduction <<endl;
    }

    // Close file
    in.close();

    EmployPtr head = new Employee;


 }

void insertAtHead(EmployPtr& head, string firstValue, string lastValue,
            float salaryValue, float bonusValue,float deductionValue)
{
    EmployPtr tempPtr= new Employee;

    tempPtr->firstN = firstValue;
    tempPtr->lastN = lastValue;
    tempPtr->salary = salaryValue;
    tempPtr->bonus = bonusValue;
    tempPtr->deduction = deductionValue;

    tempPtr->link = head;
    head = tempPtr;
}

void insert(EmployPtr& afterNode, string firstValue, string lastValue,
        float salaryValue, float bonusValue,float deductionValue)
{
    EmployPtr tempPtr= new Employee;


    tempPtr->firstN = firstValue;
    tempPtr->lastN = lastValue;
    tempPtr->salary = salaryValue;
    tempPtr->bonus = bonusValue;
    tempPtr->deduction = deductionValue;

    tempPtr->link = afterNode->link;
    afterNode->link = tempPtr;
}

此外,我尝试搜索此结果,但结果却出现了,但是它们全都打开了,并且读取数据的方式与我给出的方式有所不同.我是来自Java的c ++的新手,所以我不理解有时会看到的一些代码.

Also, I have tried searching this and results have came up but they all opened and read the data differently than what i was given. I am new to c++ coming from java so I do not understand some of the code that I see sometimes.

推荐答案

EmployPtr head = new Employee;

while( in >> first >> last >> salary >> bonus >> deduction)
{
    cout << "First, last, salary, bonus, ded: " << first << ", " << last << ", " << salary << ", " << bonus << ", " << deduction <<endl;
    insertAtHead (head, first, last, salary, bonus, deduction);
}

您已经拥有99%的解决方案.您只需要在读取文件时构造列表即可.

You already had 99% of the solution. You just have to construct the list as you read the file.

这篇关于将txt文件数据分配给链表中的struct节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆