LOL导航图

#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<assert.h> //用于getNum中的debug
#include<string.h>
#include<fstream>//用于i ofstream
#include <vector>
#define MAX 123456
#define LEN2 sizeof(struct land)//有关登录系统的结构体
using namespace std;
struct land
{
    int zhanghao;
    char password[20];
    struct land *next;
};
void land();//登录功能系统
void xinjian();//创建账号密码
void xgmm();//修改密码
void lmain();//登录界面函数
typedef struct {
    char  info[100];
    int   value;
    bool   change;
    int   parent;
    char  name[100];
}dpvexinfo;//节点信息的结构体
typedef struct {
    int arcs[100][100];  //邻接矩阵
    int vexnums,edgnums; //节点总数,边总数
    dpvexinfo totalvex[100];  //总体的
}MGraph;

//                     登录系统(待做)
/*
 *   1.查询节点信息
 *   2.找寻从某个节点到某个节点的最短路径
 *   3.修改节点信息
 *   4.增加节点
 *   5.增加路径
 *   6.删除节点
 *   7.删除路径
*/
/* 修改文件某行内容
 输入:文件名 fileName   行号   lineNum ,修改的内容 content
 输出:文件名 fileName
 无返回值


 tip:
      1.lineNum从第一行开始
      2.content需要加上换行符
*/
void showmaker(MGraph &G){
    for (int i = 0; i < G.vexnums; ++i) {
        cout<<i+1<<":"<<G.totalvex[i].name<<"    增益:"<<G.totalvex[i].info<<endl;
    }
}
void sorrymaker(MGraph &G, int index){
    cout<<"您选择的资源是"<<G.totalvex[index-1].name<<",它提供的buff是"<<G.totalvex[index-1].info<<endl;
}
int getNum(MGraph &G,char *name){
    int i;
    for (int i = 0; i < G.vexnums; ++i) {
        if (strcmp(name,G.totalvex[i].name)==0){
            return i;
        }
    }
    if (i==G.vexnums){
        return -1;
    }
}
/*
 * 4 4
红buff   攻击敌人造成减速效果
蓝buff   击杀有回复蓝量增益
F6         获得大量金币
石头人   获得大量经验
蓝buff  红buff    50
蓝buff  F6          20
F6        石头人    10
石头人  红buff    10*/
void creatMap(MGraph &G){
    ifstream in("Graph.txt");
    in>>G.vexnums>>G.edgnums;
    for (int i = 0; i < G.vexnums; ++i) {
        in>>G.totalvex[i].name;
        in>>G.totalvex[i].info;
    }
    for (int i = 0; i < G.vexnums; ++i) {
        for (int j = 0; j < G.vexnums; ++j) {
            G.arcs[i][j]=MAX;
        }
    }
    for (int i = 0; i < G.edgnums; ++i) {
        char v1[1000],v2[1000];
        int cost,x,y;
        in>>v1>>v2>>cost;
        x=getNum(G,v1);
        y=getNum(G,v2);
        //assert(x!=-1&&y!=-1);
        if (x==-1||y==-1){
            cout<<"抱歉,您在Graph.txt中的资源信息可能有误,请前往检查"<<endl;
            exit(0);
        }
        G.arcs[x][y]=cost;
        G.arcs[y][x]=cost;
    }
}
void  dijkstra(MGraph &G,int start){
    for (int i = 0; i < G.vexnums; ++i) {
        G.totalvex[i].value=MAX;
        G.totalvex[i].change= false;
    }
    G.totalvex[start-1].value=0;
    G.totalvex[start-1].parent=-1;
    while (true){
        bool flag= true;
        for (int i = 0; i < G.vexnums; ++i) {
            if (!G.totalvex[i].change){
                flag=false;
                break;
            }
        }
        if (flag){
            return;
        }
        int min=-1;
        for (int i = 0; i < G.vexnums; ++i) {
            if (!G.totalvex[i].change){
                if (min==-1){
                    min=i;
                }else{
                    if (G.totalvex[i].value<G.totalvex[min].value){
                        min=i;
                    }
                }
            }
        }
        /*cout<<"选中"<<min<<" : ";
        cout<<G.totalvex[min].value;
        cout<<endl;   */
        G.totalvex[min].change= true;
        for (int i = 0; i < G.vexnums; ++i) {
            if (G.totalvex[i].change== false&&G.arcs[min][i]+G.totalvex[min].value<G.totalvex[i].value){
                G.totalvex[i].value=G.arcs[min][i]+G.totalvex[min].value;
                G.totalvex[i].parent=min;
            }
        }

    }

}
void print(MGraph &G,int end){
    if (G.totalvex[end-1].parent==-1){
        cout<<G.totalvex[end-1].name;
    }else if(end!=0){
        print(G,G.totalvex[end-1].parent+1);
        cout<<" ->"<<G.totalvex[end-1].name;
    }
}
string chartostring(char * line){
    string s;
    for (int i = 0; line[i]!='\0'; ++i) {
        s+=line[i];
    }
    return s;
}
void updatasource(char *file, int Numline,char * s){
    ifstream in(file);
    char line[1000]={'\0'};
    int i=0;
    string strtemp;
    while (in.getline(line,sizeof(line))){
        i++;
        if (i!=Numline){
            strtemp+=chartostring(line);
        }else {
            strtemp += chartostring(s);
        }
        strtemp+='\n';
    }
    in.close();
    ofstream on(file);
    on.flush();
    on<<strtemp;
    on.close();
}
void my_Delete(string file){
    vector<string> filesource;
    fstream in(file);
    string stemp="";
    while (getline(in,stemp)){
        if (stemp!=""){
            filesource.push_back(stemp);
        }
    }
    in.close();
    ofstream on(file);
    for (vector<string>::iterator i=filesource.begin();i!=filesource.end();i++) {
        on<<*i<<endl;
    }
}
void main1(){
    system("cls");
    while (1) {
        cout<<endl<<endl;
        cout << "                                            -----------这里是spark(LOL)导航系统-------------" << endl;
        printf("\t\t\t\t\t**********************************************");
        cout<<endl;
        cout << "                                                 1.查询地图资源信息" << endl<<endl;
        cout << "                                                 2.找寻地图资源之间的最短路径" << endl<<endl;
        cout << "                                                 3.修改地图资源信息的增益" << endl<<endl;
        cout << "                                                 4.增加地图资源以及其信息" << endl<<endl;
        cout << "                                                 5.增加地图资源之间的路径" << endl<<endl;
        cout << "                                                 6.删除地图资源" << endl<<endl;
        cout << "                                                 7.删除资源之间的路径" << endl<<endl;
        cout << "                                                 8.查询路径信息" << endl<<endl;
        cout << "                                                 9.输入#号退出系统" << endl<<endl;
        printf("\t\t\t\t\t**********************************************");
        cout<<endl;
        cout << "                                                  请选择--(:--";
        MGraph G;
        creatMap(G);
        char ch;
        cin>>ch;
        if (ch=='#') {
            system("cls");
            cout << "已经成功退出" << endl;
            exit(0);
        } else if (ch=='1') {
            system("cls");
            cout << "当前地图没被男枪刷完的资源有" << endl;
            showmaker(G);
            cout << "-----------------spark---------------" << endl;

            cout << "请输入要查询资源的编号(1~" << G.vexnums  << "(输入-1退出该功能)" << endl;
            int t;
            while (1) {
                cin >> t;
                if (t == -1) {
                    break;
                } else if (t >= 1 && t <= G.vexnums + 1) {
                    sorrymaker(G, t);
                } else {
                    cout << "哦!抱歉,您的选择有误,亲,您可继续查询,或者去吃中路的兵线!!" << endl;
                }
                cout << "继续输入请输入(1~" << G.vexnums << ",退出请输入-1" << endl;
            }
        } else if (ch=='2'){
            system("cls");
            cout<<"您选择的是找寻地图资源之间的最短路径,看来您想反野了,我们spark系统,将向您提供最优质的路径选择"<<endl;
            cout<<"现在还存在的资源有:"<<endl;
            showmaker(G);
            int start,end;
            cout<<"请输入你现在所在的位置"<<endl;
            while (1){
                cin>>start;
                if (start<=0||start> G.vexnums + 1){
                    cout<<"输入有误,请重新输入"<<endl;
                }else{
                    break;
                }
            }
            cout<<"请输入目标位置"<<endl;
            while (1){
                cin>>end;
                if (end<=0||end> G.vexnums + 1){
                    cout<<"输入有误,请重新输入"<<endl;
                }else{

                    break;
                }
            }
            dijkstra(G,start);
            cout<<endl;
            cout<<"您选择的"<<G.totalvex[start-1].name<<"到"<<G.totalvex[end-1].name<<"的路径长度是"<<G.totalvex[end-1].value<<endl;
            cout<<"其中途经:"<<endl;
            print(G,end);
            cout<<endl;
            cout<<"按任意键返回"<<endl;
            getch();
        }else if (ch=='3'){
            system("cls");
            char file[100]="Graph.txt";
            showmaker(G);
            cout<<"请输入要修改地图资源的编号"<<endl;
            int x;
            cin>>x;
            cout<<"请输入新的资源信息的增益"<<endl;
            char neww[1000]={""};
            char s[100];
            cin>>s;
            strcat(neww,G.totalvex[x-1].name);
            strcat(neww," ");
            strcat(neww,s);
            updatasource(file,x+1,neww);
            cout<<"成功修改,信息如下"<<endl;
            creatMap(G);
            showmaker(G);
            cout<<"按任意键返回"<<endl;
            getch();
        }else if (ch=='4'){
            system("cls");
            cout<<"您选择添加一个新的地图资源  及其 它的增益效果"<<endl;
            cout<<"接下来你输入该资源的资源名称和增益效果"<<endl;
            char newvexname[MAX];
            char newvexinfo[MAX];
            cin>>newvexname;
            cin>>newvexinfo;
            strcat(newvexname," ");
            strcat(newvexname,newvexinfo);
            char s[MAX];
            strcpy(s,G.totalvex[G.vexnums-1].name);
            strcat(s," ");
            strcat(s,G.totalvex[G.vexnums-1].info);
            strcat(s,"\n");
            strcat(s,newvexname);
            //cout<<s<<endl;
            char file[100]="Graph.txt";
            updatasource(file,1+G.vexnums,s);
            char v1[MAX],v2[MAX];
            itoa(1+G.vexnums,v1,10);
            itoa(G.edgnums,v2,10);
            strcat(v1," ");
            strcat(v1,v2);
            updatasource(file,1,v1);
            cout<<"添加成功"<<endl;
            creatMap(G);
            showmaker(G);
            cout<<"按任意键返回"<<endl;
            getch();
        }else if (ch=='5'){
            system("cls");
            cout<<"您选择添加一个新的地图资源之间的路径"<<endl;
            cout<<"接下来你输入这两个资源以及他们之间的距离"<<endl;
            char newvexname[MAX];
            char newvexinfo[MAX];
            char cost[MAX];
            cin>>newvexname;
            cin>>newvexinfo;
            cin>>cost;
            strcat(newvexname," ");
            strcat(newvexname,newvexinfo);
            strcat(newvexname," ");
            strcat(newvexname,cost);
            ofstream write;
            write.open("Graph.txt",ios::app);
            write<<newvexname;
            write.close();
            char v1[MAX],v2[MAX];
            itoa(G.vexnums,v1,10);
            itoa(G.edgnums+1,v2,10);
            strcat(v1," ");
            strcat(v1,v2);
            updatasource("Graph.txt",1,v1);
            creatMap(G);
            cout<<"添加成功"<<endl;
            cout<<"按任意键返回"<<endl;
            getch();
        }else if(ch=='6'){
            system("cls");
            cout<<"哇呜男枪要过来脏野了!!以下是现存资源"<<endl;
            showmaker(G);
            cout<<"请输入男枪脏过的地图资源"<<endl;
            int x;
            cin>>x;
            updatasource("Graph.txt",1+x,"");
            char v1[MAX],v2[MAX];
            itoa(G.vexnums-1,v1,10);
            itoa(G.edgnums,v2,10);
            strcat(v1," ");
            strcat(v1,v2);
            updatasource("Graph.txt",1,v1);
            cout<<"删除成功"<<endl;
            my_Delete("Graph.txt");
            creatMap(G);
            cout<<"按任意键返回"<<endl;
            getch();
        } else if (ch=='7'){
            system("cls");
            cout<<"当前有以下路径"<<endl;
            ifstream in("Graph.txt");
            string  temp;
            string  fw;
            for (int i = 0; i < G.vexnums+1; ++i) {
                getline(in,fw);
            }
            for (int i = 0; i <G.edgnums ; ++i) {
                getline(in,temp);
                cout<<"第"<<i+1<<"条路径是:"<<temp<<endl;
            }
            in.close();
            cout<<"请输入你想要删除的路径编号"<<endl;
            int x;
            cin>>x;
            updatasource("Graph.txt",G.vexnums+x+1,"");
            my_Delete("Graph.txt");
            char v1[MAX],v2[MAX];
            itoa(G.vexnums,v1,10);
            itoa(G.edgnums-1,v2,10);
            strcat(v1," ");
            strcat(v1,v2);
            updatasource("Graph.txt",1,v1);
            cout<<"删除成功"<<endl;
            creatMap(G);
            cout<<"按任意键返回"<<endl;
            getch();
        }else if (ch=='8'){
            system("cls");
            ifstream in("Graph.txt");
            string  temp;
            string  fw;
            cout<<"已经存在的路径有:"<<endl;
            for (int i = 0; i < G.vexnums+1; ++i) {
                getline(in,fw);
            }
            for (int i = 0; i <G.edgnums ; ++i) {
                getline(in,temp);
                cout<<"第"<<i+1<<"条路径是:"<<temp<<endl;
            }
            cout<<"按任意键返回"<<endl;
            getch();
        }
        else{
            cout<<"输入遇到错误了呢,亲!重新输入!"<<endl;
            system("cls");
        }
        system("cls");
    }
}



