//
ForChypre.cpp : 定义控制台应用程序的入口点。
//
/*
假定文本文件a1.txt中是某高校所有参加住宅小区挑房职工的信息,
请编写程序,读出文件中的内容,再按挑房的先后次序排队后将排序号和
姓名以文本方式存放到文件a2.txt中。排队原则:先按职称排,同职称按
分房工龄排,同工龄按年龄排。职称编号:校级干部 0教授、正处
级 1副教授、副处级 2讲师、科级 3其他
*/
#include " stdafx.h "
#include < iostream >
#include < fstream >
#include < sstream >
#include < vector >
using namespace std;
class Info
{
public :
bool operator > ( const Info & info2)
{
if (turnTitle2number( this -> title) < turnTitle2number(info2.title) || this -> worktime > info2.worktime || this -> age > info2.age)
{
return true ;
}
else
return false ;
}
int turnTitle2number( string title)
{
string l[][ 3 ] = {{ " 教授 " , " 正处级 " },{ " 副教授 " , " 副处级 " },{ " 讲师 " , " 科级 " }};
for ( int i = 0 ;i < sizeof (l) / 3 * sizeof ( string );i ++ )
{
for ( int j = 0 ;j < 3 ;j ++ )
{
if (title == l[i][j])
return i; // 将职称转换成相应的数字,方便比较大小,职称越小则数字越大
}
}
}
public :
string pname; // 姓名
string title; // 职称
int age; // 年龄
int worktime; // 工龄
};
void fileRead( string fileName,vector < Info >& vec)
{
ifstream file(fileName);
string strTemp;
while ( ! file.eof()) // 如果不是文件末尾,则读出信息
{
Info info;
getline(file,strTemp); // 一行信息
istringstream str(strTemp); // 分割一行信息,以空格为分隔符
string strWord; // 存储单个单词的单元
// vector <string>vect;
int i = 0 ;
while (str >> strWord)
{
switch (i)
{
case 0 :
info.pname = strWord;
break ;
case 1 :
info.title = strWord;
break ;
case 2 :
info.worktime = atoi(strWord.c_str());
break ;
case 3 :
info.age = atoi(strWord.c_str());
break ;
}
i ++ ;
/*
if(strWord!=""&&strWord!="\r")
vect.push_back(strWord);//存入vector中. */
}
vec.push_back(info);
}
}
void sort(Info info[], int len,vector < Info > & vect)
{
int i = 0 ;
for (vector < Info > ::iterator itr = vect.begin();itr != vect.end();itr ++ ) // itr 为指针
{
info[i] =* itr;
i ++ ;
}
for ( int i = 0 ;i < len;i ++ )
{
for ( int j = i;j < len;j ++ )
{
if ( ! (info[i] > info[j]))
{
Info temp = info[i];
info[i] = info[j];
info[j] = temp; // 交换
}
}
}
vect.clear();
for ( int i = 0 ;i < len;i ++ )
{
vect.push_back(info[i]); // 入栈
}
}
void fileWrite( string & fileName,vector < Info >& vect)
{
ofstream file(fileName.c_str());
Info temp;
for (vector < Info > ::iterator itr = vect.begin();itr != vect.end();itr ++ )
{
temp =* itr;
string str;
str.append(temp.pname);
str.append( " " );
str.append(temp.title);
str.append( " " );
char str2[ 100 ];
itoa(temp.worktime, str2, 10 ); // (源,目标字符串,进制)
str.append(str2);
str.append( " " );
itoa(temp.age, str2, 10 );
str.append(str2);
str.append( " \n " );
file << str;
}
}
void main()
{
string a = " a1.txt " ;
string b = " a2.txt " ;
vector < Info > vect;
fileRead(a,vect); // a读出
Info * techers = new Info[vect.size()];
sort(techers,vect.size(),vect);
fileWrite(b,vect); // 写入b
}
//
/*
假定文本文件a1.txt中是某高校所有参加住宅小区挑房职工的信息,
请编写程序,读出文件中的内容,再按挑房的先后次序排队后将排序号和
姓名以文本方式存放到文件a2.txt中。排队原则:先按职称排,同职称按
分房工龄排,同工龄按年龄排。职称编号:校级干部 0教授、正处
级 1副教授、副处级 2讲师、科级 3其他
*/
#include " stdafx.h "
#include < iostream >
#include < fstream >
#include < sstream >
#include < vector >
using namespace std;
class Info
{
public :
bool operator > ( const Info & info2)
{
if (turnTitle2number( this -> title) < turnTitle2number(info2.title) || this -> worktime > info2.worktime || this -> age > info2.age)
{
return true ;
}
else
return false ;
}
int turnTitle2number( string title)
{
string l[][ 3 ] = {{ " 教授 " , " 正处级 " },{ " 副教授 " , " 副处级 " },{ " 讲师 " , " 科级 " }};
for ( int i = 0 ;i < sizeof (l) / 3 * sizeof ( string );i ++ )
{
for ( int j = 0 ;j < 3 ;j ++ )
{
if (title == l[i][j])
return i; // 将职称转换成相应的数字,方便比较大小,职称越小则数字越大
}
}
}
public :
string pname; // 姓名
string title; // 职称
int age; // 年龄
int worktime; // 工龄
};
void fileRead( string fileName,vector < Info >& vec)
{
ifstream file(fileName);
string strTemp;
while ( ! file.eof()) // 如果不是文件末尾,则读出信息
{
Info info;
getline(file,strTemp); // 一行信息
istringstream str(strTemp); // 分割一行信息,以空格为分隔符
string strWord; // 存储单个单词的单元
// vector <string>vect;
int i = 0 ;
while (str >> strWord)
{
switch (i)
{
case 0 :
info.pname = strWord;
break ;
case 1 :
info.title = strWord;
break ;
case 2 :
info.worktime = atoi(strWord.c_str());
break ;
case 3 :
info.age = atoi(strWord.c_str());
break ;
}
i ++ ;
/*
if(strWord!=""&&strWord!="\r")
vect.push_back(strWord);//存入vector中. */
}
vec.push_back(info);
}
}
void sort(Info info[], int len,vector < Info > & vect)
{
int i = 0 ;
for (vector < Info > ::iterator itr = vect.begin();itr != vect.end();itr ++ ) // itr 为指针
{
info[i] =* itr;
i ++ ;
}
for ( int i = 0 ;i < len;i ++ )
{
for ( int j = i;j < len;j ++ )
{
if ( ! (info[i] > info[j]))
{
Info temp = info[i];
info[i] = info[j];
info[j] = temp; // 交换
}
}
}
vect.clear();
for ( int i = 0 ;i < len;i ++ )
{
vect.push_back(info[i]); // 入栈
}
}
void fileWrite( string & fileName,vector < Info >& vect)
{
ofstream file(fileName.c_str());
Info temp;
for (vector < Info > ::iterator itr = vect.begin();itr != vect.end();itr ++ )
{
temp =* itr;
string str;
str.append(temp.pname);
str.append( " " );
str.append(temp.title);
str.append( " " );
char str2[ 100 ];
itoa(temp.worktime, str2, 10 ); // (源,目标字符串,进制)
str.append(str2);
str.append( " " );
itoa(temp.age, str2, 10 );
str.append(str2);
str.append( " \n " );
file << str;
}
}
void main()
{
string a = " a1.txt " ;
string b = " a2.txt " ;
vector < Info > vect;
fileRead(a,vect); // a读出
Info * techers = new Info[vect.size()];
sort(techers,vect.size(),vect);
fileWrite(b,vect); // 写入b
}