| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1694 人关注过本帖
标题:下面通讯管理系统缺一个按编号排序的功能,,,有知道怎么加的吗?
只看楼主 加入收藏
krisdei
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2018-4-6
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:6 
下面通讯管理系统缺一个按编号排序的功能,,,有知道怎么加的吗?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
    char score;/*编号*/
    char name[10];//姓名
    char num[15];//号码
    char email;//邮箱
    char adds[20];//住址
}person;

person pe[80];
int menu_select()
{
    char c;
    do
    {
        system("cls");
        printf("\t\t********通讯录********\n");
        printf("\t\t*                    *\n");
        printf("\t\t*       1.显示       *\n");
        printf("\t\t*       2.查询       *\n");
        printf("\t\t*       3.修改       *\n");
        printf("\t\t*       4.添加       *\n");
        printf("\t\t*       5.删除       *\n");
        printf("\t\t*       6.排序       *\n");
        printf("\t\t*       0.退出       *\n");
        printf("\t\t**********************\n");
        printf("\t\t请您选择(0~6)");
        c=getchar();
    }while(c<'0'||c>'6');
     return(c-'0');
}

int Input(Person per[],int n);
{
    int i=0;
    char sign,x[10];
    while(sign!='n'&&sign!=N)
    {
        printf("\t编号:");
        scanf("\t%d",&per(n+i).score);
        printf("\t姓名:");
        scanf("\t%s",per(n+i).name);
        printf("\t年龄:");
        scanf("\t%s",per(n+i).avg);
        printf("\t电话号码:");
        scanf("\t%s",per[n+i].num);
        printf("\t通讯住址:");
        scanf("\t%s",per[n+i].adds);
        printf("\t电子邮箱:");
        scanf("\t%s",per[n+i].email);
        
        gets(x);
        printf("\n\t是否继续添加?(Y/N)");
        scanf("\t%c",&sign);
        i++;
    }
    return(n+i);
}

void Display(Person per[],int n);
{
    int i;
    printf("-------------------------------------------------\n");//格式
    printf("  编号   姓名   年龄   电话号码   住址    邮箱");
    printf("--------------------------------------------------\n");
    for(i=1;i<n+1;i++)
    {
        printf("%-5d%-8s%-6s%-13s%-15s%-15s",per(i-1).score,per(i-1).name,per(i-1).avg,per[i-1].num,per[i-1].adds,per[i-1].email);
        if(i>1&&i%10==0)
        {
            printf("\t--------------------------------------------\n");
            printf("\t");
            system("pause");
            printf("\t---------------------------------------------\n");
        }
    }
    printf("-------------------------------------------------\n");
    system("pause");
}





int Delete_a_record(Person per[],int n);
{
    char s[20];
    int i=0,j;
    printf("\t请输入想删除记录中的名字:");
    scanf("%s",s);
    while(strcmp(per[i].name,s)!=0&&i<n)
        i++;
    if(i==n)
    {
        printf("\t通讯录中没有此人!\n");
        return(n);
    }
    for(j=i;j<n-1;j++)
    {
        strcpy(per[j].num,per[j+1].num);
        strcpy(per[j].name,per[j+1].name);
        strcpy(per[j].age,per[j+1].age);
        strcpy(per[j].adds,per[j+1].adds);
        strcpy(per[j].email,per[j+1].email);
        per[j].score=per[j+1].score;
    }
    printf("\t\t已经成功删除!\n");
    return(n-1);
}

void Query_a_record(Person per[],int n);
{
    int m;
    printf("\t\n请选择查询方式:\n");
    printf("\t|---------------------|\n");
    printf("\t|     1——姓名       |\n");
    printf("\t|     2——电话       |\n");
    printf("\t|     3——年龄       |\n");
    printf("\t|     4——返回       |\n");
    printf("\t|---------------------|\n");
    printf("请选择:");
    scanf("%d",&m);
    while(m!=1&&m!=2&&m!=3&&m!=4)
    {
        printf("输入错误!");
        scanf("%d",&m);
    }
    if(m==1)
    {
        char s[20];
        int i=0;
        printf("\t请输入想查询的姓名:");
        scanf("\t%s",s);
        while(strcmp(per[i]).name,s)!=0&&i<n)
            i++;
        if(i==n)
        {
            printf("\t通讯录没有此人\n");
            return;
        }
            printf("\t此人编号:%d\n",per[i].score);
            printf("\t此人年龄:%s\n",per[i].age);
            printf("\t电话号码:%s\n",per[i].num);
            printf("\t通讯地址:%s\n",per[i].adds);
            printf("\t电子邮箱:%s\n",per[i].email);
    }
}