/*---------------------------------------------------------*/
int tjzhgs()//统计账号个数
{
    FILE *fp;
    int zh=0,n;
    char mm[20]={'\0'};
    fp=fopen("land.txt","r");
    for (n=0;!feof(fp);n++)//逐个读文件
        fscanf(fp,"%d %s",&zh,mm);
    n--;
    fclose(fp);//关闭文件
    return (n);//返回个数
}
int match1(int m,char a[20])//匹配数据库中的账号密码
{
    FILE*fp;
    int n=0,i=0,k,t;
    int zhanghao;
    char password[20];

    if ((fp=fopen("land.txt","r"))==NULL)//不存在登录者文件
    {
        system ("cls");
        printf("\n 还未存在用户!请新建账户");
        getch();
        system("cls");
        lmain();
    }
    k=tjzhgs();
    for(t=0;t<=k;t++)
    {
        fscanf(fp,"%d%s",&zhanghao,password);
        if(m==zhanghao)
        {
                return -1;
        }
    }
    return 0;
}
void xinjian()//新建账户密码
{
    FILE *fp;
    int zhanghao;
    char password[20],password1[20];
    char hit=0;
    if ((fp=fopen("land.txt","r"))==NULL)//if语句:打开药店文件,不存在此文件则新建
    {
        fp=fopen("land.txt","w");
        fclose(fp);
    }
    system("cls");
    fp=fopen("land.txt","a");
    for(;;)//输入两次密码确认,两次相同才能确认
    {
        printf("\n请按以下格式输入账户:\n学号 密码\n");
        printf("请输入:");
        scanf("%d %s",&zhanghao,password);
        printf("再次输入密码:\n");
        scanf("%s",password1);
        if(strcmp(password,password1)==0)
        {
            if (match1(zhanghao,password)!=-1){
            fprintf(fp,"%d %s\n",zhanghao,password);
            break;
            }
            else{
                cout<<"已经存在该用户账号,请重新输入账号!"<<endl;
                fclose(fp);
                printf("请按任意键返回....");
                getch();
                system("cls");
                lmain();
            }
        }
        else
        {
            printf("两次输入密码不一致,继续创建按回车,退出按ESC");
            hit=getch();
            if(hit==27)
                system("cls");
            lmain();
        }
    }
    fclose(fp);
    printf("创建成功,按任意键返回");
    getch();
    system("cls");
    lmain();
}
void lmenu()//显示登录菜单
{

    printf("\t\t\t\n\n\t                                           欢迎使用LOL客户端登录系统\n\n");
    printf("\t\t\t\t\t**********************************************");
    printf("\t\t\t\n\n\t\t                                            1.登录系统\n\n");
    printf("\t\t\t\n\n\t\t                                            2.创建账号\n\n");
    printf("\t\t\t\n\n\t\t                                            3.修改密码\n\n");
    printf("\t\t\t\n\n\t\t                                            4.退出系统\n\n");
    printf("\t\t\t\n\n\t                                            请按键选择,回车确定\n");
    printf("\t\t\t\t\t**********************************************");
    return ;
}
void lmain()//登录功能函数
{
    void land();
    void xinjian();
    char choose;
    lmenu();
    scanf(" %c",&choose);
    switch(choose)//功能函数
    {
        case'1':
            land();
            break;
        case'2':
            xinjian();
            break;
        case'3':
            xgmm();
            break;
        case'4':
            system("cls");
            getch();
            exit(0);
            system ("cls");
            break;
    }
}

