码迷,mamicode.com
首页 > 编程语言 > 详细

c语言 结构体动态创建

时间:2014-12-02 13:29:05      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   for   div   log   

 1 #include<stdio.h>
 2 #include<malloc.h>
 3 struct Student
 4 {
 5     int num;
 6     int total;
 7     char name[20];
 8     float score[3];
 9 };
10 
11 int main()
12 {
13     int N,i,j;
14     printf("Please input N:");
15     scanf("%d",&N);
16     struct Student *stu = NULL;
17     stu = (struct Student*)malloc(sizeof(struct Student)*N);
18     for(i = 0; i < N; i++)
19     {
20         stu[i].total = 0;
21         printf("Please input the No%d student‘s number:",i+1);
22         scanf("%d",&stu[i].num);
23         printf("Please input the No%d student‘s name:",i+1);
24         scanf("%s",stu[i].name);
25         for(j = 0; j < 3; j++)
26         {
27             stu[i].score[j] = 0.0;
28             printf("Please input the No%d student‘ score of %d:",i+1,j+1);
29             scanf("%f",&stu[i].score[j]);
30                stu[i].total = stu[i].total + (int)stu[i].score[j];
31         }
32     }
33     for(i = 0; i < N; i++)
34     {
35         if(stu[i].total >= 240)
36         {
37             printf("%d %s\n",stu[i].num,stu[i].name);
38         }
39     }
40     for(i = 0; i < N; i++)
41     {
42         for(j = 0; j < 3; j++)
43         {
44             if(stu[i].score[j] < 60)
45             {
46                printf("%d %s %.2f\n",stu[i].num,stu[i].name,stu[i].score[j]);
47             }
48         }
49     //    printf(" %d \n",stu[i].total);
50     }
51     free(stu);
52     return 0;
53 }

 

c语言 结构体动态创建

标签:style   blog   io   ar   color   sp   for   div   log   

原文地址:http://www.cnblogs.com/yll-sww/p/4137125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!