void Change(Person per[],int n);
{
    char s[20];
    int i=0;
    printf("\t请输入想修改的记录的名字:");
    scanf("%s",s);
    while(strcmp(per[i].name),s)!=0&&i<n)
        i++;
    if(i==n)
    {
        printf("\t通讯录中没有此人!\n");
        return;
    }
    printf("\t编号:");
    scanf("\t%d",&per[i].score);
    printf("\t姓名:");
    scanf("\t%s",per[i].name);
    printf("\t年龄:");
    scanf("\t%s",per[i].age);
    printf("\t电话号码:");
    scanf("\t%s",per[i].num);
    printf("\t通讯地址:");
    scanf("\t%s",per[i].adds);
    printf("\t电子邮箱:");
    scanf("\t%s",per[i].email);
    printf("\t修改成功!");
}

void main();
{
    int n;
    for(;;)
    {
        switch(menu_select())
        {
        case 1:
            printf("\n\t添加到通讯录\n");
            n=Input(pe,n);
            break;

        case 2:
            printf("\n\t\t\t通讯记录表\n");
            Display(pe,m);
            break;

        case 3:
            printf("\n\t从通讯录中删除记录\n");
            n=Delete_a_record(pe,n);
            break;

        case 4:
            printf("\n\t从通讯录中查找记录\n");
            Query_a_record(pe,n);
            printf("\t");
            system("pause");
            break;

        case 5:
            printf("\n\t修改通讯录中的记录\n");
            change(pe,n);
            printf("\t");
            system("pause");
            break;
            
        case 6:
            printf("\n\t按编号排序");
            sort(pe,n);
            printf("\t");
            system("pause");
            break;

        case 0:
            printf("\n\t\t谢谢使用,再见!\n");
        }
    }
}
搜索更多相关主题的帖子: 编号 char int printf scanf 
2018-06-08 22:17
宝宝想你
Rank: 2
等 级:论坛游民
帖 子:32
专家分:40
注 册:2018-5-29
收藏
得分:10 
同样是使用strcmp函数比较编号num的大小
然后用一个快排或者神魔的交换一下数据就好了
2018-06-08 23:27
krisdei
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2018-4-6
收藏
得分:0 
回复 2楼 宝宝想你
有代码吗?神魔和快排不失很懂?
2018-06-09 14:44
宝宝想你
Rank: 2
等 级:论坛游民
帖 子:32
专家分:40
注 册:2018-5-29
收藏
得分:0 
回复 2楼 宝宝想你
#include"head.h"

void menu()
{
    printf("                主菜单                    \n");
    printf("                1.请输入学生信息        \n");
    printf("                2.学生信息显示            \n");
    printf("                3.按姓名查找            \n");
    printf("                4.删除学生信息            \n");
    printf("                5.增加学生信息            \n");
    printf("                6.修改学生信息            \n");
    printf("                7.保存到文件            \n");
    printf("                8.读取文件                \n");
    printf("                9.拷贝文件                \n");
    printf("                10.排序                    \n");
    printf("                11.退出                    \n");
}

void menu_system()
{
    menu();
    int a,lenth=0;
while (1){
    do{
        printf("请输入所选功能号:\n");
        scanf("%d", &a);
    } while (a < 1 || a>11);
    switch (a)
    {
    case 1:lenth=enter_info(stu); break;
    case 2:out_info(stu,lenth); break;
    case 3:search_name(stu,lenth); break;
    case 4:lenth=delet(stu,lenth); break;
    case 5:lenth=add(stu,lenth); break;
    case 6: modify(stu, lenth); break;
    case 7:write(stu, lenth); break;
    case 8:read(stu, lenth); break;
    case 9:copy(); break;
    case 10:sort(stu, lenth); break;
    case 11:exit(0);
    }
    menu();
    }
}

int enter_info(students stu[])
{
    int i,n;
    system("cls");
    printf("please input how many people's info that you want input!\n");
    scanf("%d",&n);
    for (i = 0; i < n; i++)
    {   system("cls");
        printf("input %dth xuehao!\n", i + 1);
        scanf_s("%s", stu[i].num, 20);
        printf("input %dth name!\n", i + 1);
        scanf_s("%s", stu[i].name, 30);
        printf("input %dth sex!\n", i + 1);
        scanf_s("%s", stu[i].sex, 4);
        printf("input %dth age!\n", i + 1);
        scanf_s("%d", &stu[i].age);
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
    return n;
}

void out_info(students stu[],int n)
{
    system("cls");
    int i;
    printf("--------------students list----------------------\n");
    printf("NO.        name        sex        age\n");
    for (i = 0; i < n; i++)
    {
        printf("%s        %s        %s        %d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
        printf("\n");
    }
    printf("------------------------------------------------\n");
}

int find_name(students stu[],int n,char* name)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (strcmp(name, stu[i].name) == 0)
            return i;
    }
    return -1;
}

