// report.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
enum esex {male, female};
struct StuffInfo {
string name;
esex sex;
int age;
string ID;
};
int file_to_vector(char *name, vector<string> &svec)
{
string filename(name);
ifstream infile(filename.c_str());
if (!infile)
{
return 1;
}
string s;
while (infile >> s)
{
svec.push_back(s);
}
infile.close();
if (infile.eof())
{
return 4;
}
if (infile.bad())
{
return 2;
}
if (infile.fail())
{
return 3;
}
}
int main(int argc, char* argv[])
{
if (argc != 3)
{
cout << "you should enter three arguments" << endl;
return -1;
}
vector<string> svec;
vector<StuffInfo> stuff;
switch (file_to_vector(argv[1], svec))
{
case 1:
cout << "error: can not open file" << endl;
return -1;
case 2:
cout << "error: system failure" << endl;
return -1;
case 3:
cout << "error: read failure" << endl;
return -1;
}
vector<StuffInfo>::size_type is = 0;
vector<string>::size_type ix = 4;
const char *p = NULL;
cout << svec[ix] << endl;
while ((ix != svec.size()) && ((svec.size() - ix) / 3 >= 0))
{
stuff[is].name = svec[ix++];
if ((svec[ix] != "男") && (svec[ix] != "女"))
{
cout << "性别非法" << endl;
return -1;
}
if (svec[ix++] == "男")
{
stuff[is].sex = male;
}
else
{
stuff[is].sex = female;
}
stuff[is].age = atoi(svec[ix++].c_str());
p = svec[ix].c_str();
while (*p)
{
if ((*p > '9') || (*p < '0'))
{
cout << "ID非法" << endl;
return -1;
}
++p;
}
stuff[is].ID = svec[ix++];
++is;
}
int sum = 0;
int boy = 0;
int girl = 0;
for (int tmp = 0; tmp != is; ++tmp)
{
if (stuff[tmp].sex == male)
{
++boy;
++sum;
}
else
if (stuff[tmp].sex == female)
{
++girl;
++sum;
}
}
ofstream outfile;
outfile.open(argv[2], ofstream::app);
outfile << "总人数:" << sum << endl
<< "男生人数:" << boy << endl
<< "女生人数:" << girl << endl;
outfile.close();
return 0;
}
|