編程小白C語言 結構體數組4

題目要求

輸入兩個學生的學號、姓名和成績,輸出成績較高學生的學號、姓名和成績。

思路分析 

這題比較簡單,如果不會做,多看幾遍分析,自己多敲幾遍代碼

  • 輸入兩個學生的信息,有學號,姓名,成績
  • 比較成績(這裏就有3中情況,A的成績比B好,B的成績比A好,A的成績等於B的成績)
  • 輸出成績比較之後的結果

代碼演示 

#include<stdio.h>
struct student
{
	int id;
	char name[20];
	double f;
} stu1,stu2;
int main()
{
	printf("請輸入學生1的信息:\n");
	scanf("%ld %s %lf",&stu1.id,stu1.name,&stu1.f);
	printf("請輸入學生2的信息:\n"); 
	scanf("%ld %s %lf",&stu2.id,stu2.name,&stu2.f);
	if(stu1.f>stu2.f)
	{
		printf("比較優秀的學生是%s,學號是%ld,分數是%5.2f",stu1.name,stu1.id,stu1.f);
	}
	else if(stu1.f<stu2.f)
	{
		printf("比較優秀的學生是%s,學號是%ld,分數是%5.2f",stu2.name,stu2.id,stu2.f);
	}
	else
	{
		printf("這些學生都非常優秀!"); 
	}
	return 0;
}

測試結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章