int match(int m,char a[20])//匹配数据库中的账号密码
{
    FILE*fp;
    int n=0,i=0,k,t;
    int zhanghao;
    char password[20];

    if ((fp=fopen("land.txt","r"))==NULL)//不存在登录者文件
    {
        system ("cls");
        printf("\n 还未存在用户!请新建账户");
        getch();
        system("cls");
        lmain();
    }
    k=tjzhgs();
    for(t=0;t<=k;t++)
    {
        fscanf(fp,"%d%s",&zhanghao,password);
        if(m==zhanghao)
        {
            if(strcmp(a,password)==0)
                return 1;
            else
                return -1;
        }
    }
    return 0;
}

void land()//输入账户密码的登录函数
{
    int zhanghao;
    char password[20];
    int i=2,j,k,m=0,n;
    char hit=0;
    system("cls");
    do
    {
        printf("\n请输入账号:\n");
        scanf("%d",&zhanghao);
        printf("确认输入请安回车,重新输入请按ECS");
        hit=getch();//暂停程序当i接收后继续下一条指令
        for (;hit!=13&&hit!=27;)//保证只能是CR和ESC才能退出循环,输入其他字符无用,暂停程序,按'CR'继续。
        {
            hit=getch();
        }
    }
    while(hit==27);
    printf("\n请输入密码:\n");
    do
    {
        password[m]=getch();
        if(password[m]=='\r')
            break;
        else if(password[m]=='\b')
        {
            if(m==0)
            {
                printf("\a");
                continue;
            }
            m--;
            printf("\b");
        }
        else
        {
            m++;
            printf("*");
        }
    } while(password[m]!='\n'&&m<20);
    password[m]='\0';
    i=match(zhanghao,password);
    if(i==1)
    {
        printf("登陆成功!按任意键继续");
        getch();
        main1();
    }
    else
    {
        if(i==-1)
        {
            printf("密码错误!");
            getch();
            land();
        }
        if(i==0)
            printf("不存在此用户");
        getch();
        system("cls");
        lmain();
    }
}

