当前位置:首页 » 编程语言 » c语言如何用一个函数解决四个功能
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言如何用一个函数解决四个功能

发布时间: 2023-03-24 14:09:19

❶ 用c语言制作一个简单的菜单程序,一个主函数四个子函数

给你一个简单的菜单程序吧。其中的子函数,填充成楼主所需即可。
#include
/*子函数1*/
fun1() {
printf ("子函数1\n");
}
/*子函数2*/
fun2() {
printf ("子函数2\n");
}
/*子函数3*/
fun3() {
printf ("子函数3\n");
}
/*子函数4*/
fun4() {
printf ("子函数4\n");
}
int main(void) {
int key; /*命令编号*/
do {
system("cls"); /*清屏*/
/*打印菜单*/
printf ("======================================================\n");
printf (" #\t功能详情\n");
printf ("------------------------------------------------------\n");
printf (" 1\t功能1\n");
printf (" 2\t功能2\n");
printf (" 3\t功能3\n");
printf (" 4\t功能4\n");
printf (" 5\t退出\n");
printf ("======================================================\n");
printf ("\n");
printf("请输入命令编号以开启操作:");
/*输入命令编号*/
scanf("%d",&key);
printf ("\n");
/*switch函数实现输入功能序号执行相应函数*/
switch (key) {
case 1: fun1(); break; /*子函数1*/

case 2: fun2(); break; /*子函数2*/

case 3: fun3(); break; /*子函数3*/

case 4: fun4(); break; /*子函数4*/

case 5: printf("程序结束!按任意键退出...\n\n"); break;

default:printf("输入错误,请重新输入!\n\n"); break;
}
/*屏幕暂留*/
if (key!=5) {
printf ("\n");
printf("按Enter键继续...\n");
printf ("\n");
fflush(stdin);
getch ();
}
} while (key!=5);
/*屏幕暂留*/
fflush(stdin);
getch ();
return 0;
}运行结果

❷ c语言自定义函数实现字符串的拼接、拷贝、比较大小、求字符串长度等四个功能

#include<stdio.h>

void s_trcat(char *s1,char *s2);//连接两个字符串

void s_trcpy(char *s1,char *s2);//字符串s2复制给字符串s1

int s_trcmp(char *s1,char *s2);//两个字符串比较大小

int s_trlen(char *s);//计算一个字符串的长度

int main()

{

char s1[11]="12345", s2[11]="ABCDE";

char x1[11]="12345",x2[11]="SSSKK";

char str1[11]="ABCD",str2[11]="ACB";

char s[11]="ABCDEF";

int n,len;

s_trcat(s1,s2);//拼接s1,s2

printf("拼接s1,s2,输出s1 ");

puts(s1);

s_trcpy(x1,x2);//将x2的内容复制给x1

printf("将x2的内容复制给x1,输出x1 ");

puts(x1);

n=s_trcmp(str1,str2);//比较str1与str2大小,返回一个整数,代表比较的结果

if(n==0)

printf("str1与str2相同 ");

if(n==1)

printf("str1大于str2 ");

if(n==-1)

printf("str1小于str2 ");

len=s_trlen(s);//计算字符串s的长度,并返回一个整数(字符串长度),

printf("字符串s的长度:%d ",len);

}

void s_trcat(char *s1,char *s2)//连接两个字符串

{

char *p=s1;

char *q=s2;

while(*p)

p++;

while(*q)

{

*p=*q;

p++;

q++;

}

*p='';//给新字符串尾部加个结束符

}

void s_trcpy(char *s1,char *s2)//字符串s2复制给字符串s1

{

char *p=s1;

char *q=s2;

while(*p++=*q++);

*p='';//给新字符串尾部加个结束符

}

int s_trcmp(char *s1,char *s2)//两个字符串比较大小

{

char *p=s1;

char *q=s2;

int len1=0,len2=0;

while(*p++)

len1++;

while(*q++)

len2++;

p=s1,q=s2;

while(*p!=''&&*q!='')

{

if(*p>*q)

return 1;

if(*p<*q)

return -1;

p++;

q++;

}

if(len1==len2)

return 0;//返回0,两个字符串相同

if(len1>len2)

return 1;//返回1,前大于后

if(len1<len2)

return -1;//返回-1,前小于后

}

int s_trlen(char *s)//计算一个字符串的长度

{

char *p=s;

int len=0;

while(*p++)

len++;

return len;//返回字符串长度

}

❸ C语言方面的。学生成绩统计,要求:以下4个功能分别用四个函数编程,主函数调用实现

