使用 printf() 函数,格式字符串:%-16s 表示输出一个字符串并占据宽度为 16 的字符位置,并左对齐。%-14.1f 表示输出一个浮点数并占据宽度为 14 的字符位置,并左对齐,并保留 1 位小数。
#include <stdio.h>
typedef struct {
char studentNumber[10];
char studentName[20];
float englishScore;
float mathScore;
float computerScore;
float CPPScore;
} Student;
int main() {
Student student[2];
student[0] = {"BJ1601", "YHZ", 88.0, 77.0, 66.0, 99.0};
student[1] = {"BJ1602", "ABC", 80.0, 90.0, 70.0, 50.0};
printf("%-16s%-16s%-14s%-14s%-14s%-14s%-14s\n", "studentNumber", "studentName", "EnglishScore", "MathScore", "ComputerScore", "CPPScore", "average");
int len = sizeof(student) / sizeof(student[0]);
for (int i = 0; i < len; ++i) {
printf("%-16s%-16s%-14.1f%-14.1f%-14.1f%-14.1f%-14.1f\n",
student[i].studentNumber, student[i].studentName, student[i].englishScore,
student[i].mathScore, student[i].computerScore, student[i].CPPScore,
(student[i].englishScore + student[i].mathScore + student[i].computerScore + student[i].CPPScore) / 4);
}
return 0;
}