int find_num(students stu[], int n,char* num)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (strcmp(num, stu[i].num) == 0)
            return i;
    }
    return -1;
}

int by_which()
{
   
    int a;
    printf("------------------------------------------------\n");
    printf("            1.by student name                    \n");
    printf("            2.by student number                    \n");
    printf("------------------------------------------------\n");
    do{
        printf("        choise:");
        scanf("%d", &a);
    } while (a < 1 || a>2);
    system("cls");
    return a;
}
void search_name(students stu[],int n)
{
    system("cls");
    int i;
    switch (by_which())
    {
    case 1:
        char name[30];
        printf("please enter name that you want search!\n");
        scanf("%s", name, 30);
        i = find_name(stu, n, name);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            printf("--------------students search list----------------------\n");
            printf("NO.        name        sex        age\n");
            printf("%s        %s        %s        %d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
            printf("\n");
        }break;
    default:
        char num[20];
        printf("please enter num that you want search!\n");
        scanf("%s", num, 20);
        i = find_num(stu, n, num);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            printf("--------------students search list----------------------\n");
            printf("NO.        name        sex        age\n");
            printf("%s        %s        %s        %d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
            printf("\n");
        }break;
    }
}

int delet(students stu[],int n)
{
    int i,j;
    system("cls");
    switch (by_which())
    {
    case 1:
        char name[30];
        printf("please enter name that you want delet!\n");
        scanf("%s", name, 30);
        i = find_name(stu, n, name);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            printf("deleting......!\n");
            for (j = i; j <n; j++)
            {
                strcpy_s(stu[j].num, strlen(stu[j + 1].num) + 1, stu[j + 1].num);
                strcpy_s(stu[j].name, strlen(stu[j + 1].name) + 1, stu[j + 1].name);
                strcpy_s(stu[j].sex, strlen(stu[j + 1].sex) + 1, stu[j + 1].sex);
                stu[j].age = stu[j + 1].age;
            }
            printf("delet succuse");
            n--;
        }
        return n;
        break;
    default:
        char num[20];
        printf("please enter num that you want delet!\n");
        scanf("%s", num, 20);
        i = find_num(stu, n, num);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            printf("deleting......!\n");
            for (j = i; j <n; j++)
            {
                strcpy_s(stu[j].num, strlen(stu[j + 1].num) + 1, stu[j + 1].num);
                strcpy_s(stu[j].name, strlen(stu[j + 1].name) + 1, stu[j + 1].name);
                strcpy_s(stu[j].sex, strlen(stu[j + 1].sex) + 1, stu[j + 1].sex);
                stu[j].age = stu[j + 1].age;
            }
            printf("delet succuse");
            n--;
        }
        return n;
        break;
    }
}

int add(students stu[],int n)
{
    int i, j;
    system("cls");
    switch (by_which())
    {
    case 1:
        char name[30];
        printf("please enter name that you want add!\n");
        scanf("%s", name, 30);
        i = find_name(stu, n, name);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            printf("adding......!\n");
            for (j = n; j >i; j--)
            {
                strcpy_s(stu[j].num, strlen(stu[j - 1].num) + 1, stu[j - 1].num);
                strcpy_s(stu[j].name, strlen(stu[j - 1].name) + 1, stu[j - 1].name);
                strcpy_s(stu[j].sex, strlen(stu[j - 1].sex) + 1, stu[j - 1].sex);
                stu[j].age = stu[j - 1].age;
            }
            printf("input %dth xuehao!\n", i + 1);
            scanf_s("%s", stu[i].num, 20);
            printf("input %dth name!\n", i + 1);
            scanf_s("%s", stu[i].name, 30);
            printf("input %dth sex!\n", i + 1);
            scanf_s("%s", stu[i].sex, 4);
            printf("input %dth age!\n", i + 1);
            scanf_s("%d", &stu[i].age);
            n++;
            printf("add succuse");
        }
        return n;
        break;
    default:
        char num[20];
        printf("please enter num that you want add!\n");
        scanf("%s", num, 20);
        i = find_num(stu, n, num);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            printf("adding......!\n");
            for (j = n; j >i; j--)
            {
                strcpy_s(stu[j].num, strlen(stu[j - 1].num) + 1, stu[j - 1].num);
                strcpy_s(stu[j].name, strlen(stu[j - 1].name) + 1, stu[j - 1].name);
                strcpy_s(stu[j].sex, strlen(stu[j - 1].sex) + 1, stu[j - 1].sex);
                stu[j].age = stu[j - 1].age;
            }
            printf("input %dth xuehao!\n", i + 1);
            scanf_s("%s", stu[i].num, 20);
            printf("input %dth name!\n", i + 1);
            scanf_s("%s", stu[i].name, 30);
            printf("input %dth sex!\n", i + 1);
            scanf_s("%s", stu[i].sex, 4);
            printf("input %dth age!\n", i + 1);
            scanf_s("%d", &stu[i].age);
            n++;
            printf("add succuse");
        }
        return n;
        break;
    }
}

