1.根据学员的成绩,输出不及格党员的详细信息。
#include<stdio.h>
struct student
{
int num;
char *name;
char sex;
float score;
};
void main()
{
static struct student stu1={1,"张三",'M',61};
static struct student stu2={2,"李四",'M',92};
static struct student stu3={3,"宫婷",'F',57};
printf("/n不及极学员名单如下:/n");
if(stu1.score<60)
printf("%d/t%s/t%c/t%5.2f/n",stu1.num,stu1.name,stu1.sex,stu1.score);
if(stu2.score<60)
printf("%d/t%s/t%c/t%5.2f/n",stu2.num,stu2.name,stu2.sex,stu2.score);
if(stu3.score<60)
printf("%d/t%s/t%c/t%5.2f/n",stu1.num,stu3.name,stu3.sex,stu3.score);
if(stu1.score>=60 && stu2.score>=60 && stu3.score>=60)
printf("/n没有不及格的学员。/n");
}
2.结构指针变量的使用。
#include<stdio.h>
struct student
{
int num;
char *name;
char sex;
float score;
}stu={1,"张三",'M',61},*pstu;
void main()
{
pstu=&stu;
printf("学号:%d 姓名:%s/n",stu.num ,stu.name );
printf("学号:%d 姓名:%s/n",(*pstu).num,(*pstu).name );
printf("学号:%d 姓名:%s/n",pstu->num,pstu->name );
}
3.结构数组的用法,求学员的总成绩和平均成绩,并统计不及格人数。
#include<stdio.h>
#define N 5
struct student
{
int num;
char *name;
char sex;
float score;
}stu[N]={{1,"张三",'M',61},{2,"李四",'M',92.5},{3,"王五",'M',45},{4,"宫婷",'F',84},{5,"紫嫣",'F',76}};
void main()
{
int i,count=0;
float ave,sum=0;
for(i=0;i<N;i++)
{
sum+=stu[i].score;
if(stu[i].score<60)
count++;
}
printf("总分:%7.2f/n",sum);
ave=sum/N;
printf("平均分:%5.2f/n",ave);
printf("不及格人数为:%d/n",count);
}
4.结构成员作参数传递给函数的用法。
#include<stdio.h>
#define N 5
struct film
{
char name[25];
char director[25];
int duration;
};
void display(char *,char *,int *);//函数原型。
void main()
{
struct film f1;
printf("/n/t请输入电影的详细信息");
printf("/n/n请输入片名:");
gets(f1.name );
fflush(stdin);
printf("/n请输入导演名:");
gets(f1.director);
fflush(stdin);
printf("/n请输入片长(分钟):");
scanf("%d",&f1.duration);
display(f1.name,f1.director,&f1.duration);
}
void display(char *n,char *d,int *m)
{
printf("/n/t电影的详细信息/n");
printf("/n 片名: %s/n",n);
printf("/n 导演: %s/n",d);
printf("/n 片长: %d/n",*m);
}
5.编写求复数的和。
#include<stdio.h>
struct complex
{
double re;//实部。
double im;//虚部。
};
struct complex add(struct complex,struct complex);
void main()
{
struct complex x={6.5,8.9},y={7.1,9.4};
struct complex z;
z=add(x,y);
printf("和为:%5.2lf+%5.2lf/n",z.re,z.im);
}
struct complex add(struct complex a,struct complex b)
{
struct complex c;
c.re=a.re+b.re;
c.im=a.im+b.im;
return c;
}
6.通过结构作参数传递求学员的总成绩和平均分,并统计不及格人数。
#include<stdio.h>
#define N 5
struct student
{
int num;
char *name;
char sex;
float score;
}stu[N]={
{1,"张三",'M',61},
{2,"李四",'M',92.5},
{3,"王五",'M',45},
{4,"宫婷",'F',84},
{5,"紫嫣",'F',76}};
void ave(struct student *ps);
void main()
{
struct student *ps;
ps=stu;
ave(ps);
}
void ave(struct student *ps)
{
int i,count=0;
float ave,sum=0;
for(i=0;i<N;i++,ps++)
{
sum+=ps->score;
if(ps->score<60)
{
count++;
}
}
printf("总分:%7.2f/n",sum);
ave=sum/N;
printf("平均分:%5.2f/n",ave);
printf("不及格人数:%d/n",count);
}
7.实现堆栈功能。如whiel##lr#e(s#*s)相当于字符串while(*s),outchar@putchar(*s=#++)相当于字符串putchar(*s++)。
#include<stdio.h>
#define M 100
struct Mystack
{
char element[M];
int top;
};
void push(struct Mystack *s,char x)//将x值压入栈顶。
{
s->element[s->top]=x;
s->top++;
}
int IsEmpty(struct Mystack *s)//判断栈是否为空。
{
if(s->top==0)
return 1;
else
return 0;
}
void pop(struct Mystack *s)//出栈操作,将栈顶元素删除。
{
s->top--;
}
void Displaystack(struct Mystack *s)//显示栈底到栈顶所有元素。
{
int i;
for(i=0;i<s->top;i++)
printf("%c",s->element[i]);
}
void Clearstack(struct Mystack *s)// 将栈清空。
{
s->top=0;
}
void main()
{
struct Mystack st;
char ch;
int i;
for(i=0;i<100;i++)
{
st.element[i]='/0';
}
st.top=0;
printf("请输入一行字符:/n");
ch=getchar();
while(ch!=EOF && ch!='/n')
{
switch(ch)
{
case '#':
if(!IsEmpty(&st))
pop(&st);
break;
case '@':
Clearstack(&st);
break;
default:
push(&st,ch);
}
ch=getchar();
}
printf("有效字符为:/n");
Displaystack(&st);
printf("/n");
}