gpt4 book ai didi

c++ - 从二进制文件中读取 double(进入类) - 不显示小数

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:51:07 27 4
gpt4 key购买 nike

读取数据后无法显示小数。

正如标题所说...这里有一些代码片段。

学生对象的标题

class StudentRecordData
{
protected:
double Marks[MAX_COURSES];
unsigned int Age;
unsigned int ID;
unsigned int CourseCount;
char FirstName[LEN_FN];
char LastName[LEN_FN];
Course Courses[MAX_COURSES];
};

学生标题:

class Student : public StudentRecordData {

public:
double *getMarks();
unsigned int getAge();
unsigned int getID();
unsigned int getCourseCount();
char *getFirstName();
char *getLastName();
Course *getCourses();
double getAverage();

int operator < (Student &);
int operator < (const char &);
};

学生实现(继承了上面的)

#include "Student.h"
#include <iostream>
#include <cstring>
using namespace std;

double *Student::getMarks() {
return Marks;
}

unsigned int Student::getAge() {
return Age;
}

unsigned int Student::getID() {
return ID;
}

unsigned int Student::getCourseCount() {
return CourseCount;
}

char *Student::getFirstName() {
return FirstName;
}

char *Student::getLastName() {
return LastName;
}

Course *Student::getCourses() {
return Courses;
}

int Student::operator<(Student &s) {
int ret = 0;

int LNRet = strcmp(this->getLastName(), s.getLastName());
int FNRet = strcmp(this->getFirstName(), s.getFirstName());

if (LNRet < 0) {
ret = 1;
} else if (LNRet == 0) {
if (FNRet < 0) {
ret = 1;
}
}

return ret;
}

int Student::operator<(const char &s) {
cout << "in char *!" << endl;
int ret = 0;

return ret;
}

double Student::getAverage() {
double total = 0.00;
for (int i = 0; i < (getCourseCount()); i++) {
total = total + getMarks()[i];
}
return total / getCourseCount();
}

主要

#include "Course.h"
#include "Student.h"
#include "Node.h"
#include "LinkedList.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main(void) {
char input[] = "OOP344.dat";
char output[] = "OUTPUT.txt";
Student student;
LinkedList list;
Node *node;

// read in file
ifstream ifile(input, ios::binary);
while (!ifile.eof()) {
ifile.read(reinterpret_cast<char *>(&student), sizeof(Student));

node = new Node(student);
list.addNode(node);
}
ifile.close();

list.sort();

// write to file
ofstream ofile(output);
if (ofile.is_open()) {

for (node = list.getFirstNode(); node; node = list.getNextNode(node)) {
cout << setw(10) << "Last Name" << setw(12) << "First Name" << setw(5) << "Age" << setw(12) << "Student ID" <<
setw(15) << "Course" << setw(7) << "Mark" << endl;
cout << setw(10) << "=========" << setw(12) << "==========" << setw(5) << "===" << setw(12) << "==========" <<
setw(15) << "======" << setw(7) << "=====" << endl;
cout << setw(10) << node->getValue().getLastName()
<< setw(12) << node->getValue().getFirstName()
<< setw(5) << node->getValue().getAge()
<< setw(12) << node->getValue().getID()
<< setw(22) << node->getValue().getAverage()
<< endl;

int sem, sem_prev;

for (int i = 0; i < node->getValue().getCourseCount(); i++) {
sem = (int)node->getValue().getCourses()[i].Semester;
sem_prev = (int)node->getValue().getCourses()[i-1].Semester;

if (!(sem == sem_prev)) {
cout << setw(45) << "Sem " << (int)node->getValue().getCourses()[i].Semester << ":"
<< setw(7) << node->getValue().getCourses()[i].Name
<< setw(7) << setprecision(4) << node->getValue().getMarks()[i] << endl;
} else {
cout << setw(54) << node->getValue().getCourses()[i].Name
<< setw(7) << setprecision(4) << node->getValue().getMarks()[i] << endl;
}

}

cout << endl;
}
}
ofile.close();

return (0);
}

输出:

Last Name  First Name  Age  Student ID         Course   Mark
========= ========== === ========== ====== =====
Sakedad Roya 25 486503 74.31
Sem 1: APC100 69
EAC150 92
ICA002 76
IOS110 87
IPC144 99
Sem 2: ULI101 62
DBS201 66
IBC233 94
INT222 58
OOP244 67
Sem 3: DBS301 61
INT322 89
SYS366 52
BAC344 80
OOP344 63
Sem 4: DCN455 74

问题出在靠近底部的这一行。它不会显示任何小数点。非常感谢任何帮助。

  setprecision(4) << node->getValue().getMarks()[i] 

我可以提供任何其他可能有用的文件。我只是希望我的错误主要在某个地方。

最佳答案

你给了我们很多代码来完成一个简单的格式问题,但我看不到你在哪里告诉输出流式传输您希望如何格式化浮点值。这定义了默认格式,以便它会导致对于所有可能的值都是可读的,但在实践中,对于任何特定的值集,它都不是您想要的(除了可能在日志文件中或出于序列化目的)。在你的情况(以及许多较小的程序),你可能只需将浮点格式设置为固定即可:

ofile.setf( std::ios_base::fixed, std::ios_base::floatfield );

更一般地说,最佳做法是定义一个您需要的不同语义值的操纵器:例如:

std::ostream&
mark( std::ostream& dest )
{
dest.setf( std::ios_base::fixed, std::ios_base::floatfield );
dest.precision( 2 );
return dest;
}

然后你可以简单地写:

ofile << ... << mark << node->getValue().getMarks()[i] << ...

但是请注意,格式选项和精度都是黏;他们将保持不变,直到改变。在任何重要的代码,最好将它们重置为原来的样子之前。

关于c++ - 从二进制文件中读取 double(进入类) - 不显示小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15750190/

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