⑴ 怎麼用java編程,實現分數的加減乘除運算
package Rational;
import java.util.StringTokenizer;
/**
* @author SunKun
* 分數四則運算
*/
public class Calculator {
int numerator; // 分子
int denominator; // 分母
Calculator(){
}
Calculator(int a,int b){
if(a == 0){
numerator = 0;
denominator = 1;
}
else{
setNumeratorAndDenominator(a,b);
}
}
void setNumeratorAndDenominator(int a, int b){ // 設置分子和分母
int c = f(Math.abs(a),Math.abs(b)); // 計算最大公約數
numerator = a / c;
denominator = b / c;
if(numerator<0 && denominator<0){
numerator = - numerator;
denominator = - denominator;
}
}
int getNumerator(){
return numerator;
}
int getDenominator(){
return denominator;
}
int f(int a,int b){ // 求a和b的最大公約數
if(a < b){
int c = a;
a = b;
b = c;
}
int r = a % b;
while(r != 0){
a = b;
b = r;;
r = a % b;
}
return b;
}
Calculator add(Calculator r){ // 加法運算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b + denominator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
Calculator sub(Calculator r){ // 減法運算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b - denominator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
Calculator muti(Calculator r){ // 乘法運算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * a;
int newDenominator = denominator * b;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
Calculator div(Calculator r){ // 除法運算
int a = r.getNumerator();
int b = r.getDenominator();
int newNumerator = numerator * b;
int newDenominator = denominator * a;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator qiuyu(double left,double right){ // 求余運算
double point_result = Math.IEEEremainder(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator pow(double left,double right){ // 求冪運算left^right
double point_result = Math.pow(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator max(double left,double right){ // 求兩數中的較大值
double point_result = Math.min(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
static Calculator min(double left,double right){ // 求兩數中的較小值
double point_result = Math.min(left, right);
int r1 = (int)point_result;
double r2 = point_result - r1;
int newNumerator = r1*10000 + (int)(r2*10000);
int newDenominator = 10000;
Calculator result = new Calculator(newNumerator,newDenominator);
return result;
}
// 封裝了具體運算,主要為對輸入進行轉換,對輸出封裝
public static void compute(String data1,String operation,String data2){
StringTokenizer fenxi = new StringTokenizer(data1,"/");
int data1_1 = Integer.parseInt(fenxi.nextToken());
int data1_2 = Integer.parseInt(fenxi.nextToken());
fenxi = new StringTokenizer(data2,"/");
int data2_1 = Integer.parseInt(fenxi.nextToken());
int data2_2 = Integer.parseInt(fenxi.nextToken());
Calculator r1 = new Calculator(data1_1,data1_2);
Calculator r2 = new Calculator(data2_1,data2_2);
Calculator result;
int a,b;
if(operation.equals("+")){ // 兩數相加
result = r1.add(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("-")){ // 兩數相減
result = r1.sub(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("*")){ // 兩數相乘
result = r1.muti(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("/")){ // 兩數相除
result = r1.div(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("%")){ // 兩數求余
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = qiuyu(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("^")){ // 兩數求冪
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = pow(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("max")){ // 兩數中的較大值
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = max(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
if(operation.equals("min")){ // 兩數中的較小值
double left = (double)data1_1/(double)data1_2;
double right = (double)data2_1/(double)data2_2;
result = min(left,right);
a = result.getNumerator();
b = result.getDenominator();
System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
}
}
}
⑵ c語言中的除怎麼表示
C語言中的除用符號 / (正斜杠)表示
用法嫌耐:
1、當被除數和除數均為整型時,結果為整型。
如1/2的結果芹罩春是0,9/2的結果是4
2、當被除數和除數任意一個為浮點數時,結果為浮點數。
如1.0/2的結果是0.50000000000000000(double型),9.0/2.0的結果是4.5000000000000000(double型)
(2)如何用編程寫除法擴展閱讀:
c語言中其他的計算函數
int isdigit(int ch) 若ch是數字('0'-'9')返回非0值,否則返回0
int isgraph(int ch) 若ch是可列印字元(不含空格)(0x21-0x7E)返回非0值,否則返回0
int islower(int ch) 若ch是小寫字母('a'-'z')返回非0值,否則返回0
double fabs(double x) 返回雙精度參數x的絕對值
long labs(long n) 返回長整型參數n的絕對值
double exp(double x) 返回指數函數ex的悶歷值
⑶ 加減乘除的c語言編程怎麼寫
c語言的加減乘除可以這樣寫,假設有int變數a,b,c
加法:c=a+b
減法:c=a-b
乘法:c=a*b
除法:c=a/b
⑷ 輸入兩個整數,進行加減乘除四則運算的c語言程序怎麼寫啊,拜託了~
代碼抄
#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d ",a + b);
printf("%d ",a - b);
printf("%d ",a * b);
printf("%d ",a / b);
return 0;
}
C語言中的加減乘除和數學中的加減乘除一樣,不同在於符號的表示問題,乘號需要用「*」表示。除號需要用「/」表示。新手學習C語言,很容易把除號和取余好混淆,強調一下,取余號是「%」,百分號就是取余的意思。因此在輸入兩個整數以後,按照數學方法就可以直接輸出結果,整數的輸入用scanf()函數。