标题:为什么代码没错,却不行啊!?
取消只看楼主
xielei0806
Rank: 2
等 级:论坛游民
帖 子:36
专家分:28
注 册:2012-3-20
结帖率:80%
已结贴  问题点数:20 回复次数:4 
为什么代码没错,却不行啊!?
#include<stdio.h>              
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct               
{
    char roomnum[4];           
    char num[10];               
    char name[20];               
}Student;
Student room1[1000];         
int menu()            
{
    char c;
    do
    {system("cls");               
    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§ 7. 按房间号查找并显示              §\n");
    printf("\t\t§ 8. 按学号查找并显示                §\n");
    printf("\t\t§ 9. 插入一条记录按学号排序并显示    §\n");
    printf("\t\t§10. 从文件中读入数据                §\n");
    printf("\t\t§11. 循环写入数据                    §\n");
    printf("\t\t§ 0. 结束程序                        §\n");
    printf("\t\t★★★★★★★★★★★★★★★★★★★★\n");
    printf("\t\t请选择您要运行的选项按(0-11):");
    c=getchar();                 
    }
    while(c<'0'||c>'11');
    return(c-'0');                    
}                 


int Input(Student room[],int n)                  
{
    int i=0;
    char sign;
    while(sign!='n' && sign!='N')           
    {
        printf("\t\t姓名:");
        scanf("\t\t%s", &room[n+i].name);
        printf("\t\t学号:");
        scanf("\t\t%s", &room[n+i].num);
        printf("\t\t房间号:");                 
        scanf("\t\t%s",&room[n+i].roomnum);
        printf("\t\t是否继续输入?(Y/N)");
        scanf("\t\t%c",&sign);               
        i++;
    }
    return(n+i);
}


void Display_all_record(Student room[],int n)      
{
    int i;
    printf("\t\t----------------------------------------\n");  
    printf("\t\t房间号    学号      姓名                \n");
    printf("\t\t----------------------------------------\n");
    for(i=1;i<n+1;i++)               
    {
        printf("\t\t%-10s%-10s%-10s\n",room[i-1].roomnum,room[i-1].num,room[i-1].name);        
    }
    printf("\t\t");
    system("pause");
}

void Display_a_record(Student room[],int n)     
{
    printf("\t\t----------------------------------------\n");  
    printf("\t\t房间号    学号      姓名                \n");
    printf("\t\t----------------------------------------\n");   
    printf("\t\t%-10s%-10s%-10s\n",room[n].roomnum,room[n].num,room[n].name);    /*输出所查找的一条记录*/
    printf("\n");
    printf("\t\t");
}

void Sort_by_name(Student room[],int n)   
{
    int i,j;
    char t[10];
    for(i=0;i<n-1;i++)               
        for(j=0;j<n-1-i;j++)
            if(strcmp(room[j].name,room[j+1].name)>0)
            {               
                strcpy(t,room[j+1].name);
                strcpy(room[j+1].name,room[j].name);
                strcpy(room[j].name,t);
               
                strcpy(t,room[j+1].num);
                strcpy(room[j+1].num,room[j].num);
                strcpy(room[j].num,t);
               
                strcpy(t,room[j+1].roomnum);
                strcpy(room[j+1].roomnum,room[j].roomnum);
                strcpy(room[j].roomnum,t);
            }
}

void Sort_by_num(Student room[],int n)     
{
    int i,j;
    char t[10];
    for(i=0;i<n-1;i++)                  
        for(j=0;j<n-1-i;j++)
            if(strcmp(room[j].num,room[j+1].num)>0)
            {
                strcpy(t,room[j+1].roomnum);
                strcpy(room[j+1].roomnum,room[j].roomnum);
                strcpy(room[j].roomnum,t);
               
                strcpy(t,room[j+1].num);
                strcpy(room[j+1].num,room[j].num);
                strcpy(room[j].num,t);
               
                strcpy(t,room[j+1].name);
                strcpy(room[j+1].name,room[j].name);
                strcpy(room[j].name,t);
            }
}


void Sort_by_roomnum(Student room[],int n)  
{
    int i,j;
    char t[10];
    for(i=0;i<n-1;i++)               
        for(j=0;j<n-1-i;j++)
            if(strcmp(room[j].roomnum,room[j+1].roomnum)>0)
            {
                strcpy(t,room[j+1].num);
                strcpy(room[j+1].num,room[j].num);
                strcpy(room[j].num,t);
               
                strcpy(t,room[j+1].roomnum);
                strcpy(room[j+1].roomnum,room[j].roomnum);
                strcpy(room[j].roomnum,t);
               
                strcpy(t,room[j+1].name);
                strcpy(room[j+1].name,room[j].name);
                strcpy(room[j].name,t);
            }
}


void Find_record_name(Student room[],int n)         
{
    char s[10];
    int i=0;
    printf("\t\t输入要查找的姓名:");               
    scanf("%s",s);
    while(strcmp(room[i].name,s)!=0 && i<n) i++;         
    if(i==n)
    {
        printf("\t\t对不起没有找到该学生!\n");                     
    }
    else
        Display_a_record(room1,i);   
}


void Find_record_rommnum(Student room[],int n)             {
    char s[20];
    int i=0;
    printf("\t\t输入要查找的房间号:");               
    scanf("%s",s);
    while(strcmp(room[i].roomnum,s)!=0 && i<n) i++;               
    if(i==n)
    {
        printf("\t\t对不起没有找到该学生!\n");                     
    }   
    else
        Display_a_record(room1,i);
}

