gpt4 book ai didi

与结构的混淆以及如何在 C 中使用指针存储值

转载 作者:行者123 更新时间:2023-11-30 15:26:15 25 4
gpt4 key购买 nike

这个问题可能很烦人,但我正在尝试以最容易在心理上处理的方式编写代码,并且此时无需调用函数。我正在将教授的示例代码重写为我自己的示例代码,但对于将其存储到什么位置有疑问?他使用了一个指针,但我只是不知道如何/在哪里声明它。

如果这个问题措辞不正确,我深表歉意。不幸的是,我是那些在编程方面不“明白”的人之一。

我的教授:

#include <stdio.h>
#include <stdlib.h>

/* Program that computes the average salary of the employees with an age equal or greater
than x, x is passed as a parameter to the main function. Employee is a structure with two
fields: age and salary. */

struct employee {
int age;
float salary;
};

void readValues(struct employee *EMP, int n ) {
int i;

for(i=0; i<n; i++) {
printf("Employee %i\n", i+1);
printf("\tAge: ");
scanf("%i", &EMP[i].age);
printf("\tSalary: ");
scanf("%f", &EMP[i].salary);
}
}

float getAvg(struct employee *EMP, int minAge, int n ) {
int i, nE = 0;
float sum=0;

for(i=0; i<n; i++) {
if(EMP[i].age >= minAge) {
sum = sum + EMP[i].salary;
nE = nE + 1;
}
}
return sum/nE;
}

int main(int argc, char *argv[]) {
int x, n;
struct employee E[100];

if (argc<3) {
printf("Error: Some parameters missing.\n");
return -1;
}
x = atoi(argv[1]);
n = atoi(argv[2]);

readValues(E, n);
float avg = getAvg(E, x, n);
printf("Avg. Salary = %.2f\n", avg);

return 0;
}

我的尝试:

#include <stdio.h>
#include <stdlib.h>

#define minage 20

struct employee{
int age;
float salary;
};

int main()

{
struct employee emp = {0};

int i, n, numb=0, sum=0, avg;

printf("How many employees do you have today?\n");
scanf("%i", n);

for(i=0; i<n; i++)
{
printf("Employee %i", i+1);
printf("Age:\n");
scanf("%i", &emp[i].age);
printf("Salary:\n");
scanf("%f", &emp[i].salary);
}

if (emp[i].age >= minage)
{
for(i=0, i<n; i++){
sum = sum + emp[i].salary
numb = numb + 1

}
}
avg = sum / numb;

printf("The average salary is %i\n", avg);

}

最佳答案

引用下面的代码:内嵌评论

#include <stdio.h>
#include <stdlib.h>

#define minage 20

struct employee{
int age;
float salary;
};

int main()
{
/* Use array of struct to store the employee record. */
struct employee emp[100];
int i, n, numb=0, sum=0;
float avg = 0; /* If avg is of type int then you will loose the precision when finding the average */

printf("How many employees do you have today?\n");
scanf("%i",&n); /* Use `&` when using scanf to read value into integer*/

for (i = 0; i < n; i++) {
printf("Employee %i", i+1);
printf("Age:\n");
scanf("%i", &emp[i].age);
printf("Salary:\n");
scanf("%f", &emp[i].salary);
}

/* Your professor is calculating avg salary only if age >= minage.
* But in your code your are trying to check if the last employee age is
* >= minage only after which calculating the avg salary of all employee's*/
for (i = 0; i < n; i++) {
if (emp[i].age >= minage) {
sum = sum + emp[i].salary;
numb = numb + 1;
}
}

/* Make sure to check whether numb != 0, else divide by zero exception
* when there are no employee's with age greater than 20 (minage) */
if (numb)
avg = sum / numb;

printf("The average salary is %.2f\n", avg);
/* return 0; when return type of main() is int.
* If not required then change to void main()*/
return 0;
}

此外,您的教授正在根据用户输入初始化 minage。在您的代码中,它被硬编码为固定数字 20

尝试从用户处读取分钟数,就像获取员 worker 数一样。

关于与结构的混淆以及如何在 C 中使用指针存储值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27436171/

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