首页 >电脑教程 >电脑知识 >C语言设计一个函数实现加减乘除运算

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

王林
王林转载
2024-01-07 18:50:141441浏览

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

# include

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.2lfn", k);

}

else if(t == '-')

{

printf("请输入要相减的两个数n");

scanf("%lf %lf", &i, &j);

k = jian(i, j);

printf("这两个数相减为%6.2lfn", k);

}

else if(t == '*')

{

printf("请输入要相*的两个数n");

scanf("%lf %lf", &i, &j);

k = cheng(i, j);

printf("这两个数相*为%6.2lfn", k);

}

else if(t == '/')

{

printf("请输入要相/的两个数n");

scanf("%lf %lf", &i, &j);

k = chu(i, j);

printf("这两个数相/为%6.2lfn", k);

}

else

{

printf("对不起目前只支持“+”“-”“*”“/”n");

}

return 0;

}

如何编写一个C语言程序让计算机自动出四则运算计算

#include

#include

#include

#include

int main()

{

int type;

int left, right;

float result;

srand(unsigned(time(NULL)));

while(1)

{

type = rand() % 4;

left = rand() % 10;

right = rand() % 10;

switch(type)

{

case 0:

printf("%d + %d = ?n", left, right);

scanf("%f", &result);

if(left + right == result)

printf("right!n");

else

printf("wrong!The result is %dn", left + right);

break;

case 1:

printf("%d - %d = ?n", left, right);

scanf("%f", &result);

if(left - right == result)

printf("right!n");

else

printf("wrong!The result is %dn", left - right);

break;

case 2:

printf("%d * %d = ?n", left, right);

scanf("%f", &result);

if(left * right == result)

printf("right!n");

运用java的基本库类实现一个计算四则运算的程序

package Ex1; //Ex1为包名

import java.util.*;

public class SzYs { //SzYs为类名

public static void main(String[] args){

double num1;

double num2;

String fuHao;

Scanner input = new Scanner(System.in);

System.out.print("输入第一个数的值:");

num1 = input.nextDouble();

System.out.print("输入第二个数的值:");

num2 = input.nextDouble();

System.out.print("输入运算符号(+ - * \ )");

fuHao = input.next();

if(fuHao.equals("+")){

System.out.print(num1 + "与" + num2 + "的和是" + (num1+num2));

}else if(fuHao.equals("-")){

System.out.print(num1 + "与" + num2 + "的差是" + (num1-num2));

}else if(fuHao.equals("8")){

System.out.print(num1 + "与" + num2 + "的积是" + (num1*num2));

}else if(fuHao.equals("\")){

if(num2==0){

System.out.print("除数不能为0");

}else{

System.out.print(num1 + "与" + num2 + "的商是" + (num1/num2));

}

}else{

System.out.print("不能辨认该符号");

}

}

}

以上是C语言设计一个函数实现加减乘除运算的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:docexcel.net。如有侵权,请联系admin@php.cn删除