如何在C程序中嵌套函数?

时间:2014-10-20 00:12:16

标签: c

有没有人知道如何更改我的代码以包含一个平均每行表格的函数(而不是在主函数中完成)?每次我尝试使用process_student函数运行我的代码时,它根本不打印表。我已经把我的工作和非功能性包括在内了#34;首先是代码,然后是我的失败代码,其中包含process_student函数。提前谢谢!

另外,我正在使用重定向的输入.txt文件!

#include <stdio.h>

int main(void)
{

int month = 0;
int date = 0;
int year = 0;
int x = 0;
int id = 0;
int min_user = 0;
int ms = 0;
int realTest = 0;
float min = 10000000;
float t1 = 0;
float t2 = 0; 
float t3 = 0;
float t4 = 0;
float avg_trial = 0;

scanf("%d %d %d", &month, &date, &year);
printf("\nStudy Date: %02d/%02d/%02d\n", month, date, year);

printf("Student Trial-1 Trial-2 Trial-3 Trial-4 Average\n");

while (x < 10) {
    realTest = 0;       
    scanf("%d %f %f %f %f", &id, &t1, &t2, &t3, &t4);
    if (t1>0)
        realTest += 1;
    if (t2>0)
        realTest += 1;
    if (t3>0)
        realTest += 1;
    if (t4>0)
        realTest += 1;
    avg_trial = (t1+t2+t3+t4)/realTest;
    if (realTest == 0)
        avg_trial = 0.000;          
    if (id<1000) { 
        printf("  %d    %.3f   %.3f   %.3f   %.3f   %.3f\n", id, t1, t2, t3, t4, avg_trial);
        x++;
        }
    else {
        printf(" %d    %.3f   %.3f   %.3f   %.3f   %.3f\n", id, t1, t2, t3, t4, avg_trial);
        x++;
    }
    if (avg_trial < min && avg_trial > 0) {
        min = avg_trial;
        min_user = id;
    }

}

 ms = min * 1000;
 printf("\nStudent %d had the lowest average of %.3f seconds (%d milliseconds)\n", min_user, min, ms);

return 0;
}

这是我的失败代码,包含process_student函数:      #include

 int main(void)
 {

 int month = 0;
 int date = 0;
 int year = 0;
 int x = 0;
 int id = 0;
 int min_user = 0;
 int ms = 0;
 float min = 10000000;
 float t1 = 0;
 float t2 = 0; 
 float t3 = 0;
 float t4 = 0;
 float avg_trial = 0;

scanf("%d %d %d", &month, &date, &year);
printf("\nStudy Date: %02d/%02d/%02d\n", month, date, year);

printf("Student Trial-1 Trial-2 Trial-3 Trial-4 Average\n");

ms = process_student(min) * 1000;
printf("\nStudent %d had the lowest average of %.3f seconds (%d milliseconds)\n", min_user, min, ms);

 int process_student(void) {
float t1, t2, t3, t4, avg_trial, min;   
int min_user;
int realTest = 0;       

while (x < 10) {    
scanf("%d %f %f %f %f", &id, &t1, &t2, &t3, &t4);
if (t1>0)
    realTest += 1;
if (t2>0)
    realTest += 1;
if (t3>0)
    realTest += 1;
if (t4>0)
    realTest += 1;
avg_trial = (t1+t2+t3+t4)/realTest;
if (realTest == 0)
    avg_trial = 0.000;
if (id<1000) { 
    printf("  %d    %.3f   %.3f   %.3f   %.3f   %.3f\n", id, t1, t2, t3, t4, avg_trial);
    x++;
}
else {
    printf(" %d    %.3f   %.3f   %.3f   %.3f   %.3f\n", id, t1, t2, t3, t4, avg_trial);
    x++;
}
if (avg_trial < min && avg_trial > 0) {
    min = avg_trial;
    min_user = id;
}

}

return min;
return min_user;
}

return 0;
}

2 个答案:

答案 0 :(得分:1)

只需在main之前定义嵌套函数,或在main之前或在标题中添加声明并在其他地方定义它。您似乎没有理由需要嵌套函数。

嵌套函数不是合法的ISO C. GNU C编译器允许它们作为扩展。但同样,似乎没有理由不宣布process_student()作为标准函数。

答案 1 :(得分:1)

你需要像这样移动你的功能。

将此添加到程序的最顶行

int process_student(float min);

您需要将“void”更改为“float min”,以便将变量传递给函数。

然后基本上将整个功能从程序中复制并粘贴到它下面。您需要在函数中重新声明变量。

简要探讨函数的工作原理,

int functionName(int sentvariable1 int sentvariable2);

// functionName将从主程序“sentvariable1”和“intsentvariable2”中读取两个整数值,并返回一个整数值。

它将从主程序中调用,如此

x = functionName(a,b)

x,a和b都在主函数的某处声明。