gpt4 book ai didi

c - 以格式化形式打印结果

转载 作者:行者123 更新时间:2023-11-30 21:13:10 25 4
gpt4 key购买 nike

我想格式化结果以便对其进行调整

output

这是我到目前为止所写的内容:

void affichage(int note[][50],char nom[][50],float moy[], float moy_e[], char module[][50],int nb_etudiant, int nb_module){
int i,j;
printf("\n\t\taffichage\n\n");
printf("\t");
strcpy(module[nb_module],"Moy G");
strcpy(nom[nb_etudiant],"Moy C");
for (i=0;i<nb_module;i++){
printf("%5s ",module[i]);
}
printf(" %5s",module[nb_module]);
printf("\n");
for(i=0;i<nb_etudiant;i++){
printf("%-5s ",nom[i]);
for (j=0;j<nb_module;j++){
printf("%-7d ", note[i][j]);
}
printf(" %-7.1f\n",moy_e[i]);
}
printf("\n\n");
printf("%5s ",nom[nb_etudiant]);

for (i=0;i<nb_module;i++){
printf("%-7.1f ",moy[i]);
}
}

最佳答案

建议,printf 返回格式化字符串的长度。
由于您想要显示学生的数据,如果我们假设您有一个包含 2 列的表
:


X chars Y chars-------|------ -------|---------| | |----------------------------------------nom | prénom | note---------------------------------------- Jane | Mari | 15 ---------------------------------------- Robert | Daniel | 16 -----------------------------------------X : number of white spaces - 2Y : number of white space - 2it can be any value, but make sure it will be greater than most of
the lengths of the property displayed in that column

For Each student, for the first cell (nom) you should first print his name, then you get the length of the printed name PN (printf's returned value), and print ( X - PN) whitespace, then you add the pipe character |.You do the same for the next property which is prenom, and at last you print \n for a new line.

#include <string.h>
#include <stdio.h>

typedef struct
{
char firstName[25];
char lastName[25];
float grade1;
float grade2;
}Student;

Student createStudent(char* firstName, char* lastName, float grade1, float grade2) {
Student student;

strcpy(student.firstName, firstName);
strcpy(student.lastName, lastName);
student.grade1 = grade1;
student.grade2 = grade2;

return student;
}

/* each column has a width equal to 20 characters 2 for borders and 18 of whitespaces or characters */
void printBorder() {
int i = 0;
int len = 0;

for(i =0 ; i<80; ++i) printf("-"); // top border
printf("\n"); // breakline

printf("|"); // left border of the left cell
len = printf("First Name"); // len is equal to 10

for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
printf("|"); // print the right border of the cell
len = printf("Last Name"); // len is equal to 9

for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
printf("|");
len = printf("Grade 1"); // len is equal to 7

for(i=0; i<18 - len; ++i) printf(" ");
printf("|");
printf("Grade 2\n");

for(i =0 ; i<80; ++i) printf("-"); // bottom border of header
printf("\n"); // breakline
}

void printBody(Student* students, int nbrOfStudents) {
int j = 0; // to iterate over students
int len = 0;
int i =0; // for drawing
for(j = 0; j< nbrOfStudents; ++j) {
printf("|"); // left border of the left cell
len = printf("%s", students[j].firstName);

for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
printf("|"); // print the right border of the cell
len = printf("%s", students[j].lastName);

for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
printf("|");
len = printf("%.2f", students[j].grade1);

for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
printf("|");
len = printf("%.2f\n", students[j].grade2);

}

for(i =0 ; i<80; ++i) printf("-"); // bottom border of header
printf("\n"); // breakline
}


int main() {
Student students[3] = {
createStudent("Jane", "Roberson", 99, 75),
createStudent("Amelia", "Coeur-de-lait", 85, 89),
createStudent("Anna", "Stone", 65, 30)
};

printBorder();
printBody(students, 3);

return 0;
}

关于c - 以格式化形式打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40185698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com