| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 885 人关注过本帖
标题:【急】一道C语言编程题,机房管理系统的课设,
只看楼主 加入收藏
衣衣
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-12
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
【急】一道C语言编程题,机房管理系统的课设,
用C语言编写软件完成以下任务:
1数据信息(学生的学号,姓名,系别,上机种类,存的机时数等)录入,保存在comp.dat文件中;
2上机签到;
3下机处理。
搜索更多相关主题的帖子: 机房管理系统 C语言 
2010-03-12 12:37
衣衣
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-12
收藏
得分:0 
拜托各位高手们了,真的很急啊!!
2010-03-12 12:38
zhuyunshen
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:212
专家分:455
注 册:2009-11-22
收藏
得分:10 
#include"stdio.h"
main()
{
  char xh[15],name[9],xb[20],sjzl[20];
  int a;
  FILE *fp;
  
}

剩下的自己写了
2010-03-12 12:42
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:10 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define FILENAME "comp.dat"
#define MAX 20

typedef struct _stuinfo{
    char num[MAX];
    char name[MAX];
    char sex[MAX];//man female
    char kind[MAX];
    time_t time;
    time_t starttime;
    struct _stuinfo *next;
}StuInfo, *pStuInfo;

typedef struct _headstu{
    int cnt;
    StuInfo *node;
}HeadStu;

HeadStu UpqueueHead ;

#define IsStuEq(p, si) (!strcmp(p->num, si->num) && !strcmp(p->name , si->name) && !strcmp(p->sex , si->sex) \
                            && !strcmp(p->kind , si->kind))

