學生檔案管理系統(無文件操作)——源碼

#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <string.h>

typedef struct undergraduate
{
    char name[20];      //姓名
    int number;         //學號
    char sex;           //性別
    int age;            //年齡
    int squad;          //班級
    char telephone[10];  //電話
    char address[30];   //地址
    struct undergraduate *next;
}student ,*link;

/*---------------------------------------------------------------------*/
/*主函數,成功返回0,失敗返回其他值*/
int main( void )
{
    int input(link, int);
    void reload ( void );
    char menu( void );
    int show(link);
    link find(link);
    int modify(link);
    int erase(link);
    int add(link);
   
    link pSCHead = NULL, pSCLink = NULL;
    int iTStudent;
    int i;
    int rInput;
    int rMenu;
    FILE *pTFile, *pFile;

    reload();

    window(1, 1, 79, 25);
    textcolor(WHITE);
    textbackground(BLUE);

BEGIN:
    rMenu = menu();
    clrscr();
    switch( rMenu )
    {
    case 'i':
    case 'I':
    {
        if (pSCHead != NULL)
        {
            gotoxy(8, 3);
            puts("You have input the information!");
            gotoxy(8, 4);
            puts("Press any key to return!");
            getch();
            goto BEGIN;
        }
        /*輸入鏈表的基本信息*/ 
        gotoxy(8, 3);
        printf("How many student there are?");
        scanf("%d", &iTStudent);

        /*以學生總數爲依託建立鏈表*/
        if ((pSCHead = (link)malloc(sizeof (student))) == NULL)
        {
            puts("/n/t");
            puts("There is no more EMS memory!/n");
            puts("Press any key to reutrn!/n");
            getch();
            exit( 1 );
        }

        pSCHead->next = NULL;

        for (i = 0; i < iTStudent; i++)
        {
            pSCLink = (link)malloc(sizeof (student));

            if (pSCLink == NULL)
            {
                puts("/n/t");
                puts("There is no more EMS memory!/n/t");
                puts("Press any key to return!/n/t");
                getch();
                exit( 1 );/*返回值能否重複呢?*/     
            }

            pSCLink->next = pSCHead->next;
            pSCHead->next = pSCLink;
        }

        rInput = input(pSCHead, iTStudent);
        if (rInput == 1)
        {
            puts("/n/t");
            puts("Ther is something wrong in input function!/n/t");
            puts("Press any key to return!/n/t");
            getch();
            exit( 1 );
        }

  }
    goto BEGIN;
    case 's':
    case 'S':
    {
        show(pSCHead);

        gotoxy(8, 24);
        puts("Press any key to return!");
        getch();
    }
    goto BEGIN;
    case 'm':
    case 'M':
    {
        modify(pSCHead);
    }
    goto BEGIN;
    case 'a':
    case 'A':
    {
        pSCLink = (link)malloc(sizeof (student));
        if (pSCLink == NULL)
        {
            gotoxy(4, 4);
            puts("There is something wrong in add function!");
            gotoxy(4, 5);
            puts("Press any key to return!");
            getch();
        }
        else
        {
            pSCLink->next = pSCHead->next;
            pSCHead->next = pSCLink;

            add(pSCLink);

            gotoxy(4, 24);
            printf("OK!We have added successfully! And press any key to return!");
            getch();
        }
    }
    goto BEGIN;
    case 'd':
    case 'D':
    {
        erase(pSCHead);
    }
    goto BEGIN;
    case 'f':
    case 'F':
    {
        find(pSCHead);
        gotoxy(8, 24);
        puts("Press any key to return!");
        getch();
    }
    goto BEGIN;
    case 'q':
    case 'Q':
    {
      exit ( 0 );
    }
    default : goto BEGIN;
    }
}
/*---------------------------------------------------------------------*/
void drawmat(char *mat, int matsize, int x, int y, int color)
/*依次:字模指針、點陣大小、起始座標(x,y)、顏色*/
{
    int i, j, k, n;

    n = (matsize - 1) / 8 + 1;//n中存放什麼數字呢?
    for (j = 0; j < matsize; j++)
        for (i = 0; i < n; i++)
            for(k = 0; k < 8; k++)
                if(mat[j * n + i] & (0x80 >> k))  /*測試爲1的位則顯示*/
                    putpixel(x + i * 8 + k, y + j, color);
}