void modify(students stu[], int n)
{
    int i;
    switch (by_which()){
    case 1:
        char name[30];
        printf("please enter name that you want search!\n");
        scanf("%s", name, 30);
        i = find_name(stu, n, name);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            int a;
            printf("------------------------------------------------\n");
            printf("            1.modify name                        \n");
            printf("            2.modify number                        \n");
            printf("            3.modify sex                        \n");
            printf("            4.modify age                        \n");
            printf("------------------------------------------------\n");
            do{
                printf("        choise:");
                scanf("%d", &a);
            } while (a < 1 || a>4);
            system("cls");
            switch (a)
            {
            case 1:
                char name[30];
                printf("please input the name you want modify!\n");
                scanf("%s", name, 30);
                strcpy_s(stu[i].name, strlen(name) + 1, name);
                printf("modify success!\n");
                break;
            case 2:
                char num[30];
                printf("please input the num you want modify!\n");
                scanf("%s", num, 30);
                strcpy_s(stu[i].num, strlen(num) + 1, num);
                printf("modify success!\n");
                break;
            case 3:
                char sex[4];
                printf("please input the sex you want modify!\n");
                scanf("%s", sex, 4);
                strcpy_s(stu[i].sex, strlen(sex) + 1, sex);
                printf("modify success!\n");
                break;
            default:
                int age;
                printf("please input the age you want modify!\n");
                scanf("%d", &age);
                stu[i].age = age;
                printf("modify success!\n");
                break;
            }
        }break;
    default:
        char num[20];
        printf("please enter num that you want search!\n");
        scanf("%s", num, 20);
        i = find_num(stu, n, num);
        if (i == -1)
        {
            printf("--------------students search list----------------------\n");
            printf("查无此人!\n");
        }
        else
        {
            int a;
            printf("------------------------------------------------\n");
            printf("            1.modify name                        \n");
            printf("            2.modify number                        \n");
            printf("            3.modify sex                        \n");
            printf("            4.modify age                        \n");
            printf("------------------------------------------------\n");
            do{
                printf("        choise:");
                scanf("%d", &a);
            } while (a < 1 || a>4);
            system("cls");

            switch (a)
            {
            case 1:
                char name[30];
                printf("please input the name you want modify!\n");
                scanf("%s", name, 30);
                strcpy_s(stu[i].name, strlen(name) + 1, name);
                printf("modify success!\n");
                break;
            case 2:
                char num[30];
                printf("please input the num you want modify!\n");
                scanf("%s", num, 30);
                strcpy_s(stu[i].num, strlen(num) + 1, num);
                printf("modify success!\n");
                break;
            case 3:
                char sex[4];
                printf("please input the sex you want modify!\n");
                scanf("%s", sex, 4);
                strcpy_s(stu[i].sex, strlen(sex) + 1, sex);
                printf("modify success!\n");
                break;
            default:
                int age;
                printf("please input the age you want modify!\n");
                scanf("%d", &age);
                stu[i].age = age;
                printf("modify success!\n");
                break;
            }
        }
    }
}

void write(students stu[],int n)
{
    system("cls");
    int i;
    FILE *file;
    if ((file = fopen("F:system.txt", "w")) == NULL)
    {
        printf("write error!");
    }
    else
    {
        printf("write success!");
        fprintf(file, "--------------students list----------------------\n");
        fprintf(file, "NO.        name        sex        age\n");
        for (i = 0; i < n; i++)
        {
            fprintf(file, "%s        %s        %s        %d", stu[i].num, stu[i].name, stu[i].sex, stu[i].age);
            fprintf(file, "\n");
        }
        fprintf(file, "------------------------------------------------\n");
        fclose(file);
    }
}

