如何在我的数据库中删除一个学生 [英] How Can I Delete One Student In My Database

查看:90
本文介绍了如何在我的数据库中删除一个学生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中删除一名学生?





ID姓名TC NO TEL NO ADRESS

15 A 2 5 YYY

25 B 2 6 YYY

35 C 3 7 YYY

45 D 4 8 YYY

55。

65.



当我输入id时:15 15的行必须全部删除。



我该怎么做?



How can i delete one student in my database?


ID NAME TC NO TEL NO ADRESS
15 A 2 5 YYY
25 B 2 6 YYY
35 C 3 7 YYY
45 D 4 8 YYY
55 .
65 .

WHen i enter the id : 15 15's line must be delete all above.

how can i do this ?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
//Prototypes functions
void add_student();
void delete_student();
void modify_student();
void list_student();
//Global variables
char deleteid[12];
char updateid[12];
char data_tc_idno[12];
char data_id[13];
char data_adress[30];
char data_name[30];
char data_telno[12];
int deleteStatus[30];
int list_no;
FILE *cfPtr; //File pointer
typedef struct list{
        char tc_idno[12];
        char id[13];
        char name[30];
        char telno[13];
        char adress[30];
};
struct list list2014[30];

int main(void)
{
        char key_command;
        printf("Student Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
       printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");
        do{
                key_command = _getch();
                if (key_command == 'a' || key_command == 'A')
                        add_student();
                else if (key_command == 'd' || key_command == 'D')
                        delete_student();
                else if (key_command == 'l' || key_command == 'L')
                        list_student();
                else if (key_command == 'q' || key_command == 'Q')
                        exit(0);
        } while (key_command != 'q' || key_command != 'Q');

        system("pause");

        return 0;
}
/////////////////////////////////////////////////////////////////////////////////
void add_student()
{
    int n=0;


if((cfPtr = fopen("student.txt", "a+"))==NULL)
	printf("FILE COULD NOT BE OPENED\n");
else{
        system("cls");
        printf("\n\nNumber of students enrolled:%d\n\n", list_no);
        printf("This is %d.student's information.\n", list_no + 1);

        printf("ID No :");
        scanf("%s", list2014[list_no].id);
        printf("T.C ID No:");
        scanf("%s", list2014[list_no].tc_idno);
        printf("Name:");
        scanf("%s", list2014[list_no].name);
        printf("Address :");
        scanf("%s", list2014[list_no].adress);
        printf("Phone Number:");
        scanf("%s", list2014[list_no].telno);
        system("cls");
        printf("\n\n1 Student added...\n\n");

// File processing Append WRÝTE START
  for(n=0;n<1;n++){
        n++;
     fprintf(cfPtr, "%s %s %s %s %s\n", list2014[list_no].id, list2014[list_no].tc_idno,list2014[list_no].name,list2014[list_no].adress,list2014[list_no].telno);

  }
       fclose(cfPtr);
// File processing END
list_no += 1;
        printf("Student Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
        printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");

}
}
//////////////////////////////////////////////////////////////////////////
void delete_student()
{
    
    ????????????????????????????????????

        printf("\nStudent deleted...\n");
        printf("\nStudent Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
        printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");
}
/////////////////////////////////////////////////////////////////////////
void list_student()
{
        int i;
        char key_command;
        system("cls");


        printf("List of Students\n");
        printf("  Student id------Student T.C------Student Name------Student Adress------Student Tel no\n");

        if((cfPtr = fopen("student.txt", "r"))==NULL)
	printf("FILE COULD NOT BE OPENED\n");
else{
    fscanf( cfPtr, "%s%s%s%s%s", list2014[i].id, list2014[i].tc_idno, list2014[i].name,list2014[i].adress,list2014[i].telno);

          while(!feof(cfPtr)) //kayit list_nosi degismediginden
        {

            printf( "  %s%16s%17s%18s%20s\n", list2014[i].id, list2014[i].tc_idno, list2014[i].name,list2014[i].adress,list2014[i].telno);
            fscanf( cfPtr, "%s%s%s%s%s", list2014[i].id, list2014[i].tc_idno, list2014[i].name,list2014[i].adress,list2014[i].telno);
            list_no++;
        }

       fclose(cfPtr);
}
        printf("\n");
         printf("\nThere is %d student in data.\n\n", list_no);
        printf("[B] Go back to the MENU\n");
        printf("[Q] Quit\n");
        printf("Choose option: ");
        key_command = _getch();
        if(key_command=='b' || key_command=='B')
        {
        system("cls");
        printf("Student Records System v1.3 developed by Ozan Manav\n");
        printf("***************************************************\n");
        printf("**** | [A]Add Student with info | ****\n**** | [D]Delete Student info   | ****\n**** | [U]Update Student info   | ****\n**** | [L]List Students         | ****\n**** | [Q]Quit                  | ****\n");
        printf("***************************************************\n");
        printf("!!! Please use ID NO for delete or update student !!!\n");
        printf("***************************************************\n");
        printf("Choose option: ");
        }
         else if (key_command == 'q' || key_command == 'Q')
                        exit(0);
}


/////////////////////////////////////////////////////////////////////////





i意味着我怎么能写delete_student函数请帮助......



i mean this how can i write delete_student function please help ...

推荐答案

0。初始化所有全局变量。



0. initialize all global variables.

char deleteid[12] = {0};
char updateid[12] = {0};
char data_tc_idno[12] = {0};
char data_id[13] = {0};
char data_adress[30] = {0};
char data_name[30] = {0};
char data_telno[12] = {0};
int deleteStatus[30] = {0};
struct student list2014[30] = {0};





1.重命名



1. rename your

typedef struct list

to

struct student

。名称列表具有误导性。对于阅读代码的人来说,像学生这样的东西应该更清楚。您将类型引用为结构,因此typedef不会为您执行任何操作。



2.加载:从文件中读取整个学生列表list2014;对于每个学生,将deleteStatus [index]设置为某个值以指示该插槽已加载且有效。在打印菜单之前调用一次。



3.打印:将完整列表打印到控制台 - 来自list2014,而不是文件。此功能应该跳过标记为已删除的学生记录(请参阅deleteStatus)或未使用。



4.删除学生,在list2014中找到学生的索引并使用该index将deleteStatus [index]设置为一个值,意思是删除。



此代码非常混乱:



. The name "list" is misleading. Something like "student" should be more clear to anyone reading your code. You reference the type as a struct, so the typedef does nothing for you.

2. load: read your whole student list "list2014" from a file; for each student, set deleteStatus[index] to some value to indicate that slot is loaded and valid. Call this once before printing menu.

3. print: print the full list to the console - from "list2014", not from a file. This function should skip student records marked as deleted (see "deleteStatus") or unused.

4. to delete a student, find the student's index in list2014 and use that index to set deleteStatus[index] to a value than means "deleted".

This code is very messy:

key_command = _getch();
if (key_command == 'a' || key_command == 'A')
    add_student();
else if (key_command == 'd' || key_command == 'D')
    delete_student();
else if (key_command == 'l' || key_command == 'L')
    list_student();
else if (key_command == 'q' || key_command == 'Q')
    exit(0);





使用toupper函数将字母转换为大写。这将简化你的if语句(甚至更好:用switch语句替换if / else)。





Use the toupper function to convert the letter to uppercase. This will simplify your if statements (even better: replace the if/else's with a switch statement).

key_command = toupper( _getch() );
if ('A' == key_command)
    add_student();
else if ('D' == key_command)
    delete_student();
else if ('L' == key_command)
    list_student();
else if ('Q' == key_command)
    exit(0);





5.保存:将完整的学生列表list2014写入另一个文件 - 在退出之前调用此方法。此功能应跳过标记为已删除的学生记录(请参阅deleteStatus)或未使用。



6.作为调试辅助工具,添加S保存命令以调用保存功能,这样你就可以看到每个操作的效果。



祝你好运。



5. save: write your full student list "list2014" to a different file - call this before exit. This function should skip student records marked as deleted (see "deleteStatus") or unused.

6. as a debugging aid, add a 'S' save command to call the save function so you can see the effects of each operation.

Good luck.


这篇关于如何在我的数据库中删除一个学生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