当前位置:首页 » 编程语言 » c语言自定义统计函数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言自定义统计函数

发布时间: 2022-12-29 21:51:23

‘壹’ c语言运用自定义函数判断一个数是不是完数,并调用该函数统计1到100内完数

#include <stdio.h>
const int M = 1000;
int main()
{
int k[11];
int i, a, n, s;
for (a = 2; a <= M; a++)
{
n = 0;
s = a;
for (i = 1; i < a; i++)
if (a % i == 0)
{
n++;
s = s - i;
k[n] = i;
}
if (s == 0)
{
printf("%d Its factors are ", a);
for (i = 1; i < n; i++)
printf("%d,", k[i]);
printf("%d\n", k[n]);
}
}
return 0;
}

‘贰’ C语言关于自定义函数

自定义函数的方法:VC中,定义函数分为两步:
I.声明:在main函数开头定义变量的位置,声明函数:定义上述函数声明是:
int add(int a,int b);
注意分号,声明前部不能有执行语句;
II.把下面的函数定义部分放在main函数外部
int add(int a,int b)
{
return a+b;
}
推荐放在main的后花括号后面,注意此处的int add(int a,int b)后面并没有分号。

VC里面,返回类型是int的函数可以省略声明部分
求采纳为满意回答。

‘叁’ C语言:自定义函数,能够统计字符数组中小写字母的个数,并在主函数调用测试。

#include<stdio.h>

intcount(char*p){
intcnt=0;
while(*p){
if(*p>='a'&&*p<='z')
cnt++;
p++;
}
returncnt;
}

intmain(){
charbuf[100]="sd12df";
printf("%d ",count(buf));
return0;
}

‘肆’ C语言自定义函数

#include<stdio.h>
intfun(intn){returnn*n;}
intmain(){intn;
scanf("%d",&n);
printf("%d",fun(n));
}
#include<stdio.h>
intfun(){intn;scanf("%d",&n);returnn*n;}
intmain(){printf("%d",fun());}

推荐使用前面那个,一般由主程序进行输入和输出,函数负责处理数据,除非是输入函数和输出函数,即使这样也不会即输入又计算n*n,导致函数功能过于复杂、调用含义不清

‘伍’ c语言 自定义函数怎么用啊

自定义函数是由用户按需要写的函数。这与库函数相对应的,库函数是由c系统提供的函数,已经定义好了如printf

scanf

getchar
、putchar、gets、puts、strcat等函数均属此类。
说白了,自定义函数是是自己写的,想实现什么功能就写什么,然后在用的时候和库函数(系统定义的)的一样调用就是了,相当于自己增加了一个自己的库函数。

‘陆’ C语言/C++ 自定义函数count

#include <stdio.h>

int count(char* str);

int main(void)
{
char s1[10000] = { '\0' }, s2[10000] = { '\0' };

printf("输入字符串 s1:");
scanf("%s", s1);
printf("输入字符串 s2:");
scanf("%s", s2);

printf("s1中小写字母个数:%d\ns2中小写字母个数:%d", count(s1), count(s2));

return 0;
}

//
int count(char* str)
{
int count = 0;

while (*str)
{
if ((*str >= 'a') && (*str <= 'z'))
count++;
str++;
}
return count;
}

‘柒’ c语言 自定义函数 一维数组

#include "stdio.h"
#define N 30
void votes(int a[])
{
int i;
int n1=0,n2=0,n3=0;
for(i=0;i<N;i++)
{
switch(a[i])
{
case 1:
n1++;
break;
case 2:
n2++;
break;
case 3:
n3++;
}
}
printf("Votes of candidate 1 is %d.\n",n1);
printf("Votes of candidate 2 is %d.\n",n2);
printf("Votes of candidate 3 is %d.\n",n3);
}
int winner(int a[])
{
int i;
int n1=0,n2=0,n3=0;
for(i=0;i<N;i++)
{
switch(a[i])
{
case 1:
n1++;
break;
case 2:
n2++;
break;
case 3:
n3++;
}
}
if(n1>n2 && n1>n3)
return 1;
if(n2>n1 && n2>n3)
return 2;
if(n3>n1 && n3>n2)
return 3;
}
void main()
{
int array[N];
int i;
printf("Please input 30 votes:");
for(i=0;i<N;i++)
scanf("%d",&a[i]);
votes(array);
printf("The winner is candidate %d.\n", winner(array));
}

‘捌’ C语言编程:自定义一个函数来统计三位十进制数中有两位数字相同的完全平方数(如144、676)的个数

代码文本:

#include "stdio.h"

void f(void){

int n,i,t;

for(n=0,i=10;(t=i*i)<1000;i++)

if(t%10==t/10%10 || t%10==t/100 || t/100==t/10%10)

n++;

printf("A total of %d such number. ",n);

}

int main(int argc,char *argv[]){

f();

return 0;

}

这样写时效较高。