文字格式问题

时间:2019-10-22 21:54:06

标签: c printf

我正在尝试完成我的程序。该程序将要求公司的n名员工,然后是namesalarysale of the month。输出将显示先前的信息以及每个信息中的comissionnet salarytotal

为更好地理解,这将是输出:

Name               Salary    Sale of the month           Comission    Net salary 
Elena Gomez       2000.00              1000.00              35.00       2035.00
Elle Johns        4000.00              1345.00              60.53       4060.53 
Stefan Cox        3200.00              4000.00             216.00       3416.00 
Total             9200.00              6345.00             311.53       9511.53 

Employee with upper comission: Stefan Cox 216.00
Employee with lower comission: Elena Gomez 35.0
Employee with upper net salary: Elle Johns 4060.53
Employee with lower net salary: Elena Gomez 2035.00

Employees with sales upper than the prom (US$2115)

Name               Salary    Sale of the month           Comission    Net salary
Stefan Cox        3200.00              4000.00             216.00       3416.00 

这是main代码:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define NUMBER_OF_STRING 6
#define MAX_STRING_SIZE 50

void print_array(const char arr[][MAX_STRING_SIZE]);
void upper_comission (float [], char [5][50],char [5][50], int);
void lower_comission (float[], char [5][50],char [5][50], int);
void upper_netsalary (float [], char [5][50],char [5][50], int);
void lower_netsalary (float[], char [5][50],char [5][50], int);
float prom_sales(float[], int);

int main ()
{
    int numemployees, cont=0;
    float salary[50], salnet[50], sale[50], comission[50], ind=0, totalsales=0,totalsalary=0,totalsalnet=0, totalcomission=0, prom;
    char header[0], nom[5][50], lastname[5][50];

char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] =
    { "Name",
      "salary",
      "Sale of the month",
      "Comission",
      "Net salary"
    };

    printf("Enter # of employees to consult: \n");
    scanf("%d",&numemployees);

     for(int i=0; i<numemployees; i++)
             {

                  printf("Enter employee's %d name : ",i+1);
                  scanf("%s", &nom[i]);

                  printf("Enter employee's %d last name : ",i+1);
                  scanf("%s", &lastname[i]);

                  printf("Enter employee's %d salary : ",i+1);
                  scanf("%f", &salary[i]);

                  printf("Enter sale of the month of the employee %d : ",i+1);
                  scanf("%f", &sale[i]);

                  system("cls");
            }

    print_array(arr); // function that shows the headers

    for(int i=0; i<=numemployees; i++)
    {

     if (sale[i] <= 1000)
        {
            comission[i]=sale[i] * 0.035;
            salnet[i]=salary[i]+comission[i];
        }

        if (sale [i] < 2000 || sale [i] > 1000)
        {
            comission[i]=sale[i] * 0.045;
            salnet[i]=salary[i]+comission[i];
        }

        if (sale [i] > 2000)
        {
            comission[i]=sale[i] * 0.054;
            salnet[i]=salary[i]+comission[i];
        }

            totalsales+=sale[i];
            totalsalary+=salary[i];
            totalcomission+=comission[i];
            totalsalnet+=salnet[i];

    printf("%25s %s %25.2f   %25.2f  %25.2f   %25.2f \n",nom[i],lastname[i], salary[i], sale[i], comission[i],salnet[i]);
    }


    printf("\t\tTotal %25.2f   %25.2f  %25.2f   %25.2f\n",totalsalary, totalsales, totalcomission, totalsalnet);


    upper_comission(comission, nom,lastname, numemployees);
    lower_comission(comission, nom,lastname, numemployees);
    upper_netsalary(salnet, nom,lastname, numemployees);
    lower_netsalary(salnet, nom,lastname, numemployees);
    prom=prom_sales(sale, numemployees);


    printf("\n");
    printf("Employees with sales upper than the prom (RD$%.2f) \n",prom);

    for (int i=0; i < numemployees; i++)
    {

    if (sale[i] > prom)
    {
        print_array(arr);
        printf("%25s %s %25.2f   %25.2f  %25.2f   %25.2f \n",nom[i],lastname[i], salary[i], sale[i], comission[i],salnet[i]);
    }

    }

    return 0;
}

功能:


void print_array(const char arr[NUMBER_OF_STRING][MAX_STRING_SIZE])
{
    for (int i = 0; i < NUMBER_OF_STRING; i++)
    {
        printf("%25s ", arr[i]);

        if (i == 5)
        {
            printf("%25s \n", arr[i]);
        }

    }
}
void upper_comission (float comission[],char nom[5][50],char lastname[5][50], int n)
{
    int i, nombre, apellido;
    float mayor;
    char name;

    for (i=0; i<n; i++)
    {
        mayor = comission[0];

        if (mayor < comission[i])
        {
            mayor = comission[i];
            nombre=i;
            apellido = i;
        }

    }
    printf("Employee with upper comission: %s %s %.2f \n",nom[nombre],lastname[apellido], mayor);
}

void lower_comission (float comission[],char nom[5][50],char lastname[5][50], int n)
{
    int i, nombre, apellido;
    float menor;

    for (i=0; i<n; i++)
    {
        menor = comission[0];

        if (menor > comission[i])
        {
            menor = comission[i];
            nombre=i;
            apellido=i;
        }

    }
    printf("Employee with lower comission: %s %s %.2f \n",nom[nombre],lastname[apellido], menor);
}

void upper_netsalary (float salnet[],char nom[5][50],char lastname[5][50], int n)
{

int i, nombre, apellido;
    float mayor;

    for (i=0; i<n; i++)
    {
        mayor = salnet[0];

        if (mayor < salnet[i])
        {
            mayor = salnet[i];
            nombre=i;
            apellido=i;
        }

    }
    printf("Employee with upper net salary: %s %s %.2f \n",nom[nombre],lastname[apellido], mayor);
}

void lower_netsalary (float salnet[], char nom[5][50],char lastname[5][50], int n)
{
int i, nombre, apellido;
    float menor;

    for (i=0; i<n; i++)
    {
        menor = salnet[0];

        if (menor > salnet[i])
        {
            menor = salnet[i];
            nombre=i;
            apellido=i;
        }

    }
    printf("Employee with lower net salary: %s %s %.2f \n",nom[nombre],lastname[apellido], menor);
}

float prom_sales(float sale[], int n)
{
    float prom=0;
    int i;

    for (i=0; i<n;i++)
    {
        prom += sale[i];
    }


return prom/n;
}

我的问题是这些:

  • 输出应与佣金/净薪金的相应名称显示相同的名称 [固定]
 Name                    salary                  Sale of the month                       Comission                       Net salary
marie gomez                                500.00                                  850.00                                   38.25                                  538.25
luca grus                                  856.00                                   80.00                                    3.60                                  859.60
Total                   1356.00                                                            930.00                                           41.85                                         1397.85
Employee with upper comission: luca grus 38.25
Employee with lower comission: luca grus 3.60
Employee with upper net salary: luca grus 859.60
Employee with lower net salary: luca grus 538.25

Employees with sales upper than the prom (RD$465.00)
Name                    salary                  Sale of the month                       Comission                       Net salary
marie gomez              500.00          850.00          38.25           538.25


  • 之前,该程序会打印下一个代码,例如the employee with upper comission等,但是现在不显示它。 [已修复]
  • 输出应该很好地对齐,我尝试使用printf("%25s");,但是我没有像开始时那样获得输出。 [已修复]

我不知道我做错了什么,请检查一下。预先感谢。

0 个答案:

没有答案