void reload ( void )
{
    char welcome[] = {
    /* 以下是 '歡' 的 16點陣宋體 字模,32 byte */
    0x00,0x80,0x00,0x80,0xFC,0x80,0x05,0xFE,
    0x85,0x04,0x4A,0x48,0x28,0x40,0x10,0x40,
    0x18,0x40,0x18,0x60,0x24,0xA0,0x24,0x90,
    0x41,0x18,0x86,0x0E,0x38,0x04,0x00,0x00,
    /* 以下是 '迎' 的 16點陣宋體 字模,32 byte */
    0x40,0x00,0x21,0x80,0x36,0x7C,0x24,0x44,
    0x04,0x44,0x04,0x44,0xE4,0x44,0x24,0x44,
    0x25,0x44,0x26,0x54,0x24,0x48,0x20,0x40,
    0x20,0x40,0x50,0x00,0x8F,0xFE,0x00,0x00,
    /* 以下是 '使' 的 16點陣宋體 字模,32 byte */
    0x08,0x40,0x0C,0x40,0x1B,0xFE,0x10,0x40,
    0x37,0xFC,0x64,0x44,0xA4,0x44,0x27,0xFC,
    0x24,0x44,0x22,0x40,0x21,0x80,0x20,0x80,
    0x21,0x70,0x22,0x1E,0x2C,0x04,0x00,0x00,
    /* 以下是 '用' 的 16點陣宋體 字模,32 byte */
    0x00,0x00,0x1F,0xFC,0x10,0x84,0x10,0x84,
    0x10,0x84,0x1F,0xFC,0x10,0x84,0x10,0x84,
    0x10,0x84,0x1F,0xFC,0x10,0x84,0x10,0x84,
    0x20,0x84,0x20,0x84,0x40,0x94,0x80,0x88,
    /* 以下是 '學' 的 16點陣宋體 字模,32 byte */
    0x01,0x08,0x10,0x8C,0x0C,0xC8,0x08,0x90,
    0x7F,0xFE,0x40,0x04,0x8F,0xE8,0x00,0x40,
    0x00,0x80,0x7F,0xFE,0x00,0x80,0x00,0x80,
    0x00,0x80,0x00,0x80,0x02,0x80,0x01,0x00,
    /* 以下是 '生' 的 16點陣宋體 字模,32 byte */
    0x00,0x80,0x10,0xC0,0x10,0x80,0x10,0x88,
    0x1F,0xFC,0x20,0x80,0x20,0x80,0x40,0x88,
    0x9F,0xFC,0x00,0x80,0x00,0x80,0x00,0x80,
    0x00,0x80,0x00,0x84,0x7F,0xFE,0x00,0x00,
    /* 以下是 '檔' 的 16點陣宋體 字模,32 byte */
    0x10,0x00,0x10,0x20,0x12,0x24,0x11,0x24,
    0xFE,0xA8,0x10,0xB0,0x3B,0xFE,0x34,0x02,
    0x54,0x02,0x51,0xFE,0x90,0x02,0x10,0x02,
    0x10,0x02,0x13,0xFE,0x10,0x02,0x10,0x00,
    /* 以下是 '案' 的 16點陣宋體 字模,32 byte */
    0x02,0x00,0x01,0x00,0x3F,0xFC,0x22,0x08,
    0x04,0x40,0x7F,0xFC,0x0C,0x80,0x03,0x80,
    0x1E,0x60,0x01,0x20,0x7F,0xFE,0x03,0x40,
    0x0D,0x30,0x71,0x0E,0x01,0x04,0x01,0x00,
    /* 以下是 '管' 的 16點陣宋體 字模,32 byte */
    0x20,0x80,0x3E,0xFC,0x51,0x20,0x8A,0x10,
    0x01,0x00,0x7F,0xFE,0x40,0x04,0x1F,0xE0,
    0x10,0x20,0x1F,0xE0,0x10,0x00,0x1F,0xF0,
    0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,
    /* 以下是 '理' 的 16點陣宋體 字模,32 byte */
    0x00,0x00,0x03,0xFC,0xFA,0x44,0x22,0x44,
    0x23,0xFC,0x22,0x44,0xFA,0x44,0x23,0xFC,
    0x22,0x44,0x20,0x40,0x23,0xFC,0x38,0x40,
    0xC0,0x40,0x00,0x40,0x0F,0xFE,0x00,0x00,
    /* 以下是 '系' 的 16點陣宋體 字模,32 byte */
    0x00,0x7C,0x3F,0x80,0x02,0x20,0x04,0x20,
    0x08,0x40,0x1F,0x80,0x03,0x20,0x0C,0x10,
    0x3F,0xF8,0x10,0x8C,0x04,0xA0,0x08,0x90,
    0x10,0x88,0x20,0x84,0x42,0x84,0x01,0x00,
    /* 以下是 '統' 的 16點陣宋體 字模,32 byte */
    0x10,0x40,0x10,0x20,0x23,0xFE,0x20,0x40,
    0x44,0x40,0xF8,0x88,0x09,0x04,0x13,0xFE,
    0x20,0x94,0x7C,0x90,0x00,0x90,0x00,0x90,
    0x1D,0x12,0xE1,0x12,0x02,0x0E,0x04,0x00,
    };
    char myname[] = {
    /* 以下是 '作' 的 16點陣宋體 字模,32 byte */
    0x08,0x80,0x0C,0x80,0x09,0x00,0x13,0xFE,
    0x12,0x80,0x34,0x88,0x50,0xFC,0x90,0x80,
    0x10,0x80,0x10,0x84,0x10,0xFE,0x10,0x80,
    0x10,0x80,0x10,0x80,0x10,0x80,0x10,0x80,
    /* 以下是 '者' 的 16點陣宋體 字模,32 byte */
    0x01,0x00,0x01,0x08,0x3F,0xEC,0x01,0x10,
    0x01,0x20,0x7F,0xFE,0x00,0x80,0x03,0x00,
    0x07,0xF8,0x1C,0x08,0xE4,0x08,0x07,0xF8,
    0x04,0x08,0x04,0x08,0x07,0xF8,0x04,0x08,
    /* 以下是 ':' 的 16點陣宋體 字模,32 byte */
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,
    0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,
    /* 以下是 ' ' 的 16點陣宋體 字模,32 byte */
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    /* 以下是 ' ' 的 16點陣宋體 字模,32 byte */
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    /* 以下是 ' ' 的 16點陣宋體 字模,32 byte */
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    /* 以下是 '周' 的 16點陣宋體 字模,32 byte */
    0x00,0x00,0x1F,0xFC,0x10,0x84,0x13,0xE4,
    0x10,0x84,0x10,0x84,0x17,0xF4,0x10,0x04,
    0x13,0xE4,0x12,0x24,0x12,0x24,0x13,0xE4,
    0x22,0x24,0x20,0x04,0x40,0x14,0x80,0x08,
    /* 以下是 '金' 的 16點陣宋體 字模,32 byte */
    0x01,0x00,0x01,0x00,0x02,0x80,0x04,0x40,
    0x08,0x30,0x30,0x0E,0xCF,0xF4,0x01,0x00,
    0x01,0x00,0x3F,0xF8,0x01,0x00,0x09,0x20,
    0x05,0x20,0x05,0x40,0xFF,0xFE,0x00,0x00,
    /* 以下是 '財' 的 16點陣宋體 字模,32 byte */
    0x00,0x08,0x7E,0x08,0x42,0x08,0x4A,0x08,
    0x4A,0xFE,0x4A,0x18,0x4A,0x18,0x4A,0x28,
    0x4A,0x28,0x4A,0x48,0x08,0x48,0x14,0x88,
    0x12,0x08,0x23,0x08,0x42,0x28,0x80,0x10,
    };

    int driver = DETECT ,mode;
    int i;

    initgraph( &driver , &mode , "");

    for (i = 0; i < 12; ++i)
    {
        drawmat(welcome + i * 32, 16, 230 + i * 15, 100, YELLOW);
    }

    for (i = 0; i < 9; ++i)
    {
        drawmat(myname + i * 32, 16, 250 + i * 15, 120, YELLOW);
    }
    outtextxy (264 , 300 , "Loading...");
    setcolor( 2 );
    bar(184 , 320 , 449 , 328 );
    sleep( 1 );
    setfillstyle(SOLID_FILL,LIGHTGREEN);
    bar(184 , 320 , 214 , 328 );
    sleep( 1 );
    bar(214 , 320 , 261 , 328 );
    sleep( 1 );
    bar(261 , 320 , 308 , 328 );
    sleep( 1 );
    bar(308 , 320 , 355 , 328 );
    sleep( 1 );
    bar(355 , 320 , 402 , 328 );
    sleep( 1 );
    bar( 402 , 320 , 449 , 328 );
    sleep( 1 );
    closegraph( );

    return;
}

