gpt4 book ai didi

c - 我的程序不能很好地执行搜索功能。为什么?

转载 作者:行者123 更新时间:2023-11-30 21:30:10 26 4
gpt4 key购买 nike

我制作了一个学生数据库程序作为我在学校的最新项目。一切正常,您可以创建包含 ID、名字、姓氏和标记的记录。

这里的主要问题是;我的代码的“名称搜索”功能。

每当我用 ID 搜索某人时,它都能完美地工作,但如果我尝试用名字搜索,就会出现一些问题。

如果您尝试搜索第一个学生,它会找到但永远不会搜索其他学生。只会显示第一个学生,无论您输入什么内容,如果您搜索相同长度的学生姓名,它可能会找到错误的学生。

示例 1:

enter image description here

正如你所看到的,我输入了 Yov,但它发现了 Wow。

示例 2:

enter image description here

正如您所看到的,我在第一个输入中输入了“测试”,并且真正的学生出现了。每当我输入 Stack 作为以下输入时,主要问题就开始了。我预计 Stack Overflow 会出现,但测试学生再次显示为输出。

这些是我在程序中遇到的“唯一”问题的两件事。我的错误在哪里?

这是我的完整代码;

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

struct
{
long long int id;
char firstname[20];
char lastname[20];
int mark;
}student;

int main()
{
long long int idnumber;
int flag,choice,shift,found,continu,length;
char studentname[20];
FILE *fp;

printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
Label1:
printf("\n1 -> Store a new record in database\n");
printf("2 -> Search a student record by Student First Name\n");
printf("3 -> Search a student record by ID\n");
printf("4 -> Quit Student Database");
printf("\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
Label2:
printf("\nEnter Student Details:\n\nID number: ");
scanf("%lld",&student.id);
printf("\nName:");
scanf("%s",student.firstname);
printf("\nSurname:");
scanf("%s",student.lastname);
printf("\nMark(0 - 100 integer) : ");
scanf("%d",&student.mark);
fp=fopen("studentfile.txt","a+");
fprintf(fp,"\n%lld\t%s\t%s\t%d\t",student.id,student.firstname,student.lastname,student.mark);
fclose(fp);
printf("A student record has been added successfully...\n");
printf("\n\n1 -> Wish to add another record to database");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label2;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("Exiting.........");
break;
}

case 2:
Label4:
printf("\nEnter student first name: ");
scanf("%s",&studentname);
printf("Searching record with studentname=%s.\n",studentname);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%lld\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
length = strlen(student.firstname);
if(student.firstname[length]==studentname[length])
found=1;
}
}
if(found)
{
printf("\nThe record is found.\n");
printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label5:
printf("\n\n1 -> Wish to search another record");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label4;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label5;
}
case 3:
Label6:
printf("\nEnter the ID: ");
scanf("%lld",&idnumber);
printf("Searching record with ID=%lld.\n",idnumber);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%lld\t%s\t%s\t%d\t",&student.id,student.firstname,student.lastname,&student.mark);
if(student.id==idnumber)
found=1;
}
}
if(found)
{
printf("\nThe record is found.");
printf("\nID no: %lld\nName: %s\nSurname: %s\nMark: %d \n",student.id,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label7:
printf("\n\n1 -> Wish to search more..");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label6;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label7;
}
case 4: break;
default :
printf(" Bad choice...Enter the choice again...\n");
goto Label1;
}

getch();
return 0;
}

最佳答案

这回答了您的问题,但请修复if问题

if(student.firstname[length]==studentname[length])

必须是

if(strcmp(student.firstname[length], studentname[length]) == 0)

还有

scanf("%s", &studentname);

应该是错误的

scanf("%s", studentname);

它只是偶然起作用。

给你,把它当作节日礼物

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

struct
{
long long int id;
char firstname[20];
char lastname[20];
int mark;
} student;

void
storeRecord()
{
FILE *fp;

printf("\nEnter Student Details:\n\nID number: ");
scanf("%lld",&student.id);

printf("\nName:");
scanf("%19s",student.firstname);

printf("\nSurname:");
scanf("%19s",student.lastname);

printf("\nMark(0 - 100 integer) : ");
scanf("%d",&student.mark);

fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
if (fp == NULL)
return;
fprintf(fp, "\n%lld\t%s\t%s\t%d\t",
student.id,
student.firstname,
student.lastname,
student.mark);
fclose(fp);

printf("A student record has been added successfully...\n");
getchar();
}

void
printStudent()
{
printf("\nThe record is found.\n");
printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
student.id,
student.firstname,
student.lastname,
student.mark
);
}

void
searchStudentByName()
{
char studentname[20];
FILE *fp;
int found;
int matches;

printf("\nEnter student first name: ");
scanf("%19s", studentname);

printf("Searching record with studentname=%s.\n", studentname);

found = 0;
fp = fopen("studentfile.txt", "r");
if (fp == NULL)
{
printf("IO error\n");
return;
}

matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t",
&student.id,
student.firstname,
student.lastname,
&student.mark);