帮你完成了第1、2个问题 剩下的你自己照样子写吧
#include"stdio.h"
struct student
{
char id[10];
int mark;
};
main()
{
struct student stu[30];
int i,j,sum;
i=-1;
sum=0;
do{
i++;
printf("\n输入一个学生的学号:");
scanf("%s",stu[i].id);
printf("\n输入学宏迹弊生的成绩:");
scanf("%d",&stu[i].mark);
i++;

}while(stu[--i].mark!=-1);
printf("不蔽族及格学生学号:");

for(j=0;j<i;j++)
if(stu[j].mark<60)
{printf("%s ",stu[j].id);sum++;}
printf("\n不及格州脊人数:%d",sum);
getchar();

}

❹ C语言,设计一个函数,实现加,减,乘,除运算。

# include<stdio.h>

double jia(double x, double d)
{
double s;
s = x+d;
return s;
}

double jian(double x, double d)
{
double s;
s = x-d;
return s;
}

double cheng(double x, double d)
{
double s;
s = x*d;
return s;
}

double chu(double x, double d)
{
double s;
s = x/d;
return s;
}

int main(void)
{
double i, j, k;
char t;
k = 0;
printf("请输入要计算什么运算“+”“-”“*”“/”\n");
scanf("%c", &t);
if(t == '+')
{
printf("请输入要相加的两个数\n");
scanf("%lf %lf", &i, &j);
k = jia(i, j);
printf("这两个数相加为%6.2lf\n", k);
}

else if(t == '-')
{

printf("请输入要相减的两个数\n");
scanf("%lf %lf", &i, &j);
k = jian(i, j);
printf("这两个数相减为%6.2lf\n", k);
}

else if(t == '*')
{

printf("请输入要相*的两个数\n");
scanf("%lf %lf", &i, &j);
k = cheng(i, j);
printf("这两个数相*为%6.2lf\n", k);
}
else if(t == '/')
{

printf("请输入要相/的两个数\n");
scanf("%lf %lf", &i, &j);
k = chu(i, j);
printf("这两个数相/为%6.2lf\n", k);
}

else
{
printf("对不起目前只支持“+”“-”“*”“/”\n");
}
return 0;
}

❺ C语言 如何在一个函数中先后实行不同的功能

int doit(int x, int y,int (*fun)(int ,int ))
{
......................
a=(*fun)(x,y);
..................调用一个函数;
return(a);
}
int max(int x,int y)
{
............
}
int min...................
...........连续四个函数;
void mian()
{
.................
将tingcase=max ,min, .,............其中一个;就可以旦陪实现你要的功能了;
result=doit(x,y,tingcase);
}

主干部模空蠢分就是这样了,函数的实现部分。应该知道了吧,望采纳哦。亏戚嘻嘻

❻ C语言:编写一个函数,在数函数中调用它时,每次实现不同的功能

#include<stdio.h>
intadd(inta,intb)
{
returna+b;
}
intsub(inta,intb)
{
returna-b;
}
intmul(inta,intb)
{
迅丛扰returna*b;
}
intprocess(int(*fun)(int,int),inta,intb)
{
returnfun(a,b);
}
intmain()
{
inta,b,r;
printf("输入a、b两数: ");
scanf("%d%d",&a,&b);
printf("两数之和:亩旦%d ",process(add,a,b));
printf("两数之差:%d ",process(sub,a,b));
printf("两数之积:%d "郑兆,process(mul,a,b));
}

❼ <用C语言软件做出>利用函数.设计一个综合性程序. 进行四则运算. 用户可以选择...详细见补充说明

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
int show()
{
int operation;
cout<<"**********请选择操作**********"<<endl;
cout<<"\t1. 加法"<<endl;
cout<<"\t2. 减法"<<endl;
cout<<"\t3. 乘法"<<散知盯endl;
cout<<"\t4. 除法"<<endl;
cout<<"冲和\t5. 显示成绩"<<endl;
cout<<"\t6. 退出"<<endl;
cin>>operation;
return operation;
}

