回 帖 发 新 帖 刷新版面

主题:课程设计中  要求要防止乱输入数据格式……实在不懂……求助一个办法……

[code=c]
请填写代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 60
char filename[60];

struct student_list
{
    int num;    
    double stuID;
    char name[20];
    char gender[20];
}stu[NUM];
void Exit();

void Load()
{
    FILE *fp;
    int x;
    char ch[60];
    if(!(fp=fopen(filename,"r")))
    {
        printf("不 能 读 取 文 件 !\n");
        printf("------------------------------------------------------------------------\n");
        gets(ch);
        Exit();
    }
    for(x=0;x<NUM;x++)
    {
        fread(&stu[x],sizeof(struct student_list),1,fp);
    }
    fclose(fp);
}

void Save(int i)
{
    FILE *fp;
    int x=0;
    if(!(fp=fopen(filename,"wb")))
    {
        printf("不 能 写 入 文 件!\n");
        printf("------------------------------------------------------------------------\n");
        return;
    }
    for(x=0;x<i;x++)
    {
        if(fwrite(&stu[x],sizeof(struct student_list),1,fp)!=1)
        {
            printf(" 写 入 错 误 !\n");
            printf("------------------------------------------------------------------------\n");
            
        }
    }
    fclose(fp);
}
void Print()
{
    int x;
    Load();
    printf("*\t编号\t*\t    学号      \t*\t姓名\t*\t性别\t*\n");
    for(x=0;x<NUM;x++)
    {
        if(stu[x].stuID==0)break;
        else
            printf("*\t%d\t*\t%15.0lf\t*\t%s\t*\t%s\t*\n",stu[x].num,stu[x].stuID,stu[x].name,stu[x].gender);
    }
    printf("------------------------------------------------------------------------\n");
    
}
void Print_1(int x)
{
    printf("*\t%d\t*\t%15.0lf\t*\t%s\t*\t%s\t*\n",stu[x].num,stu[x].stuID,stu[x].name,stu[x].gender);
}

void Del()
{
    int x,i;
    Print();
    printf("请 输 入 你 要 删 除 的 数 据 的 编 号 :");
    scanf("%d",&x);
    Load();
    Print_1(x);
    for(i=x;i<NUM;i++)
    {
        stu[i].stuID=stu[i+1].stuID;
        strcpy(stu[i].name,stu[i+1].name);
        strcpy(stu[i].gender,stu[i+1].gender);
    }
    Save(NUM);
    printf("------------------------------------------------------------------------\n");
    Print();
    getchar();
    Exit();
}



void Correct(int x)
{
    int y;
    char ch,ch1;
    char z[20];
    Load();
    printf("是 否 修 改 ?y 或 者 n ?\n");
    ch=getchar();
    scanf("%c",&ch);
    printf("------------------------------------------------------------------------\n");
    if(ch=='y')
    {
        printf("你 想 修 改 的 是 :\n1.学   号\n2.姓   名\n3.性   别\n");
        scanf("%d",&y);
        if(ch1!='\n')
            {
                for(;;)
                {
                    ch1=getchar();
                    if(ch1=='\n')break;
                }
            }
        printf("------------------------------------------------------------------------\n");
        switch(y)
        {
        case 1:        
            printf("输 入 修 改 后 的 数 据 :\n");
            scanf("%lf",&stu[x].stuID);
            printf("------------------------------------------------------------------------\n");
            break;
        case 2:
            printf("输 入 修 改 后 的 数 据 :\n");
            scanf("%s",z);
            printf("------------------------------------------------------------------------\n");
            strcpy(stu[x].name,z);
            break;
        case 3:
            printf("输 入 修 改 后 的 数 据 :\n");
            scanf("%s",z);
            printf("------------------------------------------------------------------------\n");
            strcpy(stu[x].gender,z);
            break;
        default:
            printf("输 入 错 误 !\n");
            printf("------------------------------------------------------------------------\n");
        }
        Print_1(y);
        Save(NUM);
    }else if(ch=='n') Exit();
    
}
void Lookup_Name()
{
    char ch[20];
    int x;
    Load();
    printf("请 输 入 要 查 找 的 姓 名 :");
    for(x=0;x<NUM;x++)
    {
        if(strcmp(stu[x].name,ch)==0)break;
    }
    Print_1(x);
}


