Can be any number of the Calculator

本文介绍了一个使用C语言编写的计算器程序,该程序能够执行加、减、乘、除及阶乘运算。通过自定义字符串处理函数实现了大数运算的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

the following translation by google

With C language can be written in any number of  calculator.

Source as follows:

# Include "PocketC / REPLACE.h"
Int k, n1, n2, n3;
Max (int a, int b)
(
If (a> b) return a;
Else return b;
)
Min (int a, int b)
(
If (a <b) return a;
Else return b;
)
Repeat (string bpt, int num)
(String str;
Int j;

For (j = 1; j <= num; j = j +1)
(Str = str + bpt;
)
Return str;
)
Cfoo (string a)
(Int i;
For (i = 0; i <strlen (a) i = i +1)
(
If (strleft (a, 1) == 0) strright a = (a, strlen (a) -1);
Else break;
)
Return a;
)
Compare (string a, string b)
(String str1, str2; int num, i, j;
Cfoo a = (a);
B = cfoo (b);
If (strleft (a, a )=="-"&& strleft (b, a )!="-") return 2;
If (strleft (a, a )!="-"&& strleft (b, a )=="-") return 1;
N1 = strlen (a);
N2 = strlen (b);
If (n1> n2) return 1;
If (n1 <n2) return 2;
For (i = 0; i <n1; i = i +1)
Strmid str1 = ((a, i, 1);
Str2 = strmid (b, i, 1);
If ((char) str1> (char) str2) return 1;
If ((char) str1 <(char) str2) return 2;
)
)
Plus (string a, string b)
(
Int i;
Int in3, in4, in5, in6, in7;
String str4;
N1 = strlen (a);
N2 = strlen (b);
In6 = 0;
For (i = max (n1, n2); i> 0; i = i-1)
Strright in3 = ((a, 1);
In4 = strright (b, 1);
In5 = in3 + in4 + in6;
If (strlen (in5) == 2) (
Strleft in6 = ((string) in5, 1);
Strright in7 = ((string) in5, 1);)
Else
(In6 = 0; in7 = in5;)
Str4 = (string) in7 + str4;
Strleft a = (a, strlen (a) -1);
B = strleft (b, strlen (b) -1);
)
If (in6 == 0)
Return str4;
Else
Return (string) in6 + str4;
)
Minus (string a, string b)
(Int i;
Int in3, in4, in5, in6, in7, in8;
String str4, str1, str3;
N1 = strlen (a);
N2 = strlen (b);
In6 = 0;
In8 = compare (a, b);
If (in8 == 2) (a = str1; a = b, b = str1;)
For (i = max (n1, n2) -1; i> = 0; i = i-1)
Strright in3 = ((a, 1);
In4 = strright (b, 1);
In5 = in3-in4-in6;
If (in5 <0) (in6 = 1; in5 = 10 + in5;)
Else (in6 = 0;)
Str4 = (string) in5 + str4;
Strleft a = (a, strlen (a) -1);
B = strleft (b, strlen (b) -1);
)
If (strleft (str4, 1) == 0) = strright str4 (str4, strlen (str4) -1);
Strleft str4 = (a, strlen (a) -1) + str4;
If (in8 == 2)
Return "-" + str4;
Else
Return str4;
)
Smul (string a, int b)
(
Int i, str1, str2, str3;
String str4;
Str2 = 0;
For (i = strlen (a) -1; i> = 0; i = i-1)
(
Str1 = (int) strmid (a, i, 1) * b + (int) str2;
If (strlen (str1) == 2) (
Str2 strmid = ((string) str1, 0,1);
Str3 strmid = ((string) str1, 1,1);)
Else
(Str2 = 0;
Str3 = str1;
)
Str4 = str3 + str4;
)
If (str2! = "0")
Str4 = str2 + str4;
Return str4;
)
Multiply (string a, string b)
(
Int i, in1, in2, in3, in4;
String str1, str2, str4;
For (i = strlen (b) -1; i> = 0; i = i-1)
Strmid in1 = ((b, i, 1);
Str1 = smul (a, in1);
Str1 + = str1 repeat ( "0", strlen (b)-1-i);
Str4 = plus (str4, str1);
)
Return str4;
)
Divide (string a, string b)
(Int i, j, k, l;
String str4, str1, str2, str3;
N1 = strlen (a);
N2 = strlen (b);
For (i = n1-n2; i> = 0; i = i-1)
B + = (str2 repeat ( "0", i);
K = 0;
If (compare (str2, a) == 1) (a = cfoo (a); str4 = str4 +0; continue;)
For (j = 0; j <10; j = j +1)
Str1 = (minus (a, str2);
A = str1;
If (compare (a, str2) == 2)
(K = k +1; break;)
Else
(K = k + 1;)
)
Str4 = str4 + k;
)
Return cfoo (str4);
)
Fractorial (string numb)
(
If (numb <= 1) return 1;
Return multiply (numb, fractorial (minus (numb, 1)));
)
Evnt (int evt, int gid)
(
Switch (evt)
(
Case 5: / / mouseup
Switch (gid)
(Case 104:
N1 = cbgetcur (103);
Switch (n1)
(Case 0:
Editset (102, plus (replace (editget (100), "/ r / n ","",- 1), replace (editget (101)," / r / n ","",- 1)));
Break;
Case 1:
Editset (102, minus (replace (editget (100), "/ r / n ","",- 1), replace (editget (101)," / r / n ","",- 1)));
Break;
Case 2:
Editset (102, multiply (replace (editget (100), "/ r / n ","",- 1), replace (editget (101)," / r / n ","",- 1)));
Break;
Case 3:
Editset (102, divide (replace (editget (100), "/ r / n ","",- 1), replace (editget (101)," / r / n ","",- 1)));
Break;
Case 4:
Editset (101, fractorial (replace (editget (100), "/ r / n ","",- 1)));
Break;
) Break;
) Break;
)
)
Main ()
(Int i, j, k, evt, gid;
String one, str4, s1, s2, s3;
Createctrl ( "edit", "", 0x10a00044, 0x0, 5, 4, 160, 60100);
Createctrl ( "edit", "", 0x10a00044, 0x0, 5, 68, 160,60101);
Createctrl ( "edit", "", 0x10a00044, 0x0, 5, 132, 160,60102);

Createctrl ( "combobox", "", 0x10a00044, 0x0, 170, 20, 60180103);
Createctrl (the "button" and "calculated", 0x10000000, 0x0, 170, 50, 60,80104);
Cbaddstr (103, "plus"); cbaddstr (103, "by"); cbaddstr (103, "x"); cbaddstr (103, "except"); cbaddstr (103, "factorial");
Cbsetcur (103, 0);
While (1)
Evt = (event (1);
Gid = guiid ();
Sleep (1);
Evnt (evt, gid);
)


/ * One = "10000";
For (i = 1; i <10000; i = i +4)
Str4 = (plus (str4, divide (one, i));
)
For (i = 3; i <10000; i = i +4)
Str4 = (minus (str4, divide (one, i));
)
Alert (smul (str4, 4 ));*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值