正しいコードを書くための助けが必要です plz q:学生情報の単純なデータベースを編成するプログラムを書いてください。
プログラムは、ユーザーが一連の学生データ レコードを入力できるようにする必要があります。各学生データ レコードには、姓、名、年齢の 3 つのフィールドがあります。ユーザーは、次の 4 つの操作を実行できる必要があります。1) レコードの入力、2) レコードの削除、3) すべてのレコードの印刷、および 4) すべてのレコードの並べ替え。4 つのオプションすべてを最初に出力する必要があります。ユーザーは、実行したい操作に対応する番号を入力できるプロンプトを受け取る必要があります。レコードの入力操作では、ユーザーは同じ行に 3 つの文字列 (フィールドごとに 1 つの文字列) を入力するように求められます。すべてのレコードを印刷する操作では、各レコードを別の行に印刷する必要があります。すべてのレコードのソート操作は、姓に基づいてソートする必要があります。
私が書いたコードは次のとおりです:(表示と追加は機能していますが、削除と並べ替えは機能していません!)
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct student
{
char fname[29];
char lname[29];
int age;
struct student *next;
};
int main()
{
int i,n,ch,ps,x,k;
k=0;
struct student *h,*t,*t1,*w,*q;
h=NULL;
printf("\n/* student data records*/");
while(1)
{
printf("\n1.display\n2:add\n3.delete\n4.exit\n5.sort by first name\n");
printf("\nenter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(h==NULL)
{
printf("no records are available");
}
w=h;
while(w!=NULL)
{
printf("\nfirst name of a student:%s\nlast name of a student:%s\nage of a student:%d\n",
w->fname,w->lname,w->age);
w=w->next;
}
break;
case 2:
printf("\nenter the new record=\t");
if(h==NULL)
{
h=t=(struct student *)malloc(sizeof(struct student));
printf("\nfirst Name of a student:\t");
scanf("%s",&t->fname);
printf("\nlast Name of a student:\t");
scanf("%s",&t->lname);
printf("\nage of a student:\t");
scanf("%d",&t->age);
t->next=NULL;
break;
}
else
{
t1=(struct student *)malloc(sizeof(struct student));
printf("\nFirst Name of a student:\t");
scanf("%s",&t1->fname);
printf("\nlast Name of a student:\t");
scanf("%s",&t1->lname);
printf("\nage of a student:\t");
scanf("%d",&t1->age);
t1->next=t->next;
t->next=t1;
t=t1;
}
break;
case 3:
printf("enter name of student whos record is to be deleted=\n");
scanf("%d",&ps);
t=h;
while(t->fname!=ps-1)
{
t=t->next;
}
t1=t->next;
t->next=t1->next;
free(t1);
break;
case 4:
exit(0);
break;
case 5:
printf("not working yet");
{
void sort( student[],int n)
{ int i,j,comp=0,passes=0;
student temp;
for(i=1;i<n;i++)
{
passes++;
for(j=0;j<n-i;j++)
{ comp++;
if(st[j].lname < st[j+1].lname)
{ temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
}
}
break;
}
}
}