1、
li 45 300.0
chang 30 200.0
chang
2、
#include<stdio.h>
struct students
{
char sid[100];
char name[100];
float score[3];
}student;
void main()
{ int i; float j;
printf("\nPlease input sid: ");
scanf("%s",student.sid);
printf("\nPlease input name: ");
scanf("%s",student.name);
printf("\nPlease input 3 score:(like1,1,1) ");/ *輸入逗號(hào)隔開*/
scanf("%f,%f,%f",&student.score[0],&student.score[1],&student.score[2]);
printf("\nsid = %s",student.sid);
printf("\nname = %s",student.name);
j=(student.score[0]+student.score[1]+student.score[2])/3.0;
printf("\naverage = %.2f",j);
getch();
}
3、
#include<stdio.h>
#include<string.h>
#define F sizeof(student)
#define NULL 0
typedef struct scores
{ int english;
int math;
int c_language;
int all;
}TP;
typedef struct students
{ char sid[15];
char name[15];
TP score;
struct students *next;
}student;
student *input()
{ student *head,*p1,*p2;
int n=0; char ch;
clrscr();
head=(student *)malloc(F);head->next=NULL;
do
{ n++;
printf("\n\nPlease input %d student message: \n\n",n);
printf("\t%d student sid: ",n);
p1=(student *)malloc(F);p1->next=NULL;
scanf("%s",p1->sid);
printf("\n\t%d student name: ",n);
scanf("%s",p1->name);
printf("\n\t%d student scores(englesh,math,c_language): ",n);
scanf("%d,%d,%d",&p1->score.english,&p1->score.math,&p1->score.c_language);
p1->score.all=p1->score.english+p1->score.math+p1->score.c_language;
if(n==1)
{ head->next=p1;p2=p1; }
else
{ p2->next=p1;
p2=p1;
}
printf("\n\n\t\t\tContinue or back (press y/n): ");
ch=getch();
}while(ch=='y'||ch=='Y');
return head;
}
void average1(student *head)
{ student *p; int j;
clrscr();
p=head->next;
while(p)
{ j=p->score.all/3;
printf("\n\nname: %s\taverage: %d",p->name,j);
p=p->next;
}
printf("\n\n\nPress eny key return.");
getch();
}
void average2(student *head)
{
student *p;int n=0,temp1=0,temp2=0,temp3=0;
p=head->next;
while(p)
{ temp1+=p->score.english;
temp2+=p->score.math;
temp3+=p->score.c_language;
p=p->next;n++;
}
printf("\n\naverage english is : %d\naverage math is : %d\naverage c_language is : %d\t",temp1/n,temp2/n,temp3/n);
}
student *sort(student *head)
{ student *head1,*p,*q,*r; int temp1=0,temp2=0,temp3=0,temp4; char s[15],n[15];
head1=head;
for(p=head1->next;p->next!=NULL;p=p->next)
{ r=p;
for(q=p->next;q;q=q->next)
if(q->score.all>r->score.all)
r=q;
if(r!=p)
{ strcpy(s,p->sid);strcpy(n,p->name);
temp1=p->score.english;
temp2=p->score.math;
temp3=p->score.c_language;
temp4=p->score.all;
strcpy(p->sid,r->sid);strcpy(p->name,r->name);
p->score.english=r->score.english;
p->score.math=r->score.math;
p->score.c_language=r->score.c_language;
p->score.all=r->score.all;
strcpy(r->sid,s);strcpy(r->name,n);
r->score.english=temp1;
r->score.math=temp2;
r->score.c_language=temp3;
r->score.all=temp4;
}
}
return head1;
}
void output(student *head)
{ student *head2,*p;int i=1;
clrscr();
head2=sort(head);
for(p=head2->next;p!=NULL;p=p->next)
printf("\n\nname: %s\tsid: %s\tenglish: %d\tmath: %d\tc_language: %d\taverage: %d\tmingci: %d",p->name,p->sid,p->score.english,p->score.math,p->score.c_language,p->score.all/3,i++);
average2(head);
printf("\n\n\n\t\tPress eny key back.");
getch();
}
void main()
{ student *head,*p1,*p2;
int i=0,j=1;
head=input();
do
{ clrscr();
printf("\n\n(1): average1.\n\n(2): average2.\n\n(3): sort.\n\n(4): output.\n\n\n Please choose: ");
scanf("%d",&i);
switch(i)
{ case 1: average1(head); break;
case 2: clrscr();average2(head); printf("\n\n\nPress eny key retuen.");getch(); break;
case 3: clrscr();p1=sort(head); for(p2=p1->next;p2!=NULL;p2=p2->next) printf("\n\t\tname: %s\tmingci:%d",p2->name,j++);printf("\n\n\nPress eny key back.");getch(); break;
case 4: output(head); break;
default: printf("\nYour choose is not right.");break;
}
}while(i!=-1);
}
4、
#include<stdio.h>
#include<string.h>
#define NULL 0
#define F sizeof(worker)
typedef struct work
{ char sid[15];
char name[15];
int money;
struct work *next;
}worker;
int min=0,max=0;
char a[15],b[15];
worker *input()
{ worker *head,*p,*q;int n=0;
char ch;
head=(worker *)malloc(F); head->next=0;
do
{ n++;
p=(worker *)malloc(F); p->next=0;
printf("\n\n\tPlease input %d worker message : ",n);
printf("\n%d worker sid: ",n);scanf("%s",p->sid);
printf("\n%d worker name: ",n);scanf("%s",p->name);
printf("\n%d worker money: ",n);scanf("%d",&p->money);
if(n==1)
{ head->next=p; q=p;
max=p->money;strcpy(a,p->name);
min=p->money;strcpy(b,p->name);
}
else
{ q->next=p;
if(p->money>max) {max=p->money;strcpy(a,p->name);}
if(p->money<min) {min=p->money;strcpy(b,p->name);}
q=p;
}
printf("\n\t\ty/n");ch=getch();
}while(ch=='y'||ch=='Y');
return head;
}
void output()
{ clrscr();
printf("\nThe max money is: %d\t\tname is: %s\n\n",max,a);
printf("\nThe min money is: %d\t\tname is: %s",min,b);
}
void main()
{
input();
output();
getch();
}
5、6、
#include"stdio.h"
#define F sizeof(stu)
#define NULL 0
typedef struct student
{
int sid;
int average;
struct student *next;
}stu;stu *head;
stu *create()
{ stu *p1,*p2;
int n=0; char ch;
head=(stu *)malloc(F);head->next=NULL;
do
{ n++;
printf("\n\nPlease input %d student message: \n\n",n);
printf("\t%d student sid: ",n);
p1=(stu *)malloc(F);p1->next=NULL;
scanf("%d",&p1->sid);
printf("\n\t%d student average: ",n);
scanf("%d",&p1->average);
if(n==1)
{ head->next=p1;p2=p1; }
else
{ p2->next=p1;
p2=p1;
}
printf("\n\n\t\t\tContinue or back (press y/n): ");
ch=getch();
}while(ch=='y'||ch=='Y');
return head;
}
stu *select(stu *head,int x)
{
stu *s;
s=head->next;
while(s)
{
if(s->sid==x)
break;
s=s->next;
}
return s;
}
stu *insert(stu *head,int x,int y)
{
stu *p,*r,*q;
clrscr();
p=head->next;
r=(stu *)malloc(sizeof(stu));
r->sid=x;
r->average=y;
if(p==NULL)/ *如果插入空表*/
{
p=r;
r->next=NULL;
printf("\ninsert success!!");
}
else
{ while(x>p->sid) / *找到插入的位置,按學(xué)號(hào)大小。(找到位置或者到了表尾都會(huì)跳出循環(huán))*/
{ if(p->next==NULL)break; p=p->next; }
if(x<p->sid) / *插到中間位置*/
{ r->sid=p->sid;
r->average=p->average;
p->sid=x;
p->average=y;
r->next=p->next;
p->next=r;
printf("\ninsert success!!");
}
else if(x==p->sid) / *學(xué)號(hào)不能相同*/
printf("\nError--->your input this same sid.");
else / *插到末尾*/
{ p->next=r;
r->next=NULL;
printf("\ninsert success!!");
}
}
return head;
}
stu *get(stu *head,int n) / *得到位置為n的結(jié)點(diǎn)的指針*/
{ stu *p;int i;
p=head->next;
if(n==0) return head;
else
{ for(i=1;i<n;i++)
p=p->next;
return p;
}
}
stu *delete(stu *head,int sid)
{
stu *p,*q;int temp=0,i=0;
p=head->next;
if(!p)
{ printf("\nlist is empty. press eny key back.");getch();return head;}/ *表空*/
else
{ while(p) / *查找學(xué)號(hào)為sid的結(jié)點(diǎn)的指針*/
{i++; / *標(biāo)記學(xué)號(hào)為sid的結(jié)點(diǎn)的位置*/
if(p->sid==sid)
{temp=1;break;} / *temp=1標(biāo)記找到了*/
p=p->next;
}
if(temp==1) / *如果有學(xué)號(hào)為sid的結(jié)點(diǎn)*/
{ q=get(head,i-1);/ *得到sid的前一個(gè)結(jié)點(diǎn)的指針*/
q->next=p->next;
free(p);
printf("\n\ndelete sucess !!!");
return head;
}
else / *沒有找到*/
{ printf("\n\nNO this data.\n");
return head;
}
}
}
void print(stu *head)
{
stu *p;
p=head->next;
if(!p){printf("\nlist is empty. press eny key back.");getch();}
while(p)
{
printf("\n%d :\t%d ",p->sid,p->average);
p=p->next;
}
}
main()
{
stu *p1,*p2;
char ch1;
int n,i=0,j=0;
head=create();
do
{clrscr();
printf("\n1. insert. ");
printf("\n2. select.");
printf("\n3. delect.");
printf("\n4. print list. ");
printf("\n5. EXIT ");
printf("\n ............choice (1-5).............");
ch1=getch();
switch(ch1)
{
case '1':
{ clrscr();
printf("\nplease input insert sid. and average(like 1,1):");
scanf("%d,%d",&i,&j);
head=insert(head,i,j);
printf("\n\n\nPress eny key back.");getch();
break;
}
case '2':
{ clrscr();
printf("\ninput you want to selete sid: ");
scanf("%d",&n);
p1=select(head,n);
{ if(p1) printf("\nsid:%d\taverage:%d",p1->sid,p1->average);
else printf("\nNo this data.");
}
printf("\n\n\nPress eny key back.");getch();
break;
}
case '3':
{ clrscr();printf("\nPlease input you want delete sid: ");
scanf("%d",&n);
head=delete(head,n);
printf("\n\n\nPress eny key back.");getch();
break;
}
case '4':
{ clrscr();
printf("All information :");
print(head);
printf("\n\n\nPress eny key back.");getch();
break;
}
case '5': return;
default: printf("\n\nYour enter is not right. press eny key back.");getch();
}
}while(n);
}
7、
#include<stdio.h>
#define F sizeof(L)
typedef struct list
{ char data;
struct list *next;
}L;
L *set_list()
{ L *head,*p1,*p2;
char c;
int n=0;
head=(L *)malloc(F); head->next=0; / *建立鏈表*/
p1=p2=head;
printf("\nPlease input char(press * finish):");
scanf("%c",&c);
while(c!='*')
{ n++;
if(n==1)
p1->data=c;
else
{ p1=(L *)malloc(F);
p1->data=c;
p2->next = p1;
p2 = p1;
p1->next = 0;
}
scanf("%c",&c);
}
p1=head;
while(p1)
{ printf("%c ",p1->data);p1=p1->next; }
printf("\n\n\n");
return head;
}
void change_list(L *head1) / *算法:p2指向最后一個(gè)元素,p1指向第一個(gè)元素。交換他們的值,p1,p2同時(shí)往中間靠攏。*/
{ L *p1,*p2,*p3;
int i,j,k,n=1;
char temp;
p1=head1;p2=head1;p3=head1;
while(p3->next)
{ p3=p3->next;n++; }/ *求鏈長(zhǎng)*/
for(i=n;i>(n/2);i--) / *外循環(huán)使p1后移,p2前移。*/
{ p2=head1;
for(j=1;j<i;j++)
p2=p2->next; / *p2指向最后一個(gè)元素*/
temp=p1->data;p1->data=p2->data;p2->data=temp;/ *交換他們的值*/
p1=p1->next;/ *p1向后移*/
}
while(head1)
{ printf("%c ",head1->data); head1=head1->next; }
}
void main()
{ L *head;
head=set_list();
change_list(head);
getch();
}