主题:这两个程序输出都不对,帮忙看看啊
我是初学者,如果错误很幼稚,见笑啊~~
第一个:
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
float data[5];
int count;
char filename[20];
puts("Enter 5 numbers.");
for(count=0;count<5;count++)
scanf("%f\n",&data[count]);
fflush(stdin);
puts("Enter a filename.");
gets(filename);
if((fp=fopen(filename,"w"))==NULL)
{
fprintf(stderr,"Error opening file %s",filename);
exit(1);
}
for(count=0;count<5;count++)
{
fprintf(fp,"\ndata[%d]=%f",count,data[count]);
fprintf(stdout,"\ndata[%d]=%f",count,data[count]);
}
fclose(fp);
printf("\n");
}
第二个:
#include<stdio.h>
void func(int x);
void one();
void two();
void other();
void main()
{
int nbr;
for(;;)
{
puts("\nEnter a number between 1 and 10,0 to exit.");
scanf("%d\n",&nbr);
if(nbr==0)
break;
func(nbr);
}
}
void func(int x)
{
void (* ptr)();
if(x==1)
ptr=one;
else if(x==2)
ptr=two;
else ptr=other;
ptr();
}
void one()
{
puts("You enter 1.");
}
void two()
{
puts("You enter 2.");
}
void other()
{
puts("You enter something other than 1 and 2.");
}
第一个:
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
float data[5];
int count;
char filename[20];
puts("Enter 5 numbers.");
for(count=0;count<5;count++)
scanf("%f\n",&data[count]);
fflush(stdin);
puts("Enter a filename.");
gets(filename);
if((fp=fopen(filename,"w"))==NULL)
{
fprintf(stderr,"Error opening file %s",filename);
exit(1);
}
for(count=0;count<5;count++)
{
fprintf(fp,"\ndata[%d]=%f",count,data[count]);
fprintf(stdout,"\ndata[%d]=%f",count,data[count]);
}
fclose(fp);
printf("\n");
}
第二个:
#include<stdio.h>
void func(int x);
void one();
void two();
void other();
void main()
{
int nbr;
for(;;)
{
puts("\nEnter a number between 1 and 10,0 to exit.");
scanf("%d\n",&nbr);
if(nbr==0)
break;
func(nbr);
}
}
void func(int x)
{
void (* ptr)();
if(x==1)
ptr=one;
else if(x==2)
ptr=two;
else ptr=other;
ptr();
}
void one()
{
puts("You enter 1.");
}
void two()
{
puts("You enter 2.");
}
void other()
{
puts("You enter something other than 1 and 2.");
}