这是一个library.h的片段 顺便说一下这是一个library.h的片段。
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#pragma warning (disable:4267)
#pragma warning (disable:4311)
#pragma warning (disable:4313)
#pragma warning (disable:4244)
#pragma warning (disable:4996)
#pragma warning (disable:4800)
#pragma warning (disable:4018)
#pragma comment( lib, "library.lib" )
using namespace std;
Library.h是一个头文件,它是一个图形库,也包含已经包含的大部分C ++库,我需要打印来自database1.txt的所有值,顺便说一下,如果我的代码看起来像灾难没有编码C ++已经四年了。
#include <library.h>
#include <iterator>
#include <vector>
#include <sstream>
struct st {
double month;
double day;
double year;
double social;
string fname;
string lname;
double address;
}roll [14];
void rawr (st dat);
void main()
{
ifstream meow("database1.txt");
{
if (meow.fail())
cout << "Not enough data in the file!, NOOB";
}
int n;
while (meow){
double d;
string e, f;
double g;
double a, x, c;
meow>>a;
meow>>x;
meow>>c;
meow>> d;
meow>> e;
meow>> f;
meow>>g;
cout << a<<" "<<x<<" "<< c<< " "<<d<<" "<<e<<" "<<f<<" "<<g;
for (n=0; n<14; n++)
{
stringstream(a)>> roll[n].month;
stringstream(x)>> roll[n].day;
stringstream(c)>> roll[n].year;
stringstream(d)>> roll[n].social;
stringstream(e)>> roll[n].fname;
stringstream(f)>> roll[n].lname;
stringstream(g)>> roll[n].address;
}
for (n=0; n<14; n++)
rawr (roll[n]);
}
}
void rawr (st dat)
{
cout << dat.month<<" "<<dat.day<<" "<< dat.year<< " "<<dat.social<<" "<<dat.fname<<" "<<dat.lname<<" "<<dat.address;
}
答案 0 :(得分:1)
我认为你应该在担心库标题之前让你的简单程序工作。你的程序有太多问题。让我们更喜欢更简单的方法:
struct Record
{
unsigned int month; // Month's can't be negative nor floating point.
unsigned int day;
unsigned int year;
std::string social; // Social has form xxx-xx-xxxx, so it can't be floating point.
std::string first_name;
std::string last_name;
std::string address;
};
要更轻松地从流中输入,请过载operator >>
:
struct Record
{
// Same as above
friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream& operator>>(std::istream& input, Record& r)
{
input >> r.month;
input >> r.day;
input >> r.year;
input >> r.social;
input >> r.first_name;
input >> r.last_name;
input >> r.address;
return input;
}
重载operator>>
会使输入变得更加容易:
int main(void)
{
std::ifstream database_stream("database1.txt");
if (!database_stream)
{
std::cerr << "Error opening database1.txt\n";
return EXIT_FAILURE;
}
// This code will be execute if the file was opened successfully.
std::vector<Record> database;
Record r;
while (database_stream >> r)
{
database.push_back(r);
}
return EXIT_SUCCESS;
}
无需stringstream
。 Record
的输入功能已封装到Record
对象中,从而简化了main
功能。
operator<<
也可以在Record
结构中以类似的方式重载main
。这也将简化month
功能。
编辑1:重构
day
,year
和into a separate
可以是*重构struct Date
{
unsigned int month;
unsigned int day;
unsigned int year;
friend std::istream& operator>>(std::istream& input, Date& d);
friend std::ostream& operator>>(std::ostream& output, const Date& d);
};
std::istream&
operator>>(std::istream& input, Date& d)
{
input >> d.month >> d.day >> d.year;
return input;
}
std::ostream&
operator<<(std::ostream& output, const Date& d)
{
output << d.month << "\t" << d.day << "\t" << d.year;
return output;
}
Date`类(可以在其他作业中重复使用):
Record
您的struct Record
{
Date d;
std::string social; // Social has form xxx-xx-xxxx, so it can't be floating point.
std::string first_name;
std::string last_name;
std::string address;
friend std::istream& operator>>(std::istream& input, Record& r);
然后简化为:
Record::operator>>
};
您的std::istream&
operator>>(std::istream& input, Record& r)
{
input >> r.d >> r.social >> r.first_name >> r.last_name >> r.address;
return input;
}
方法简化为:
Date
如果将Date.cpp
结构放入单独的文件中,则可以通过将头文件包含到其他源文件中来重复使用它,然后将<!--data: [[{id: 'A'},{id: 'B'},{id: 'C'}], [{id: 'X'},{id: 'Y'}]]-->
<div ng-repeat = "item in data">
<div ng-repeat = "sub-item in item">
<div id = "{{$index}}">
</div>
</div>
文件添加到项目中。无需浪费时间重写或测试它。 : - )