首頁 >電腦教學 >電腦知識 >C語言設計一個函數實作加減乘除運算

C語言設計一個函數實作加減乘除運算

王林
王林轉載
2024-01-07 18:50:141448瀏覽

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.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語言程式讓電腦自動出四則運算計算

#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 %d\n", 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 %d\n", 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刪除