char menu( void )
{
    char choice;

    clrscr();
    gotoxy( 17 , 5 );
    printf(" ===================================== ");
    gotoxy(17 , 6);
    printf("  I----------->input the information   ");
    gotoxy(17 , 7);
    printf("  S----------->show all information    ");
    gotoxy(17 , 8);
    printf("   =================================   ");
    gotoxy(17 , 9);
    printf("  M----------->modify the information  ");
    gotoxy(17 , 10);
    printf("  A----------->add the information     ");
    gotoxy(17 , 11);
    printf("  D----------->delete the information  ");
    gotoxy(17 , 12);
    printf("  F----------->find the information    ");
    gotoxy(17 , 13);
    printf("   =================================   ");
    gotoxy(17 , 14);
    printf("  Q----------->quit the system         ");
    gotoxy(17 , 15);
    printf(" ===================================== ");
    gotoxy(17 , 16);
    printf("Please input your choice:");
    scanf("%c",&choice);

    return choice ;
}
 
/*---------------------------------------------------------------------*/
/*輸入函數*/
/*成功返回0,否則返回其他*/
int input(link pSCHeadfp, int iTStudentfp)
{
    void inputInterface( void );
    void readdata(link);

    int i;
    link pSCTemp = pSCHeadfp->next;

    if (pSCHeadfp == NULL)
    {
        puts("There something wrong in input function!/n/n/t");
        puts("Press any key to continue!/n/t");
        getch();
        return 1;
    }

    for (i = 0; i < iTStudentfp; i++)
    {
        inputInterface();

        gotoxy(8, 3);
        printf("There are already %d students include this one!", iTStudentfp - i);

        readdata(pSCTemp);

        pSCTemp = pSCTemp->next;
    }

    return 0;
}

