我正在尝试使用c中的struct学习新的算法,然后尝试进行一些测试。但是代码太长了,我想使其更简单。
struct employee{
char ID[6];
char name[20];
char address[50];
long salary;
long T;
}casher1,casher2;
int main(int argc, char const *argv[]) {
int ch;
printf("Choose : ");
scanf("%d",&ch );
if (ch == 1) {
printf("Input Name : " );
scanf("%s",casher1.name );
printf("Input ID : " );
scanf("%s",casher1.ID );
printf("Input Salary : " );
scanf("%d",&casher1.salary);
printf("Input T : " );
scanf("%d",&casher1.T );
printf("\n");
casher1.salary = casher1.salary + casher1.T;
printf("ID : %s\n",casher1.ID );
printf("Name : %s\n",casher1.name );
printf("Salary : %d\n",casher1.salary );
}
else if(ch == 2) {
printf("Input Name : " );
scanf("%s",casher2.name );
printf("Input ID : " );
scanf("%s",casher2.ID );
printf("Input Salary : " );
scanf("%d",&casher2.salary);
printf("Input T : " );
scanf("%d",&casher2.T );
printf("\n");
casher2.salary = casher2.salary + casher2.T;
printf("ID : %s\n",casher2.ID );
printf("Name : %s\n",casher2.name );
printf("Salary : %d\n",casher2.salary );
}
return 0;
}
我希望每个收银员都能得到这样的输出 编号:12345 名称:测试 薪酬:$ 2000
答案 0 :(得分:1)
如果您还没有学习功能,则应该阅读一些有关它们的内容。我不知道您到底想做什么,因为您不再使用chasher的代码,所以现在的代码不知道您是否需要存储它们(但是我假设您确实要存储)他们)。
在这种情况下,无需将收银机放入数组中,就可以创建一个将收银机作为参数的函数,
#include <stdio.h>
struct employee{
char ID[6];
char name[20];
char address[50];
long salary;
long T;
}casher1,casher2;
int casherFunction(struct employee *casher);
int main(int argc, char const *argv[]) {
int ch;
printf("Choose : ");
scanf("%d",&ch );
if( 1 == ch ){
casherFunction(&casher1);
}else if( 2 == ch ){
casherFunction(&casher2);
}
return 0;
}
int casherFunction(struct employee *casher){
printf("Input Name : " );
scanf("%s",casher->name );
printf("Input ID : " );
scanf("%s",casher->ID );
printf("Input Salary : " );
scanf("%ld",&casher->salary);
printf("Input T : " );
scanf("%ld",&casher->T );
printf("\n");
casher->salary = casher->salary + casher->T;
printf("ID : %s\n",casher->ID );
printf("Name : %s\n",casher->name );
printf("Salary : %ld\n",casher->salary );
return 0;
}
所以在这里您首先声明该函数:
int casherFunction(struct employee *casher);
,然后在main之后定义它:
int casherFunction(struct employee *casher){
printf("Input Name : " );
scanf("%s",casher->name );
printf("Input ID : " );
scanf("%s",casher->ID );
printf("Input Salary : " );
scanf("%ld",&casher->salary);
printf("Input T : " );
scanf("%ld",&casher->T );
printf("\n");
casher->salary = casher->salary + casher->T;
printf("ID : %s\n",casher->ID );
printf("Name : %s\n",casher->name );
printf("Salary : %ld\n",casher->salary );
return 0;
}
没有错误时,返回值0是标准返回值。
还请注意,该函数需要一个指针casherFunction(struct employee *casher)
,这就是为什么在调用该函数时我们将写成casherFunction(&casher1)
和&
的原因。
此外,结构指针中的元素位于casher->ID
下,而不是casher.ID
下。
同样,如果您希望将更改记录在收银机中,这将很有用。然后,例如,您可以创建一个查看收银员的函数(在这种情况下,您无需传递指针,只需复制该结构即可)。
代码将是:
#include <stdio.h>
struct employee{
char ID[6];
char name[20];
char address[50];
long salary;
long T;
}casher1,casher2;
int casherFunction(struct employee *casher);
int casherShow(struct employee casher);
int main(int argc, char const *argv[]) {
int ch;
printf("Choose : ");
scanf("%d",&ch );
if( 1 == ch ){
casherFunction(&casher1);
}else if( 2 == ch ){
casherFunction(&casher2);
}
printf("%ld\n\n",casher1.salary);
casherShow(casher1);
return 0;
}
int casherFunction(struct employee *casher){
printf("Input Name : " );
scanf("%s",casher->name );
printf("Input ID : " );
scanf("%s",casher->ID );
printf("Input Salary : " );
scanf("%ld",&casher->salary);
printf("Input T : " );
scanf("%ld",&casher->T );
printf("\n");
casher->salary = casher->salary + casher->T;
printf("ID : %s\n",casher->ID );
printf("Name : %s\n",casher->name );
printf("Salary : %ld\n",casher->salary );
return 0;
}
int casherShow(struct employee casher){
printf("Name: ");
printf("%s\n",casher.name );
printf("Input ID : " );
printf("%s\n",casher.ID );
printf("Salary : " );
printf("%ld\n",casher.salary);
return 0;
}