void GetStuInfo(StuInfo *si, int flag)
{
    printf("input num: \t");
    scanf("%s", si->num);
    printf("input name: \t");
    scanf("%s", si->name);
    printf("input sex: \t");
    scanf("%s", si->sex);
    printf("input kind: \t");
    scanf("%s", si->kind);
    if (flag)
    {
        printf("input time: \t");
        scanf("%d", &si->time);
    }
}
void Input(void)
{
    FILE *fp;
    StuInfo si, sitmp, *p;
    if ((fp = fopen(FILENAME, "a+")) == NULL)
    {
        printf("open dat error!\n");
        return;
    }

    GetStuInfo(&si, 1);
    fseek(fp, 0, SEEK_SET);
   
    while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
    {
        p = &sitmp;
        if (IsStuEq(p, (&si)))
        {
            printf("%s already exist!\n", si.name);
            fclose(fp);
            return;
        }
    }
   
    fseek(fp, 0, SEEK_END);
    fwrite(&si, sizeof(si)-4-sizeof(time_t), 1, fp);
    fclose(fp);
}
void FreeQueue(void)
{
    StuInfo *p = UpqueueHead.node;
    while(p)
    {
        UpqueueHead.node = p->next;
        free(p);
        p = UpqueueHead.node;   
    }
   
}
void FreeAll(void)
{
    FILE *fp;
    time_t curtime;
    StuInfo *p, *q = UpqueueHead.node, sitmp;
    if ((fp = fopen(FILENAME, "r+")) == NULL)
    {
        printf("userinfo data lost!\n");
        FreeQueue();
        return;
    }
   
    while (q)
    {
        fseek(fp, 0, SEEK_SET);
        while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
        {
            p = &sitmp;
            if (IsStuEq(p, q))
            {
                curtime = time(NULL);
                if (((q->time -= curtime - q->starttime)) <= 0)
                {
                    printf("Not enough time to use!");
                }
                fseek(fp, -(sizeof(sitmp)-4-sizeof(time_t)), SEEK_CUR);
                fwrite(q, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
                break;
            }
        }
        q = q->next;
    }
    FreeQueue();
    fclose(fp);
}

void AddQueue(StuInfo si)
{
    StuInfo *tmpsi = (StuInfo *)malloc(sizeof(StuInfo));
    memcpy(tmpsi, &si, sizeof(StuInfo));
   
    tmpsi->next = UpqueueHead.node;
    UpqueueHead.node = tmpsi;
   
}
int Up(StuInfo si)
{
    FILE *fp;
    StuInfo sitmp;
    int ret;
    StuInfo *p = UpqueueHead.node;
    while(p)
    {
        if (IsStuEq(p, (&si)))
        {
            return 1;
        }
        p = p->next;
    }
   
    if ((fp = fopen(FILENAME, "a+")) == NULL)
    {
        printf("open dat error!\n");
        return 4;
    }
    fseek(fp, 0, SEEK_SET);

    while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
    {
        p = &sitmp;
        if (IsStuEq(p, (&si)))
        {
            if (p->time <= 0)
            {
                printf("Not enough time to use!\n");
                fclose(fp);
                return -2;
            }
            time(&sitmp.starttime);
            AddQueue(sitmp);
            fclose(fp);
            return 0;
        }
        
    }
    fclose(fp);
    return -1;
}

int Down(StuInfo si)
{
    FILE *fp;
    time_t curtime;
    StuInfo sitmp;
    StuInfo *p = UpqueueHead.node, *prev = NULL;
    while(p)
    {
        if (IsStuEq(p, (&si)))
        {
            curtime = time(NULL);
            if (((p->time -= curtime - p->starttime)) <= 0)
            {
                printf("Not enough time!");
            }
            
            if ((fp = fopen(FILENAME, "r+")) == NULL)
            {
                printf("open dat error!\n");
                return 4;
            }
            fseek(fp, 0, SEEK_SET);
            while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
            {
                if (IsStuEq(p, (&sitmp)))
                {
                    fseek(fp, -(sizeof(sitmp)-4-sizeof(time_t)), SEEK_CUR);
                    fwrite(p, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
                    fclose(fp);
                    if (prev == NULL)
                    {
                        UpqueueHead.node = p->next;
                    }else
                    {
                        prev->next = p->next;   
                    }
                    free(p);
                    
                    return 0;
                }   
            }
            fseek(fp, 0, SEEK_END);
            fwrite(p, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
            fclose(fp);
            if (prev == NULL)
            {
                UpqueueHead.node = p->next;
            }else
            {
                prev->next = p->next;   
            }
            free(p);
            
            printf("Lose user data, rewrite!\n");
            return -2;
        }
        prev = p;
        p = p->next;
    }
   
    if ((fp = fopen(FILENAME, "r+")) == NULL)
    {
        printf("open dat error!\n");
        return 4;
    }
    fseek(fp, 0, SEEK_SET);
    while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
    {
        p = &sitmp;
        if (IsStuEq(p, (&si)))
        {
            printf("%s don't online!\n", p->name);
            fclose(fp);
            return -3;
        }
        
    }
    fclose(fp);
    return -1;
}
void AppendTime(StuInfo si)
{
    FILE *fp;
    StuInfo *p, sitmp;
    if ((fp = fopen(FILENAME, "r+")) == NULL)
    {
        printf("open dat error!\n");
        return;
    }
    fseek(fp, 0, SEEK_SET);
    while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
    {
        p = &sitmp;
        if (IsStuEq(p, (&si)))
        {
            sitmp.time += si.time;
            fseek(fp, -(sizeof(sitmp)-4-sizeof(time_t)), SEEK_CUR);
            fwrite(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
            fclose(fp);
            printf("Append time success!\n");
            return;
        }
        
    }
    printf("Append time fail!\n");
    fclose(fp);
}
void PrintUpqueue()
{
    StuInfo *p = UpqueueHead.node;

    printf("num\t\tname\t\tsex\t\tname\t\tCur Used time\n");
    while (p)
    {
        printf("%s\t\t%s\t\t%s\t\t%s\t\t%d\n", p->num, p->name, p->sex, p->kind, time(NULL)-p->starttime);
        p = p->next;
    }
}
void PrintAllUser()
{   
    FILE *fp;
    StuInfo *p, sitmp;
    if ((fp = fopen(FILENAME, "a+")) == NULL)
    {
        printf("open dat error!\n");
        return;
    }
    fseek(fp, 0, SEEK_SET);
   
    printf("num\t\tname\t\tsex\t\tname\t\ttime\n");
    while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
    {
        p = &sitmp;
        printf("%s\t\t%s\t\t%s\t\t%s\t\t%d\n", p->num, p->name, p->sex, p->kind, p->time);
    }
    fclose(fp);
   
}
int main(void)
{
    char c;
    int flag;
    StuInfo si;
    = 0;
    UpqueueHead.node = NULL;
    printf(">>   ");
    while(1)
    {
        c = getchar();
        switch (c)
        {
        case 'u'://上机
            GetStuInfo(&si, 0);
            
            if ((flag = Up(si)) == -1)
            {
                printf("%s is not exist!\n", si.name);   
            } else if (flag == 1)
            {
                printf("%s have Up !\n", si.name);
            }else if (flag == 0)
            {
                printf("%s Up Success!\n", si.name);
            }
            break;
        case 'd'://下机
            GetStuInfo(&si, 0);
            
            if ((flag = Down(si)) == -1)
            {
                printf("%s is not exist!\n", si.name);   
            }else if (flag == 1)
            {
                printf("%s Have down !\n", si.name);
            }else if (flag == 0)
            {
                printf("%s Down Success!\n", si.name);
            }
            break;
        case 'i'://录入
            Input();
            break;
        case 'a'://充值
            GetStuInfo(&si, 1);
            
            AppendTime(si);
            break;
        case 'p'://打印在线用户
            PrintUpqueue();
            break;
        case 'P'://打印录入了的用户
            PrintAllUser();
            break;
        case 'q'://退出
            FreeAll();
            exit(0);
        default:
            printf("Undefine!\n");
        }
        while ((c = getchar()) != '\n')
            continue;
        printf(">> ");
   
    }
}
2010-03-13 17:46
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:0 
代码有点儿乱,但能用,有些细节没有考虑,你自己可以去补充补充
2010-03-13 17:50
快速回复:【急】一道C语言编程题,机房管理系统的课设,
数据加载中...
 
   



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

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