| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 228 人关注过本帖
标题:求教各位帮忙提一下解决办法
只看楼主 加入收藏
smbb0079
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-12
结帖率:50%
收藏
 问题点数:0 回复次数:1 
求教各位帮忙提一下解决办法
1.怎么能将该程序的密码验证改为从文件中调取与输入的对比?
2.怎么解决输入的信息未能填满结构体数组时,后续的空结构体中会自动生成乱码的问题?



附程序代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
struct student
{
    int num;
    char name[20];
    char sex;
    int age;
    char beizhu[1000];

};
void GetInformation(struct student *stu,int n)
{   
    system("cls");
    int i;
    for(i=0;i<n;i++)
    { char jin;
     printf("请输入学生%d的信息:学号、姓名、性别、年龄、备注\n",(i+1));
     scanf("%d %s %c %d %s",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].age,stu[i].beizhu);
     n++;
     printf("是否继续输入?Y/N\n");
     getchar();
     scanf("%c",&jin);
     getchar();
     if(jin!='Y')
     break;
    }
}
void Print(struct student* stu,int n)
{   
    system("cls");
    int i;
    for(i=0;i<n;i++)
   {
      printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n备注:%s\n",
      stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].beizhu);
   }
    printf("按任意键继续操作");
    getchar();
    getchar();
}
void Save(struct student* stu,int n)
{   FILE*fp;int i;
    fp=fopen("学生信息","w+");
    fprintf(fp,"%d\n",n);
    for( i=0;i<n;i++)
    {
       fprintf(fp,"学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n备注:%s\n",
       stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].beizhu);
    }
    printf("保存成功\n");
    fclose(fp);
    printf("按任意键继续操作");
    getchar();
    getchar();
}
void Delete(struct student* stu,int n)
{
    system("cls");
    int xuehao,i1,flag=0,i;
    printf("请输入要删除学生的学号:\n");
    scanf("%d",&xuehao);
    for( i=0;i<n;i++)
    {
      if(xuehao==stu[i].num)
      {
        flag=1;
        for(i1=i;i1<n-1;i1++)
        {
           stu[i1]=stu[i1+1];
        }
      }
    }
    if(flag==0)
    {
      printf("查无此人\n");
      printf("按任意键继续操作");
      getchar();
      getchar();
    }
    if(flag==1)
    {
      n=n-1;
      printf("删除成功\n");
      printf("按任意键继续操作");
      getchar();
      getchar();
    }
}
void Search(struct student* stu,int n)
{
                            system("cls");
                            int x,i;
                            printf(" (1).按学号查询\n (2).按姓名查询\n (3).按性别查询\n (4).按年龄查询\n");
                            printf("请选择:");
                            scanf("%d",&x);
                            if(x==1)
                            {
                              system("cls");
                              int b1,i=0;
                              printf("请输入学号:");
                              scanf("%d",&b1);
                              for(i=0;i<n;i++)
                              {
                                if(b1==stu[i].num)
                                 break;
                              }
                                if(i>=n)
                                  {printf("查无此人\n");printf("按任意键继续操作");getchar();getchar();}
                                else
                                  {printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n备注\n",
                                  stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].beizhu);
                                printf("按任意键继续操作");getchar();getchar();}
                            }
                            else if(x==2)
                            {
                                system("cls");
                                getchar();
                                int j=0;
                                char name1[20];
                                printf("请输入姓名:");
                                gets(name1);
                                for(i=0;i<n;i++)
                                {
                                if(strcmp(name1,stu[i].name)==0)
                                  {printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n备注%s\n",
                                  stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].beizhu);
                                j=1;printf("按任意键继续操作");getchar();getchar();}
                                }
                                if(j==0)
                                  {printf("查无此人\n");printf("按任意键继续操作");getchar();getchar();}
                            }
                            else if(x==3)
                            {
                               system("cls");
                               char sex1;
                               getchar();
                               printf("请输入性别:");
                               scanf("%c",&sex1);
                               for(i=0;i<n;i++)
                               {
                               if(sex1==stu[i].sex)
                                 {printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n备注%s\n",
                                 stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].beizhu);
                                printf("按任意键继续操作");getchar();getchar();}
                               }
                            }
                            else if(x==4)
                            {
                               system("cls");
                               int y;
                               printf("请输入年龄:");
                               scanf("%d",&y);
                               for(i=0;i<n;i++)
                               {
                               if(y==stu[i].age)
                                 {printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n备注%s\n",
                                 stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].beizhu);
                                printf("按任意键继续操作");getchar();getchar();}
                               }
                            }
                            else
                               {printf("无此项操作\n");printf("按任意键继续操作");getchar();getchar();}
                        }
