当前位置:编程学习 > C/C++ >>

可以编一个计算器的c语言程序给我吗

答案:应该可以的!

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define UNKNOWN_CHAR 2
#define DIVISOR_IS_ZERO 3
#define FALSE 0
#define TRUE 1
/*定义结构,主要用于GetString函数,可以不管它*/
typedef struct string
{char str;
struct string *next;
}string;

/*函数声明*/

/*可以无限接受字符,输入一个字符就给你个空间,返回一个字符串首地址 . */
/*这个函数可以看成普通的scanf()*/
char *GetString();
/*从字符串str第*rank个字符开始分析直到遇到 ')' or ']' or '}' or '\0' . */
/*有错误就把错误代号装在*errorNum中,返回从中计算出的值*/
double GetASResultOf(char * str, int *rank, int *errorNum);
/*从字符串str第*rank个字符开始分析直到遇到不符合要求的字符,返回这个单位值.*/
/*如:str="12+sin(30)+[12+cos(60)]"
GetUnitageOf(str,0,errorNum)=12
GetUnitageOf(str,3,errorNum)=0.5
GetUnitageOf(str,11,errorNum)=12.5 */
double GetUnitageOf(char * str, int *rank, int *errorNum);
int main()
{
/*定义变量*/
double value;
int rank=0;
int errorNum=TRUE;
char * str;

/*接收字符*/
printf("counter v1.0\n");
printf("Made by PT\n");
printf("Date 2005-07-16\n\n");
printf("Please input your arithmetic:\n");
str=GetString();

/*调用处理函数,*/
value = GetASResultOf(str, &rank, &errorNum);

/*错误处理(没完善)*/
if (errorNum==TRUE) printf("The result is: %lf\n",value, rank);
else if (errorNum==UNKNOWN_CHAR) printf("char error!\n");
else if (errorNum==DIVISOR_IS_ZERO) printf("Divisor could not be 0\n");
else printf("Other error\n");

getch();
return 0;
}


/*0==[=========>无限动态字符接受函数*/
char *GetString()
{
char *CharToString(string * sHead, int arraySize);
int arraySize=0;
string *s;
string *sHead;

s = (string *)malloc(sizeof(string));
sHead = s;
s->str = getchar();
arraySize++;

while((s->str)!='\n')
{
arraySize++;
s->next=(string *)malloc(sizeof(string));
s=s->next;
s->str=getchar();
}

return CharToString(sHead, arraySize);
}

char *CharToString(string * sHead, int arraySize)
{
char *strArray;
string *s=sHead;
int i=0;
strArray = (char*)malloc(arraySize*sizeof(char));

while(i<arraySize)
{
if (s->str=='=' || s->str=='\n') break;
if (s->str!=32) strArray[i++]=s->str;
s=s->next;
}
strArray[i]='\0';
return strArray;
}
/*0==[=========> 得到一个单位的值*/
double GetUnitageOf(char * str, int *rank, int *errorNum)
{
double CharToNum(char * str, int *rank);
double value;

/*如果是数字或负号就返回这个数字*/
if ((str[*rank]>=48 && str[*rank]<=57) || (str[*rank]=='-'))
{if (str[*rank]=='-') {(*rank)++;value = (-1)*GetUnitageOf(str, rank,errorNum);}
else {value = CharToNum(str, rank);}}

/*如果是括号就用GetASResultOf取得括号里面的值并返回*/
else if (str[*rank]=='(' ||
str[*rank]=='[' ||
str[*rank]=='{' )
{(*rank)++;
value = GetASResultOf(str, rank, errorNum);}

/*返回sin(x)*/
else if (str[*rank]=='s' &&
str[(*rank)+1]=='i' &&
str[(*rank)+2]=='n')
{ (*rank)=(*rank)+3;
value = GetUnitageOf(str, rank, errorNum);
value = sin(value);
}

/*返回cos(x)*/
else if (str[*rank]=='c' &&
str[(*rank)+1]=='o' &&
str[(*rank)+2]=='s')
{*rank=*rank+3;
value = cos(GetUnitageOf(str, rank, errorNum));}

/*返回tan(x)*/
else if (str[*rank]=='t' &&
str[(*rank)+1]=='a' &&
str[(*rank)+2]=='n')
{*rank=*rank+3;
value = tan(GetUnitageOf(str, rank, errorNum));}
/*还可以加一些的,懒得加了*/

/*defult:处理错误*/
else {*errorNum = UNKNOWN_CHAR;value = 0;}

/*检查errorNum*/
if (*errorNum==TRUE){return value;}
else return FALSE;
}

