Chinaunix首页 | 论坛 | 博客
  • 博客访问: 466910
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 13:25:43


点击(此处)折叠或打开

  1. 1.#结构体变量的定义,赋值
  2. #include <stdio.h>

  3. struct student
  4. {
  5.     int id;
  6.     char name[32];
  7.     int score;
  8. };

  9. //全局变量
  10. struct student stu1={1,"Jack",100};
  11. struct student stu2={2,"Tom",89};

  12. int main(int argc, const char *argv[])
  13. {
  14.     //局部变量
  15.     struct student stu3;
  16.     memset(&stu3,0,sizeof(stu3));
  17.     //将stu3中的内容置0
  18.     //共12个字节,4字节对齐。数据线32根,4字节对齐存取,填充字节时存取方便
  19.     //对齐方式由编译器决定,#prakgma pack(1) 设置1字节对齐
  20.     //#prakgma pack() 恢复默认设置
  21.     return 0;
  22. }
  23. 2.#structl类型可以整体赋值
  24. #include <stdio.h>

  25. struct student
  26. {
  27.     int id;
  28.     char name[32];
  29.     int score;
  30. };

  31. int main(int argc, const char *argv[])
  32. {
  33.     struct student stu1={1,"Jack",100};
  34.     struct student stu2;
  35.     stu2=stu1; //都为struct类型变量,struct类型可以整体赋值,实现整体复制。
  36.     return 0;
  37. }
  38. 3.#函数的返回值为struct类型
  39. #include <stdio.h>

  40. struct student
  41. {
  42.     int id;
  43.     char name[32];
  44.     int score;
  45. }stu1={1,"Jack",100};

  46. struct student print_stu(struct student stu)
  47. {
  48.     printf("id=%d name=%s score=%d\n",stu.id+1,stu.name,stu.score);
  49.     return stu;
  50. }

  51. int main(int argc, const char *argv[])
  52. {
  53.     struct student a;
  54.     a=print_stu(stu1);
  55.     return 0;
  56. }
  57. 4.#用struct类型实现求一个元素个数为10的数组的最大值,平均值和数组中偶数的个数
  58. #include <stdio.h>

  59. struct stru
  60. {
  61.     int max;
  62.     float avg;
  63.     int evenn;
  64. };

  65. struct stru func(int *a,int n)
  66. {
  67.     int i;
  68.     struct stru arr={a[0],0.0,0};
  69.     float sum=0.0;
  70.     for (i = 0; i < n; i++)
  71.     {
  72.         if(a[i]>arr.max)
  73.             arr.max=a[i];
  74.         sum+=a[i];
  75.         if(a[i]%2==0)
  76.             arr.evenn++;
  77.     }
  78.     arr.avg=sum/n;
  79.     return arr;
  80. }

  81. int main(int argc, const char *argv[])
  82. {
  83.     int i,a[10]={12,34,23,12,35,7,8,2,68,90};
  84.     struct stru aa;
  85.     for (i = 0; i < 10; i++)
  86.     {
  87.         printf("%4d",a[i]);
  88.     }
  89.     printf("\n");
  90.     aa=func(a,10);
  91.     printf("max=%d avg=%.3f evenn=%d\n",aa.max,aa.avg,aa.evenn);
  92.     return 0;
  93. }
  94. 5.#用typedef定义一个struct类型
  95.   #typedef 类型 别名 用来给某种类型起别名,不会替换,真正定义了一个类型,多个指针也不会出错。
  96.   # #define 替换名 类型 宏定义 在后面程序中遇到(替换名)替换为(类型),多个指针时会出错。
  97. #include <stdio.h>

  98. typedef struct person
  99. {
  100.     char name[32];
  101.     char ***[20];
  102.     int age;
  103. }s_t;


  104. int main(int argc, const char *argv[])
  105. {
  106.     int i,n;
  107.     printf("num=");
  108.     scanf("%d",&n);
  109.     s_t a[n];
  110.     printf("enter:name *** age\n");
  111.     for (i = 0; i < n; i++)
  112.     {
  113.         scanf("%s %s %d",&a[i].name,&a[i].***,&a[i].age);
  114.     }
  115.     for (i = 0; i < n; i++)
  116.     {
  117.         printf("%d:%s\t%s\t%d\n",i+1,a[i].name,a[i].***,a[i].age);
  118.     }
  119.     return 0;
  120. }
  121. 6.#定义一个如下结构的struct类型,实现输入5个学生的信息,并找出平均成绩最高的打印.
  122. #include <stdio.h>
  123. typedef struct student
  124. {
  125.     int id;
  126.     char name[32];
  127.     int chinese;
  128.     int math;
  129.     int english;
  130.     float average;
  131. } stu_t;
  132. int main(int argc, const char *argv[])
  133. {
  134.     int i,n;
  135.     printf("num=");
  136.     scanf("%d",&n);
  137.     stu_t a[n];
  138.     printf("enter:id name chinese math english\n");
  139.     for (i = 0; i < n; i++)
  140.     {
  141.         scanf("%d %s %d %d %d",&a[i].id,&a[i].name,&a[i].chinese,&a[i].math,&a[i].english);
  142.     }
  143.     stu_t max=a[0];
  144.     for (i = 0; i < n; i++)
  145.     {
  146.         a[i].average=(float)(a[i].chinese+a[i].math+a[i].english)/3;
  147.         if(a[i].average>max.average)
  148.         {
  149.             max=a[i];
  150.         }

  151.     }
  152.     printf("higest average:\n");
  153.     printf("id\tname\tchinese\tmath\tenglish\taverage\n%d\t%s\t%d\t%d\t%d\t%f\n",max.id,max.name,max.chinese,max.math,max.english,max.average);
  154.     return 0;
  155. }

阅读(554) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~