day 12 homework

 

 #include <stdio.h>
#include <stdlib.h>
typedef struct
 {
   char name[20];
   char sex;
   char job;
   union
   {
       float score;
       char position[20];
   }; 
  }p,*q;
q createe(int n)
{
  q xx=(p*)malloc(sizeof(p)*5);
  if(xx==NULL)
      return NULL;
  else
      return xx;
}
void input(q xx,int n)
{
  int i;
  for(i=0;i<n;i++)
  {
    puts("please input the name\n");
    scanf("%s",(xx+i)->name);
    puts("please input the sex(f-boy;m-girl)\n");
    scanf(" %c",&((xx+i)->sex));
loop:
    puts("please input the job(s-student;t-teacher\n");
    scanf(" %c",&((xx+i)->job));
    if('s'==(xx+i)->job || 'S'==(xx+i)->job)
       {
         puts("please input the score\n");
         scanf("%f",&((xx+i)->score));
       }
    else if('t'==(xx+i)->job || 'T'==(xx+i)->job)  
       {
         puts("please input the position\n");
         scanf("%s",(xx+i)->position);
        }
       else
        goto loop ;
}}
void output(q xx,int n)
{
  int i;
  for(i=0;i<n;i++)
  {
    if('t'==(xx+i)->job || 'T'==(xx+i)->job)
        printf("%s\t%c\t%c\t%s\t\n",(xx+i)->name,(xx+i)->sex,(xx+i)->job,(xx+i)->position);
    else
         printf("%s\t%c\t%c\t%.2f\t\n",(xx+i)->name,(xx+i)->sex,(xx+i)->job,(xx+i)->score);
  }}
float aver(q ss,int n)
{                 
  float i=0.00;
  int j=0;
  int x;
  for(x=0;x<n;x++)
  {
    if((ss+x)->job=='s' ||(ss+x)->job=='S')
             {
                 i=i+((ss+x)->score);
                 j++;
             }
  }
  return i/j;
}
int numbteach(q ss,int n)
{
  int i=0;
  int x;
  for(x=0;x<n;x++)
  {
    if((ss+x)->job=='T' ||(ss+x)->job=='t')
             {
                 i++;
             }
  }
 return i;
}
int main()
{
  float ave;
  int numbt;
  q arr=createe(5);
  input(arr,5);
  output(arr,5);
  ave=aver(arr,5);
  printf("the average is %.2f\n",ave);
  numbt=numbteach(arr,5);
  printf("teacher total %d\n",numbt);
  free(arr);
  arr=NULL;
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值