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

标签 c

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

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 - 2 Y : number of white space - 2 it 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/

相关文章:

c - zsh :abort message after all the program has been run?

c - 在 C 中求和并查找数组的非零项

c - 如何检查 libpcap 中的数据包可用性

c - 如何在机器级别或内存级别的编译器中实现变量的范围

c - Print Caesar Shift in C 正在打印输入的 HELLO 文本

c - C : client not recieving 中的简单 TCP 客户端服务器模型

c - 数组指针与普通指针的区别

c - 如何使用 Msys2 和 MinGW 在 Windows 上构建 OpenLDAP 库?

C编程自增和自减运算符问题

C:如何在二维字符数组的每个空间中放置一个字符串?