void xg(int z,char m[20])//修改函数
{
    FILE *fp;
    int zhanghao1,n,j,k;
    char mima1[20];
    struct land *head,*p,*p1,*p2;

    fp=fopen("land.txt","r");
    j =tjzhgs();

    for (k=0;k<=j;k++)
    {
        fscanf(fp,"%d %s",&zhanghao1,mima1);
        if (z!=zhanghao1)//比较名字,将不同名字的信息复制到链表
        {
            n++;//相同返回值为0不执行if语句继续循环,不同则执行直到将所有不同的书名建立成链表
            if (n==1)//建立链表
            {
                p1=p2=(struct land*)malloc(LEN2);
                head=p1;
            }
            else
            {
                p2->next=p1;
                p2=p1;
                p1=(struct land*)malloc(LEN2);//新建链表
            }
            p1->zhanghao=zhanghao1;
            strcpy(p1->password,mima1);//复制账号密码
        }
    }
    if (n==0)
    {
        head=NULL;
    }
    else//建立链表的最后剩余一个储存空间,所以封底
    {
        p2->next=p1;
        p1->next=NULL;
        fclose(fp);
    }
    fp=fopen("land.txt","w");//清空文件,只写打开,然后关闭
    fclose(fp);
    fp=fopen("land.txt","a");//追加文件
    p=head;
    for (;p!=NULL;)//把链表内容覆盖到文件
    {
        fprintf(fp,"%d %s%\n",p->zhanghao,p->password);
        p=p->next;
    }
    fprintf(fp,"%d %s\n",z,m);
    fclose(fp);
    system ("cls");

}



