| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1004 人关注过本帖
标题:为什么代码没错,却不行啊!?
只看楼主 加入收藏
xielei0806
Rank: 2
等 级:论坛游民
帖 子:36
专家分:28
注 册:2012-3-20
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:13 
为什么代码没错,却不行啊!?
#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
星112
Rank: 5Rank: 5
来 自:山西朔州
等 级:职业侠客
威 望:2
帖 子:94
专家分:342
注 册:2012-6-22
收藏
得分:5 
没有问题啊,我亲自验证过的,你的程序写的真不错啊
2012-06-24 00:25
雪狼MJ
Rank: 8Rank: 8
来 自:甘肃
等 级:蝙蝠侠
威 望:4
帖 子:267
专家分:853
注 册:2012-5-27
收藏
得分:0 
我把你的程序改了改,现在前面的0到9的功能正常了(我把结构里的号数和寝室号类换成了整数型):
#include<stdio.h>              
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct               
{
    int roomnum;           
    int num;               
    char name[20];               
}Student;
Student room1[1000];         
int menu()            
{
    int 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):");
    scanf("%d", &c);               
    }while(c<0 && c>0);
    return(c-0);                    
}                 


int Input(Student room[],int n)                  
{
    int i=1;
    char sign;
    while(sign!='n' && sign!='N')           
    {
        printf("\t\t姓名:");
        scanf("\t\t%s", &room[i].name);
        printf("\t\t学号:");
        scanf("\t\t%d", &room[i].num);
        printf("\t\t房间号:");                 
        scanf("\t\t%d",&room[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;i++)               
    {
        printf("\t\t%-10d%-10d%-10s\n",room[i].roomnum,room[i].num,room[i].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%-10d%-10d%-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=1;i<n;i++)               
        for(j=i+1;j<n;j++)
            if(strcmp(room[i].name,room[j].name)>0)
            {     
                room[0]=room[i];
                room[i]=room[j];
                room[j]=room[0];     
            }}

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


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


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


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

void Find_record_num(Student room[],int n)                     
{
    int s;
    int i=1;
    printf("\t\t输入要查找的学号:");               
    scanf("%d",&s);
    while(i<n){
         if(room[i].num==s) Display_a_record(room,i);
         else if(i==n-1) {
            printf("\t\t对不起没有找到该学生!\n");                     
          }
          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%d",&room[n].num);
    printf("\t\t学生房间号:");         
    scanf("\t\t%d",&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);
    fp=fopen(filename,"rb");                    
    if(fp==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;
}
由于关于文件的读入读出函数fprintf和fscanf我还没有进行深入的探索,还用不怎么好,所以后面的文件操作就没改了。。。。

Edsger Dijkstra:算法+数据结构=程序
2012-06-24 19:40
随风飘荡
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:208
专家分:598
注 册:2011-9-9
收藏
得分:5 
'11'?这是一个什么字符?
2012-06-25 02:58
雪狼MJ
Rank: 8Rank: 8
来 自:甘肃
等 级:蝙蝠侠
威 望:4
帖 子:267
专家分:853
注 册:2012-5-27
收藏
得分:0 
嗯?有吗?如果有的话,就是||(或)字符喽。。。。。。。。

Edsger Dijkstra:算法+数据结构=程序
2012-06-25 08:28
雪狼MJ
Rank: 8Rank: 8
来 自:甘肃
等 级:蝙蝠侠
威 望:4
帖 子:267
专家分:853
注 册:2012-5-27
收藏
得分:0 
改过的查找函数还是有问题,不管找没找到,都会输出"对不起没有找到该学生!".

又改进了一下查找函数:
void Find_record_name(Student room[],int n)         
{
    char s[10];
    int i, j=0;
    printf("\t\t输入要查找的姓名:");               
    scanf("%s",s);
    for(i=1;i<n;){
        if(strcmp(room[i].name,s)==0){
            Display_a_record(room,i);
            j++;
        }  
        i++;           
    }     
    if(j==0) printf("\t\t对不起没有找到该学生!\n");         
}


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

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


Edsger Dijkstra:算法+数据结构=程序
2012-06-25 09:13
阿鞠尼
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:首尔
等 级:蒙面侠
威 望:5
帖 子:1467
专家分:4442
注 册:2012-5-30
收藏
得分:0 
我运行了一下也没有问题
图片附件: 游客没有浏览图片的权限,请 登录注册

喜欢睡觉 却经常熬夜
2012-06-25 10:10
雪狼MJ
Rank: 8Rank: 8
来 自:甘肃
等 级:蝙蝠侠
威 望:4
帖 子:267
专家分:853
注 册:2012-5-27
收藏
得分:0 
是的,它是能编译,但你用输入数据,检查函数功能的实现就出问题了

Edsger Dijkstra:算法+数据结构=程序
2012-06-25 11:59
阿鞠尼
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:首尔
等 级:蒙面侠
威 望:5
帖 子:1467
专家分:4442
注 册:2012-5-30
收藏
得分:5 
回复 8楼 雪狼MJ
嗯 是的 不过大多数的功能还是能实现的
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册

喜欢睡觉 却经常熬夜
2012-06-25 12:39
雪狼MJ
Rank: 8Rank: 8
来 自:甘肃
等 级:蝙蝠侠
威 望:4
帖 子:267
专家分:853
注 册:2012-5-27
收藏
得分:5 
你是运行的是我改过的程序还是楼主原本的程序呢?

Edsger Dijkstra:算法+数据结构=程序
2012-06-25 13:32
快速回复:为什么代码没错,却不行啊!?
数据加载中...
 
   



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

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