📄 6.c
字号:
#include<malloc.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
typedef struct note
{
char name[20];
char number[20];
float score;
int place;
struct note *next;
} note;
#define LEN sizeof(note)
note *creat();
void output(note *p);
note *dele(note *p,int n);
note *find(note *p,int m);
void output1(note *p);
note *insert(note *p);
int N=0;
main()
{
int n,m;
note *p1;
note *p;
note *p2;
note *p3;
p=creat();
output(p);
printf("\ndo you want to insert");
p2=insert(p);
p3=p2;
output(p2);
printf("\nwhich place do you want to find?\n");
printf("m:\n");
scanf("%d",&m);
p1=find(p3,m);
output1(p1);
printf("\nwhich place do you want to delete? \n");
printf("n:\n");
scanf("%d",&n);
p=dele(p3,n);
output(p);
}
note *insert(note *p)
{
note *p1;
note *p2;
note *head=p;
p1=(note *)malloc(LEN);
printf("\n姓名");
scanf("%s",&p1->name);
printf("学号");
scanf("%s",&p1->number);
printf("分数");
scanf("%f",&p1->score);
p1->next=NULL;
p2=head;
head=p1;
head->next=p2;
return(head);
}
void output1(note *p)
{
note *head;
head=p;
printf("\n姓名");
printf("%s\n",head->name);
printf("学号");
printf("%s\n",head->number);
printf("分数");
printf("%f\n",head->score);
printf("名次");
printf("%d\n",head->place);
}
note *find(note *p,int m)
{ note *head,*p1,*p2;
head=p;
p1=p;
p2=p;
if(head==NULL)
printf("\nlist null!\n");
else
{
while(m!=p1->place&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(m==p1->place)
{
head=p1;
}
else
printf("%d not been found ! \n",m);
}
return(head);
}
note *dele(note *p,int n)
{
note *head,*p1,*p2;
head=p;
p1=p;
if(head==NULL)
printf("\nlist null!\n");
else
{
while(n!=p1->place&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1!=NULL)
{
if(p1==head)
head=p1->next;
else if(p1->next!=NULL)
p2->next=p1->next;
else
p2->next=NULL;
}
else
printf("%d not been found ! \n",n);
}
return(head);
}
note *creat()
{
note *head;
note *p1,*p2;
p1=p2=(note *)malloc(LEN);
printf("\n姓名");
scanf("%s",&p1->name);
printf("学号");
scanf("%s",&p1->number);
printf("分数");
scanf("%f",&p1->score);
head=NULL;
while(strcmp(p1->name,"none")!=0)
{
N=N+1;
if(N==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(note *)malloc(LEN);
printf("\n姓名");
scanf("%s",&p1->name);
if(strcmp(p1->name,"none")==0)
break;
else
printf("学号");
scanf("%s",&p1->number);
printf("分数");
scanf("%f",&p1->score);
}
p2->next=NULL;
return(head);
}
void output(note *p)
{
int i=0,j=0,m;
float f,k;
note *p1,*p2,*p3;
note *head;
p1=p;
p2=p;
head=p;
while(p2!=NULL)//找出总共的指针数
{
i++;
p2=p2->next;
}
N=i;
if(N!=1)
{
p2=p;
f=p2->score;
while(p2!=NULL)//找出最大的分数值,并将其名次赋值
{
if(f<p2->score)
{ f=p2->score;
p2->place=1;
p2=p2->next;
}
else
p2=p2->next;
}
for(i=2;i<=N;i++)
{ p2=p;
while(p2!=NULL)//按顺序找一个比最大值小的数
{
if(p2->score<f)
{
k=p2->score;
p3=p2;
p2=p2->next;
j=1;
break;
}
else
p2=p2->next;
}
if(j)
{
p2=p;
while(p2!=NULL)//找出比最大值小的另一数,以前一数为基准进行比较,找出第二大数
{m=1;
if(p2->score<f)
{ if(k<p2->score)
{
k=p2->score;
p2->place=i;
p2=p2->next;
m=0;
}
else
{p2=p2->next;}
}
else
{p2=p2->next;}
}
if(m)
p3->place=i;
f=k;
}
}
}
else
p->place=1;
if(head!=NULL)
do
{ printf("\n姓名");
printf("%s",head->name);
printf("学号");
printf("%s",head->number);
printf("分数");
printf("%f",head->score);
printf("名次");
printf("%d",head->place);
head=head->next;
}while(head!=NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -