【发布时间】:2020-07-17 14:27:45
【问题描述】:
所以我对一般的编程相当陌生,我正在做一个项目,我必须创建几个函数。 第一个函数一切都很好,但我不知道如何将值从第一个函数传递到第二个函数 我知道我需要使用一些指针来解决问题,但我只是想知道是否有更简单的方法,直到我掌握了使用指针的窍门。 提前致谢!
这是我乱七八糟的代码
#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
{
int Month_num[12];
double sales[12];
bool active;
}Branch;
double function_one(void);
double function_two(void);
double 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);
printf("\n");
Branch Branches[number_of_branches];
RecPointer r;
r = (RecPointer)malloc(sizeof(Rec));
//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("%lf",&Branches[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("%.2lf\t",(Branches[b].sales[x]));
}
printf("\n");
}
return 0;
Branch * Branchplace = (Branch *) malloc(sizeof(Branch));
}
double function_two(void)
{
Branch Branches[number_of_branches++];
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("%lf",&Branches[number_of_branches].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("%.2lf\t",(Branches[b].sales[x]));
//printf("\n%lf\n",Branches[b].sales[x]);
//fprintf(file, "%d,%lf",x,Branches[b].sales[x]);
}
printf("\n");
}
return 0;
}
double 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);
return 0;
}
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("%.lf\n",Branches[n].sales[x]);
}
}
【问题讨论】:
-
你要传递给哪个函数,在你的代码中,给我函数名
-
typedef struct { ... } Branch;是 C 中的一个成语,我建议只用 C++ 编写struct Branch { ... };。我还推荐const int START_SIZE = 2;,而不是#define START_SIZE 2。粗略地说,第一个说 START_SIZE 应该引用常量整数值 2,而宏说 我写的任何地方 START_SIZE 都应该替换为标记 2。 一个是语义,而编译器会以直观的方式帮助你,另一个是词法的,虽然在某些情况下是合适的,但可能不是你想要的。
标签: c arrays for-loop while-loop switch-statement