/*#include <stdio.h>
int main()
{
struct Student //聲明結構體類型
{
long int num;
char name[20];
char sex;
char addr[20];
}a = {10101,"Li lin",'M',"123 Beijing Road"}; //定義結構體變量a並初始化
printf("No.:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr);
return 0;
}*/
/*#include <stdio.h>
int main()
{
struct Student
{
int num;
char name[20];
float score;
}student1,student2;
scanf("%d%s%f",&student1.num,student1.name,&student1.score);
scanf("%d%s%f",&student2.num,student2.name,&student2.score);
printf("The higer score is:\n");
if( student1.score > student2.score )
printf("%d %s %6.2f\n",student1.num,student1.name,student1.score);
else if( student1.score < student2.score )
printf("%d %s %6.2f\n",student2.num,student2.name,student2.score);
else
{
printf("%d %s %6.2f\n",student1.num,student1.name,student1.score);
printf("%d %s %6.2f\n",student2.num,student2.name,student2.score);
}
return 0;
}*/
//有3個候選人,每個選民只能投票選一人,要求編一個統計選票的程序,先後輸入被候選人的名字,最後輸出個人得票結果
/*#include <stdio.h>
#include <string.h>
struct Person //聲明結構體類型struct Person
{
char name[20]; //候選人姓名
int count; //候選人得票數
}leader[3] = {"Li",0,"Zhang",0,"Sun",0}; //定義結構體數組並初始化
int main()
{
int i,j;
char leader_name[20]; //定義字符數組
for( i =1; i <= 10; i++ )
{
scanf("%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);
return 0;
}*/
//有n個學生的信息(包括學號,姓名,成績),要求按照成績的高低順序輸出各學生的信息
/*#include <stdio.h>
struct Student
{
int num;
char name[20];
float score;
};
int main()
{
struct Student stu[5] = {{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"li",86},
{10108,"ling",73.5},{10110,"Sun",100}};
struct 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%8s%6.2f\n",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;
}*/
4.2結構體
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.