12.结构体.枚举.条件编译案列代码

结构体.枚举案列代码1

#import <Foundation/Foundation.h>
struct birthday{
    int _year;
    int _month;
    int _date;
};
struct student{
    int _id;
    char _name[100];
    float _score;
//    int _age;
//    struct birthday bir;
};
//1:输入两个学生的学号、姓名、和成绩,输出成绩较高的学生的学号、姓名和成绩。
//2:输入三个学生的学号、姓名和成绩,按成绩高低排序,并输出排序后的结果
void sort ();
int main(int argc, const char * argv[])
{

    @autoreleasepool {
//        struct student stu1,stu2;
//        printf("请输入第一个学生的信息:\n");
//        scanf("%d%s%f",&stu1._id,stu1._name,&stu1._score);
//        puts("请输入学号:");
//        scanf("%d",&stu1._id);
//        getchar();
//        puts("请输入姓名:");
//        gets(stu1._name);
//        puts("请输入成绩:");
//        scanf("%f",&stu1._score);
//        printf("请输入第二个学生的信息:\n");
//        puts("请输入学号:");
//        scanf("%d",&stu2._id);
//        puts("请输入姓名:");
//        getchar();
//        gets(stu2._name);
//        puts("请输入成绩:");
//        scanf("%f",&stu2._score);
//        if (stu1._score>stu2._score) {
//            printf("%d\t%s\t%.2f\n",stu1._id,stu1._name,stu1._score);
//        }else{
//            printf("%d\t%s\t%.2f\n",stu2._id,stu2._name,stu2._score);
//        }
//        struct student stu1={101,"zhangsan",18},stu2;
//        struct student (* pStu1)= &stu1;
//        stu2._age=17;
//        stu2._id = 102;
//        strcpy(stu2._name,"lisi");
//        printf("%d\n%s\n%d\n",pStu1->_id,pStu1->_name,pStu1->_age);
        
        
        sort();
    }
    return 0;
}
void sort (){
    struct student stu1,stu2,stu3,t;
    printf("请输入第一个学生的信息:");
    scanf("%d%s%f",&stu1._id,stu1._name,&stu1._score);
    printf("请输入第二个学生的信息:");
    scanf("%d%s%f",&stu2._id,stu2._name,&stu2._score);
    printf("请输入第三个学生的信息:");
    scanf("%d%s%f",&stu3._id,stu3._name,&stu3._score);
    if (stu1._score<stu2._score) {
        t= stu1;
        stu1 = stu2;
        stu2 = t;
    }
    if (stu1._score<stu3._score) {
        t = stu1;
        stu1 = stu3;
        stu3 = t;
    }
    if (stu2._score<stu3._score) {
        t = stu2;
        stu2 = stu3;
        stu3 = t;
    }
    printf("第一名学生如下:\n%d\t%s\t%.2f\n",stu1._id,stu1._name,stu1._score);
    printf("第二名学生如下:\n%d\t%s\t%.2f\n",stu2._id,stu2._name,stu2._score);
    printf("第三名学生如下:\n%d\t%s\t%.2f\n",stu3._id,stu3._name,stu3._score);
}

结构体.枚举案列代码2

#import <Foundation/Foundation.h>
//输入输出5个学生的信息
struct student{
    int _id;
    char _name[100];
    int _age;
};
void information();
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        information();
        
    }
    return 0;
}
void information(){
    struct student arr[5];
    for (int i=0; i<5; i++) {
        printf("请输入第%d个学生的信息:\n",i+1);
        scanf("%d%s%d",&arr[i]._id,arr[i]._name,&arr[i]._age);
    }
    puts("学生信息如下:\n");
    for (int i=0; i<5; i++) {
        printf("第%d个学生的信息如下:\n%d\t%s\t%d\n",i+1,arr[i]._id,arr[i]._name,arr[i]._age);
    }
}

枚举案列代码

#import <Foundation/Foundation.h>
#define SUM(a,b) ((a)*(b))
typedef enum  {
  spring=1,
  summer,
  autumn,
  winter
}Season;
int main(int argc, const char * argv[])
{

  @autoreleasepool {
      
//        Season season = 0;
//        for (season=spring; season<=winter; season++){
//            printf("season = %d\n", season);
//        }
//        int a=3,b=4;
//        PRINT(a);
      printf("%d\n",SUM(3+1, 4+3));
      
  }
  return 0;
}

条件编译

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
#if 1
        printf("hello\n");
#else
        printf("how are you?\n");
#endif
        
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容