int main()
{
int operation;
int i,j,result,r;
int right=0,total=0;
int time;
operation=show();
srand((int)time(0));
while(operation!=6)
{
time=1;
switch(operation)
{
case 1:
cout<<"您选择的是加法"<<endl;
i = rand()%100;
j = rand()%100;
cout<<"随机数1:"<<i<<endl;
cout<<"随机数2:"<<j<<endl;
cout<<"您的计算结果是:";
cin>>r;
result=i+j;
while(time<3)
{
if(r==result)
{
right++;
total++;
cout<<"恭喜猛陪您答对了"<<endl;
operation=show();
break;
}
else
{
cout<<"您答错了,请您再次输入答案:";
cin>>r;
time++;
}
}
if(time==3)
{
cout<<"对不起,您没有答对!正确答案是"<<result<<endl;
total++;
operation=show();
break;
}
case 2:
cout<<"您选择的是减法"<<endl;
i = rand()%100;
j = rand()%100;
cout<<"随机数1:"<<i<<endl;
cout<<"随机数2:"<<j<<endl;
cout<<"您的计算结果是:";
cin>>r;
result=i-j;
while(time<3)
{
if(r==result)
{
right++;
total++;
cout<<"恭喜您答对了"<<endl;
operation=show();
break;
}
else
{
cout<<"您答错了,请您再次输入答案:";
cin>>r;
time++;
}
}
if(time==3)
{
cout<<"对不起,您没有答对!正确答案是"<<result<<endl;
total++;
operation=show();
break;
}
case 3:
cout<<"您选择的是乘法"<<endl;
i = rand()%100;
j = rand()%100;
cout<<"随机数1:"<<i<<endl;
cout<<"随机数2:"<<j<<endl;
cout<<"您的计算结果是:";
cin>>r;
result=i*j;
while(time<3)
{
if(r==result)
{
right++;
total++;
cout<<"恭喜您答对了"<<endl;
operation=show();
break;
}
else
{
cout<<"您答错了,请您再次输入答案:";
cin>>r;
time++;
}
}
if(time==3)
{
cout<<"对不起,您没有答对!正确答案是"<<result<<endl;
total++;
operation=show();
break;
}
case 4:
cout<<"您选择的是除法"<<endl;
i = rand()%100;
j = rand()%100;
cout<<"随机数1:"<<i<<endl;
cout<<"随机数2:"<<j<<endl;
cout<<"您的计算结果是:";
cin>>r;
result=i/j;
while(time<3)
{
if(r==result)
{
right++;
total++;
cout<<"恭喜您答对了"<<endl;
operation=show();
break;
}
else
{
cout<<"您答错了,请您再次输入答案:";
cin>>r;
time++;
}
}
if(time==3)
{
cout<<"对不起,您没有答对!正确答案是"<<result<<endl;
total++;
operation=show();
break;
}
case 5:
cout<<"您一共答了"<<total<<"道题"<<endl;
cout<<"您一共答对了"<<right<<"道题"<<endl;
operation=show();
break;
case 6:
system("pause");
break;
default:
cout<<"您的选择有误,请您输入正确的操作号!"<<endl;
system("pause");
break;
}
}
system("pause");
return 0;
}

❽ 用C语言编写几个子函数(至少四个子函数),用主函数来调用实现其功能,并对函数进行注释

一、//调用函数事例

#include<stdio.h>

#include<string.h>

二、//计算加法函数

int add(int a, int b)

{

return a+b;

}

三、//计算减法函数

int subtract(int a, int b)

{

return a-b;

}

四、//计算乘法函数

int multiply(int a, int b)

{

return a*b;

}

(8)c语言如何用一个函数解决四个功能扩展阅读:

函数作为另一个函数调用的实际参数出现。这种情况是把该函数的返回值作为实参进行传送,因此要求该函数必须是有返回值的。例如: printf("%d",max(x,y)); 即是把max调用的返回值又作为printf函数的实参来使用的。在函数调用中还应该注意的一个问题是求值顺序的问题。

❾ 用C语言编一个能完成基本的四则运算和求余的函数

运算规则:数字1 回车 运算符 回车 数字2 回车
运算范围乱孙早:+ - * / %
输入非运算符退出
#include<stdio.h>
int main()
{
int a,b,i,sum=1;
char c;

while(1)
{
printf("请输入:\n");
scanf("%d",&a);
fflush(stdin);
scanf("%c",&c);
scanf("%d",&b);
if(c=='+')
{
printf("%d%c%d=%5.2f\n",a,c,b,(float)a+b);
}
else if(c=='-')
{
printf("%d%c%d=%5.2f\n",a,c,b,(float)a-b);
}
else if(c=='*')
{
printf("%d%c%d=%5.2f\n",a,c,b,(float)a*b);
}
else if(c=='/凯枝')
{
if(b==0)
printf("被除数不能为0!\n");
else
printf("%d%c%d=%5.2f\n",a,c,b,(float)a/b);
}
else if(c=='%')
{
printf("%d%c%d=%d\n",a,c,b,a%b);
}
else
{
printf("非哗雀此计算器功能!\n");
break;
}
}
}

❿ c语言 编写一个函数每次调用时实现多个功能(为什么我的错了)

process函数橘旅神里似乎只应镇枣该有一句:

returnfun(a,b);

你圆亏那样判断完全是多余