计算器.docx
《计算器.docx》由会员分享,可在线阅读,更多相关《计算器.docx(23页珍藏版)》请在冰豆网上搜索。
![计算器.docx](https://file1.bdocx.com/fileroot1/2023-1/22/2c1908b3-f522-4996-8c09-d202fe5000e0/2c1908b3-f522-4996-8c09-d202fe5000e01.gif)
计算器
文件一计算器原件(完美版).c
#include
#include
#include
#include
#include
#include"shows.h"/*调用自定义头文件,用来显示界面*/
#include"hlsjs.h"/*调用行列式计算函数*/
#include"shuzizhuanhuan.h"/*数字转换*/
#defineMaxSize99
voidtranslate(charstr[],charexp[])/*将算术表达式转换成后缀表达式*/
{
struct
{
chardata[MaxSize];
inttop;/*top为栈顶*/
}op;/*定义一个含data和top的结构体*/
charch;
inti=0,t=0,n=0,m;/*n的作用是检验第一个数是否是负数,m的作用是检验括号里的第一个数是不是负数*/
op.top=-1;
ch=str[i];/*将str的每一个数转换成ch*/
i++;
while(ch!
='\0')/*ch对应不同的符号的时候对应的转换情况*/
{
switch(ch)
{
case'(':
/*当是(的时候,将此括号存入栈op*/
if(exp[t-1]=='-'&&exp[t-2]=='$')/*实现-(1+2)之类的运算*/
{
op.top++;
op.data[op.top]='*';
exp[t]='1';t++;
exp[t]='#';t++;
}
op.top++;op.data[op.top]=ch;
m=1;
break;
case')':
while(op.data[op.top]!
='(')/*括号内的转换优先级最高,故先提取表达式*/
{
exp[t]=op.data[op.top];
op.top--;
t++;
}
op.top--;
break;
case'+':
case'-':
while(op.top!
=-1&&op.data[op.top]!
='('&&op.data[op.top]!
='^')
{
exp[t]=op.data[op.top];
op.top--;
t++;
m=0;
}
while(op.top!
=-1&&(op.data[op.top]=='^'||op.data[op.top]=='*'))
{
exp[t]=op.data[op.top];
op.top--;
t++;
}
if((n==0||(op.data[op.top]=='('&&m!
=0)||op.data[op.top]=='^')&&ch=='-')/*检验第一个数或括号里第一个数或^后的数是不是负数*/
{
exp[t]='$';/*标记是负数*/
t++;
exp[t]=ch;
t++;
n++;
}
else
{
op.top++;/*恢复可插入位置*/
op.data[op.top]=ch;
}
break;
case'%':
case'*':
case'/':
while(op.top!
=-1&&(op.data[op.top]=='/'||op.data[op.top]=='*'||op.data[op.top]=='%'||op.data[op.top]=='^'))/*优先级*/
{
exp[t]=op.data[op.top];
op.top--;
t++;
}
op.top++;
op.data[op.top]=ch;
break;
case'^':
op.top++;
op.data[op.top]=ch;
break;
case'':
/*忽略空格,排除误操作*/
break;
default:
if(ch>='0'&&ch<='9'||ch=='.')
{
while(ch>='0'&&ch<='9'||ch=='.')
{
exp[t]=ch;t++;
ch=str[i];i++;
}
i--;
exp[t]='#';/*分隔操作数,为了美观,也为了以后好分隔操作数,呵呵*/
t++;
m=0;
}
else
{
printf("您输入的表达式有误!
请重新输入!
");
Sleep(2000);
}
}
n++;/*注意第一个操作符如果是'-'的话,可能造成错误,故n要随之改变*/
ch=str[i];
i++;
}
while(op.top!
=-1)/*得到剩下的部分*/
{
exp[t]=op.data[op.top];
t++;
op.top--;
}
exp[t]='\0';/*表达式结束*/
}
doublecal_value(charexp[])
{
struct
{
doubledata[MaxSize];
inttop;
}st;/*操作数栈*/
charch,comp=0;/*comp的作用是验证是否为负数*/
charchange[MaxSize];
intt=0;
inti=0,n;
st.top=-1;
ch=exp[t];
t++;
while(ch!
='\0')
{
switch(ch)/*运算主体*/
{
case'+':
st.data[st.top-1]=st.data[st.top-1]+st.data[st.top];
st.top--;
break;
case'*':
st.data[st.top-1]=st.data[st.top-1]*st.data[st.top];
st.top--;
break;
case'/':
if(st.data[st.top]!
=0)
st.data[st.top-1]=st.data[st.top-1]/st.data[st.top];
else
printf("\n\t\t\t\t除0是错误的\n");
st.top--;
break;
case'%':
if(st.data[st.top]!
=0)
st.data[st.top-1]=(int)st.data[st.top-1]%(int)st.data[st.top];/*把double转换为int类型的数*/
else
printf("\n\t\t\t\t模0是错误的");
st.top--;
break;
case'^':
st.data[st.top-1]=pow(st.data[st.top-1],st.data[st.top]);
st.top--;
break;
case'$':
comp=ch;
break;
default:
if(ch=='-'&&comp==0)
{
st.data[st.top-1]=st.data[st.top-1]-st.data[st.top];
st.top--;
break;
}
for(n=0;n
change[n]=0;
i=0;
while(ch>='0'&&ch<='9'||ch=='.'||(ch=='-'&&comp=='$'))/*从后缀表达式中获取操作数,#作用在此体现*/
{/*注意第一个数有可能是负数*/
change[i]=ch;
i++;
if(ch=='-'&&comp=='$')
comp=0;
ch=exp[t];
t++;
}
st.top++;
st.data[st.top]=atof(change);/*将字符转化为数字*/
}
ch=exp[t];
t++;
}
returnst.data[st.top];
}
intjcdigui(intn)/*阶乘递归*/
{
if(n==1)
return1;
else
return(n*jcdigui(n-1));
}
intmain()/*可以提到前面去*/
{
charstr[MaxSize],exp[MaxSize];/*str为算术表达式,exps为后缀表达式*/
charresponse;
charn;/*行列式的阶数*/
charm[3];/*阶乘的数*/
intdebug;
for(;;){
do{
mainshow();
printf("请输入您的选择:
");
fflush(stdin);
scanf("%c",&response);
if(response!
='1'&&response!
='2'&&response!
='3'&&response!
='4'&&response!
='5'&&response!
='!
')
{
printf("\n您输入选择有误,请重新输入!
");
Sleep(2000);
system("cls");
}
}while(response!
='1'&&response!
='2'&&response!
='3'&&response!
='4'&&response!
='5'&&response!
='!
');
system("cls");
switch(response)
{
case'1':
show1();
do{
printf("请输入一个求值表达式\n");
printf("表达式:
");
fflush(stdin);
gets(str);/*输入一个算术表达式*/
if(str[0]=='!
')
break;
translate(str,exp);/*将算术表达式转换成后缀表达式*/
system("cls");
show1();
printf("计算结果:
%s=%lg\n\n\n",str,cal_value(exp));/*通过后缀表达式来求值*/
}while(str[0]!
='!
');
break;
case'2':
do{
debug=0;
m[0]=m[1]=m[2]=0;
show2();
printf("n的值如果大于12可能不正确!
\n");
printf("请输入n的值:
");
fflush(stdin);
gets(m);
if(m[0]=='!
')
break;
if(m[0]=='\n')
debug=1;
if(m[0]>'9'||m[0]<'0')
{
debug=1;
printf("您输入的数字有误,请重新输入!
");
Sleep(2000);
}
if(debug==0)
{
if(m[0]=='0')
printf("\n\n计算结果:
0!
=1");
else
printf("\n\n计算结果:
%d!
=%d",(int)atof(m),jcdigui((int)atof(m)));
printf("\n\n\n");
system("pause");
}
system("cls");
}while(m[0]!
='!
');
break;
case'3':
for(;;){
show3();
printf("请输入您要求的阶数(2到6之间):
");
fflush(stdin);
scanf("%c",&n);
if(n=='!
')
break;
if(n>='2'&&n<='6')
hlsjshs(n);/*行列式计算函数*/
else
{
printf("\n您输入的数字有误,请重新输入!
");
Sleep(2000);
}
system("cls");
}
break;
case'4':
show4();
transform();
break;
case'5':
show5();
printf("\n");
system("pause");
break;
case'!
':
printf("\n\n\n\n\n\n\n\n\n\nO(∩_∩)O谢谢使用!
");
Sleep(3000);
exit(0);
}
system("cls");
}
return0;
}
文件二hanglieshi.h
doublepow2(doublenum[][2])
{
doubleresult=0;
result=num[0][0]*num[1][1]-num[0][1]*num[1][0];
returnresult;
}
doublepow3(doublenum[][3])
{
doubletrans3[3][2][2];
doubleresult=0;
doubleline3_1[3];
doubleshift[2][2];
intsign=-1;
inti,j,n;
for(i=0;i<3;i++)
line3_1[i]=num[0][i];
for(n=0;n<3;n++)
for(i=0;i<2;i++)
for(j=0;j<3;j++)
if(jtrans3[n][i][j]=num[i+1][j];
elseif(j>n)
trans3[n][i][j-1]=num[i+1][j];
else
continue;
for(n=0;n<3;n++)
{
sign*=-1;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
shift[i][j]=trans3[n][i][j];
result+=line3_1[n]*sign*pow2(shift);
}
returnresult;
}
doublepow4(doublenum[][4])
{
doubletrans4[4][3][3];
doubleresult=0;
doubleline4_1[4];
doubleshift[3][3];
intsign=-1;
inti,j,n;
for(i=0;i<4;i++)
line4_1[i]=num[0][i];
for(n=0;n<4;n++)
for(i=0;i<3;i++)
for(j=0;j<4;j++)
if(jtrans4[n][i][j]=num[i+1][j];
elseif(j>n)
trans4[n][i][j-1]=num[i+1][j];
else
continue;
for(n=0;n<4;n++)
{
sign*=-1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
shift[i][j]=trans4[n][i][j];
result+=line4_1[n]*sign*pow3(shift);
}
returnresult;
}
doublepow5(doublenum[][5])
{
doubletrans5[5][4][4];
doubleresult=0;
doubleline5_1[4];
doubleshift[4][4];
intsign=-1;
inti,j,n;
for(i=0;i<5;i++)
line5_1[i]=num[0][i];
for(n=0;n<5;n++)
for(i=0;i<4;i++)
for(j=0;j<5;j++)
if(jtrans5[n][i][j]=num[i+1][j];
elseif(j>n)
trans5[n][i][j-1]=num[i+1][j];
else
continue;
for(n=0;n<5;n++)
{
sign*=-1;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
shift[i][j]=trans5[n][i][j];
result+=line5_1[n]*sign*pow4(shift);
}
returnresult;
}
doublepow6(doublenum[][6])
{
doubletrans6[6][5][5];
doubleresult=0;
doubleline6_1[5];
doubleshift[5][5];
intsign=-1;
inti,j,n;
for(i=0;i<6;i++)
line6_1[i]=num[0][i];
for(n=0;n<6;n++)
for(i=0;i<5;i++)
for(j=0;j<6;j++)
if(jtrans6[n][i][j]=num[i+1][j];
elseif(j>n)
trans6[n][i][j-1]=num[i+1][j];
else
continue;
for(n=0;n<6;n++)
{
sign*=-1;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
shift[i][j]=trans6[n][i][j];
result+=line6_1[n]*sign*pow5(shift);
}
returnresult;
}
文件三hlsjs.h
#include"hanglieshi.h"
#include
#include
voidhlsjshs(charn)
{
inti,j;
doubleresult;
doublenum2[2][2],num3[3][3],num4[4][4],num5[5][5],num6[6][6];
switch(n)
{
case'2':
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
printf("num[%d][%d]=",i+1,j+1);
scanf("%lf",&num2[i][j]);
}
result=pow2(num2);
break;
case'3':
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf("num[%d][%d]=",i+1,j+1);
scanf("%lf",&num3[i][j]);
}
result=pow3(num3);
break;
case'4':
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
printf("num[%d][%d]=",i+1,j+1);
scanf("%lf",&num4[i][j]);
}
result=pow4(num4);
break;
case'5':
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
printf("num[%d][%d]=",i+1,j+1);
scanf("%lf",&num5[i][j]);
}
result=pow5(num5);
break;
case'6':
for(i=0;i<6;i++)
for(j=0;j<6;j++)
{
printf("num[%d][%d]=",i+1,j+1);
scanf("%lf",&num6[i][j]);
}
result=pow6(num6);
break;
}
printf("\n计算结果=%lg",result);
printf("\n");
system("pause");
}
文件四shows.h
#include
voidmainshow()
{
printf("\n\n");
printf("●●●●●●●●●●●●●●●●●●●●●\n");
printf("●●\n");
printf("●★康瑞计算器★●\n");
printf("●●\n");
printf("●【1】普通运算●\n");
printf("●【2】阶乘运算●\n");
printf("●【3】行列式运算●\n");
printf("●【4】进制转换●\n");
printf("●【5】产品说明●\n");
printf("●●\n");
printf("●【!
】退出●\n");
printf("●●\n");
printf("●●●●●●●●●●●●●●●●●●●●●\n");
printf("\n\n");
}
voidshow1()
{
printf("\n\n");
printf("●●●●●●●●●●●●●●●●●●●●●\n");
printf("●●\n");
printf("●●\n");
printf("●★普通计算★●\n");
printf("●●\n");
printf("●●\n");
printf("●【!
】返回●\n");
printf("●●\n");
printf("●●\n");
printf("●●●●●●●●●●●●●●●●●●●●●\n");
printf("\n\n");
}
voidshow2()
{
printf("\n\n");
printf("●●●●●●●●●●●●●●●●●●●●●\n");
printf("●●\n");
printf("●●\n");
printf("●★阶乘计算★●\n");
printf("●●\n");
printf("●●\n");
printf("●【!
】返回●\n");
printf("●●\n");
printf("●●\n");
printf("●●●●●●●●●●●●●●●●●●●●●\n");
print