void Lookup_Num()
{
    int x,y;
    for(y=0;y<NUM;y++)
    {
        printf("请 输 入 你 要 找 的 学 生 数 据 的 编 号 :\n");
        scanf("%d",&x);
        printf("------------------------------------------------------------------------\n");
        Load();
        Print_1(x);
        Correct(x);
    }    
}
void Add_1(int i)
{
    FILE *fp;
    int x=NUM+1,y;
    fp=fopen(filename,"ab");
    Load();
    printf("请 输 入 增 加 的 学 生 的 学 号 ,姓 名 ,性 别 :\n\n");
    scanf("%lf%s%s",&stu[x].stuID,&stu[x].name,&stu[x].gender);
    printf("------------------------------------------------------------------------\n");
    for(y=1;;)
    {        
        if(stu[y].num==0)break;    
        y++;
    }
    stu[x].num=y;
    fseek(fp,0,SEEK_END);
    fwrite(&stu[x],sizeof(struct student_list),1,fp);
    i--;
    fclose(fp);
    if(i>0)
        Add_1(i);
    else
    {
        Print();
    }
}

void Add()
{
    
    int x=NUM+1,i;
    printf("请 输 入 要 新 增 的 数 据 个 数 :\n\n");
    scanf("%d",&i);
    printf("------------------------------------------------------------------------\n");
    Load();
    if(i>0)
        Add_1(i);
    else
    {
        Print();
        printf("添 加 成 功 !");
    }
}

void Amend()
{
    int x;
    char y;
    printf("请 选 择 :\n\n1. 添 加\n\n2. 删 除\n\n");
    scanf("%d",&x);
    if(y!='\n')
        {
            for(;;)
            {
                y=getchar();
                if(y=='\n')break;
            }
        }
    switch(x)
    {
    case 1:
        Add();
        break;
    case 2:
        Del();
        break;
    default:
        printf(" 输 入 错 误 !\n\n");
        printf("------------------------------------------------------------------------\n");
        Amend();
    }    
}

void New()
{
    int i,x;
    int c;

    printf("请 输 入 想 新 建 的 学 生 数 据 个 数 :\n\n");
    scanf("%d",&i);
    while((c=getchar())!='\n' && c!=EOF);
    printf("------------------------------------------------------------------------\n");
    for(x=0;x<i;x++)
    {
        stu[x].num=x;
        printf("请 输 入 学 生 的 学 号 ,姓 名 ,性 别:\n\n");
        scanf("%lf%s%s",&stu[x].stuID,&stu[x].name,&stu[x].gender);
        
    }
    Save(i);
    Print();
    printf("新 建 完 成!\n");
    printf("------------------------------------------------------------------------\n");
    getchar();
    Exit();
}

void Lookup_StuID()
{
    double x;
    int y;
    Load();
    printf("请 输 入 想 要 查 找 的 学 生 的 学 号 :");
    scanf("%lf",&x);
    for(y=0;y<NUM;y++)
    {
        if(stu[y].stuID==x)
        {
            Print_1(y);
            Correct(y);
            exit(0);
        }
    }printf("查 无 此 数 据 !\n");
}

void Search()
{
    int x;
    char ch;
    printf("输 入 什 么 数 据 进 行 搜 索 ?\n1.编 号\n2.学 号\n");
    scanf("%d",&x);
    if(ch!='\n')
        {
            for(;;)
            {
                ch=getchar();
                if(ch=='\n')break;
            }
        }
    switch(x)
    {
        case 1:
            Lookup_Num();
            break;
        case 2:
            Lookup_StuID();
            break;
        case 3:
            Lookup_Name();
            break;
        default:
        printf("输 入 错 误 !\n");
        Search();
    }
}