/*輸入界面過程*/
void inputInterface( void )
{
    clrscr();

    gotoxy(8, 5);
    puts("Name :");
    gotoxy(8, 7);
    puts("Number :");
    gotoxy(8, 9);
    puts("Sex(F&M) :");
    gotoxy(8, 11);
    puts("Age :");
    gotoxy(8, 13);
    puts("Class :");
    gotoxy(8, 15);
    puts("Telephone :");
    gotoxy(8, 17);
    puts("Address :");
}

/*讀入數據*/
void readdata(link pSCTempfp)
{
    gotoxy(20, 5);
    scanf("%s", pSCTempfp->name);
    gotoxy(20, 7);
    scanf("%d", &pSCTempfp->number);
    gotoxy(20, 9);
    getchar();          //取消回車符的錄入
    pSCTempfp->sex = getchar();
    gotoxy(20, 11);
    scanf("%d", &pSCTempfp->age);
    gotoxy(20, 13);
    scanf("%d", &pSCTempfp->squad);
    gotoxy(20, 15);
    scanf("%s", pSCTempfp->telephone);
    gotoxy(20, 17);
    scanf("%s", pSCTempfp->address);
}
/*---------------------------------------------------------------------*/

/*輸出數據信息*/
/*成功則返回1,否則返回其他*/
int show(link pSCHeadfp)
{
    void printHead(void);
    int printData(link);

    link pSCTemp = pSCHeadfp;

    clrscr();

    if (pSCHeadfp == NULL)
    {
        gotoxy(8, 3);
        puts("There somthing wrong in show function!");
        gotoxy(8, 4);
        puts("Press any key to continue!");
        getch();
        return 0;
    }

    printHead();

    while (pSCTemp->next != NULL)
    {
        pSCTemp = pSCTemp->next;

        printData(pSCTemp);
    }

    return 1;
}