void xgmm()//修改密码
{

    FILE *fp;
    int zh=0,k=0,many=0,m=0,n=0;
    int chazhao,hit;
    char mima[20]={'\0'},password1[20]={'\0'};
    char  mm[20]={'\0'};
    char i;

    if ((fp=fopen("land.txt","r"))==NULL)//打开文件
    {
        system ("cls");
        printf("\n记录文件不存在!按任意键返回");
        getch();
        system("cls");
        lmain();
    }
    system("cls");
    printf("请输入你的帐号和旧密码:\n");
    scanf("%d %s",&chazhao,mima);
    m =tjzhgs();
    for (n=0;n<=m;n++)
    {
        fscanf(fp,"%d %s",&zh,mm);
        if(zh==chazhao)
        {
            if(!strcmp(mm,mima))
            {
                printf("请输入新的密码");
                scanf("%s",mima);
                printf("再次输入密码:\n");
                scanf("%s",password1);
                if(strcmp(mima,password1)==0)
                {
                    xg(chazhao,mima);
                    getch();
                    lmain();
                    system("cls");
                }
                else
                {
                    printf("两次输入密码不一致,按任意键退出");
                    hit=getch();
                    system("cls");
                    lmain();

                }
            }
            else
            {
                printf("旧密码错误,按任意键返回!");
                getch();
                system("cls");
                lmain();

            }

        }
    }
    printf("不存在此账号,按任意键返回");
    fclose(fp);//修改结束
    getch();
    system("cls");
    lmain();
}

int main() {
    system("color f1");
    lmain();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值