第2个回答 2010-07-05
刚才回答了个类似的,你可以参考下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*错误时,输出用法 并结束程序 */
void usage()
{
printf("参数错误\n");
printf("命令行计算器\n");
printf("compute 操作数1 + | - | * | / | # | ! [操作数2]\n");
exit(-1); /*结束程序*/
}
/*求阶乘*/
double factorial(int n)
{
int i = 1;
double result = 1.0;
for(i = 1; i <=n ; i++)
result *= i;
return result;
}
/*判断运算符需要的操作数,不合法时返回-1,双目返回2,单目返回1*/
int opnum(char op)
{
int n = -1;
if(op == '+' || op == '-' || op == '*' || op == '/')
n = 2;
else if(op == '#' || op == '!')
n = 1;
return n;
}
/*根据字符型的运算符进行相应的运算*/
double compute(double num1, char op,double num2)
{
double result = 0.0;
switch(op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
/*除数为0时,输出错误信息并结束程序*/
if(!num2)
{
printf("除0错误\n");
exit(-1);
}
result = 1.0 * num1 / num2;
break;
case '!':
/*不能求负数的阶乘*/
if(num1 < 0)
{
printf("不能求负数的阶乘\n");
exit(-1);
}
result = factorial((int)num1);
break;
case '#':
/*实数范围内,不能求负数的平方根*/
if(num1 < 0)
{
printf("实数范围内平方根错误\n");
exit(-1);
}
result = sqrt(num1);
break;
}
return result;
}
int main(int argc, char* argv[])
{
/*
argc是命令行参数的个数, argv字符数组存放每个参数
注意程序名本身也算一个参数,如argv[0] = "compute"
用法 compute num1 + | - | * | / | # | ! [num2]
compute 操作数1 + | - | * | / | # | ! 操作数2
其中
#表示求平方根,操作数2可省
!表示求阶乘,操作数2可省
*/
int n = -1; /*运算符所需操作数个数*/
double result = 0.0; /*结果*/
/*至少需要3个参数,否则输出用法并结束程序*/
if(argc < 3)
usage();
/*第3个参数为运算符,不能多于1个字符,此条件可以不用*/
if(strlen(argv[2]) > 1)
usage();
/*求出所需操作数,不合法时为-1 */
n = opnum(argv[2][0]);
/*如果操作数< 0说明运算符不合法
双目运算符时 第4个参数即操作数2不能省
否则输出用法,并结束程序
*/
if((n < 0) || (n == 2 && argc < 4))
usage();
/*双目运算符
按a + b = c 的形式输出
*/
if(n == 2)
{
result = compute(atof(argv[1]), argv[2][0], atof(argv[3]));
printf("%s %c %s = %f\n", argv[1], argv[2][0], argv[3], result);
}
/*单目运算符
按a ! = c 的形式输出
*/
else
{
result = compute(atof(argv[1]), argv[2][0], 0);
printf("%s %c = %f\n", argv[1], argv[2][0], result);
}
return 0;
}
参考资料:http://z.baidu.com/question/164768676.html
第3个回答 2010-07-05
/*
1. 支持加减乘除。可以扩展,只要修改cal()函数和增加运算符判断。
2. 回车和等于号或者下一个运算符都可以作为求值号,结果可以作为下一个运算对象,也可以不作为运算对象,程序自行判断。
3. 支持容错功能,整数和实数任意可以运算,程序自行判断。
4. 一直在循环接受输入,每一个输入都认为是字符,由程序自己判断为运算数或者运算符号并且智能转化,非法输入会被拒绝,不会显示在终端上
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <math.h>
#include <conio.h>
typedef unsigned char bool_t;
enum bool_value
{
PAL_FALSE = 0,
PAL_TRUE
};
enum operate_value
{
PLUS = 0,
MINUS,
MULTIP,
DIVIDE
};
char* opr_str[] = {"+", "-", "*", "/" };
#define FLOAT 0x1
#define INT 0x2
#define OPR 0x3
#define NONE 0x0
/*去掉输错的字符,重新输出*/
#define BACK_GRACEFUL(s) \
{(s)--; \
*(s) = '\0'; \
system("cls"); \
display_menu(); \
printf("%s", in_str);}
/*判断是否是数字*/
#define IS_DIGIT(x) \
((x) <= '9' && (x) >= '0')
/*判断是否是运算符*/
#define IS_OPR(x) \
(((x) == '+') \
|| ((x) == '-') \
|| ((x) == '*') \
|| ((x) == '/'))
/*判断是否是小数点*/
#define IS_POINT(x) \
((x) == '.')
#define IS_CLS(x) \
((x) == 'c' || (x) == 'C')
#define IS_RTN(x) \
((x) == 'r' || (x) == 'R')
#define IS_EQUAL(x) \
((x) == '=' || (x) == 10 || (x) == 13)
#define CPY_RST \
{ \
memcpy(&pa1, &result, sizeof(result));\
strcpy(str_pa1, in_str); \
result.type = NONE; \
result.u.i = 0; \
}
#define CLS_MEM \
{ \
memset(in_str, 0 , strlen(in_str)); \
memset(str_pa1, 0, strlen(str_pa1));\
memset(str_pa2, 0, strlen(str_pa2));\
ppa1 = str_pa1; \
ppa2 = str_pa2; \
pstr = in_str; \
pa1.type = NONE; \
pa2.type = NONE; \
opr.type = NONE; \
system("cls"); \
display_menu(); \
result.type = NONE; \
result.u.i = 0; \
}
unsigned char2int(char *str)
{
unsigned int len = 0;
int i = 0;
unsigned int rtn = 0;
char max[] = "4294967295";
if(!str)
{
return 0;
}
len = strlen(str);
if(len > 10 || ( len == 10 && strncmp(max, str, 10) < 0))
{
return 0;
}
for(i = 0; i < (int)len; i++)
{
if(str[i] > '9' || str[i] < '0')
{
return 0;
}
}
for(i = 0; i < (int)len; i++)
{
rtn += (str[i] - '0') * (unsigned)pow(10, len - 1 - i);
}
return rtn;
}
int str2int(char *str, unsigned *ret)
{
char constant[11] = "4294967295";
int digit_num = 0;
unsigned res = 0;
if(str == (char*)0)
{
return -1;
}
if(strlen(str) > 10)
{
return -1;
}
else if(strlen(str) == 10)
{
if(strcmp(constant, str) < 0)
{
return -1;
}
}
digit_num = strlen(str);
for(int i = 0; i < digit_num; i++)
{
res += (str[i] - '0') * pow((double)10, (double)(digit_num - i -1));
}
*ret = res;
return 0;
}
int str2float(char *str, float *ret)
{
char *p = NULL;
char ints[10] = {0};
char fs[10] = {0};
unsigned int i = 0;
unsigned int flo = 0;
float f = 0.0;
unsigned len_f = 0;
if(!str)
{
return -1;
}
p = str;
while(*p != 0)
{
if(*p == '.')
{
p++;
continue;
}
if(*p < '0' || *p > '9')
{
return -1;
}
p++;
}
p = strchr(str, '.');
if(!p)
{
strcpy(ints, str);
}
else
{
strncpy(ints, str, p - str);
strcpy(fs, ++p);
len_f = strlen(fs);
}
i = char2int(ints);
flo = char2int(fs);
f = (float)flo / (float)pow(10, len_f);
f += i;
*ret = f;
return 0;
}
void display_menu()
{
printf("\n**********************************************\n");
printf("* *\n");
printf("* 计算器 1.0版 *\n");
printf("* *\n");
printf("* c(清屏) r(返回菜单) *\n");
printf("* *\n");
printf("**********************************************\n");
printf("\n\nCaculator: ");
}
int get_input(char *in)
{
char ch;
ch = getch();
if(IS_DIGIT(ch) ||
IS_OPR(ch) ||
IS_POINT(ch))
{
printf("%c", ch);
}
else if(IS_CLS(ch) ||
IS_RTN(ch) ||
IS_EQUAL(ch))
{
;
}
else
{
return -1;
}
*in = ch;
return 0;
}
struct member
{
unsigned type;
union
{
unsigned i;
unsigned o;
float f;
}u;
};
int cal(struct member *pa1, struct member *opr,
struct member *pa2, struct member *ret)
{
int is_float;
if(!pa1 || !pa2 || !opr || !ret)
{
return -1;
}
if(pa1->type == FLOAT || pa2->type == FLOAT)
is_float = 1;
else
is_float = 0;
switch(opr->u.o)
{
case PLUS:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f += pa1->u.f;
else
ret->u.f += pa1->u.i;
if(pa2->type == FLOAT)
ret->u.f += pa2->u.f;
else
ret->u.f += pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i + pa2->u.i;
}
break;
case MINUS:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f += pa1->u.f;
else
ret->u.f += pa1->u.i;
if(pa2->type == FLOAT)
ret->u.f -= pa2->u.f;
else
ret->u.f -= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i - pa2->u.i;
}
break;
case MULTIP:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f = pa1->u.f;
else
ret->u.f = pa1->u.i;
if(pa2->type == FLOAT)
ret->u.f *= pa2->u.f;
else
ret->u.f *= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i * pa2->u.i;
}
break;
case DIVIDE:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f = pa1->u.f;
else
ret->u.f = pa1->u.i;
if(pa2->type == FLOAT)
ret->u.f /= pa2->u.f;
else
ret->u.f /= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i / pa2->u.i;
}
break;
}
return 0;
}
void caculator()
{
char in_ch;
struct member result;
struct member pa1, pa2;
struct member opr;
char in_str[50] = {0}, *pstr;
char str_pa1[20] = {0}, *ppa1;
char str_pa2[20] = {0}, *ppa2;
CLS_MEM;
while(1)
{
if(0 == get_input(&in_ch))
{
*pstr++ = in_ch;
if(IS_DIGIT(in_ch))
{
if(result.type != NONE)
{
CLS_MEM;
*pstr++ = in_ch;
printf("%s", in_str);
}
if(opr.type != NONE)
{
if(pa2.type != NONE)
{
*ppa2++ = in_ch;
if(pa2.type == INT)
{
if(str2int(str_pa2, &pa2.u.i))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2int(str_pa2, &pa2.u.i);
continue;
}
}
else if(pa2.type == FLOAT)
{
if(str2float(str_pa2, &pa2.u.f))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2float(str_pa2, &pa2.u.f);
continue;
}
}
}
else
{
*ppa2++ = in_ch;
if(str2int(str_pa2, &pa2.u.i))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2int(str_pa2, &pa2.u.i);
continue;
}
pa2.type = INT;
}
}
else if(pa1.type == NONE)
{
pa1.type = INT;
*ppa1++ = in_ch;
if(str2int(str_pa1, &pa1.u.i))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2int(str_pa1, &pa1.u.i);
continue;
}
}
else if(pa1.type == INT)
{
*ppa1++ = in_ch;
if(str2int(str_pa1, &pa1.u.i))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2int(str_pa1, &pa1.u.i);
continue;
}
}
else if(pa1.type == FLOAT)
{
*ppa1++ = in_ch;
if(str2float(str_pa1, &pa1.u.f))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2float(str_pa1, &pa1.u.f);
continue;
}
}
else
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
continue;
}
}
if(IS_OPR(in_ch))
{
if(pa1.type == NONE && result.type != NONE)
CPY_RST;
if(pa1.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}
if(opr.type == NONE)
{
opr.type = OPR;
switch(in_ch)
{
case '+':
opr.u.o = PLUS;
break;
case '-':
opr.u.o = MINUS;
break;
case '*':
opr.u.o = MULTIP;
break;
case '/':
opr.u.o = DIVIDE;
break;
}
continue;
}
else if(pa2.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}
else
{
if(pa2.u.i == 0 && opr.u.o == DIVIDE)
{
BACK_GRACEFUL(pstr);
continue;
}
if(0 == cal(&pa1, &opr, &pa2, &result))
{
opr.type = OPR;
switch(in_ch)
{
case '+':
opr.u.o = PLUS;
break;
case '-':
opr.u.o = MINUS;
break;
case '*':
opr.u.o = MULTIP;
break;
case '/':
opr.u.o = DIVIDE;
break;
}
memset(in_str, 0, strlen(in_str));
memset(str_pa1, 0, strlen(str_pa1));
memset(str_pa2, 0, strlen(str_pa2));
ppa1 = str_pa1;
ppa2 = str_pa2;
pstr = in_str;
pa1.type = NONE;
pa2.type = NONE;
if(result.type == INT)
sprintf(in_str, "%d", result.u.i);
else
sprintf(in_str, "%f", result.u.f);
system("cls");
display_menu();
strcat(in_str, opr_str[opr.u.o]);
pstr = in_str + strlen(in_str);
printf("%s", in_str);
if(pa1.type == NONE && result.type != NONE)
{
CPY_RST;
}
}
}
}
if(IS_POINT(in_ch))
{
if(pa1.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}
if(opr.type != NONE && pa2.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}
if(opr.type == NONE)
{
*ppa1++ = in_ch;
pa1.type = FLOAT;
}
else
{
*ppa2++ = in_ch;
pa2.type = FLOAT;
}
}
if(IS_CLS(in_ch))
{
CLS_MEM;
continue;
}
if(IS_RTN(in_ch))
{
return;
}
if(IS_EQUAL(in_ch))
{
if(pa1.type != NONE && opr.type != NONE
&& pa2.type != NONE)
{
if(pa2.u.i == 0 && opr.u.o == DIVIDE)
{
BACK_GRACEFUL(pstr);
continue;
}
if(0 == cal(&pa1, &opr, &pa2, &result))
{
memset(in_str, 0, strlen(in_str));
memset(str_pa1, 0, strlen(str_pa1));
memset(str_pa2, 0, strlen(str_pa2));
ppa1 = str_pa1;
ppa2 = str_pa2;
pstr = in_str;
pa1.type = NONE;
pa2.type = NONE;
opr.type = NONE;
system("cls");
display_menu();
if(result.type == INT)
sprintf(in_str, "%d", result.u.i);
else
sprintf(in_str, "%f", result.u.f);
pstr = in_str + strlen(in_str);
printf("%s", in_str);
}
}
else
{
BACK_GRACEFUL(pstr);
continue;
}
}
}
}
}
void main()
{
while(1)
{
caculator();
}
}本回答被网友采纳
第4个回答 2010-07-06
//实现计算机功能的程序 a program which can work the functions as a computer.
#include <stdio.h>//头文件
#include <conio.h>
void menu();//声明部分
void add();
void sub();
void mul();
void div();
void remain();
void add_n_to_m();
void factor();
main()
{
int i;
while(1)
{
system("cls");//清屏功能
menu();
printf("choose function:");
scanf("%d",&i);
switch(i)
{
case 1:add();getch();break;//调用部分
case 2:sub();getch();break;
case 3:mul();getch();break;
case 4:div();getch();break;
case 5:remain();getch();break;
case 6:add_n_to_m();getch();break;
case 7:factor();getch();break;
case 8: exit(0);break;
}
}
}
//以下是自己定义的函数
void menu()//菜单
{
printf("+====my counter===+\n");
printf("+功能如下: +\n");
printf("+ 1.加法 +\n");
printf("+ 2.减法 +\n");
printf("+ 3.乘法 +\n");
printf("+ 4.除法 +\n");
printf("+ 5.求余 +\n");
printf("+ 6.从n到m的和+\n");
printf("+ 7.阶乘 +\n");
printf("+ 8.退出 +\n");
printf("+=================+\n");
}
void add()//加法运算
{
double a,b;
printf("input two numbers:");
scanf("%lf%lf",&a,&b);
printf("%lf+%lf=%lf\n",a,b,a+b);
}
void sub()//减法运算
{
double a,b;
printf("input two numbers:");
scanf("%lf%lf",&a,&b);
printf("%lf-%lf=%lf\n",a,b,a-b);
}
void mul()//乘法运算
{
double a,b;
printf("input two numbers:");
scanf("%lf%lf",&a,&b);
printf("%lf*%lf=%lf\n",a,b,a*b);
}
void div()//除法运算
{
double a,b;
printf("input two numbers:");
scanf("%lf%lf",&a,&b);
if(b==0)//被除数不能为0
printf("error\n");
else
printf("%lf/%lf=%lf\n",a,b,a/b);
}
void remain()//求余运算
{
int a,b;
printf("input two numbers:");
scanf("%d%d",&a,&b);
if(b==0)//被除数不能为0
printf("error\n");
else
printf("%d%%%d=%d\n",a,b,a%b);
}
void add_n_to_m()//累加
{
int m,n,i=0,s=0;
printf("input n and m:");
scanf("%d%d",&n,&m);
if(n>m) printf("error.\n");//条件限制
else
{
for(i=n;i<=m;i++)
s+=i;
printf("%d+...+%d=%d\n",n,m,s);
}
}
void factor()//阶乘
{
int n,i,s;
printf("input a number:");
scanf("%d",&n);
if(n<=0) printf("error.\n");//条件限制
else
{
for(i=1,s=1;i<=n;i++)
s*=i;
printf("%d!=%d\n",n,s);
}