/*0==[=========>这个是把字符转换成数字的函数,只在GetUnitageOf()里用*/
/*数字不能超过15个,其余的将被抹去.*/
double CharToNum(char * str, int *rank)
{
int j=0;
double value=0;

while((str[*rank]>=48 && str[*rank]<=57) || (str[*rank]==46))
{
if (str[*rank]!='.')
{if ((j++)<=15) {value = value*10+(str[*rank]-48);}}
else
{if (j<=15)
{
for ((*rank)++,j=1; (str[*rank]>=48 && str[*rank]<=57 && str[*rank]!='\0'); (*rank)++, j++)
{value = value+(str[*rank]-48)*pow(0.1,j);}
return value;
}
}

(*rank)++;
}
return value;
}
/*0==[=========>这个函数写的太复杂了,实在不好作解释,反正是在GetPDResultOf
函数和GetUnitageOf函数之间转啊转的*/
/*简单地说就是把str中第一个加减号前的式子算出来放在value变量中.再用函数
GetPDResultOf算出直到二个加减号前的式子然后与value相加或相减放在
变量value中,以此类推,直到遇上 ')' or ']' or '}' or '\0' */
double GetASResultOf(char * str, int *rank, int *errorNum)
{
double GetPDResultOf(char * str, int *rank, int *errorNum);
double value;
double dwIf_Zero;

/*遇上 ')' or ']' or '}' or '\0' 就返回*/
while (str[*rank]!=')' &&
str[*rank]!=']' &&
str[*rank]!='}' &&
str[*rank]!='\0')
{
if ((str[*rank]>=48 && str[*rank]<=57) ||
str[*rank]=='s' ||
str[*rank]=='c' ||
str[*rank]=='t')
{
if ((str[*rank-1]==')' ||
str[*rank-1]==']' ||
str[*rank-1]=='}') &&
*rank!=0)
{value = value*GetUnitageOf(str, rank, errorNum);}
else
{value = GetUnitageOf(str, rank, errorNum);}
}

else if (str[*rank]=='+')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value + GetPDResultOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return 0;}}


else if (str[*rank]=='-')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value - GetPDResultOf(str, rank, errorNum);}
else {value = GetUnitageOf(str, rank, errorNum);}}

else if (str[*rank]=='*')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value * GetUnitageOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return 0;}}

else if (str[*rank]=='/')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++;
dwIf_Zero=GetUnitageOf(str, rank, errorNum);
if (dwIf_Zero==0) {*errorNum=DIVISOR_IS_ZERO;return 0;}
else value = value /dwIf_Zero;}
else {*errorNum=UNKNOWN_CHAR;return 0;}}

else if (str[*rank]== '(' ||
str[*rank]== '[' ||
str[*rank]== '{' )
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value*GetASResultOf(str, rank, errorNum);}
else {value = GetUnitageOf(str, rank, errorNum);}}


else {*errorNum=UNKNOWN_CHAR;return FALSE;}


if (*errorNum!=TRUE) return FALSE;
}
(*rank)++;
return value;
}

double GetPDResultOf(char * str, int *rank, int *errorNum)
{
double value;
double dwIf_Zero;
value = GetUnitageOf(str, rank, errorNum);
if (str[*rank]=='\0') return value;

while (str[*rank]!='+' &&
str[*rank]!='-' &&
str[*rank]!=')' &&

上一个:谁能用c语言写一个钩子函数的例子
下一个:单片机定时器T0100ms定时中断,怎么用C语言实现

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,