do
{
matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t",
&student.id,
student.firstname,
student.lastname,
&student.mark);
if (matches == 4)
found = (strcmp(student.firstname, studentname));
} while ((matches == 4) && (found == 0));
if (found != 0)
printStudent();
else
printf("Not found...\n");
getchar();
}

void
searchStudentById()
{
int id;
int found;
int matches;
FILE *fp;

printf("\nEnter student first name: ");
scanf("%d", &id);

printf("Searching record with id=%d.\n", id);

found = 0;
fp = fopen("studentfile.txt", "r");
if (fp == NULL)
{
printf("IO error\n");
return;
}

do
{
matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t",
&student.id,
student.firstname,
student.lastname,
&student.mark);
if (matches == 4)
found = (student.id == id);
} while ((matches == 4) && (found == 0));
if (found != 0)
printStudent();
else
printf("Not found...\n");
getchar();
}

int main()
{
int choice;

choice = 0;
while (choice != 4)
{
printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
printf("\n1 -> Store a new record in database\n");
printf("2 -> Search a student record by Student First Name\n");
printf("3 -> Search a student record by ID\n");
printf("4 -> Quit Student Database");
printf("\n\n");
printf("Enter your choice : ");

scanf("%d",&choice);
switch(choice)
{
case 1:
storeRecord();
break;
case 2:
searchStudentByName();
break;
case 3:
searchStudentById();
break;
}
}
return 0;
}

这是可读的,并且对于DRY还有一个改进。原则。您显然忽略了这一点。

您不需要将学生声明为全局变量。

此代码比前一个更好

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

struct Student
{
long long int id;
char firstname[20];
char lastname[20];
int mark;
} student;

void
storeRecord()
{
FILE *fp;

printf("\nEnter Student Details:\n\nID number: ");
scanf("%lld",&student.id);

printf("\nName:");
scanf("%19s",student.firstname);

printf("\nSurname:");
scanf("%19s",student.lastname);

printf("\nMark(0 - 100 integer) : ");
scanf("%d",&student.mark);

fp = fopen("studentfile.txt","a+"); /* check if the file was opened */
if (fp == NULL)
return;
fprintf(fp, "\n%lld\t%s\t%s\t%d\t",
student.id,
student.firstname,
student.lastname,
student.mark);
fclose(fp);

printf("A student record has been added successfully...\n");
getchar();
}

int
compareStudentsById(struct Student lhs, struct Student rhs)
{
return (lhs.id == rhs.id);
}

int
compareStudentsByName(struct Student lhs, struct Student rhs)
{
return (strcmp(lhs.firstname, rhs.firstname) == 0);
}

void
printStudent()
{
printf("\nThe record is found.\n");
printf("\nID: %lld\nName: %s\nSurname: %s\nMark: %d \n",
student.id,
student.firstname,
student.lastname,
student.mark
);
}

void
searchStudent(int(*compare)(struct Student,struct Student), const char *const name, int id)
{
FILE *fp;
int found;
int matches;


if (name != NULL)
printf("Searching record with Name = %s.\n", name);
if (id != -1)
printf("Searching record with ID = %d.\n", id);

found = 0;
fp = fopen("studentfile.txt", "r");
if (fp == NULL)
{
printf("IO error\n");
return;
}

matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t",
&student.id,
student.firstname,
student.lastname,
&student.mark);

do
{
struct Student other;

if (name != NULL)
strcpy(other.firstname, name);

other.id = id;
matches = fscanf(fp,"\n%lld\t%s\t%s\t%d\t",
&student.id,
student.firstname,
student.lastname,
&student.mark);

if (matches == 4)
found = (compare(student, other) != 0);

} while ((matches == 4) && (found == 0));

if (found != 0)
printStudent();
else
printf("Not found...\n");

getchar();
}

void
searchStudentByName()
{
char studentname[20];

printf("\nEnter student first name: ");
scanf("%19s", studentname);

searchStudent(compareStudentsByName, studentname, -1);
}

void
searchStudentById()
{
int id;

printf("\nEnter student first name: ");
scanf("%d", &id);

searchStudent(compareStudentsByName, NULL, id);
}

int main()
{
int choice;

choice = 0;
while (choice != 4)
{
printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
printf("\n1 -> Store a new record in database\n");
printf("2 -> Search a student record by Student First Name\n");
printf("3 -> Search a student record by ID\n");
printf("4 -> Quit Student Database");
printf("\n\n");
printf("Enter your choice : ");

scanf("%d",&choice);
switch(choice)
{
case 1:
storeRecord();
break;
case 2:
searchStudentByName();
break;
case 3:
searchStudentById();
break;
}
}
return 0;
}

关于c - 我的程序不能很好地执行搜索功能。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27711042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com