void Update(struct student * stu,int n)
{
                           system("cls");
                           int pwsave=123456,pwscf1,i;
                           char n1[20],c2;
                           int  c1,k;
                           printf("请输入密码:\n");
                           scanf("%d",&pwscf1);
                           if(pwscf1==pwsave)
                           {
                              printf("请输入要修改学生的学号:");
                              scanf("%d",&c1);
                              for(i=0;i<n;i++)
                              {
                                 if(stu[i].num==c1)
                                 {
                                    while(1)
                                    {
                                       if(k==5)
                                           break;
                                       system("cls");
                                       printf("*********************************\n");
                                       printf("1.修改姓名\n");
                                       printf("2.修改年龄\n");
                                       printf("3.修改性别\n");
                                       printf("4.修改备注\n");
                                       printf("5.退出\n");
                                       printf("*********************************\n");
                                       printf("请输入你的选择:");
                                       scanf("%d",&k);
                                       switch(k)
                                       {
                                       case 1:system("cls");
                                              printf("请输入新姓名:");
                                              scanf("%s",n1);
                                              strcpy(stu[i].name,n1);break;
                                       case 2:system("cls");
                                              printf("请输入新年龄:");
                                              scanf("%d",&c1);
                                              stu[i].age=c1;break;
                                       case 3:system("cls");
                                              printf("请输入新性别:");
                                              getchar();
                                              scanf("%c",&c2);
                                              stu[i].sex=c2;break;
                                       case 4:system("cls");
                                              printf("请输入新备注:");
                                              scanf("%s",n1);
                                              strcpy(stu[i].beizhu,n1);break;
                                       case 5:system("cls");break;
                                       default:printf("无此项操作\n");
                                       }
                                    }
                                 }
                              }
                              printf("按任意键继续操作");
                              getchar();
                              getchar();
                           }
}
void main()
{
    int pwsave=123456,pwscf1,xiu;
    int count=0,i,gg=0;
    int select;
    struct student stu[N];
    FILE * fp;         
       fp=fopen("d:\\课程设计成果\\cg\\学生信息.txt","w+");
       {
         fscanf(fp,"%d\n",&count);
         for(i=0;i<count;i++)
         {
            fscanf(fp,"学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n备注:%s\n",&stu[i].num,stu[i].name,&stu[i].sex,&stu[i].age,stu[i].beizhu);
         }
        fclose(fp);
       }
    printf("*********************************\n");
    printf("-----------欢迎使用系统-------------\n");
    printf("----------制作者:李宇阳------------\n");
    printf("*********************************\n");
    printf("请输入密码\n");xiu=4;
    while(scanf("%d",&pwscf1))
    {

        if(pwscf1==pwsave)
        {
            while(1)
            {
                system("cls");
                printf("*********************************\n");
                printf("------1   学生基本信息录入------\n");
                printf("------2   学生基本信息显示------\n");
                printf("------3   学生基本信息保存------\n");
                printf("------4   学生基本信息删除------\n");
                printf("------5   学生基本信息修改(要求先输入密码)\n");
                printf("------6   学生基本信息查询------\n");
                printf("------7   退出程序        ------\n");
                printf("*********************************\n");
                scanf("%d",&select);
                {
                   switch(select)
                   {
                   case 1:
                      GetInformation(stu,N);
                      break;
                   case 2:
                      Print(stu,N);
                      break;
                   case 3:
                      Save(stu,N);
                      break;
                   case 4:
                      Delete(stu,N);
                      break;
                   case 5:
                      Update(stu,N);
                      break;
                   case 6:
                      Search(stu,N);
                      break;
                   case 7:
                       gg=1;
                       break;
                   }
                }if(gg==1)break;
            }if(gg==1)break;

        }
        else
        {
            system("cls");
            xiu--;
            if(xiu==0)
                break;
            printf("密码错误,请重新输入,您还有%d次机会\n",xiu);
        }
    }
}



[ 本帖最后由 smbb0079 于 2012-11-14 13:25 编辑 ]
2012-11-14 12:05
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
1.可以
2.创建数组时自己初始化

授人以渔,不授人以鱼。
2012-11-14 12:39
快速回复:求教各位帮忙提一下解决办法
数据加载中...
 
   



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

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