c++ - 我在让我的代码输出整个链接列表时遇到问题。它只输出我修改后的一些文本文件,而不是整个文件
问题描述
我需要帮助弄清楚如何使用更改后的 GPA 和专业来输出文本文件的其余部分。查看当前输出和代码。
来自文本文件的原始学生名单:
Mickey Mouse 1400 ecology 2.1
Donald Duck 1200 swimming 3.4
Loui Duck 1300 art 2.7
Little Mermaid 1100 Dance 3.9
Goofy Goofball 1500 dumb 1.3
Pluto Dog 1250 running 2.4
Prince Charming 1350 Kinglyness 3
King Arthur 1450 dragons 3.75
Robin Hood 1275 stealing 0.9
Xena Warrior 1325 fighting 4.3
Captain Nemo 1425 fishing 3.3
Captain Kirk 1360 Goofingoff 2.6
Ensign Zulu 1550 Communication 3.7
Peter Parker 1090 swinging 4.75
Wonder Woman 1225 spinning 3.8
Friar Tuck 1150 Religion 2
Richie Rich 1175 beingsnobby 3.6
Bart Simpson 1111 snotty 4.8
更改学生名单:
Mickey Mouse 1400 Air-Security 2.8
Donald Duck 1200 Biology 4
Loui Duck 1300 Biology 2.8
Little Mermaid 1100 Chemistry 4
Goofy Goofball 1500 Biology 4
Pluto Dog 1250 Pre-Med 2.8
Prince Charming 1350 Air-Security 2.8
King Arthur 1450 Air-Security 2.8
Robin Hood 1275 Chemistry 0.1
Xena Warrior 1325 Biology 0.1
我的代码:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iomanip>
using namespace std;
//Student class
class Student
{
public:
double Gpa;
int id;
string first, last, major;
Student()
{
first = " ";
last = " ";
major = " ";
id = 0000;
Gpa = 0;
}
Student(string firstName, string lastName, string selectedMajor, int studentID, double studentGPA)
{
first = firstName;
last = lastName;
major = selectedMajor;
id = studentID;
Gpa = studentGPA;
}
//Seting New Major for students
void setMajor(string newMajor)
{
major = newMajor;
}
//Seting New GPA
void setGpa(double newGPA)
{
Gpa = newGPA;
}
};
class StudentNode
{
public:
StudentNode(){
}
StudentNode(Student s, StudentNode* n) {
student = s;
next = n;
}
StudentNode* getNext() {
return next;
}
Student getStudent(){
return student;
}
void setNext(StudentNode* n) {
next = n;
}
private:
Student student;
StudentNode *next;
};
//Function Definitions
void add(StudentNode* &head, Student value);
void print(StudentNode* head);
int main()
{
cout << "The Original Student List from Text File:" << endl;
ifstream in("indata10.txt");
StudentNode* head = NULL;
string majors[5] = {"Engineering", "Biology", "Pre-Med", "Chemistry", "Air-Security"};
string first, last, major;
double gpa[5] = {2.0, 3.5, 2.8, 4.0, 0.1};
double Gpa;
int id, ran1, ran2, integer = 0;
while(!in.eof())
{
in >> first >> last >> id >> major >> Gpa; //Read in the data for each student
cout << first << " " << last << " " << id << " " << major << " " << Gpa << endl; //Print the data of the student
Student student(first, last, major, id, Gpa);
integer++;
if (integer <= 10)
{
//Altering the Gpa's and Major's using random numbers
ran1 = rand() % 4 + 1;
ran2 = rand() % 4 + 1;
student.setGpa(gpa[ran1]);
student.setMajor(majors[ran2]);
add(head, student);
}
}
print(head);
return 0;
}
//Adding to the linkedlist
void add(StudentNode* &head, Student value)
{
StudentNode* current = head;
if (current == NULL)
{
head = new StudentNode(value, NULL);
return;
}
while (current->getNext() != NULL)
{
current = current->getNext();
}
current->setNext(new StudentNode(value, NULL));
}
//Prints the linked list
void print(StudentNode* head)
{
StudentNode* current = head;
cout << "\n Changed Student List :" << endl;
while (current != NULL)
{
Student temp = current->getStudent();
cout << temp.first << " " << temp.last << " " << temp.id << " " <<
temp.major << " " << temp.Gpa << "\t" << endl;
current = current->getNext();
}
cout << endl;
}
我希望更改整个原始文本文件并将其放在已更改的学生列表下
解决方案
if (integer <= 10)
{
//Altering the Gpa's and Major's using random numbers
ran1 = rand() % 4 + 1;
ran2 = rand() % 4 + 1;
student.setGpa(gpa[ran1]);
student.setMajor(majors[ran2]);
add(head, student);
}
上面的 if 不允许你在链表中添加超过 10 个元素,其余的都很好。
推荐阅读
- python - 计算列表中重复元组的数量
- android - Ionic Cordova Build Android 期间面临错误
- c# - 我们可以使用 C# 以编程方式将 DLL 文件添加到特定文件夹中吗
- laravel - Laravel 站点错误地重定向到 site.com/public/public
- c# - Excel 2016 到 Windows 窗体 Visual Studio 2019 社区中的 DataGridView
- c# - 无法加载文件或程序集“Microsoft.mshtml,版本=7.0.3300.0
- spring - 在另一个 bean 中使用来自一个 bean(组件)的方法是否正确?
- amazon-web-services - 在 S3 中访问数据的权限不足
- android - 使用 Android NDK 使用 clang++ 编译 C++ 代码时对“_Unwind_Resume”的未定义引用
- reactjs - 尝试实现全选/取消全选时,ANTD Select mode="multiple" 很慢