void Find_record_num(Student room[],int n)                     
{
    char s[20];
    int i=0;
    printf("\t\t输入要查找的学号:");               
    scanf("%s",s);
    while(strcmp(room[i].num,s)!=0 && i<n) i++;         
    if(i==n)
    {
        printf("\t\t对不起没有找到该学生!\n");                    
    }   
    else
        Display_a_record(room1,i);
}



int Insert_a_record(Student room[],int n)     
    {printf("\t\t学生姓名:");
    scanf("\t\t%s",&room[n].name);
    printf("\t\t学生学号:");
    scanf("\t\t%s",&room[n].num);
    printf("\t\t学生房间号:");         
    scanf("\t\t%s",&room[n].roomnum);
    n++;
    Sort_by_num(room,n);                        
    printf("\t\t插入记录成功!\n");               
    return(n);
}

int AddfromText(Student room[],int n)            
{
    int i=0,num;
    FILE *fp;                                 
    char filename[20];                           
    printf("\t\t输入文件名:");
    scanf("\t\t%s",&filename);                     
    if((fp=fopen(filename,"rb"))==NULL)           
    {
        printf("\t\t无法打开该文件\n");        
        printf("\t\t");
        system("pause");
        return(n);
    }
    fscanf(fp,"%d",&num);               
    while(i<num)               
    { fscanf(fp,"%s%s%s",room[n+i].roomnum,room[n+i].num,room[n+i].name);
    i++;
    }
    n+=num;
    fclose(fp);         
    printf("\t\t读入成功!\n");
    printf("\t\t");
    system("pause");
    return(n);
}
void WritetoText(Student room[],int n)              
{
    int i=0;
    FILE *fp;         
    char filename[100];                 
    printf("\t\t循环写入数据\n");     
    printf("\t\t输入文件名:");
    scanf("\t\t%s",&filename);
    if((fp=fopen(filename,"w"))==NULL)         
    {
        printf("\t\t无法打开该文件\n");
        system("pause");
        return;
    }
    fprintf(fp,"%d\n",n);           
    while(i<n)
    {
        fprintf(fp,"%-10s%-10s%-10s\n",room[i].roomnum,room[i].num,room[i].name);
        i++;
    }
    fclose(fp);           
    printf("写入成功!\n");         
}


int main()         
{
    int n=0;
    for(;;)
    {
        switch(menu())         
        {
        case 1:
            printf("\t\t输入记录\n");            
            n=Input(room1,n);
            break;
        case 2:
            printf("\t\t显示记录\n");            
            Display_all_record(room1,n);
            break;
        case 3:
            printf("\t\t按姓名排序并显示\n");
            Sort_by_name(room1,n);               
            printf("\t\t排序成功!\n");
            Display_all_record(room1,n);
            printf("\t\t");
            break;
        case 4:
            printf("\t\t按房间号排序并显示\n");
            Sort_by_roomnum(room1,n);            
            printf("\t\t排序成功!\n");
            Display_all_record(room1,n);
            printf("\t\t");
            break;
        case 5:
            printf("\t\t按学号排序并显示\n");
            Sort_by_num(room1,n);                 
            printf("\t\t\t排序成功!\n");
            Display_all_record(room1,n);
            printf("\t\t");
            break;                 
        case 6:
            printf("\t\t按姓名查找\n");
            Find_record_name(room1,n);         
            printf("\t\t");
            system("pause");
            break;
        case 7:
            printf("\t\t按房间号查找\n");
            Find_record_rommnum(room1,n);         
            printf("\t\t");
            system("pause");
            break;
        case 8:
            printf("\t\t按学号查找\n");
            Find_record_num(room1,n);         
            printf("\t\t");
            system("pause");
            break;
        case 9:
            printf("\t\t插入一条记录按学号排序并显示\n");
            n=Insert_a_record(room1,n);                       Display_all_record(room1,n);
            printf("\t\t");
            break;
        case 10:
            printf("\t\t从文件中读入数据\n");
            AddfromText(room1,n);            
            printf("\t\t");
            system("pause");
            break;
        case 11:
            printf("\t\t循环写入数据\n");
            WritetoText(room1,n);               
            printf("\t\t");
            system("pause");
            break;
        case 0:
            printf("\t\t谢谢使用,祝您好运,再见!\n");   
            printf("\t\t");
            system("pause");
            exit(0);
        }
    }
    return 0;
}
第十和第十一出错了
搜索更多相关主题的帖子: 记录 管理系统 include system 
2012-06-24 00:11
xielei0806
Rank: 2
等 级:论坛游民
帖 子:36
专家分:28
注 册:2012-3-20
得分:0 
回复 2楼 星112
不行啊!10和11的不行
2012-07-07 20:57
xielei0806
Rank: 2
等 级:论坛游民
帖 子:36
专家分:28
注 册:2012-3-20
得分:0 
回复 3楼 雪狼MJ
谢谢啊!
2012-07-07 20:58
xielei0806
Rank: 2
等 级:论坛游民
帖 子:36
专家分:28
注 册:2012-3-20
得分:0 
回复 4楼 随风飘荡
数字11啊!
2012-07-07 20:59
xielei0806
Rank: 2
等 级:论坛游民
帖 子:36
专家分:28
注 册:2012-3-20
得分:0 
回复 3楼 雪狼MJ
你的不能运行啊?
2012-07-07 21:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-372127-1-1.html




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

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