1.结构体(学生信息的统计)
#include<stdio.h>
struct Student
{
int num;
char name[20];
float score;
};
int main()
{
struct student stu[5] = { {10011,"zhang",78},{10012,"wang",98},{10013,"li",50} ,{10014,"ling",75.3},{10015,"sun",90} };
student temp;
const int n = 5;
int i, j, k;
printf("the order is:\n");
for (i = 0; i < n - 1; i++)
k = i;
for (j = i + 1; j < n; j++)
if (stu[j].score > stu[k].score)
k = j;
{
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
for (i = 0; i < n; i++)
printf("%6d%8d%6.2f\n", stu[i].num, stu[i].name, stu[i].score);
printf("\n");
return 0;
}
2.选举人的票数统计
#include<stdio.h>
#include<string.h>
struct person
{
char name[20];
int count;
};
struct person leader[3] = { "li",0,"zhang",0,"sun",0 };
int main()
{
int i, j;
char leader_name[20];
for (i = 1; i <= 10; i++)
{
gets_s(leader_name);
for (j = 0; j < 3; j++)
if (strcmp(leader_name, leader[j].name) == 0)
leader[j].count++;
}
printf("\nresult:\n");
for (i = 0; i < 3; i++)
printf("%5s:%d\n", leader[i].name, leader[i].count);
getchar();
getchar();
return 0;
}