-
Notifications
You must be signed in to change notification settings - Fork 0
/
学生信息录入.txt
106 lines (101 loc) · 2.55 KB
/
学生信息录入.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//用"-"将信息分开,可以乱序录入,输入学生的姓名,学院,性别,年龄。
#include<iostream>
#include<vector>
#include <Windows.h>
#include <string>
#include<fstream>
using namespace std;
class stu
{
public:
string age;
string sex;
string xueyuan;
string name;
void sex1(vector<string>::iterator it);
void age1(vector<string>::iterator it);
void name1(vector<string>::iterator it);
void xueyuan1(vector<string>::iterator it);
};
void complete(string a,int b,int c,stu &s)//b是字符串第一个元素位置,c是'-'
{
//姓名没什么特征,年龄占两个字节全为数字,性别占两个字节(有的汉字是两个字节有的是三个)
//且汉字的编码为Unicode(vs2019)
if (c - b == 2&&(a[b]<0||a[b]>127))
{
s.sex = a.substr(b, c - b);
}
else if (a[b]>='0' && a[b] <= '9')s.age = a.substr(b, c - b);
else if (a.substr(c-4,4)=="学院")s.xueyuan = a.substr(b, c -b);
else
{
s.name = a.substr(b, c - b);
}
}
void separate(string a,stu &s)
{
int b = 0;
for (int i = 0;; ++i)
{
if (a[i] == '-')
{
if (b == 0)
{
complete(a,b, i, s);
}
else
{
complete(a, b + 1, i, s);
}
b = i;
}
if (i == size(a))
{
complete(a, b + 1, i + 1, s);
break;
}
}
}
void presentation(vector<stu>students, int a)
{
cout << "----------------------------------------" << endl;
cout << "姓名----年龄----性别----学院------------" << endl;
cout << "----------------------------------------" << endl;
for (int i = 0; i < a; ++i)
cout << students[i].name << "\t" << students[i].age << "\t" <<
students[i].sex << "\t" << students[i].xueyuan << endl;
}
void filein(fstream &file,stu s)
{
file << s.name << "\t" << s.age << "\t" <<
s.sex << "\t" << s.xueyuan << endl;
}
int is_chinese(string str)
{
int i;
if (strlen(str) < 2) return 0;
for (i = 0; i < strlen(str); i++)
{
if (str[i] < 161 || str[i] == 255) return 0;
if (!(i % 2) && (str[i] < 176 || str[i] >= 248)) return 0;
}
return 1;
}
int main()
{
vector<stu>students;
string s;
stu stus;
fstream file("example.txt", ios::out);
file << "----------------------------------------" << endl;
file << "姓名----年龄----性别----学院------------" << endl;
file << "----------------------------------------" << endl;
while (getline(cin,s))
{
if (s == "exit")break;
separate(s, stus);
students.push_back({ stus.age,stus.sex,stus.xueyuan,stus.name });
filein(file, stus);
}
presentation(students, size(students));
}