當前位置:首頁 » 編程語言 » 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);

你圓虧那樣判斷完全是多餘