void Open()
{
    int x;
    char y;
    Print();
    printf("1.添 加 删 除 花 名 册 成 员 数 据\n\n2.搜 索 修 改 花 名 册 中 成 员\n\n");
    scanf("%d",&x);
    if(y!='\n')
        {
            for(;;)
            {
                y=getchar();
                if(y=='\n')break;
            }
        }
    switch(x)
    {
    case 1:
        Amend();
        break;
    case 2:
        Search();
        break;
    default:
        printf("输 入 错 误 !\n");
        printf("------------------------------------------------------------------------\n");
        Open();
    }
    
}

void main()
{
    int i;
    char ch,x;
    printf("************************************************************************\n");
    printf("*                                                                      *\n");
    printf("*                         班级花名册维护程序                           *\n");
    printf("*                                                                      *\n");
    printf("************************************************************************\n");
    printf("\n请 输 入 你 要 进 行 的 操 作:\n\n1. 打 开 已 有 花 名 册\n\n2. 创 建 新 的 花 名 册\n\n3. 退 出 系 统 \n\n");
    scanf("%d",&i);
    x=getchar();
    if(x!='\n'){
        for(;;)
        {
            x=getchar();
            if(x=='\n')break;
        }printf("输 入 错 误 !\n");
        main();
    }
    printf("------------------------------------------------------------------------\n");
    switch(i)
    {
    case 1:
        printf("请 输 入 想 打 开 文 件 的 路 径 :\n\n");
        scanf("%s",filename);
        printf("------------------------------------------------------------------------\n");
        Open();
        break;
    case 2:
        printf("请 输 入 想 新 建 文 件 的 路 径 :\n\n");
        scanf("%s",filename);
        printf("------------------------------------------------------------------------\n");
        New();
        break;
    case 3:
        system("pause");
        exit(0);
    default:
        printf("无 法 打 开 文 件\n");
        main();
        ch=' ';
    }
    system("pause");
}

void Exit()
{
    char ch;
//    ch=getchar();
    for(;;){
        printf("是 否 重 复 运 行 该 程 序 ?y 或 n ?");
        scanf("%c",&ch);
        //    ch=getchar();
        if(ch=='y'||ch=='n')break;
        else 
        {
            printf("输 入 错 误 !\n");
            continue;
        }
    }
    if(ch=='y')main();
    else if(ch=='n')
    {
        system("pause");
        exit(0);
    }
}
/*这是一个花名册系统,为了不挂科,我要做什么防狼系统……以免那坑爹老师一上来就乱输入就把我的程序弄崩溃后说一句:你挂了!那我就悲剧了……我想知道怎么限制别人不要乱输入数据格式?例如输入学号(INT),然后不让别人输入字符……*/
[/code]

回复列表 (共5个回复)

沙发

判断 scanf 的返回值
如果不对,清空输入缓冲,重新scanf

板凳


想请问一下  怎么清空?因为我还算是一个新手 所以这个不懂……

3 楼

[quote]判断 scanf 的返回值
如果不对,清空输入缓冲,重新scanf[/quote]这个怎么做呢?(我还是新手 不怎么懂,请教一下……)

4 楼

[quote]判断 scanf 的返回值
如果不对,清空输入缓冲,重新scanf[/quote]
怎么判断??……

5 楼


先对scanf返回的值进行判断,如果输入为int类型的值则继续运行程序,如果不行,则返回ERROR,请重新输入.由于C语言对各个字符之间对应的ascll码对应相应的字符,您主要是要防止编译器可能对此进行转换。崩溃跟乱输入好像没什么关系的呢!乱输入会报错的!

我来回复

您尚未登录,请登录后再回复。点此登录或注册