c语言增加函数 c语言怎么添加函数

C语言 补充函数/* 请在此文件中完成以下两个函数实现,并调试程序,实现程序功能*/
/* int readFile( struct stuinfo stu[] );*/
/* void input_high( struct stuinfo stu[], float high[], int n )*/
/**/
/* 其它函数内容请勿更改*/
#include stdio.h
#include stdlib.h
#include string.h
struct stuinfo{
int serial;//学生编号
char name[10]; //学生姓名
};
int readFile( struct stuinfo stu[] );
void input_high( struct stuinfo stu[], float high[], int n );
void write_data();
void main()
{
struct stuinfo stu[30];
float stuHigh[30];
int stuNum;
int topStu=0;
// write_data();
stuNum = readFile( stu );
input_high( stu, stuHigh, stuNum );
return ;
}
/* 从文件"StuInfo.dat"中读出各位学生记录,以及记录个数 */
/* 学生记录存放在数组 stu中,函数返回学生人数*/
int readFile( struct stuinfo stu[] )
{
FILE *fp;
struct stuinfo *p;
int i=0;
if( (fp = fopen("StuInfo.dat", "rb " )) == NULL)
{
printf("file canot read!\n");
exit(1);
}
p=(struct stuinfo *)malloc(sizeof(struct stuinfo));
while(fread( p, sizeof(struct stuinfo), 1, fp)!=0)
{
stu[i]=*p;
i;
p=(struct stuinfo *)malloc(sizeof(struct stuinfo));
}
fclose(fp);
return i;
}
/*输入每个学生c语言增加函数的身高信息*/
/*显示每位同学基本信息后c语言增加函数,输入该同学c语言增加函数的身高*/
/*计算并显示所有同学c语言增加函数的平均身高*/
void input_high( struct stuinfo stu[], float high[], int n )
{
int i;
float sum=0;
for(i=0;in;i)
{
printf("Student %d serial, name:%s\n",i 1,stu[i].name);
printf("Please input student's height:");
scanf("%f",high[i]);
sum =high[i];
}
printf("student's average height:%f",sum/n);
}
/* 从键盘上输入学生信息,保存到StuInfo.dat文件中*/
void write_data( )
{
FILE *fp;
struct stuinfo stu[30];
int i, stuNum=0;
printf("Please input student number(30): ");
scanf("%d",stuNum);
if( stuNum30 )
{
printf("data error");
exit(1);
}
for( i=0; istuNum; i)
{
printf("Please input Student %d serial, name:\n", i 1 );
scanf("%d", stu[i].serial);
scanf("%s",stu[i].name);
}
if( (fp = fopen("StuInfo.dat", "wb " )) == NULL)
{
printf("file canot write!\n");
exit(1);
}
fwrite( stu, sizeof(struct stuinfo), stuNum, fp);
fclose( fp );
return;
}
用C语言怎么写个 字符串插入函数程序的大体思路可以是这样:
str1是原字符串,str2是待插入的字符串 , position是待插入的位置,我们可以这样,用一个指针p_cur指向字符串1 str1中的待插入位置position,另一个指针p_end指向字符串1 str1的尾部,每次插入字符前,把str1中从当前位置开始一直到结束字符全部后移一个位置 , 空出当前位置,然后把要插入的字符放进这个位置,这样就完成了一个字符的插入,重复这个步骤,直到str2被完全插入 。
代码如下:
#include stdio.h
#include string.h
void insert_str(char str1[],char str2[],int position)
{
/*
insert_str()函数
功能:将字符串str2插入到str1的position位置处
参数:char str1,char str2 ,int position
返回值:无
*/
int i;
char *p_end,*p_cur,*p;/*p_end指向第一个字符串的尾部,p_cur指向被插入的位置*/
p_end=str1 strlen(str1)-1;
p_cur=str1 position-1;
for(i=0;str2[i]!='\0';i)
{
for(p=p_end;p=p_cur;p--)
{
*(p 1)=*p;/*从p_cur到p_end的全部元素后移一个位置,此时p_cur指向的位置就空出来了*/
}
*p_cur=str2[i];/*把字符串2中的字符插入空出来的位置*/
p_cur;/*p_cur下移一个位置*/
p_end;/*多了一个字符,因此p_end也下移一个位置*/
}
}
void main()
{
char s1[100],s2[20];
int position;
printf("输入字符串1:\n");
gets(s1);
printf("输入插入位置:");
do
{
scanf("%d",position);
while(getchar()!='\n');/*这一句可以把输入position的时候输入的回车去掉*/
}while(position0||positionstrlen(s1));
printf("输入字符串2:\n");
gets(s2);
insert_str(s1,s2,position);
printf("字符串被插入后变成:\n");
puts(s1);
}
C语言怎样才能在头文件中实现函数定义?C语言在头文件中实现函数定义c语言增加函数,可以按如下方法:
新建一个demo.h(也可以为其它文件名)c语言增加函数的头文件c语言增加函数,在头文件中添加函数定义,如:extern void demo();
新建一个c文件,在文件中将demo.h包含进来:#include“demo.h”,在此文件中将函数c语言增加函数的主体写上,如下:
void demo()
{
printf("Hello.");
}
经过上述步骤,即可完成在头文件中实现函数定义 。
c语言中怎么调用自定义函数可以的,前提是 , 在使用一个函数之前必须先对他进行声明:
//void B();声明B函数的存在 。
void A()
【c语言增加函数 c语言怎么添加函数】{
B();//非法,程序执行到此时并不知道B函数的存在 。
}
void B()
{
}
或者
#include stdio.h
#include stdlib.h
#include math.h
int fa(int n)
{
int a;
for(a=2;a=sqrt(n*1.0),n%a!=0;a);
if(asqrt(n*1.0))
return(1);
else
return(0);
}
void main( )
{
int n,q;
scanf("%d",n);
扩展资料
从函数定义的角度看,函数可分为库函数和用户定义函数两种 。
(1)库函数
由C系统提供,用户无须定义,也不必在程序中作类型说明 , 只需在程序前包含有该函数原型的头文件即可在程序中直接调用 。在前面各章的例题中反复用到printf 、 scanf 、 getchar 、putchar、gets、puts、strcat等函数均属此类 。
(2)用户定义函数
由用户按需要写的函数 。对于用户自定义函数, 不仅要在程序中定义函数本身,而且在主调函数模块中还必须对该被调函数进行类型说明,然后才能使用 。
c语言增加函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言怎么添加函数、c语言增加函数的信息别忘了在本站进行查找喔 。

    推荐阅读