#include<iostream>
#include<string>
#include<fstream>
#include<windows.h>
#include<WinUser.h>
//这是基于数组完成的。对于数据结构还可用其他的。其中struct和class方便。
using namespace std;
int i = 0;//多少用户
char index = 'l';
string name[100][4];
char st;
void start();
void rn(char st)
{
if (st == 'r')
{
start();
}
else
{
cout<<"输入错误"<<endl;
cout << "提示:r:回到开始菜单" << endl;
cout << "重新输入:" ;
cin >> st;
rn(st);
}
}
//===========================================================
void initializationArray(string name[][4])
{
i = 0;
ifstream file;
string out = " ";
file.open("PlayerInfor.txt", ios::in);
while (file >> out)
{
//name[i][0] = out;
for (int j = 0; j <= 3; j++)
{
name[i][j] = out;
file >> out;
cout << "j:"<<j << ":"<<name[i][j] << endl;
}//离开这里,是自动再下一行
cout << "------" << endl;
i++;
}
cout << "初始化成功" << endl;
cout << "===========" << endl;
//cout <<"i:"<< i << endl;
/*for(int j=0;j<i;j++)
{
if (name[0][0] == '名' & name[0][1] == '字')
{
for (int k; k< size(out);k++)
{
if (out[k] == ':')
{
}
}
while (out[0] == '编' & out[1] == '号')
{
}
}
}*/
}
//=================================================
void add(string name[][4])//申明
{
cout << "添加" << endl;
ofstream file;
file.open("PlayerInfor.txt", ios::app);
string name1;
//=====================
cout << "注意不要有空格,名字:" ;
cin >> name1;//索引
name[i++][0] = "名字:" + name1;
file << "名字:" + name1 <<endl;
//cout << endl;
//===============
cout << "注意不要有空格,性别:" ;
cin >> name1;
name[i++][1] = " 性别:" + name1;
file << " 性别:" + name1 << endl;
//================
cout << "注意不要有空格,年龄:";
cin >> name1;
name[i++][2] = " 年龄:" + name1;
file << " 年龄:" + name1 << endl;
//===============
cout << "注意不要有空格,编号:";
cin >> name1;
name[i++][2] = " 编号::" + name1;
file << " 编号:" + name1 << endl;
file << "l" << endl;
i++;
cout << "添加成功" << endl;
cout << "===========" << endl;
}
//===================================================
int display(string name[][4], char in)
{
if (in == 'o')
{
for (int j = 0; j < i; j++)
{
for (int k = 0; k < 4; k++)
{
cout << endl;
cout << name[j][k] << endl;
}
}
return 0;
}
else if (in == 's')
{
char as;
cout << "输入a:使用名字查找,b:使用编号" << endl;
cin >> as;
if (as == 'a')
{
string appealation;
cout << "输入名字:" << endl;
cin >> appealation;
//cout << "i:"<<i << endl;
for (int j = 0; j < i; j++)
{
string nume;
cout <<"1:"<< appealation << endl;
int p = 6;
for (int p = 6; p < size(name[j][0]); p++)
{
nume += name[j][0][p];
//cout << "s:"<<nume << endl;//sizeof()
}
cout << "s:"<<nume << endl;//sizeof()
if (appealation == nume)
{
//cout << "1 "<< endl;
for (int k = 0; k < 4; k++)
{
cout << name[j][k] << endl;
}
return j;
}
//cout << "123" << endl;
}
cout << "寻找失败" << endl;
cout << "===========" << endl;
return -1;
}
else if (as == 'b')
{
string ine;
cout << "输入编号:" << endl;
cin >> ine;
for (int j = 0; j < i; j++)
{
string nu;
for (int p = 6; p < size(name[j][3]); p++)
{
nu += name[j][3][p];
//cout << "s:"<<size(name[j][3])<<"a:"<<nu << endl;
}
if (ine == nu)
{
for (int k = 0; k < 4; k++)
{
cout << name[j][k] << endl;
}
return j;
}
}
cout << "寻找失败" << endl;
cout << "===========" << endl;
return -1;
}
}
return -1;
}
//=====================================
void del(string name[][4])
{
int num=-1;
/*cout << "选择查找,a:用名字,b:用编号" << endl;
cin >> c;*/
num=display(name, 's');
cout << num << endl;
cout << "开始删除" << endl;
if (num != -1)
{
for (int q = num; q <= i; q++)
{
for (int w = 0; w < 4; w++)
{
name[q][w] = name[q + 1][w];
}
}
i--;
ofstream file;
file.open("PlayerInfor.txt", ios::out);
for (int q = 0; q <i; q++)
{
file << name[q][0] << endl;
for (int w = 1; w < 4; w++)
{
file << " " + name[q][w] << endl;
}
file << "l" << endl;
}
cout << "完成删除" << endl;
cout << "===========" << endl;
initializationArray(name);
}
else
{
cout << "删除失败" << endl;
cout << "===========" << endl;
}
}
void fix(string name[][4])
{
int num = -1;
string ne;
int dex;
num=display(name, 's');
cout << "寻找要修改的属性" << endl;
cout << "提示:" << endl;
cout << "0:名字" << "1:性别" << "2:年龄" << "3:编号" << "4:重新修改"<<"5:退出修改" << endl;
cout << "输入:";
cin >> dex;
//==============================================================
switch (dex)
{
case 0: {cout << "名字:";
cin >> ne;
name[num][0] = ne; };
case 1: {cout << "性别:";
cin >> ne;
name[num][1]; };
case 2: {cout << "年龄:";
cin >> ne;
name[num][2]; };
case 3: {cout << "编号:";
cin >> ne;
name[num][3]; };
case 4:fix(name);
case 5:return;
}
ofstream file;
file.open("PlayerInfor.txt", ios::out);
for (int q = 0; q < i; q++)
{
file << name[q][0] << endl;
for (int w = 1; w < 4; w++)
{
file << " " + name[q][w] << endl;
}
file << "l" << endl;
}
cout << "完成修改" << endl;
cout << "========" << endl;
}
void sort(string name[][4])
{
string sr="";
string nw[5];
for (int j = 0; j < i; j++)
{
for (int k = j + 1; k < i - j - 1; k++)
{
if (name[j][3][6] + name[j][3][7] + name[j][3][8] > name[k][3][6] + name[k][3][7] + name[k][3][8])
{
for (int p = 0; p < 4; p++)
{
nw[p] = name[k][p];
}
for (int p = 0; p < 4; p++)
{
name[k][p] = name[j][p];
}
for (int p = 0; p < 4; p++)
{
name[j][p] = nw[p];
}
}
}
}
ofstream file;
file.open("PlayerInfor.txt", ios::out);
for (int q = 0; q < i; q++)
{
file << name[q][0] << endl;
for (int w = 1; w < 4; w++)
{
file << " " + name[q][w] << endl;
}
file << "l" << endl;
}
cout << "完成排序" << endl;
cout << "========" << endl;
}
//====================================
void start()
{
while (index != 'o')
{
cout << "======" << endl;
cout << "开始菜单:" << endl;
cout << "a:添加" << "d:显示所有.S:根据名字或编号显示" << endl;
cout << "w:删除" << endl;
cout << "o:退出" << endl;
cout << "f:修改" << endl;
cout << "n:排序" << endl;
cout << "===========" << endl;
cin >> index;
switch (index)
{
case 'a':add(name);
case 'd':display(name, 'o');
case 's':display(name, 's');
case 'w':del(name);
case 'o':exit(0);
case 'f':fix(name);
case 'n':sort(name);
}
}
}
//==========================
int main()
{
//string name[100][4];
system("title marriage certificate");
system("color 4e");
initializationArray(name);
//char index='l';
//while (index!='o')
//{
// cout << "a:添加" << "d:显示所有.S:根据名字或编号显示" <<endl;
// cout << "w:删除" << endl;
// cout << "o:退出" << endl;
// cout << "===========" << endl;
// cin >> index;
// switch (index)
// {
// case 'a':add(name);
// case 'd':display(name,'o');
// case 's':display(name,'s');
// case 'w':del(name);
// }
//}
start();
cout << i << endl;
return 0;
}