目录
一:案例要求:
二:代码:(多文件形式)
1:main.h
2 main.cpp
4 功能1用户信息的增删改查.h
5功能1用户信息的增删改查.cpp
6功能2运动信息管理.h
7功能2运动信息管理.cpp
8功能3运动排行榜.h
9功能3运动排行榜.cpp
10功能4定制运动路线.h
11功能4定制运动路线.cpp
12功能5 数据文件操作.h
13功能5 数据文件操作.cpp
一:案例要求:
二:代码:(多文件形式)
1:main.h
#ifndef _MENU_ALL_H_
#define _MENU_ALL_H_
#include<string.h>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int status;
#define MAXSIZE 100
typedef struct {
string idcard;
string name;
string sex;
int age;
int step[7];
double averagesteps;
//int day;
}people;
typedef struct{
people *elem;
int length;
}user;
void menuall();
#endif
2 main.cpp
#include "menu_all.h"
#include "reach1.h"
//void reach1(user &users);
void reach2(user &users);
void reach3(user &users);
void reach4(user &users);
void reach5(user &users);
int main() {
user users;
users.elem=new people[MAXSIZE];
users.length=0;
int flag = 1;
while (1) {
menuall();
int x = -1;
cout << "请选择:";
cin >> x;
switch (x) {
case 0:exit(0);
case 1: system("cls");
reach1(users);
if (flag == 0) {
cout << "请先完成数据加载!"; break;
}break;
case 2:system("cls");
reach2(users);
if (flag == 0) {
cout << "请先完成数据加载!"; break;
} system("cls"); break;
case 3: system("cls");reach3(users);
if (flag == 0) {
cout << "请先完成数据加载!"; break;
} system("cls"); break;
case 4: system("cls");reach4(users);system("cls");
if (flag == 0) {
cout << "请先完成数据加载!"; break;
} break;
case 5: system("cls");
reach5(users);system("cls");break;
default: cout << "Input Error"<<endl; break;
}
}
system("pause");
system("cls");
return 0;
}
3 menu.cpp(这个可以拆开写在每一个.cpp里面)
#include "menu_all.h"
void menuall()
{
cout << "************手机运动信息管理系统************" << endl;
cout << endl;
cout << " 1,用户信息管理 2,运动信息管理 " << endl;
cout << " 3,运动排行榜 4,定制运动路线 " << endl;
cout << " 5,读取数据文件 0,退出 " << endl;
cout << endl;
cout << "********************************************" << endl;
}
void menu1()
{
cout << "**************用户信息管理**************" << endl;
cout << endl;
cout << " 1,添加用户 2,删除用户 " << endl;
cout << " 3,查询用户 4,修改用户 " << endl;
cout << " 0,返回上一级 5,显示全部用户 " << endl;
cout << endl;
cout << "****************************************" << endl;
}
void menu2()
{
cout << "**************运动信息管理**************" << endl;
cout << endl;
cout << " 1,添加用户运动信息 " << endl;
cout << " 2,修改用户运动信息 " << endl;
cout << " 3,显示所有用户运动信息 " << endl;
cout << " 0,返回上一级 " << endl;
cout << endl;
cout << "****************************************" << endl;
}
void menu3()
{
cout << "**************运动排行榜*************" << endl;
cout << endl;
cout << " 1,当天排行榜 " << endl;
cout << " 2,连续运动排行榜 " << endl;
cout << " 3,指定用户的一周排行榜 " << endl;
cout << " 0,返回上一级 " << endl;
cout << endl;
cout << "*************************************" << endl;
}
void menu4()
{
cout << "**************定制运动路线**************" << endl;
cout << endl;
cout << " 1,绘制运动路线 " << endl;
cout << " 2,选择运动路线 " << endl;
cout << " 0,返回上一级 " << endl;
cout << endl;
cout << "****************************************" << endl;
}
void menu5()
{
int n;
cout << "**************读取数据文件**************" << endl;
cout << endl;
cout << " 1,数据加载 " << endl;
cout << " 2,数据保存 " << endl;
cout << " 0,返回上一级 " << endl;
cout << endl;
cout << "****************************************" << endl;
}
4 功能1用户信息的增删改查.h
#ifndef ff
#define ff
//用户信息的增删改查
void menu1();
void reach1(user &users);
bool add_1(user &users);
bool delete_1(user &users);
bool find_1(user &users);
bool modify_1(user &users);
void output(user users);
#endif
5功能1用户信息的增删改查.cpp
#include "menu_all.h"
#include "reach1.h"
#include "reach5.h"
void reach1(user &users) {
while (1) {
menu1();
int x = -1;
bool flag = false;
cout << "请选择:";
cin >> x;
switch (x)
{
case 1:
if (add_1(users)) {
cout << "成功添加用户" << endl;
}
else {
cout << "添加用户失败" << endl;
} break;
case 2:
if (delete_1(users)) {
cout << "成功删除用户" << endl;
}
else {
cout << "删除用户失败" << endl;
} break;
case 3:
if (find_1(users)) {
cout << "成功查找用户" << endl;
}
else {
cout << "查找用户失败" << endl;
} break;
case 4:
if (modify_1(users)) {
cout << "成功修改用户信息" << endl;
}
else {
cout << "修改用户信息失败" << endl;
} break;
case 0: system("cls"); return;
case 5: output(users); break;
default:cout << "Input Error"; break;
}
system("pause");
system("cls");
}
}
bool add_1(user &users) {
people t1;
cout << "请决定添加几个用户信息:";
int x = -1,flag=0;
cin >> x;
string idcard;
for (int i = 0; i < x; i++) {
cout << "用户名(手机号(11位)):";
while (true) {
cin >> idcard;
if (idcard[0] == '1' && idcard.length() == 11) {
for (int j=0;j<users.length;j++) {//唯一性
if (users.elem[i].idcard==idcard) {
cout << "用户名重复,请重新输入:";
flag = 1;
}
}
if (flag == 0) {
t1.idcard = idcard; break;
}
}
else cout << "输入错误(手机号首位1,长度11),请重新输入:";
}
cout << "请输入昵称(4~12个):";
flag = 0;string name;
while (true) {
cin >> name;
if (name.length() >= 4 && name.length() <= 12) {
for (int j = 0; j < users.length;j++) {//唯一性
if (users.elem[j].name==name) {
cout << "昵称重复,请重新输入:";
flag = 1;
}
}
if (flag == 0) t1.name = name; break;
}
else cout << "输入错误(长度(4~12)),请重新输入:";
}flag = 0;
cout << "请输入性别:";string sex;
while (true) {
cin >> sex;
if (sex == "男" || sex == "女") {
t1.sex = sex; break;
}
else cout << "性别有误,请重新输入:";
}
cout << "请输入年龄:";int age;
while (true) {
cin >> age;
if (age < 0 || age>200) {
cout << "输入错误,请重新输入:";
}
else {
t1.age = age; break;
}
}
for(int j=1;j<=7;j++){
t1.step[j-1]=0;
}
t1.averagesteps=0;
write(t1);
users.elem[users.length++]=t1;
}
return true;
}
bool delete_1(user &users) {
cout << "请输入要删除用户的手机号或者昵称:(0/1)";
int x=-1;
cin>>x;
int flag = 0,k=0;
string target;
switch(x){
case 0:
cout<<"请输入手机号:";
cin >> target;
for (int i = 0; i < users.length; i++) {
if (target.compare(users.elem[i].idcard) != 0) {
users.elem[k++]=users.elem[i];
}else flag=1;
}
if(flag==1)
users.length--;break;
case 1:
cout<<"请输入昵称:";
cin>>target;
for(int i=0;i<users.length;i++){
if(target.compare(users.elem[i].name)!=0){
users.elem[k++]=users.elem[i];
}else flag=1;
}
if(flag==1)
users.length--;break;
default: cout<<"Input Error"<<endl;
}
if (flag == 1) return true;
return false;
}
void menu1_1() {
cout << "===============查询方式===================" << endl;
cout << " 1用户名 2性别 3昵称 " << endl;
cout << " 0返回上一级" << endl;
cout << "==========================================" << endl;
}
bool find_1(user &users) {
menu1_1();
int x = -1,flag=0;
string idcard,name;
while (true) {
cout << "选择查询方式:";
cin >> x;
switch (x) {
case 0:return true;
case 1:
cout << "请输入用户名:";
cin >> idcard;
for (int i=0;i<users.length;i++) {
if (users.elem[i].idcard.compare(idcard)==0) {
cout << "用户名: " << users.elem[i].idcard << " 昵称:" << users.elem[i].name << " 性别:" << users.elem[i].sex
<< " 年龄:" << users.elem[i].age <<endl; flag = 1;
}
}if (flag == 0)cout << "查无此人"<<endl; break;
case 2:
cout << "请输入昵称:";
cin >> name;
for (int i=0;i<users.length;i++) {
if (users.elem[i].name.compare(name)==0) {
cout << "用户名: " << users.elem[i].idcard << " 昵称:" << users.elem[i].name << " 性别:" << users.elem[i].sex
<< " 年龄:" << users.elem[i].age <<endl; flag = 1;
}
}if (flag == 0)cout << "查无此人"<<endl; break;
deault: cout << "输入错误,请重新输入:" << endl; break;
}
}
if(flag==1)
return true;
return false;
}
bool modify_1(user &users) {
people t1;
int flag = 0,x=-1,flag1=0;
cout << "请输入待修改的用户名:";
string target;
cin >> target;
for (int i = 0; i < users.length; i++) {
if (target.compare(users.elem[i].idcard) == 0) {//这个地方可以和add_1部分功能复用,可以改进
flag1=1;
cout << "是否修改用户名(0:no 1yes):";
cin >> x;
if (x == 1) {
cout << "用户名(手机号(11位)):";string idcard;
while (true) {
cin >> idcard;
if (idcard[0] == '1' && idcard.length() == 11) {
for (int j=0;j<users.length;j++) {//唯一性
if (users.elem[j].idcard.compare(idcard) == 0) {
cout << "用户名重复,请重新输入:";
flag = 1;break;
}
}
if (flag == 0) {
users.elem[i].idcard = idcard; break;
}
}
else cout << "输入错误(手机号首位1,长度11),请重新输入:";
}
}
cout<<"是否修改昵称(0no/1yes):";
cin>>x;
if(x==1){
cout<<"修改昵称为:";string name;
while (true) {
cin >> name;
if (name.length() >= 4&& name.length() <= 12) {
for (int j=0;j<users.length;j++) {//唯一性
if (users.elem[j].name.compare(name) == 0) {
cout << "昵称重复,请重新输入:";
flag = 1;break;
}
}
if (flag == 0) {
users.elem[i].name = name; break;
}
}
else cout << "输入错误(手机号首位1,长度11),请重新输入:";
}
}
cout<<"是否修改性别(0no/1yes):";
string sex;
cin>>x;
if(x==1){
while(true){
cout<<"请输入性别:";
cin>>sex;
if(sex=="男"||sex=="女"){
users.elem[i].sex=sex;break;
}else{
cout<<"输入性别有误,请重新输入!"<<endl;
}
}
}
cout<<"是否修改年龄(0no/1yes):";
int age;
cin>>x;
if(x==1){
while(true){
cout<<"请输入待修改的年龄:";
cin>>age;
if(age>=0&&age<=120){
users.elem[i].age=age;break;
}else{
cout<<"年龄输入有误,请重新输入:"<<endl;
}
}
}
}
}
if(flag1==1)
return true;
return false;
}
void output(user users){
for(int i=0;i<users.length;i++){
cout << "用户名: " << users.elem[i].idcard << " 昵称:" << users.elem[i].name << " 性别:" << users.elem[i].sex
<< " 年龄:" << users.elem[i].age <<endl;
}
}
6功能2运动信息管理.h
#ifndef ea
#define ea
//运动信息管理
void menu2();
void reach2(user &users);
void show_2(user &users);
void modify_2(user &users);
void add_2(user &users);
int account(people t);
#endif
7功能2运动信息管理.cpp
#include "menu_all.h"
#include "reach2.h"
void reach2(user &users){
while(1){
menu2();
cout<<"请选择:";
int m;
cin>>m;
switch(m){
case 0:return ;
case 1:add_2(users);break;
case 2:modify_2(users);break;
case 3:show_2(users);break;
default:cout<<"Input Error"<<endl;break;
}
system("pause");
system("cls");
}
}
void add_2(user &users){
for(int i=0;i<users.length;i++){
int foot=0;
cout<<"请输入昵称为"<<users.elem[i].name<<"当天的运动步数:";
cin>>foot;
int k=0,sum=0,max=0,day=0;
for(int j=1;j<7;j++){
users.elem[i].step[k]=users.elem[i].step[j];
sum+=users.elem[i].step[k];
k++;
}
users.elem[i].step[k]=foot;
sum+=users.elem[i].step[k];
users.elem[i].averagesteps=sum/7.0;
}
}
void modify_2(user &users){
cout<<"请输入要修改步数的用户名:";
string target;
cin>>target;
int flag=0,foot=0;
for(int i=0;i<users.length;i++){
if(!users.elem[i].idcard.compare(target)){
flag=1;
cout<<"请输入修改的步数:";
cin>>foot;
users.elem[i].step[6]=foot;
}else{
int k=0;
for(int j=1;j<7;j++){
users.elem[i].step[k++]=users.elem[i].step[j];
}
users.elem[i].step[k]=0;
}
}
if(flag==0)
cout<<"查无此人"<<endl;
if(flag==1) cout<<"修改成功!"<<endl;
}
void show_2(user &users){
cout<<" 用户名"<<" 昵称 "<<" 性别 "<<" 年龄 "<<" 第一天 "<<" 第二天 "<<" 第三天 "
<<" 第四天 "<<" 第五天 "<<" 第六天 "<<" 第七天 "<<" 连续运动天数 "<<" 平均步数 "<<endl;
for(int i=0;i<users.length;i++){
cout<<left<<users.elem[i].idcard<<" "<<left<<users.elem[i].name<<" "<<left<<users.elem[i].sex<<" "<<left<<users.elem[i].age<<" ";
for(int j=0;j<7;j++){
cout<<left<<users.elem[i].step[j]<<" ";
}
cout<<" "<<account(users.elem[i])<<" "<<users.elem[i].averagesteps<<endl;
}
}
int account(people t){//计算连续运动天数
int max=0,num=0;
for(int i=0;i<7;i++){
if(t.step[i]>0){
num++;
if(max<num) max=num;
}else{
if(max<num) {
max=num;num=0;
}else num=0;
}
}
return max;
}
8功能3运动排行榜.h
#ifndef eh
#define eh
//运动排行榜
void menu3();
void reach3(user &users);
void sort_1(user users);
void sort_2(user users);
void sort_3(user users);
#endif
9功能3运动排行榜.cpp
#include "menu_all.h"
#include "reach3.h"
void reach3(user &users){
while(1){
menu3();
cout<<"请选择排序方式:";
int x=-1;
cin>>x;
switch(x){
case 0: return ;
case 1: sort_1(users);break;
case 2: sort_2(users);break;
case 3: sort_3(users);break;
default:cout<<"Input Error"<<endl;break;
}
system("pause");
system("cls");
}
}
void sort_1(user users){
cout<<"按照当天运动步数排行:"<<endl;
cout<<" 用户名 "<<" 昵称 "<<" 当天运动步数:"<<endl;
for (int i = 0; i < users.length-1; i++) {
int min = i;
for (int j = i; j < users.length; j++) {
if (users.elem[min].step[6] < users.elem[j].step[6]) {
min = j;
}
}
if (min != i) {
swap(users.elem[i],users.elem[min]);
}
}
for(int i=0;i<users.length;i++){
cout<<users.elem[i].idcard<<" "<<users.elem[i].name<<" "<<users.elem[i].step[6]<<endl;
}
}
void sort_2(user users) {
cout<<"按照连续运动天数排行:"<<endl;
cout<<" 用户名 "<<" 昵称 "<<" 连续运动天数:"<<endl;
for (int i = 0; i < users.length-1; i++) {
int min = i;
for (int j = i; j < users.length; j++) {
if (users.elem[min].day < users.elem[j].day) {
min = j;
}
}
if (min != i) {
swap(users.elem[i],users.elem[min]);
}
}
for(int i=0;i<users.length;i++){
cout<<users.elem[i].idcard<<" "<<users.elem[i].name<<" "<<users.elem[i].day<<endl;
}
}
void sort_3(user users){
cout<<"按照一周总步数排行:"<<endl;
cout<<" 用户名 "<<" 昵称 "<<" 平均步数:";
for (int i = 0; i < users.length-1; i++) {
int min = i;
for (int j = i; j < users.length; j++) {
if (users.elem[min].averagesteps < users.elem[j].averagesteps) {
min = j;
}
}
if (min != i) {
swap(users.elem[i],users.elem[min]);
}
}
for(int i=0;i<users.length;i++){
cout<<users.elem[i].idcard<<" "<<users.elem[i].name<<" "<<users.elem[i].averagesteps<<endl;
}
}
10功能4定制运动路线.h
#ifndef ge
#define ge
//定制运动路线
#define Maxint 32767//表示无穷大
#define MVNum 20//最大顶点数
typedef char VerTexType;//设顶点的数据类型为字符型
typedef double ArcType;//假设边的权值类型为浮点型
typedef struct {
VerTexType vexs[MVNum][50];//顶点表
ArcType arcs[MVNum][MVNum];//邻接矩阵
int vexnum, arcnum;//图的当前点数和边数
}AMGraph;
void menu4();
void reach4(user &users);
status LocateVex(AMGraph G, char* u);
status Selectgraph(AMGraph G,char *s);
void draw(user users,AMGraph& G);
void choice(user users,AMGraph G);
status Readtxtgraph(AMGraph& G);
#endif
11功能4定制运动路线.cpp
#include"menu_all.h"
#include "reach4.h"
status Readtxtgraph(AMGraph& G)
{
int i = 0, j, k;
double w;
int s;
char s1[50], s2[50];
fstream f;
f.open("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu1.txt", ios::in);
while (f >> s >> G.vexs[i])
{
i++;
}
f.close();
G.vexnum = i;
for (i = 0; i < G.vexnum; i++)
for (j = 0; j < G.vexnum; j++)
G.arcs[i][j] = Maxint;//边的权值均设置为无穷大
i = 0;
f.open("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu2.txt", ios::in);
while (f >> s1 >> s2 >> w)
{
k = LocateVex(G, s1);
j = LocateVex(G, s2);//确定v1,v2在G中的位置
G.arcs[k][j] = w;
G.arcs[j][k] = w;
i++;
}
f.close();
G.arcnum = i;
cout << "运动地点名称:" << endl;
for (i = 0; i < G.vexnum; i++)
{
cout << G.vexs[i];
cout << endl;
}
cout << "各运动地点间距离得出的邻接矩阵:" << endl;
for (i = 0; i < G.vexnum; i++)
{
for (j = 0; j < G.vexnum; j++)
{
if (G.arcs[i][j] == 32767)
cout << left << setw(4) << "∞";
else
cout << left << setw(4) << G.arcs[i][j];
}
}
return 1;
}
status LocateVex(AMGraph G, char* u)
{
int o;
for (o = 0; o < G.vexnum; o++)
{
if (strcmp(G.vexs[o], u) == 0)
return o;
}
return -1;
}
void draw(user users,AMGraph& G){
if(Readtxtgraph(G)){
cout<<"绘制完成"<<endl;
}
return ;
}
void choice(user users,AMGraph G){
char s[50];
cout<<"请输入运动起点:";
cin>>s;
Selectgraph(G,s);
}
status Selectgraph(AMGraph G,char *s)
{
int n;
for(n=0;n<G.vexnum;n++)
{
if(strcmp(G.vexs[n],s)==0)
break;
}
if(n==G.vexnum)
return 0;
int p[G.vexnum][G.vexnum];
int i,S[G.vexnum],j,k;
int a[G.vexnum];
int num=1,u;
double dist[G.vexnum],wmin;
for(i=0;i<G.vexnum;i++)
{
a[i]=-1;
}
for(i=0;i<G.vexnum;i++)
{
for(j=0;j<G.vexnum;j++)
{
p[i][j]=-1;
}
}
for(i=0;i<G.vexnum;i++)
{
dist[i]=G.arcs[n][i];
S[i]=0;
}
S[n]=1;
for(i=0;i<G.vexnum;i++)
{
p[i][0]=n;
}
do
{
wmin=Maxint;
u=n;
for(i=0;i<G.vexnum;i++)
{
if(S[i]==0)
{
if(dist[i]<wmin)
{
u=i;
wmin=dist[i];
}
}
}
S[u]=1;
for(i=0;i<G.vexnum;i++)
{
if(p[u][i]==-1)
{
p[u][i]=u;
break;
}
}
for(i=0;i<G.vexnum;i++)
{
if(S[i]==0)
{
if(dist[u]+G.arcs[u][i]<dist[i])
{
k=1;
for(j=0;j<G.vexnum;j++)
{
if(p[i][j]==-1&&a[i]==-1)
a[i]=j;
if(p[i][j]==-1)
{
break;
}
}
j=a[i];
while(p[u][k]!=-1)
{
p[i][j]=p[u][k];
k++;
j++;
}
dist[i]=dist[u]+G.arcs[u][i];
}
if(num==G.vexnum-2)
p[i][j]=i;
}
}
num++;
}while(num!=G.vexnum-1);
for(i=0;i<G.vexnum;i++)
{
j=0;
if(i==n)
continue;
else
{
while(p[i][j]!=-1)
{
cout<<left<<setw(16)<<G.vexs[p[i][j]];
j++;
}
cout<<dist[i]<<endl;
}
}
return 1;
}
void reach4(user &users){
AMGraph G;
while(1){
menu4();
cout<<"请输入选择:";
int x;
cin>>x;
switch(x){
case 0: return ;
case 1:draw(users, G);break;
case 2:choice(users,G);break;
default:cout<<"Input Error"<<endl;break;
}
system("pause");
system("cls");
}
}
12功能5 数据文件操作.h
#ifndef geh
#define geh
//数据文件操作
void menu5();
status reach5(user &users);
status read(user &users);
status write(people t1);
status writeall(user &users);
#endif
13功能5 数据文件操作.cpp
#include "menu_all.h"
#include "reach5.h"
string idcard;
string name;
string sex;
int age;
double averagesteps;
status reach5(user &users) {
while(1){
menu5();
cout << "请选择:";
int x=-1;
cin >> x;
switch (x) {
case 0: system("cls");return 1;
case 1:read(users); break;
case 2:writeall(users);break;
default:cout << "Input Error,Please Rnew Input"<<endl; break;
}
system("pause");
system("cls");
}
return 1;
}
status read(user &users) {//读取用户信息
int n,j=0;
people t1;
fstream f("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu.txt",ios::in);
if (!f) //打开失败
{
cout << "文件打开失败!" << endl;
return 1;
}
else {
cout << "文件打开成功" << endl;
}
while (f >> t1.idcard>>t1.name >> t1.sex >> t1.age)
{
n = 0;
while (f >> t1.step[n])
{
n++;
if (n == 7)
break;
}
f >> t1.averagesteps;
f >> t1.day;
users.elem[j++]=t1;
users.length++;
}
f.close();
for (int i = 0; i <users.length; i++)
{
cout << setw(16) << users.elem[i].idcard;
cout << left << setw(8) << users.elem[i].name;
cout << left << setw(8) << users.elem[i].sex;
cout << left << setw(8) << users.elem[i].age;
for (int j = 0; j < 7; j++)
{
cout << left << setw(8) << users.elem[i].step[j];
}
cout << left << setw(16) << users.elem[i].averagesteps;
//cout << left << setw(8) << users.elem[i].day;
cout << endl;
}
return 1;
}
status write(people t1)//写临时文件,防止断电等紧急关闭
{
int i;
fstream f;
f.open("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu3.txt",ios::out|ios::app);
if(!f){
cout<<"文件打开失败!"<<endl; return 1;
}
f<<left<<setw(16)<<t1.idcard;
f<<left<<setw(8)<<t1.name;
f<<left<<setw(8)<<t1.sex;
f<<left<<setw(8)<<t1.age;
for(int j=0;j<7;j++)
{
f<<left<<setw(8)<<t1.step[j];
}
f<<left<<setw(16)<<t1.averagesteps;
f<<endl;
f.close();
return 1;
}
status writeall(user &users){
ifstream ifa;
people t1;
ifa.open("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu3.txt", ios::in);
char c;
ifa >> c;
if (ifa.fail())
{
//cout << "文件为空!" << endl;
ifa.close();
}else{
cout<<"检测到有临时数据是否恢复并保存(0no 1yes):";
while(1){
int x=-1,flag=0;
cin>>x;
switch(x){
case 1:
while (ifa >> t1.idcard>>t1.name >> t1.sex >> t1.age)
{
int n = 0;
while (ifa >> t1.step[n])
{
n++;
if (n == 7)
break;
}
ifa >> t1.averagesteps;
users.elem[users.length-1]=t1;
users.length++;
}cout<<"数据恢复成功!";flag=1;break;
case 0: flag=1;break;
default:cout<<"选择错误,请重新选择:"; break;
}
if(flag==1) break;
}
}
ifa.close();
fstream f;
f.open("C:\\Users\\a1474\\OneDrive\\桌面\\新建项目\\stu4.txt",ios::out);
if(!f){
cout<<"文件打开失败!"<<endl; return 1;
}
for( int i=0; i<users.length; i++) {
f<<left<<setw(16)<<users.elem[i].idcard;
f<<left<<setw(8)<<users.elem[i].name;
f<<left<<setw(8)<<users.elem[i].sex;
f<<left<<setw(8)<<users.elem[i].age;
for(int j=0;j<7;j++)
{
f<<left<<setw(8)<<users.elem[i].step[j];
}
f<<left<<setw(16)<<users.elem[i].averagesteps;
f<<endl;
}
f.close();
cout<<"文件保存成功!"<<endl;
return 1;
}