例9.1
#include<stdio.h>
struct student3{
int no;
char name[10];
int age;
char sex;
char addr[50];
double score;
} ;
void main()
{
struct student3 s={940114,"李红",19,'F',"杭州下沙学林街80号",655.5};
printf("%d %s %d %c %s %.1f\n",s.no,s.name,s.age,s.sex,s.addr,s.score);
}
例9.2
#include<stdio.h>
struct student2{
char name[10];
int age;
float score[5],ave;
};
void main()
{
struct student2 stu;
int i;
stu.ave = 0;
scanf("%s%d",stu.name,&stu.age);
for(i=0;i<5;i++)
{
scanf("%f",&stu.score[i]);
stu.ave+=stu.score[i]/5.0;
}
printf("Output:\n");
printf("%-s%-4d\n",stu.name,stu.age);
for(i=0;i<5;i++)
printf("%-6.1f",stu.score[i]);
printf(" average=%6.1f\n",stu.ave);
}
例9.3
#include<stdio.h>
#include<math.h>
struct comp
{
double x,y;
double m;
};
void main()
{
struct comp s1,s2;
double compare(struct comp a,struct comp b);
printf("Input:\n");
scanf("%lf%lf",&s1.x,&s1.y);
scanf("%lf%lf",&s2.x,&s2.y);
if(compare(s1,s2)==0)
printf("Equal\n");
else
printf("Unequal\n");
}
double compare(struct comp a,struct comp b)
{
a.m=sqrt(a.x*a.x+a.y*a.x);
b.m=sqrt(b.x*b.x+b.y*b.y);
return (a.m-b.m);
}
例9.4
#include<stdio.h>
struct comp
{
double x,y;
double m;
};
void main()
{
struct comp s1,s2,z;
struct comp add(struct comp a,struct comp b);
printf("Input:\n");
scanf("%lf%lf",&s1.x,&s1.y);
scanf("%lf%lf",&s2.x,&s2.y);
z=add(s1,s2);
printf("%.2f%+.2fi\n",z.x,z.y);
}
struct comp add(struct comp a,struct comp b)
{
struct comp c;
c.x=a.x+b.x;
c.y=a.y+b.y;
return c;
}
例9.5
#include<stdio.h>
#include<math.h>
#define N 6
struct comp
{
double x,y;
double m;
};
void main()
{
struct comp a[N],temp;
int i,j,k;
printf("Input complex:\n");
for(i=0;i<N;i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
a[i].m=sqrt(a[i].x*a[i].x+a[i].y*a[i].y);
}
for(i=0;i<N-1;i++){
k=i;
for(j=i+1;j<N;j++)
if(a[k].m>a[j].m) k=j;
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("After sort:\n");
for(i=0;i<N;i++)
printf("%0.1f%+.1fi\n",a[i].x,a[i].y);
}
例9.6
#include<stdio.h>
#include<math.h>
#define N 6
struct comp{
double x,y;
double m;
};
void main()
{
struct comp a[N];
int i;
void sort (struct comp *,int);
printf("Input complex:\n");
for(i=0;i<N;i++){
scanf("%lf%lf",&a[i].x,&a[i].y);
a[i].m=sqrt(a[i].x*a[i].x+a[i].y*a[i].y);
}
sort(a,N);
printf("After sort : \n");
for(i=0;i<N;i++)
printf(" %.1f%+.1fi",a[i].x,a[i].y);
}
void sort(struct comp * pa,int n)
{
int i,j,k;
struct comp temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if((pa+k)->m>(pa+j)->m)
k=j;
temp=*(pa+i);
*(pa+i)=*(pa+k);
*(pa+k)=temp;
}
}
例9_7
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[10];
float score;
struct student * next;
};
void main()
{
struct student * insert(struct student *head);
void print(struct student * head);
struct student * head;
int i,n;
printf("Input the number of nodes:\n");
scanf("%d",&n);
printf("Input nodes:\n");
head=(struct student *)malloc(sizeof(struct student));
scanf("%s%f",head->name,&head->score);
head->next=NULL;
for(i=1;i<n;i++)
head=insert(head);
printf("Output:\n");
print(head);
}
void print(struct student * head)
{
struct student *p=head;
while(p!=NULL){
printf("%s %.1f\n",p->name,p->score);
p=p->next;
}
}
struct student *insert(struct student *head)
{
struct student *p,*pnew,*pold;
pnew=(struct student *)malloc(sizeof(struct student));
scanf("%s%f",pnew->name,&pnew->score);
p=head;
if(pnew->score>head->score){
pnew->next=head;
head=pnew;
}
else{
while(p!=NULL&&pnew->score<=p->score){
pold=p;
p=p->next;
}
pnew->next=p;
pold->next=pnew;
}
return head;
}
#include<stdio.h>
#include<stdlib.h>;
struct student
{
char name[10];
float score;
struct student * next;
};
void main()
{
struct student * create();
struct student *pdelete() ;
void print();
struct student * head;
int n;
printf("Input the number of nodes:\n");
scanf("%d",&n);
head=create(n);
printf("Output all nodes:\n");
print(head);
head=pdelete(head,60);
printf("Output fail-nodes:\n");
print(head);
}
void print(struct student * head)
{
struct student *p=head;
while(p!=NULL){
printf("%s %.1f\n",p->name,p->score);
p=p->next;
}
}
struct student *create(int n)
{
struct student * head,* pnew,* ptail;
int i;
pnew=(struct student *)malloc(sizeof(struct student));
scanf("%s%f",pnew->name,&pnew->score);
head=ptail=pnew;
for(i=1;i<n;i++)
{
pnew=(struct student * )malloc(sizeof(struct student));
scanf("%s%f",pnew->name,&pnew->score);
ptail->next=pnew;
ptail=pnew;
}
ptail->next=NULL;
return head;
}
struct student * pdelete(struct student *head,int grade)
{
struct student *p,*pold;
p=head;
while(head!=NULL&&head->score>=grade){
head=head->next;
free(p);
p=head;
}
if(head==NULL)
return head;
p=head->next;
pold=head;
while(p!=NULL){
if(p->score>=grade){
pold->next=p->next;
free(p);
p=pold->next;}
else{
pold=p;
p=p->next;
}
}
return head;
}