void read(students stu[], int n)
{
    system("cls");
    char buf[500] ={NULL};
    FILE *file;
    if ((file = fopen("F:system.txt", "r")) == NULL)
    {
        printf("read error!\n");
    }
    else
    {
        printf("read success!\n");
        while (!feof(file))
        {
            printf("%s", buf);
            fgets(buf, 500, file);
        }
        fclose(file);
    }
}

void copy()
{
    char outfile[20];
    char buf[100] = {NULL};
    FILE*sfp,*fp;
    sfp = (fopen("F:system.txt", "r"));
    if (sfp ==NULL)
    {
        printf("read error!\n");
    }
    else
    {
        printf("read success!\n");
        printf("请输入要拷贝到的文件:形式如D:\\text\\t.txt:\n");
        scanf("%s",outfile,20);
        fp = (fopen(outfile, "w"));
        if (fp == NULL)
        {
            printf("write error!\n");
        }
        else
        {
            while (!feof(sfp))
            {
                fputs(buf, fp);
                fgets(buf, 100, sfp);
            }
            fclose(sfp);
            fclose(fp);
        }
    }
}
void sort(students stu[], int n)
{
    struct students
    {
        char num[20];
        char name[30];
        char sex[4];
        int age;
    }t[N];
    int i, j,min;
    switch (by_which())
    {
    case 1:
        for (i = 0; i < n; i++)
        {
            min = i;
            for (j = i + 1; j < n; j++)
            {
                if (strcmp(stu[min].name, stu[j].name) > 0)
                {
                    min = j;
                }
            }
            strcpy_s(t[1].num, strlen(stu[i].num) + 1, stu[i].num);
            strcpy_s(stu[i].num, strlen(stu[min].num) + 1, stu[min].num);
            strcpy_s(stu[min].num, strlen(t[1].num) + 1, t[1].num);   
            
            strcpy_s(t[1].name, strlen(stu[i].name) + 1, stu[i].name);
            strcpy_s(stu[i].name, strlen(stu[min].name) + 1, stu[min].name);
            strcpy_s(stu[min].name, strlen(t[1].name) + 1, t[1].name);
            
            strcpy_s(t[1].sex, strlen(stu[i].sex) + 1, stu[i].sex);
            strcpy_s(stu[i].sex, strlen(stu[min].sex) + 1, stu[min].sex);
            strcpy_s(stu[min].sex, strlen(t[1].sex) + 1, t[1].sex);
        }
        break;
    default:
        for (i = 0; i < n; i++)
        {
            min = i;
            for (j = i + 1; j < n; j++)
            {
                if (strcmp(stu[min].num, stu[j].num) > 0)
                {
                    min = j;
                }
            }
            strcpy_s(t[1].num, strlen(stu[i].num) + 1, stu[i].num);
            strcpy_s(stu[i].num, strlen(stu[min].num) + 1, stu[min].num);
            strcpy_s(stu[min].num, strlen(t[1].num) + 1, t[1].num);

            strcpy_s(t[1].name, strlen(stu[i].name) + 1, stu[i].name);
            strcpy_s(stu[i].name, strlen(stu[min].name) + 1, stu[min].name);
            strcpy_s(stu[min].name, strlen(t[1].name) + 1, t[1].name);

            strcpy_s(t[1].sex, strlen(stu[i].sex) + 1, stu[i].sex);
            strcpy_s(stu[i].sex, strlen(stu[min].sex) + 1, stu[min].sex);
            strcpy_s(stu[min].sex, strlen(t[1].sex) + 1, t[1].sex);
        }
        break;
    }
}
void main(){
    menu_system();
    system("pause");
}
2018-06-12 22:37
宝宝想你
Rank: 2
等 级:论坛游民
帖 子:32
专家分:40
注 册:2018-5-29
收藏
得分:0 
回复 2楼 宝宝想你
以前写的一个学生管理系统里面有根据学号跟姓名大小排序的你可以看下
2018-06-12 22:38
宝宝想你
Rank: 2
等 级:论坛游民
帖 子:32
专家分:40
注 册:2018-5-29
收藏
得分:0 
回复 4楼 宝宝想你
结构体是可以直接赋值的,所以结构体直接做一个交换就可以,不需要跟我一样一个个成员去交换
2018-06-12 22:40
krisdei
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2018-4-6
收藏
得分:0 
回复 6楼 宝宝想你
谢谢你 在我快要放弃这个题的时候解答
2018-06-14 13:56
快速回复:下面通讯管理系统缺一个按编号排序的功能,,,有知道怎么加的吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027235 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved