gpt4 book ai didi

c - 如何使用 Looping 或 Array 使代码更简单?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:35 26 4
gpt4 key购买 nike

我正在尝试使用 c 中的结构学习新算法,然后尝试进行一些测试。但是代码太长了,我想让它更简单。

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

最佳答案

如果您还没有学习函数,您应该阅读一些关于函数的内容。我不知道你到底想做什么,因为你不再使用 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;
}

return 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;
}

关于c - 如何使用 Looping 或 Array 使代码更简单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55803101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com