C語言練習-結構體初試

1.學生成績處理

#include <stdio.h>
#define N 100  /*N表示最多允许的学生人数*/
struct Student
{
   int num; //学号
   int c; //C语言成绩
   int math; //数学成绩
   float aver;  //两科均分
};
int main( )
{
   int n,i;
   //定义结构体数组存放多名同学的成绩
   struct Student Stu[N];
   printf("Enter the number of students: ");
   scanf("%d", &n);
   printf("enter the num, C score and math score: \n");
   //输入数据
   for(i=0;i<n;i++)
   {
       scanf("%d %d %d",&Stu[i].num,&Stu[i].c,&Stu[i].math);
       Stu[i].aver = (3*Stu[i].c+4*Stu[i].math)/7.0;     // use 2.0 instead of 2
   }
   //输出成绩单
   printf("Number\tC\tMath\tAverage\n");
   for(i=0;i<n;i++)
   {
       printf("%d\t%d\t%d\t%.2f\n",Stu[i].num,Stu[i].c,Stu[i].math,Stu[i].aver);
   }
   int ave_c=0,ave_m=0;

   for(i=0;i<n;i++)
   {
       ave_c += Stu[i].c;
       ave_m += Stu[i].math;
   }
   ave_c /= n;
   ave_m /= n;
   printf("average c and math is %d   %d\n",ave_c,ave_m);
   printf("the ones got the prize are: \n");
   for(i=0;i<n;i++)
   {
       if(Stu[i].c>60 && Stu[i].math>60 &&Stu[i].aver>80 )
            printf("%d\t",Stu[i].num);
   }
   return 0;
}

2.點結構體

#include<stdio.h>
#include<math.h>
int main()
{
    float d;
    struct Point
    {
        float x;
        float y;
    };
    struct Point p1,p2;
    printf("Enter the coordinate for P1: ");
    scanf("%f %f",&p1.x,&p1.y);
    printf("Enter the coordinate for P2: ");
    scanf("%f %f",&p2.x,&p2.y);
    d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    printf("the distance between two points is : %.3f\n",d);
    printf("p1 關於x軸的對稱點是(%.2f,%.2f)\n",p1.x,-1*p1.y);
    printf("p2 關於o的對稱點是(%.2f,%.2f)\n",-1*p2.x,-1*p2.y);
    return 0;
}

3.結構體求個稅

#include<stdio.h>
#define BASE 3500
int main()
{
    struct TaxNode
    {
        int payTax;
        double rate;
        int deducted;
    } taxNode[10]=
    {
        {0,0.03,0},
        {1500,0.10,105},
        {4500,0.20,555},
        {9000,0.25,1005},
        {35000,0.30,2755},
        {55000,0.35,5505},
        {80000,0.45,13505}
    };
    double salary,tax;
    int grade,i;
    printf("Enter your salary : ");
    scanf("%lf",&salary);
    for(i=0;i<6;i++)
    {
        if((salary-BASE)>=taxNode[i].payTax && (salary-BASE)<taxNode[i+1].payTax)
            grade=i;
    }
    tax=(salary-BASE)*taxNode[grade].rate - taxNode[grade].deducted;
    printf("your salary is %.2lf, the tax is %.2lf\n",salary,tax);
    return 0;
}

4.緊急救援-結構體定義距離和人數,求時間

#include<stdio.h>
#include<math.h>
#define N 9
struct Roof
{
    float x;
    float y;
    int p;
};

int main()
{
    struct Roof roof[N];
    int i;
    float D,time,persons;
    for(i=0;i<N;i++)
    {
        scanf("%f %f %d",&roof[i].x,&roof[i].y,&roof[i].p);
        D += sqrt(roof[i].x*roof[i].x + roof[i].y*roof[i].y);
        persons += roof[i].p;
    }
    time = D/50 + persons*1.5;
    printf("total time is %.3f\n",time);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38486169/article/details/86664226