Выходные изменения в C - PullRequest
0 голосов
/ 06 апреля 2020

Так что я работал над этим проектом, основной целью которого является регистрация продаж для компании ... Я также довольно новичок во всех видах программирования. Моя проблема здесь в том, что есть проблема с выходом, где некоторые продажи должны быть записаны, но вместо этого выводить ноль. Я провел много исследований об указателях и распределении памяти, и мне кажется, что я понимаю большинство концепций, но многое еще предстоит изучить ... так вот пример вывода

1. Enter sales data.
2. Add a record for a new branch.
3. Delete record of an existing branch.
4. Calculate total sales.
5. Calculate percentage share of each branch.
6. Determine the month of peak sales.
7. Display sales of a specific month.
8. Display sales of a specific branch.
0. Done
1
Ender number of branches: 1

Sales for branch 1: 
Sales for month 1: 1
Sales for month 2: 1
Sales for month 3: 1
Sales for month 4: 1
Sales for month 5: 1
Sales for month 6: 1
Sales for month 7: 11
Sales for month 8: 1
Sales for month 9: 1
Sales for month 10: 1
Sales for month 11: 1
Sales for month 12: 1


Branch\Month:   1       2       3       4       5       6       7       8       9       10      11      12
Branch 1:       1.0     1.0     1.0     1.0     1.0     1.0     11.0    1.0     1.0     1.0     1.0     1.0
1. Enter sales data.
2. Add a record for a new branch.
3. Delete record of an existing branch.
4. Calculate total sales.
5. Calculate percentage share of each branch.
6. Determine the month of peak sales.
7. Display sales of a specific month.
8. Display sales of a specific branch.
0. Done
2
Sales for the new branch number 2
Sales for month 1: 2
Sales for month 2: 3
Sales for month 3: 23
Sales for month 4: 2
Sales for month 5: 32
Sales for month 6: 2
Sales for month 7: 3
Sales for month 8: 3
Sales for month 9: 3
Sales for month 10: 3
Sales for month 11: 3
Sales for month 12: 3

Branch\Month:   1       2       3       4       5       6       7       8       9       10      11      12
Branch 1:       1.0     1.0     1.0     1.0     1.0     1.0     0.0     0.0     0.0     0.0     1.0     1.0
Branch 2:       2.0     3.0     23.0    2.0     32.0    2.0     3.0     3.0     3.0     3.0     3.0     3.0

как Вы видите, что месяцы 7,8,9,10 в ветви 1 были записаны ранее, но как только я добавил ветку 2, они снова стали нулями.

Вот исходный код, который я написал Отказ от ответственности "Я знаю, что это действительно грязно"

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#define START_SIZE 2

int number_of_branches=0;
typedef struct
           {
              float sales[12];
              bool active;
           }Branch;

        Branch *Branchplace;
double function_one(void);
double function_two(void);
void function_three(void);
void function_four(void);



int main()
{
    int User_Selection;
    do
    {
    printf("1. Enter sales data.\n2. Add a record for a new branch.\n3. Delete record of an existing branch.\n4. Calculate total sales.\n5. Calculate percentage share of each branch.\n6. Determine the month of peak sales.\n7. Display sales of a specific month.\n8. Display sales of a specific branch.\n0. Done\n");
    scanf("%i",&User_Selection);

    switch (User_Selection)
    {
    case 0:
    {
        printf("Thankyou for your time :)\n");
        break;
    }

    case 1:
    {
        function_one();
        break;
    }

    case 2:
    {
      function_two();
        break;
    }
    case 3:
    {
        function_three();
        break;
    }
    case 4:
    {
         function_four();
        break;
    }
    default:
    {
        printf("Please enter a valid input :)\n");
        break;
    }
    }
    }
    while(User_Selection!=0);
}


double function_one(void)
{
    //FILE *file = fopen("Sales.csv","a");
    printf("Ender number of branches: ");
    scanf("%d",&number_of_branches);
    //THIS IS FOR ALLOCATING MEMORY
    Branchplace = (Branch*) malloc(number_of_branches * sizeof(Branch));
    //Memory should be allocated by now...
    printf("\n");
        Branch Branches[number_of_branches];

        //printf("%d",number_of_branches);
        for(int b=0; b<number_of_branches; b++)
        {
            printf("Sales for branch %d: \n",(b+1));
            for(int x=0;x<12;x++)
            {
                printf("Sales for month %d: ",(x+1));
                scanf("%f",&(Branchplace+b)->sales[x]);
                //printf("\n%lf\n",Branches[b].sales[x]);
                //fprintf(file, "%d,%.2lf\n",x,Branches[b].sales[x]);
                //fprintf(file,"\n");
            }
            printf("\n\n");
        }
     //fclose(file);
     printf("Branch\\Month:\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\n");
     for(int b=0; b<number_of_branches; b++)
        {
            printf("Branch %d:\t",(b+1));
            for(int x=0;x<12;x++)
            {
                printf("%.1lf\t",(Branchplace+b)->sales[x]);

            }
            printf("\n");
        }
        return 0;


}

double function_two(void)
{
    //Reallocates enough memory for new branch
    number_of_branches++;
    Branchplace = realloc(Branchplace,(number_of_branches));
    //Enough memory should be allocated by now...
    printf("Sales for the new branch number %d\n",(number_of_branches));
    for(int x=0;x<12;x++)
{
                printf("Sales for month %d: ",(x+1));
                scanf("%f",&(Branchplace+number_of_branches-1)->sales[x]);
                //printf("\n%d\n",x);
}
     printf("\n");
     printf("Branch\\Month:\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\n");
     for(int b=0; b<number_of_branches; b++)
        {
            printf("Branch %d:\t",(b+1));
            for(int x=0;x<12;x++)
            {
                printf("%.1lf\t",(Branchplace+b)->sales[x]);

            }
            printf("\n");
        }
        return 1;

}

void function_three(void)
{
    Branch Branches[number_of_branches];
    int deleted_branch;
    printf("Which branch do you want to delete?\n");
    scanf("%d",&deleted_branch);
    Branches[deleted_branch].active=false;
    printf("Deleted branch %d\n",deleted_branch);
}

void function_four(void)
{
Branch Branches[number_of_branches];

    for(int n=0;n<number_of_branches;n++)
    {
        for(int x=0;x<12;x++)
        printf("%.f\n",Branches[n].sales[x]);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...