美文网首页C语言
2021-04-01(结构体 宏)

2021-04-01(结构体 宏)

作者: 张轻舟 | 来源:发表于2021-04-01 18:09 被阅读0次

复合类型结构

1.结构体 2.共用体
结构体:

struct Car
{
        char chepai[20];
         char color[20];
          int num;
};
int main()
{
  struct Car A={"lu-123456","black",5};
printf("%s%s%d\n",A.chepai,A.color,A.num);
scanf("%s%s%d",A.chepai,A.color,&A.num);
//A.color="blue"//不能这么写,字符串不能直接给值
//strcop(A.color,"blue")
//A.num=7;
printf("%s%s%d\n",A.chepai,A.color,A.num);
a[i].chepai,a[i].color
return 0;
}

现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数,读取N条记录输入,再按要求输出。 N<100


#include<stdio.h>
struct stu
{
    char num[20];
    char name[20];
    int a;
    int b;
    int c;

};

int main()
{
    int i,n;
    struct stu A[100];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%s%s%d%d%d",A[i].num,A[i].name,&(A[i].a),&(A[i].b),&(A[i].c));   
    }
        for(i=0;i<n;i++)
    {
        printf("%s,%s,%d,%d,%d\n",A[i].num,A[i].name,A[i].a,A[i].b,A[i].c); 
    }
        return 0;
}

#include<stdio.h>
struct stu
{
    char num[20];
    char name[20];
    int a;
    int b;
    int c;

};
int input(struct stu A[],int n)
{       
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%s%s%d%d%d",A[i].num,A[i].name,&(A[i].a),&(A[i].b),&(A[i].c));   
    }
    return 0;
}
int print(struct stu A[],int n)
{       
    int i;
    for(i=0;i<n;i++)
    {
        printf("%s,%s,%d,%d,%d\n",A[i].num,A[i].name,A[i].a,A[i].b,A[i].c); 
    }
    return 0;
}

int main()
{
    int i,n;
    struct stu A[100];
    scanf("%d",&n);
    input(A,n);
    print(A,n);

    
        return 0;
}
image.png
//共用体
union students
{
    int name;
    int number'
    char sex;
}
int main()
{
    union students s;
    s.number=101;
    s.name=90;
    s.sex='a';
}
共用体共用内存最大的那块

定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。


#include<stdio.h>
struct stu
{
    int year;
    int month;
    int day;
};
int main()
{   
    struct stu a;
    int i;
    int sum=0;
    scanf("%d%d%d",&(a.year),&(a.month),&(a.day));
    int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};

    for(i=0;i<a.month-1;i++)
    {
        sum+=month[i];  
    }
    sum+=a.day;
    if(a.year%4==0 && a.year%100!=0 || a.year%400==0)
    {
    if(a.month>2)
    {
        sum++;
    }

    }
    printf("%d",sum);
    return 0;


}

宏命令

define N 123

用N 来替换 123

undef 结束

define Max(x,y) x>y?x:y

include<math.h>

sqrt 平方根函数
a=sqrt(25);
a=5;
fabs(-5)=5 绝对值函数
pow(10,6) 10的六次方


输入两个整数,求他们相除的余数。用带参的宏来实现,编程序。


#include<stdio.h>
#define N(a,b) t=a%b;
int main(){
    int a,b,t;
    scanf("%d%d",&a,&b);
    t=N(a,b);
    printf("%d\n",t);
    return 0;
}

三角形面积=SQRT(S(S-a)(S-b)*(S-c)) 其中S=(a+b+c)/2,a、b、c为三角形的三边。 定义两个带参的宏,一个用来求area, 另一个宏用来求S。 写程序,在程序中用带实参的宏名来求面积area。


#include<stdio.h>
#include<math.h>
#define s(a,b,c) (a+b+c)/2
#define area(S,a,b,c) sqrt(S*(S-a)*(S-b)*(S-c))
int main()
{
    float a,b,c,S,area,n,m;
    scanf("%f%f%f",&a,&b,&c);
    n=s(a,b,c);
    m=area(n,a,b,c);
    printf("%.3f\n",m);
   
    return 0;
    }

给年份year,定义一个宏,以判别该年份是否闰年。提示:宏名可以定义为LEAP_YEAR,形参为y,既定义宏的形式为 #define LEAP_YEAR(y) (读者设计的字符串)


#include<stdio.h>
#include<math.h>
#define LEAP_YEAR(y)  y%100 !=0 && y%4==0 || y%400==0
int main()
{   int y;
    scanf("%d",&y);
    if(LEAP_YEAR(y))
    {
        printf("L\n");
    }   
    else
        printf("N\n");
    return 0;
}

分别用函数和带参的宏,从三个数中找出最大的数。


include<stdio.h>

define MAX(x,y,z) (x>y?x:y)>z?(x>y?x:y):z

double max(double x,double y,double z)
{
return (x>y?x:y)>z?(x>y?x:y):z;
}

int main()
{
double x,y,z,max1,max2;
scanf("%lf%lf%lf",&x,&y,&z);
max1 = MAX(x,y,z);
max2 = max(x,y,z);
printf("%.3lf\n%.3lf\n",max1,max2);
return 0;
}

相关文章

  • 2021-04-01(结构体 宏)

    复合类型结构 1.结构体 2.共用体结构体: 现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。 编写一...

  • C结构体和链表

    一,结构体变量定义及初始化 二,无名结构体 备注:无名结构体很少使用 三,宏定义结构体 四,结构体嵌套 五,结构体...

  • 1220学习总结

    复杂数据类型 1.结构体 2.结构体变量的初始化 3.无名结构体 4.宏定义结构体 5.结构体的嵌套 6.结构体数...

  • 慕课网-Linux C语言结构体-学习笔记

    Linux C语言结构体 编译指令:预处理,宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 逻辑运算符...

  • 结构体中的宏定义

    下面代码是ACL库中的一个示例 作用 把宏定义放在结构体中,主要目的是方便阅读,提升代码的可读性。从语法层面上来看...

  • iOS开发经验(15)-内存分配、变量、常量和修饰符

    目录 内存分配 变量:全局与局部 const,static,extern 宏 结构体,枚举(typeof与type...

  • c语言基本学习_基本记录

    基本数据类型 可变参数 宏定义 结构体 联合体[和结构体类似,成员共占一块空间,数据大小根据最大的数据类型确定] ...

  • 2021-04-01

    2021-04-01

  • linux内核数据结构——链表

    linux内核数据结构——链表 源码分析 重要宏定义 结构体 链表操作 初始化 插入 删除 遍历 移植 仿写的单向...

  • 预处理,const,sizeof

    1.宏定义 用宏定义去求一个结构体struct里某个变量相对struct的偏移量。要求偏移量首先struct a;...

网友评论

    本文标题:2021-04-01(结构体 宏)

    本文链接:https://www.haomeiwen.com/subject/yugzhltx.html