/*打印表頭*/
void printHead( void )
{
    gotoxy(1, 5);
    puts("Name");
    gotoxy(15, 5);
    puts("Number");
    gotoxy(25, 5);
    puts("Sex");
    gotoxy(30, 5);
    puts("Age");
    gotoxy(40, 5);
    puts("Class");
    gotoxy(50, 5);
    puts("Telephone");
    gotoxy(60, 5);
    puts("Address");
}

/*輸出數據*/
/*成功則返回1,否則返回其他*/
int printData(link pSCTempfp)
{
    static int line = 6;
    if (pSCTempfp == NULL)
    {
        gotoxy(8, 3);
        puts("There is something wrong in show function!");
        gotoxy(8, 4);
        puts("Press any key to continue!");
        getch();
        return 0;
    }

    gotoxy(1, line);
    puts(pSCTempfp->name);
    gotoxy(15, line);
    printf("%d", pSCTempfp->number);
    gotoxy(25, line);
    putchar(pSCTempfp->sex);
    gotoxy(30, line);
    printf("%d", pSCTempfp->age);
    gotoxy(40, line);
    printf("%d", pSCTempfp->squad);
    gotoxy(50, line);
    puts(pSCTempfp->telephone);
    gotoxy(60, line);
    puts(pSCTempfp->address);

    ++line;
    return 1;
}
/*---------------------------------------------------------------------*/

/*查找函數*/
/*找到則返回1,沒找到返回0,返回其他則失敗*/
link find(link pSCHeadfp)
{
    void printHead(void);
    int printData(link);

    link pSCTemp = pSCHeadfp;
    int iFNumber;
    int isExist = 0;/*判斷是否存在元素,存在則置1,否則置0*/

    clrscr();
    if (pSCHeadfp == NULL)
    {
        gotoxy(4, 2);
        puts("There is something wrong in find function!");
        gotoxy(4, 3);
        puts("Press any key to continue!");
        getch();
        return NULL;
    }

    gotoxy(4, 4);
    puts("Please input the number that you want to find: ");
    scanf("%d", &iFNumber);

    while (pSCTemp->next != NULL)
    {
        pSCTemp = pSCTemp->next;

        if (pSCTemp->number == iFNumber)
        {
            isExist = 1;
            break;
        }
        else
        {
            isExist = 0;
        }
    }

    if (isExist == 1) /*找到*/
    {
        printHead();
        printData(pSCTemp);

        return pSCTemp;
    }
    else
    {
        gotoxy(4, 6);
        puts("Sorry! You havn't input the student!");
        return NULL;
    }
}
/*---------------------------------------------------------------------*/

