c语言学习总结

c语言学习总结

根据王桂林c语言学习资料自己写出教务管理系统程序,此程序综合性较高,涉及结构体,指针,链表,fread和fwrite等。程序调试无错误,但运行却无法显示。
可能原因:
1 程序问题;
2 平台问题,换平台再测试;
以下是程序:

#include <stdio.h>
#include <string.h>

//1,初始化数据库,此时的数据库是文件
//2,读数据库,生成内存数据模型,链表
//3,增,查,改,删,排序
//4,更新数据库。

//定义结构体;
typedef struct student
{
    char name[30];
    char sex;
    int age;
    float score;
}Stu;

typedef struct _StuNode
{
    Stu data;
    struct _StuNode *next;
}StuNode;

void initData2File()
{
    //结构体初始化;
    Stu s[4] =
    {
        "liudehua",'x',50,100,
        "zhangxueyou",'x',60,98,
        "lining",'f',50,88,
        "guofucheng",'m',49,90,
        
    };
    
    FILE *pf = fopen("stu,data","w+");
    if(NULL == pf)
        exit(-1);
    
    fwrite((void*)s,sizeof(s),1,pf);
    fclose(pf);
    return ;
}

StuNode *createListFromFile(char *filePath)
{
    FILE *pf = fopen(filePath,"r+");
    if(NULL == pf)
        exit(-1);
    
    StuNode *head = (StuNode *)malloc(sizeof(StuNode));
    head->next = NULL;
    
    StuNode *cur = (StuNode *)malloc(sizeof(StuNode));
    while(fread((void*)&cur->data,sizeof(Stu),1,pf)) // 1 0
    {
        cur->next = head->next;
        head->next = cur;
        
        cur = (StuNode *)malloc(sizeof(StuNode));
    }
    free(cur);
    return head;
}

void traverseStuList(StuNode *head)
{
    printf("\t\t\t Student Management System\n");
    printf("\t\t\t\t\t\t\t\tcopeLeft\n\n");
    printf("namn\t\t\tsex\t\t\tage\t\tscore\n");
    head = head->next;
    while(head)
    {
        printf("-10s\t\t%c\t\t%d\t\t%.2f\n",
               head->data.name,head->data.sex,
               head->data.age,head->data.score);
        head = head->next;
    }
    putchar(10);
}

void addListStuNode(StuNode *head)
{
    StuNode *cur = (StuNode *)malloc(sizeof(StuNode ));
    printf("name  :");
    scanf("%s",cur->data.name);
    
    getchar();
    printf("sex  :");
    scanf("%c",cur->data.sex);
    
    printf("age  :");
    scanf("%d",cur->data.age);
    
    printf("score  :");
    scanf("%f",cur->data.score);
    
    cur->next = head->next;
    head->next = cur;
}

StuNode * searchListStu(StuNode *head)
{
    char name[30];
    printf("please input your search name:");
    scanf("%s",name);
    
    head = head->next;
    while(head)
    {
        if(strcmp(head->data.name,name) == 0)
            break;
        head = head->next;
    }
    return head;
}

void deleteListNodeStu(StuNode *head)
{
    StuNode *pfind = searchListStu(head);
    if(pfind == NULL)
    {
        printf("您要输入的人不存在\n");
        getchar();getchar();
        return ;
    }
    while(head->next != pfind)
        head = head->next;
    head->next = pfind->next;
    free(pfind);
    return ;
}

int lenListStu(StuNode *head)
{
    int len = 0;
    head = head->next;
    while(head)
    {
        len++;
        head = head->next;
    }
    return len;
}

void sortListStu(StuNode *head)
{
    int len = lenListStu(head);
    StuNode *prep,*p,*q;
    for(int i=0;i<len-1;i++)
    {
        prep = head;
        p = prep->next;
        q = p->next;
        for(int j=0;j<len-1-j;j++)
        {
            if(strcmp(p->data.name,q->data.name)>0)
            {
                prep->next = q;
                p->next = q->next;
                q->next = p;
                
                prep = q;
                q = p->next;
                continue;
            }
            prep = prep->next;
            p = p->next;
            q = p->next;
        }
    }
}

void saveListStu2File(StuNode *head,char *filePath)
{
    FILE *pf = fopen(filePath,"w+");
    if(NULL == pf)
        exit(-1);
    head = head->next;
    while (head)
    {
        fwrite((void*)&head->data,sizeof(Stu),1,pf);
        head = head->next;
    }
    fclose(pf);
}

void destroyListStu(StuNode *head)
{
    StuNode *t;
    while (head)
    {
        t = head;
        head = head->next;
        free(t);
    }
}

int main()
{
    //    initData2File();
    StuNode *head =createListFromFile("stu.data");
    while(1)
    {
        system("cls");
        traverseStuList(head);
        printf("1->add\t 2->search 3->delete 4->exit\n");
        int choice;
        StuNode *pfind;
        scanf("d",&choice);
        switch (choice)
        {
        case 1:
            addListStuNode(head);
            break;
        case 2:
            if(pfind = searchListStu(head))
            {
                printf("您要找的数据在本系统中\n");
                printf("\t%-10s\t\t%c\t\t%d\t\t%.2f\n",
                       pfind->data.name,pfind->data.sex,
                       pfind->data.age,pfind->data.score);
            }
            else
                printf("查无此人\n");
            getchar();
            getchar();
            break;
        case 3:
            deleteListNodeStu(head);
            break;
        case 4:
            sortListStu(head);
            break;
        case 5:
            saveListStu2File(head,"stu.data");
            destroyListStu(head);
            return 0;
            break;
        default:
            printf("您输错了\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值