我想通过模拟人的计算方式,了解人工智能或意识。编了几个函数。它能任意精确度计算,纯C代码。
禁止用于商业目的。
plus.h
#ifndef __PLUS_H__
#define __PLUS_H__
#ifdef __cplusplus
extern "C" {
#endif
char* xunhuanAll(char*chh1,char*chh2,char ch);
void xunhuan1(char *chh2, char chh, int n);
char* xunhuanplus(char *chh1, char *chh2);
char* clrspchr(char * str); // 删除str中全部空白字符
char* limltnum(char *str);
char* xunhuansubtract(char *chh1, char *chh2);
char* xunhuanmultiply(char *chh1, char *chh2);
char* xunhuandivided(char *chh1, char *chh2, char k, char l, char dot1, char dot2);
void xunhuansubtract1(char *chh2, char chh, int n);
void xunhuanmultiply1(char *chh2, char chh, int n);
void xunhuandivided1(char *chh2, char chh, int n);
char* shuym(char*str);//输入输出口
#ifdef __cplusplus
}
#endif
#endif
//
plus.cpp
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
#include "plus.h"
#define lonng 300
/*char* xunhuanAll(char*chh1, char*chh2, char ch);
void xunhuan1(char *chh2, char chh, int n);
char* xunhuanplus(char *chh1, char *chh2);
char* clrspchr(char * str); // 删除str中全部空白字符
char* limltnum(char *str);
char* xunhuansubtract(char *chh1, char *chh2);
char* xunhuanmultiply(char *chh1, char *chh2);
char* xunhuandivided(char *chh1, char *chh2, char k, char l, char dot1, char dot2);
void xunhuansubtract1(char *chh2, char chh, int n);
char* shuym(char*str);*/
char suanshu[600] = { "0+0=0;0+1=1;0+2=2;0+3=3;0+4=4;0+5=5;0+6=6;0+7=7;0+8=8;0+9=9;\
1+1=2;1+2=3;1+3=4;1+4=5;1+5=6;1+6=7;1+7=8;1+8=9;1+9=10;\
2+2=4;2+3=5;2+4=6;2+5=7;2+6=8;2+7=9;2+8=10;2+9=11\
3+3=6;3+4=7;3+5=8;3+6=9;3+7=10;3+8=11;3+9=12;\
4+4=8;4+5=9;4+6=10;4+7=11;4+8=12;4+9=13;\
5+5=10;5+6=11;5+7=12;5+8=13;5+9=14;\
6+6=12;6+7=13;6+8=14;6+9=15;\
7+7=14;7+8=15;7+9=16;\
8+8=16;8+9=17;\
9+9=18;" };
char shengfa[600] = { "0*0=0;0*1=0;0*2=0;0*3=0;0*4=0;0*5=0;0*6=0;0*7=0;0*8=0;0*9=0;\
1*1=1;1*2=2;1*3=3;1*4=4;1*5=5;1*6=6;1*7=7;1*8=8;1*9=9;\
2*2=4;2*3=6;2*4=8;2*5=10;2*6=12;2*7=14;2*8=16;2*9=18;\
3*3=9;3*4=12;3*5=15;3*6=18;3*7=21;3*8=24;3*9=27;\
4*4=16;4*5=20;4*6=24;4*7=28;4*8=32;4*9=36;\
5*5=25;5*6=30;5*7=35;5*8=40;5*9=45;\
6*6=36;6*7=42;6*8=48;6*9=54;\
7*7=49;7*8=56;7*9=63;\
8*8=64;8*9=72;\
9*9=81;" };
//
plus.cpp
char * xunhuanplus(char *chh1, char *chh2){
int k = strlen(chh1); k = k - 1; int j = k, j1 = 0, booa = 0;
char chh11[7] = {}, chh22[7] = {}, plus[2] = {}, multiply[2] = {};
strcpy(plus, "+");
while (k + 1> 0){
if (chh2[k] == '.')k = k - 1;
chh11[0] = 'w';
chh11[0] = chh1[k]; chh11[1] = '\0';
chh22[0] = chh2[k]; chh22[1] = '\0';
/判断高位是否全为零
while (j >= 0 && !booa){
if (chh2[j] != '0' || chh1[j] != '0'){
j1 = j - 1;//因循环倒一‘0‘;倒二非‘0’的最后一次。
}
if (j == 0)booa = 1;
j--;
};
if (booa && k == j1){
break;
}
//
if (chh11[0] < chh22[0]){
strcat(chh11, plus); strcat(chh11, chh22);
}
else
{
strcat(chh22, plus); strcat(chh22, chh11);
strcpy(chh11, chh22);
}
int i = 0;
while (1)
{
if (!strncmp(suanshu + i, chh11, 3)){
chh22[0] = suanshu[i + 4];
if (suanshu[i + 5] != ';'){
chh22[1] = suanshu[i + 5];
chh22[2] = '\0'; break;
}
if (suanshu[i + 5] == ';'){
chh22[1] = '\0'; break;
}
};
if (suanshu[i] == '\0')break;
i++;
}
if (chh22[1] == '\0'){
chh2[k] = chh22[0]; chh22[0] = '0';
}
if (chh22[1] != '\0'){
chh2[k] = chh22[1]; chh22[1] = '0';;
xunhuan1(chh2, chh22[0], k);
}
k--;
};
return chh2;
}
void xunhuan1(char *chh2, char chh, int n){
char chh1[lonng] = {}, chh4[lonng] = {}, chh3[lonng] = {},
chh11[5] = {}, chh22[5] = {}, plus[2] = { '+' };
int i = 0, k = n, j = k, j1 = 0, booa = 0;
while (k > 0){
chh11[0] = chh; chh11[1] = '\0'; chh = 0;
if (chh2[k - 1] == '.')k = k - 1;
chh22[0] = chh2[k - 1]; chh22[1] = '\0';
/判断高位是否全为零
while (j >= 0 && !booa){
if (chh2[j] != '0' || chh1[j] != '0'){
j1 = j - 2;//因循环倒一‘0‘;倒二非‘0’的最后一次。
}
if (j == 0)booa = 1;
j--;
};
if (booa && k == j1){
break;
}
//
if (chh11[0] < chh22[0]){
strcat(chh11, plus); strcat(chh11, chh22);
}
else
{
strcat(chh22, plus); strcat(chh22, chh11);
strcpy(chh11, chh22);
}
i = 0;
while (1)
{
if (!strncmp(suanshu + i, chh11, 3)){
chh22[0] = suanshu[i + 4];
if (suanshu[i + 5] != ';'){
chh22[1] = suanshu[i + 5];
chh22[2] = '\0'; break;
}
if (suanshu[i + 5] == ';'){
chh22[1] = '\0'; break;
}
};
if (suanshu[i] == '\0')break;
i++;
}
k = k - 1;
if (chh22[1] == '\0'){
chh2[k] = chh22[0]; chh22[0] = '0'; return;
}
if (chh22[1] != '\0'){
chh2[k] = chh22[1]; chh22[1] = '0';
xunhuan1(chh2, chh22[0], k);
}
k--;
}
}
char* clrspchr(char * str)
{
for (char*s = str, *p = str; (isspace(*s) || '\b' == *s) ? *s++ : (*p++ = *s++);) continue;
return str;
}
char* limltnum(char *str){//只允许数字及+—*/ 进入
int i = 0, f = 0, a = -2, b = 0;
char chstr[10] = {}, shun[10] = {}, str1[10000] = {}; memset(chstr, 'x', sizeof(char) * 10);
while (1){
if (str[i] >= '0' &&str[i] <= '9' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '.' || str[i] == '\0')
{
if (str[i] == '+'){ shun[f++] = '+'; if (a != i - 1){ a = i; }; b = i; }
if (str[i] == '-'){ shun[f++] = '-'; if (a != i - 1){ a = i; }; b = i; }
if (str[i] == '*'){ shun[f++] = '*'; if (a != i - 1){ a = i; }; b = i; }
if (str[i] == '/'){ shun[f++] = '/'; if (a != i - 1){ a = i; }; b = i; }
if (str[i] == '\0'){ break; }
}
else{
chstr[0] = '0'; return chstr;
}
if (a == b - 1){ chstr[0] = '0'; return chstr; }
i++;
}
if (str[0] == '-'&&shun[1] == '*'){ shun[0] = '*'; shun[2] = '-'; }
if (str[0] == '-'&&shun[1] == '/'){ shun[0] = '/'; shun[2] = '-'; }
if (str[0] == '-' && (shun[1] == '+' || shun[1] == '-' || shun[1] == '*' || shun[1] == '/')){
strcpy(str1, str + 1); strcpy(str, str1);
}
chstr[0] = shun[0]; chstr[1] = shun[1]; chstr[2] = shun[2]; chstr[9] = '\0';
if (f == 0)chstr[0] = '0';
return chstr;
}
///
char * xunhuansubtract(char *chh1, char *chh2){
int k = 0, ka = strlen(chh1), j = k, j1 = 0, booa = 0, booanegative = 0, booamultiple = 0, i = 0;
char chh11[7] = {}, chh22[7] = {}, chh3[lonng] = {}, chh4[lonng] = {};
//f (strlen(chh1) != strlen(chh2))
if (strlen(chh1) != strlen(chh2)){
ka = strlen(chh1); k = strlen(chh2); chh4[0] = '\0'; chh3[0] = '\0';
if (ka > k){
while (1){
if (i++ >= ka - k)break; strcat(chh4, "0");
}
}
else{
while (1){
if (i++ >= k - ka)break; strcat(chh4, "0");
}
}
if (ka > k){
strcat(chh4, chh2); strcpy(chh2, chh4);
}
else{
strcat(chh4, chh1); strcpy(chh1, chh4);
}
ka = strlen(chh1);
}
strcpy(chh4, chh1); k = ka - 1; i = 0;
while (k + 1 > 0){
booanegative = 0, booamultiple = 0;
if (chh2[k] == '.')k = k - 1;
chh11[0] = chh4[k]; chh11[1] = '\0';
chh22[0] = chh2[k]; chh22[1] = '\0';
/判断高位是否全为零
while (j >= 0 && !booa){
if (chh2[j] != '0' || chh1[j] != '0'){
j1 = j - 1;//因循环倒一‘0‘;倒二非‘0’的最后一次。
}
if (j == 0)booa = 1;
j--;
};
if (booa && k == j1){
break;
}
//
//加数 被加数t
if (chh22[0]>chh11[0]){
booanegative = 1;
if ((chh22[0] - '0' + chh22[0] - '0') >= ('9' - '0' + chh11[0] - '0' + '1' - '0')){
//用零表示未知数
strcpy(chh3, "0+");
strcat(chh3, chh22);
strcat(chh3, "=1");
strcat(chh3, chh11);
strcpy(chh11, chh3);
}
else{
strcat(chh22, "+0=1"); strcat(chh22, chh11); strcpy(chh11, chh22);
booamultiple = 1;
}
}
else
{
if ((chh22[0] - '0' + chh22[0] - '0') >= (chh11[0] - '0')){
strcpy(chh3, "0+");
strcat(chh3, chh22);
strcat(chh3, "=");
strcat(chh3, chh11);
strcpy(chh11, chh3);
}
else{
booamultiple = 1;
strcat(chh22, "+0="); strcat(chh22, chh11); strcpy(chh11, chh22);//用零表示未知数
}
}
int i = 0;
while (1)
{
if (booamultiple&&!booanegative&&!strncmp(suanshu + i, chh11, 2) && !strncmp(suanshu + i + 3, chh11 + 3, 2) ||//用于1+0=7模式
booamultiple&&booanegative&&!strncmp(suanshu + i, chh11, 2) && !strncmp(suanshu + i + 3, chh11 + 3, 3) ||//用于1+0=17模式
!booamultiple&&!booanegative&&!strncmp(suanshu + i + 1, chh11 + 1, 4) ||//用于0+4=7模式
!booamultiple&&booanegative&&!strncmp(suanshu + i + 1, chh11 + 1, 5)//用于0+6=11模式
){
if (booamultiple){
chh4[k] = suanshu[i + 2]; break;
}
if (!booamultiple){
chh4[k] = suanshu[i]; break;
}
};
if (suanshu[i] == '\0')break;
i++;
}
if (booanegative){
strcpy(chh1, chh4); chh11[0] = '1';//用于上一位减一。
xunhuansubtract1(chh1, chh11[0], k); strcpy(chh4, chh1);
}
k--;
}
//
strcpy(chh1, chh4);
return chh1;
}
void xunhuansubtract1(char *chh1, char chh, int n){
char chh2[lonng] = {}, chh4[lonng] = {}, chh3[lonng] = {},
chh11[7] = {}, chh22[7] = {};
int i = 0, k = n, booamultiple = 0, booanegative = 0, j = k, j1 = 0, booa = 0;
while (k > 0){
if (chh1[k - 1] == '.')k = k - 1;
chh22[0] = chh; chh22[1] = '\0'; chh = '0'; chh11[0] = '0';
chh11[0] = chh1[k - 1]; chh11[1] = '\0';
if (chh22[0] == '0')break;
//加数 被加数t
if (chh22[0]>chh11[0]){
booanegative = 1;
if ((chh22[0] - '0' + chh22[0] - '0') >= ('9' - '0' + chh11[0] - '0' + '1' - '0')){
//用零表示未知数
strcpy(chh3, "0+");
strcat(chh3, chh22);
strcat(chh3, "=1");
strcat(chh3, chh11);
strcpy(chh11, chh3);
}
else{
strcat(chh22, "+0=1"); strcat(chh22, chh11); strcpy(chh11, chh22);
booamultiple = 1;
}
}
else
{
if ((chh22[0] - '0' + chh22[0] - '0') >= (chh11[0] - '0')){
strcpy(chh3, "0+");
strcat(chh3, chh22);
strcat(chh3, "=");
strcat(chh3, chh11);
strcpy(chh11, chh3);
}
else{
booamultiple = 1;
strcat(chh22, "+0="); strcat(chh22, chh11); strcpy(chh11, chh22);//用零表示未知数
}
}
i = 0;
while (1)
{
if (booamultiple&&!booanegative&&!strncmp(suanshu + i, chh11, 2) && !strncmp(suanshu + i + 3, chh11 + 3, 2) ||//用于1+0=7模式
booamultiple&&booanegative&&!strncmp(suanshu + i, chh11, 2) && !strncmp(suanshu + i + 3, chh11 + 3, 3) ||//用于1+0=17模式
!booamultiple&&!booanegative&&!strncmp(suanshu + i + 1, chh11 + 1, 4) ||//用于0+4=7模式
!booamultiple&&booanegative&&!strncmp(suanshu + i + 1, chh11 + 1, 5)//用于0+6=11模式
){
if (booamultiple){
chh1[k - 1] = suanshu[i + 2];
break;
}
if (!booamultiple){
chh1[k - 1] = suanshu[i];
break;
}
};
if (suanshu[i] == '\0')break;
i++;
}
if (booanegative){
k = k - 1;
chh11[0] = '1';用于上一位减一。
xunhuansubtract1(chh1, chh11[0], k);
}
k--;
}
}
char* xunhuanmultiply(char *chh1, char *chh2){
int ka = strlen(chh1), i = 0, i1 = 0, ii1 = 0, k = 0, j = 0, j1 = 0, booa = 0, k1 = 0
, jj = 0, jj1 = 0, booaa = 0;
char chh4[lonng] = {}, chh3[lonng] = {};
char chh11[7] = {}, chh22[7] = {}, plus[2] = { '*' };
if (strlen(chh1) != strlen(chh2)){
ka = strlen(chh1); k = strlen(chh2); chh4[0] = '\0'; chh3[0] = '\0';
if (ka > k){
while (1){
if (i++ >= ka - k)break; strcat(chh4, "0");
}
}
else{
while (1){
if (i++ >= k - ka)break; strcat(chh4, "0");
}
}
if (ka > k){
strcat(chh4, chh2); strcpy(chh2, chh4);
}
else{
strcat(chh4, chh1); strcpy(chh1, chh4);
}
ka = strlen(chh1);
}
///
memset(chh4, '0', sizeof(char) * lonng); memset(chh3, '0', sizeof(char) * lonng);
chh3[lonng - 1] = '\0'; chh4[lonng - 1] = '\0';
k1 = ka - 1;
aa: k = ka - 1; //因‘0‘
while (k + 1 > 0){
if (chh1[k] == '.')k = k - 1;
if (chh2[k1] == '.')k1 = k1 - 1;
chh11[0] = 'w';
chh11[0] = chh1[k]; chh11[1] = '\0';
chh22[0] = chh2[k1]; chh22[1] = '\0';
/判断高位是否全为零
j = k;
while (j >= 0 && !booa){
if (chh1[j] != '0'){
j1 = j - 1;//因循环倒一‘0‘;倒二非‘0’的最后一次。
}
if (j == 0)booa = 1;
j--;
};
if (booa && k == j1){
break;
}
jj = k1;
while (jj >= 0 && !booaa){
if (chh2[jj] != '0'){
jj1 = jj - 1;//因循环倒一‘0‘;倒二非‘0’的最后一次。
}
if (jj == 0)booaa = 1;
jj--;
};
if (booaa && k1 == jj1){
k1 = 0; break;
}
//
if (chh11[0] < chh22[0]){
strcat(chh11, plus); strcat(chh11, chh22);
}
else
{
strcat(chh22, plus); strcat(chh22, chh11);
strcpy(chh11, chh22);
}
int i = 0;
while (1)
{
if (!strncmp(shengfa + i, chh11, 3)){
chh22[0] = shengfa[i + 4];
if (shengfa[i + 5] != ';'){
chh22[1] = shengfa[i + 5];
chh22[2] = '\0';
i1 = i1 + 2;
chh4[lonng - 1 - i1] = chh22[0]; chh4[lonng - 1 - i1 + 1] = chh22[1]; chh4[lonng - 1] = '\0';
if (chh22[0] != '0' || chh22[1] != '0') xunhuanplus(chh4, chh3);
chh4[lonng - 1 - i1] = '0'; chh4[lonng - 1 - i1 + 1] = '0'; i1 = i1 - 1;//加法位移
break;
}
if (shengfa[i + 5] == ';'){
i1 = i1 + 1;
chh4[lonng - 1 - i1] = chh22[0];
chh4[lonng - 1] = '\0';
if (chh22[0] != '0')xunhuanplus(chh4, chh3);
chh4[lonng - 1 - i1] = '0';//加法位移
chh22[1] = '\0'; break;
}
};
if (shengfa[i] == '\0')break;
i++;
}
k--;
}
//i1 =++ii1;//乘法位移
if (k1-- > 0){ i1 = ++ii1; goto aa; }//用于乘法双循环。
while (1){//去除前置
if (chh3[0] != '0' || chh3[0] == '0'&&chh3[1] == '.' || chh3[0] == '0'&&chh3[1] == '\0')break;
if (chh3[0] == '0'&&chh3[1] != '.'){ strcpy(chh2, chh3 + 1); strcpy(chh3, chh2); }
}
return chh3;
}
char* xunhuandivided(char *chh1, char *chh2, char k, char l, char dot1, char dot2)
{
int dotdevided = 0, booaBCHouYi = 0, dot = 0, booa = 0, i = 0, i1 = 0, end = 0, booaend = 1, booazero = 0, BCHouYi = 0;//被除后移
char chh3[lonng] = {}, chh4[lonng] = {}, chh55[lonng] = {}, chh6[lonng] = {}, chh7[lonng] = {}, chh8[lonng] = {};
if (dot2 > 1){//消除dot2
strncpy(chh4, chh2, l); strcat(chh4, chh2 + l + 1); strcpy(chh2, chh4); l = l + dot2 - 1;
while (1){
if (dot2 == 1)break;
if (dot1 > 1){
if (chh1[k + 1] == '\0')chh1[k + 1] = '0';
if (!booa)strncpy(chh4, chh1, k); booa = 1; chh4[k] = chh1[k + 1]; chh4[k + 1] = '\0'; chh1[k + 1] = chh1[k]; //dot1移动
k++;
}
if (dot1 == 0){
if (!booa)strcpy(chh4, chh1); booa = 1; chh4[k] = '0'; chh4[k + 1] = '\0';
k++;
}
dot2--;
}
if (dot1 > 1){ strcat(chh4, chh1 + k); strcpy(chh1, chh4); };
chh4[k] = '\0';
if (dot1 == 0)strcpy(chh1, chh4);
}
strcpy(chh4, chh1); memset(chh1, 0, sizeof(char) * lonng);
while (1){//去除前置'0'
if (chh2[0] != '0' || (chh2[0] == '0'&&chh2[1] == '.') || chh2[0] == '0'&&chh2[1] == '\0')break;
if (chh2[0] == '0'&&chh2[1] != '.'){ strcpy(chh8, chh2 + 1); strcpy(chh2, chh8); k--; }
}
l = strlen(chh2);
i1 = 0;
aa: i = 0;
i1 = strlen(chh3);
booa = 0;
if (chh4[0] == '0'&&chh4[1] == '.'&&end == 0){ i = i + 1; }//对0.n的0,进行跳过。
while (1){ //装入数组
if (chh4[i] == '.'){ i = i + 1; booaBCHouYi = 1; dot = 1; }
if (chh4[i] == '\0' || booazero){ booazero = 1; chh4[i] = '0'; dot = 1; }
chh3[i1] = chh4[i];//
if (booaBCHouYi || booazero){ BCHouYi++; if (!dot)dotdevided++; };
i++; i1++; if (strlen(chh3) - 1 >= l - 1)break;//被除数与除数位数同。
}
chh3[i1] = '\0'; strcpy(chh7, chh4 + i); strcpy(chh4, chh7);
i = 0; i1 = 0; booa = 0;
strcpy(chh6, chh2);
bb:
itoa(i1, chh55, 10);
strcpy(chh7, chh6);//备用
while (1){//去除前置'0'
if (chh3[0] != '0' || (chh3[0] == '0'&&chh3[1] == '.') || chh3[0] == '0'&&chh3[1] == '\0')break;
if (chh3[0] == '0'&&chh3[1] != '.'){ strcpy(chh8, chh3 + 1); strcpy(chh3, chh8); k--; }
}
strcpy(chh6, xunhuanmultiply(chh2, chh55));//减数乘以i倍,再比较。
i = 0; booa = 0;
while (1){
if (strlen(chh6)>strlen(chh3)){ booa = 1; break; }
if (strlen(chh6)<strlen(chh3)){ booa = 0; break; }
if (chh6[i] >= chh3[i]){
if (chh6[i] > chh3[i]){ booa = 1; break; }
if (chh6[i] == chh3[i] && chh6[i] == '\0'){ booa = 1; strcpy(chh7, chh6); i1 = i1 + 1; break; }
}
else break;
i++;
}
if (!booa){ i1++; goto bb; }
strcpy(chh8, chh3);
if (booa){ strcpy(chh3, xunhuansubtract(chh3, chh7)); }//小于被减数chh3的,且最接近的。
/输出处理
if (dot && booaend){ //'.'前加‘0‘
while (1){
if (dotdevided-- <= 0) break;
BCHouYi-- <= 1; chh1[end++] = '0';
}
} dotdevided = 0;//被除后移
if (dot&&booaend){
if (!end){
chh1[end++] = '0'; chh1[end++] = '.'; booaend = 0;
if (BCHouYi > 1){
while (1){
if (BCHouYi-- <= 1)break; chh1[end++] = '0';
}
} //被除后移
}
else{
chh1[end++] = '.'; booaend = 0;
if (BCHouYi > 1){
while (1){ if (BCHouYi-- <= 1)break; chh1[end++] = '0'; }
} booaBCHouYi = 1;//被除后移
}
}
if (BCHouYi > 1){
while (1){ if (BCHouYi-- <= 1)break; chh1[end++] = '0'; }
} booaBCHouYi = 1;//被除后移
chh1[end++] = char(i1 - 1 + '0'); chh1[end] = '\0'; BCHouYi = 0;
if (end<lonng / 2) goto aa;
return chh1;
}
char* shuym(char*strshu){ //参数在函数体过程中改变,其传入的实际参数相应改变。
剩余源代码可下载。