/*修改函數*/
/*成功則返回1,否則返回其他*/
int modify(link pSCHeadfp)
{
    link find(link);

    link pSCTemp = pSCHeadfp;
    char *pstemp, stemp[10];
    char *str[7] = {
        "name", "number", "sex","age",
        "squad", "telephone", "address"
        };
    int i;

    pSCTemp = find(pSCHeadfp);

    clrscr();
    if (pSCTemp != NULL)/*存在此記錄*/
    {
        gotoxy(4, 4);
        printf("Which field do you want to modify?  ");
        scanf("%s", stemp);
        pstemp = stemp;
        for (i = 0; i < 7; i++)
        {
            if (strcmp(pstemp, str[i]) == 0)
            {
                break;
            }
        }

        gotoxy(4, 5);
        switch (i)
        {
        case 0:
        {
            printf("The old data of the field is :%s/n/t", pSCTemp->name);
            printf("The new data is : ");
            scanf("%s", pSCTemp->name);
        }
        break;
        case 1:
        {
            printf("The old data of the field is :%d/n/t", pSCTemp->number);
            printf("The new data is : ");
            scanf("%d", &pSCTemp->number);
        }
        break;
        case 2:
        {
            printf("The old data of the field is :%c/n/t", pSCTemp->sex);
            printf("The new data is :");
            getchar();
            scanf("%c", &pSCTemp->sex);
        }
        break;
        case 3:
        {
            printf("The old data of the field is :%d/n/t", pSCTemp->age);
            printf("The new data is :");
            getchar();
            scanf("%c", &pSCTemp->age);
        }
        break;
        case 4:
        {
            printf("The old data of the field is :%d/n/t", pSCTemp->squad);
            printf("The new data is :");
            scanf("%d", &pSCTemp->squad);
        }
        break;
        case 5:
        {
            printf("The old data of the field is :%s/n/t", pSCTemp->telephone);
            printf("The new data is :");
            scanf("%s", pSCTemp->telephone);
        }
        break;
        case 6:
        {
            printf("The old data of the field is :%s/n/t", pSCTemp->address);
            printf("The new data is :");
            scanf("%s", pSCTemp->address);
        }
        break;
        case 7:
        {
            printf("There is no that field!/n/t");
            printf("Press any key to return!");
            getch();
            return -1;
        }
        }

        gotoxy(4, 25);
        printf("OK! We have modified the record! And press any key to return!");
        getch();
    }
    else
    {
        gotoxy(4, 4);
        puts("There is no that student!");
        gotoxy(4, 5);
        puts("Press any key to ruturn!");
        getch();
        return 0;
    }

    return 1;
}
/*---------------------------------------------------------------------*/

/*添加數據函數*/
/*成功則返回1,否則返回其他*/
int add(link pSCLinkfp)
{
    void inputInterface( void );
    void readdata(link);

    gotoxy(8, 4);
    printf("Please input the information you want to add :");

    inputInterface();
    readdata(pSCLinkfp);

    return 1;
}
/*---------------------------------------------------------------------*/
/*刪除數據函數*/
/*成功則返回1,否則返回其他*/
int erase(link pSCHeadfp)
{
    link find(link);

    link pSCTemp, pSCTPrior = pSCHeadfp;

    pSCTemp = find(pSCHeadfp);

    clrscr();
    if (pSCTemp != NULL)/*存在*/
    {
        while (pSCHeadfp->next != pSCTemp)
        {
            pSCTPrior = pSCTPrior->next;
        }

        pSCTPrior->next = pSCTemp->next;
        free(pSCTemp);

        gotoxy(4, 4);
        puts("The record have delete successful!");
        gotoxy(4, 6);
        puts("Press any key to return!");
        getch();
    }
    else
    {
        gotoxy(4, 4);
        puts("There is no that student!");
        gotoxy(4, 5);
        puts("Press any key to ruturn!");
        getch();
        return 0;       
    }

    return 1;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章