怎样用c语言编一个简单的计算器?

作者&投稿:茅柄 (若有异议请与网页底部的电邮联系)
怎么用C语言设计一个简单计算器?~

#include void add(int a,int b,int c) { c=a+b; printf("%d",c); printf("
"); } void minus(int a,int b,int c) { c=a-b; printf("%d",c); printf("
"); } void multiplication(int a,int b,int c) { c=a*b; printf("%d",c); printf("
"); } void div(int a,int b,int c) { c=(float)a/(float)b; printf("%f",c); printf("
"); } main() { int a,b,c; char p; puts("input A:
"); scanf("%d",&a); puts("input B:
"); scanf("%d",&b); puts("input operation:
"); getchar(); p=getchar(); if(p=='+') add(a,b,c);else if(p=='-') minus(a,b,c);else if(p=='*') multiplication(a,b,c);else if(p=='/') div(a,b,c);else puts("没有注册这个运算符号
"); }以上是设计的一个简易计算器。可以进行相应的加减乘除。
简介:C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点。它由美国贝尔研究所的D.M.Ritchie于1972年推出,1978年后,C语言已先后被移植到大、中、小及微型机上,它可以作为工作系统设计语言,编写系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。它的应用范围广泛,具备很强的数据处理能力,不仅仅是在软件开发上,而且各类科研都需要用到C语言,适于编写系统软件,三维,二维图形和动画,具体应用比如单片机以及嵌入式系统开发。

#include
int main() {
double num1 = 0; //输入1
double num2 = 0; //输入2
char ch; //操作
double ret = 0; //结果 printf( "输入第一个数:" );
scanf( "%lf", &num1 );
printf( "输入第二个数:" );
scanf( "%lf", &num2 );
printf( "操作[+ - * /]:" );
getchar();
scanf( "%c", &ch ); switch( ch ) {
case '+':
ret = num1 + num2;
break;
case '-':
ret = num1 - num2;
break;
case '*':
ret = num1 * num2;
break;
case '/':
ret = num1 / num2;
break;
default:
break;
}
printf( "结果:%.2lf
", ret ); return 0;
} 写个简单易懂的,你操作计算器的步骤就是编写程序的思路呀

//简单计算器,含加减乘除、乘方运算。
#include
#include
#include // malloc()等
#include // INT_MAX等
#include // EOF(=^Z或F6),NULL
#include // atoi()
#include // eof()
#include // floor(),ceil(),abs()
#include // exit()
#include // cout,cin
// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
// #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int Boolean; // Boolean是布尔类型,其值是TRUE或FALSE

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

//***************************************************************************
//栈的储存结构

typedef struct{
//运算符栈
char *base;
char *top;
int stacksize;
}SqStack1;

typedef struct{
//运算数栈
float *base;
float *top;
int stacksize;
}SqStack2;

//***************************************************************************
//以下是运算符栈的基本操作函数

Status InitStack(SqStack1 &S){
//初始化一个栈
S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack

Status DestroyStack(SqStack1 &S){
//销毁栈S
free(S.top);
free(S.base);
return OK;
}//DestroyStack

char GetTop(SqStack1 S){
//若栈不空,则返回S的栈顶元素,并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
return *(S.top-1);
}//Gettop

Status Push(SqStack1 &S,char e){
//插入元素e为新的栈顶元素
if(S.top-S.base>=S.stacksize){
//栈满,追加储存空间
S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//Push

Status Pop(SqStack1 &S,char &e){
//若栈不空,则删除S的栈顶元素,用e返回其值;并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
e=*(--S.top);
return OK;
}//Pop

//***************************************************************************
//以下是运算数栈的基本操作函数

Status InitStack(SqStack2 &S){
//初始化一个栈
S.base=(float *)malloc(STACK_INIT_SIZE*sizeof(float));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//InitStack

Status DestroyStack(SqStack2 &S){
//销毁栈S
free(S.top);
free(S.base);
return OK;
}//DestroyStack

float GetTop(SqStack2 S){
//若栈不空,则返回S的栈顶元素,并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
return *(S.top-1);
}//Gettop

Status Push(SqStack2 &S,float e){
//插入元素e为新的栈顶元素
if(S.top-S.base>=S.stacksize){
//栈满,追加储存空间
S.base=(float *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(float));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//Push

Status Pop(SqStack2 &S,float &e){
//若栈不空,则删除S的栈顶元素,用e返回其值;并返回OK;否则返回ERROR
if(S.top==S.base)return ERROR;
e=*(--S.top);
return OK;
}//Pop

//***************************************************************************
//以下是相关的运算符判断函数

char Precede(char A,char B){
//比较运算符A, B的优先关系,A,B的范围仅限于'+','-','*','/','^','(',')','='
//返回'>','<','='
switch(A){
case '+':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '-':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '*':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '/':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '<';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '^':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '>';
case '(':return '<';
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '(':switch(B){
case '+':return '<';
case '-':return '<';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':return '=';
case '=':printf("表达式错误!\n");exit(0);
default:printf("表达式错误!\n");exit(0);
}
case ')':switch(B){
case '+':return '>';
case '-':return '>';
case '*':return '>';
case '/':return '>';
case '^':return '>';
case '(':printf("表达式错误!\n");exit(0);
case ')':return '>';
case '=':return '>';
default:printf("表达式错误!\n");exit(0);
}
case '=':switch(B){
case '+':return '<';
case '-':return '<';
case '*':return '<';
case '/':return '<';
case '^':return '<';
case '(':return '<';
case ')':printf("表达式错误!\n");exit(0);
case '=':return '=';
default:printf("表达式错误!\n");exit(0);
}
default:printf("表达式错误!\n");exit(0);
}
}//Precede

Status InOP(char c){
//判断c是否是运算符,是则返回TRUE,否则返回FALSE
switch(c){
case '+':return TRUE;
case '-':return TRUE;
case '*':return TRUE;
case '/':return TRUE;
case '^':return TRUE;
case '(':return TRUE;
case ')':return TRUE;
case '=':return TRUE;
default:return FALSE;
}
}//InOP

//***************************************************************************

float Operate(float a,char theta,float b){
switch(theta){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':
if(b==0){
printf("分母不能为0!\n");
exit(0);
}
else return a/b;
case '^':
if(a==0&&b<=0){
printf("0的指数必须大于0!\n");
exit(0);
}
else return (float)pow(a,b);
default:printf("表达式错误!\n");exit(0);
}
}//Operate

Status EvaluateExpression(){
//算术表达式求值
char c,x,theta,prec;
//c是每次读取的字符,x是存放脱括号后的多余的括号,theta是运算符,prec是c的前一个字符
float a,b,result;//a、b是每次从运算数栈中取出的要进行运算的数,result存放最终结果
float cc,flag,ii,minus=1;
//cc存放由字符串转化而来的浮点数,flag用于标记是否已读取过小数点,
//ii存放小数部分需要缩小的倍数,minus用于记录该数前是否有负号
SqStack1 OPTR;
SqStack2 OPND;
InitStack(OPTR);InitStack(OPND);
Push(OPTR,'=');
prec='=';scanf("%c",&c);
while(c!='='||GetTop(OPTR)!='='){
cc=0;flag=0;ii=10;
if(c=='-'&&(prec=='='||prec=='(')){minus=-1;prec=c;scanf("%c",&c);}
//若某“-”前面是“=”(第一个符号就是“-”)或“(”,则此为负号,不是减号
else if(!InOP(c)){
while(!InOP(c)){
if(c>=48&&c<=57){
if(flag==0)cc=cc*10+c-48;//小数点之前
else if(flag==1){cc=cc+(c-48)/ii;ii*=10;}//小数点之后
else {printf("小数点错误!\n");exit(0);}//小数点有错
}
else if(c=='.')flag++;//读到小数点
else {printf("表达式错误!\n");exit(0);}
prec=c;scanf("%c",&c);
}
cc*=minus;minus=1;
Push(OPND,cc);
}//不是运算符则进OPND栈
else
switch(Precede(GetTop(OPTR),c)){
case '<':Push(OPTR,c);prec=c;scanf("%c",&c);break;//栈顶元素优先级低
case '=':Pop(OPTR,x);prec=c;scanf("%c",&c);break;//脱括号并接收下一字符
case '>'://退栈并将运算结果入栈
Pop(OPTR,theta);
Pop(OPND,b);Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
}
}
result=GetTop(OPND);
printf("%f\n",result);
//DestroyStack(OPTR);
//DestroyStack(OPND);
return OK;
}//EvaluateExpression

void main(){
printf(" **********************\n");
printf(" * 欢迎使用计算器! *\n");
printf(" **********************\n");
printf("请输入表达式,以“=”结束:\n");
printf("(支持实数间的加(+)、减(-)、乘(*)、除(/)、乘方(^)、单目减(-)运算)\n");
EvaluateExpression();
exit (0);
}

#include
<stdio.h>
#include
<stdlib.h>
void
min()
{
float
a,b;
char
op;
printf("输入第一个数:");
scanf("%f",&a);
fflush(stdin);
printf("输入一个运算符+-*
/:");
scanf("%c",&op);
fflush(stdin);
printf("输入第二个数:");
scanf("%f",&b);
fflush(stdin);
switch(op)
{
case
'+':
a=a+b;
break;
case
'-':
a=a-b;
break;
case
'*':
a=a*b;
break;
case
'/':
a=a/b;
break;
default:
printf("+-/*!!!error");
return;
}
printf
("%f\n",a);
return
;
}
int
main()
{
char
ch;
printf("请输入选择\na开始运算c清屏x退出:");
ch=getchar();
while(ch!='x')
{
min();
ch=getchar();
if(ch=='c')
system("cls");
}
return
0;
}

#include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <stdio.h>
#include <process.h>
#define EXCAPE 27
#define ENTER 13
main(){
int press,i,x,y,x1,y1,ch_z=0;
int dian=0;
char ch='0'; /*input + - * / */
char emp[80],sum[80],*e,*s;
double yuan=0.000000000000;
void init(void);
void clear_z(char *u);
double strtoflt(char *p);
int getkey();
int gd=DETECT, gm;
initgraph(&gd, &gm, "");
e=emp;
s=sum;
init();
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
while(1){
press = getkey();
switch(press){
case EXCAPE:
exit(0);
case 47:
bar (x + 10, y + 80 + 10, x + 60 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
if (strtoflt(emp)==0.0){
ch='0';
ch_z=0;
dian=0;
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
outtextxy(x+30,y+40,"error!!!!!");
break;
}
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);

}
else{
outtextxy(x+30,y+40,sum);

}
}
ch='/';
ch_z=0;
emp[0]='\0';
e=emp;
dian=0;
break;
case 42:
bar (x + 60 + 10, y + 80 + 10, x + 60 * 2 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='*';
ch_z=0;
dian=0;
break;
case 45:
bar (x + 60 * 2 + 10, y + 80 + 10, x + 60 * 3 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='-';
ch_z=0;
dian=0;
break;
case 43:
bar (x + 60 * 3 + 10, y + 80 + 10, x + 60 * 4 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='+';
ch_z=0;
dian=0;
break;
case 49:
bar (x + 10, y + 80 + 53 + 10, x + 60 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='1';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 50:
bar (x + 60 + 10, y + 80 + 53 + 10, x + 60 * 2 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='2';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 51:
bar (x + 60 * 2 + 10, y + 80 + 53 + 10, x + 60 * 3 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='3';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case ENTER:
bar (x + 60 * 3 + 10, y + 80 + 53 + 10, x + 60 * 4 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else{
outtextxy(x+30,y+40,sum);
}
}
ch='0';
ch_z=1;
dian=0;
break;
case 52:
bar (x + 10, y + 80 + 53 * 2 + 10, x + 60 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='4';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 53:
bar (x + 60 + 10, y + 80 + 53 * 2 + 10, x + 60 * 2 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='5';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 54:
bar (x + 60 * 2 +10, y + 80 + 53 * 2 + 10, x + 60 * 3 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='6';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 46:
bar (x + 60 * 3 + 10, y + 80 + 53 * 2 + 10, x + 60 * 4 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (dian==0){
if (ch_z==0){
*e='.';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
}
else{
if (ch_z==0)
outtextxy(x+30,y+40,emp);
else
outtextxy(x+30,y+40,sum);
}
dian=1;
break;
case 55:
bar (x + 10, y + 80 + 53 * 3 + 10, x + 60 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='7';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 56:
bar (x + 60 + 10, y + 80 + 53 * 3 + 10, x + 60 * 2 -10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='8';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 57:
bar (x + 60 * 2 + 10, y + 80 + 53 * 3 + 10, x + 60 * 3 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='9';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 48:
bar (x + 60 * 3 + 10, y + 80 + 53 * 3 + 10, x + 60 * 4 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='0';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 32:
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
ch='0';
ch_z=0;
dian=0;
init();
break;
case 8:
delay(8000);
for(i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (i==0)
break;
if (i!=79&&i!=0){
i--;
emp[i]='\0';
e=&emp[i];
}
init();
outtextxy(x+30,y+40,emp);
break;
}
}
}
/*---------------------------------------------------------------------*/
void init(void){
int x, y, x1, y1, i, j;
char emp;
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
cleardevice();
setbkcolor(3);
setfillstyle(1, 15);
setcolor(15);
settextstyle(1,0,1);
rectangle (x, y, x1, y1);
rectangle (x - 7, y - 7, x1 + 7, y1 + 7);
rectangle (x + 10, y + 10, x1 - 10, y + 80 - 10);
line (x, y + 80, x1, y + 80);
y = y + 80;
for (j = 1; j <= 4; j++){
x = (getmaxx() / 2) - 120;
for (i = 1; i <= 4; i++){
/* bar (x + 10, y + 10, x + 60 - 10, y + 60 - 10);*/
rectangle(x + 10, y + 10, x + 60 - 10, y + 60 - 10);
if (j == 1){
if (i == 1)
outtextxy(x + 20, y + 20, "/");
if (i == 2)
outtextxy(x + 25, y + 20, "*");
if (i == 3)
outtextxy(x + 27, y + 20, "-");
if (i == 4)
outtextxy(x + 25, y + 20, "+");
}
if (j == 2){
if (i == 1)
outtextxy(x + 25, y + 20, "1");
if (i == 2)
outtextxy(x + 25, y + 20, "2");
if (i == 3)
outtextxy(x + 25, y + 20, "3");
if (i == 4)
outtextxy(x + 25, y + 20, "=");
}
if (j == 3){
if (i == 1)
outtextxy(x + 25, y + 20, "4");
if (i == 2)
outtextxy(x + 25, y + 20, "5");
if (i == 3)
outtextxy(x + 25, y + 20, "6");
if (i == 4)
outtextxy(x + 25, y + 20, ".");
}
if (j == 4){
if (i == 1)
outtextxy(x + 25, y + 20, "7");
if (i == 2)
outtextxy(x + 25, y + 20, "8");
if (i == 3)
outtextxy(x + 25, y + 20, "9");
if (i == 4)
outtextxy(x + 25, y + 20, "0");
}
x = x + 60;
}
y = y + 53;
}
}
/*---------------------------------------------------------------------*/
int getkey(){
char lowbyte;
int press;
while(bioskey(1)==0);
press = bioskey(0);
press = press&0xff? press&0xff: press>>8;

return(press);

}
double strtoflt(char *p)
{
double rtl=0.000000000000;
double pnt=0.000000000000;
double t = 10;
int ispoint = 0;
while (*p!='\0'||*p!='.'){
if(*p<'0'||*p>'9')
break;
rtl*=10;
rtl+=*p-'0';
p++;
}
if (*p=='.'){
ispoint=1;
p++;
}
while(ispoint&&*p!='\0'){
pnt+=(double)(*p-'0')/t;
t*=10;
p++;
}
rtl+=pnt;
return (rtl);
}
/*-----------------------------------------------------------------------*/
void clear_z(char u[]){
int i;
for(i=strlen(u)-1;i>=0;i--){
if (u[i]!='0')
break;
}
if (u[i]=='.'){
u[i]='\0';
}
else{
i++;
u[i]='\0';
}
}


用C语言做一个计算器,能实现加减乘除混合运算?
是的,可以使用C语言编写一个计算器程序,能够实现加、减、乘、除等混合运算。下面是一个简单的示例程序:```c include <stdio.h> int main() { char operator;double num1, num2, result;printf("Enter an operator (+, -, *, \/): ");scanf("%c", &operator);printf("Enter two ...

C语言一道简单的数组编程题 编写一个程序,把1到1000放入数组中,并把能...
include<stdio.h> main(){ int i, num[1024], cot = 0;for(i = 1; i <= 1000; i++){ num[i - 1] = i;\/\/存储 if(i % 7 == 0 || i % 11 == 0){ if(i % 77 != 0){ cot++;printf("%d ", i);if(cot == 5){ puts("");cot = 0;} } } } } ...

用c语言编一个程序:从任意10个数中任选4个数并全输出?
include <stdlib.h> include int main(){ int num[10] = {1,2,3,4,5,6,7,8,9,10}, i;srand((unsigned)time(0));printf("从原数组: \\n");for(i = 0; i < sizeof num\/sizeof num[0]; ++i)printf("%d ", num[i]);printf("\\n中随机选出的4个数为: ");for(i =...

C语言编写一个程序,把560分钟换算成用小时和分钟表示,然后输出
一分钟读懂.c语言程序设计

怎么用C语言编写一个程序,输入三个数值,然后输出其中最大者。
有很多方法,初学的话不必用数组,用三个变量比较清晰:include<stdio.h> int main(){ int a,b,c;printf("输入第一个数:");scanf("%d",&a);printf("输入第二个数:");scanf("%d",&b);printf("输入第三个数:");scanf("%d",&c);if(a<b) \/\/比较前两个数,如果第二个数大,就把...

C语言编写一个程序,实现如下功能:从键盘输入一个三位数,求各位数字之...
include "stdio.h"void main(){ int n,sum=0;printf("请输入一个三位数:");scanf("%d",&n);sum=n\/100+n%100\/10+n%10;\/\/百位数+十位数+个位数 printf("这个三位数各位数字之和是%d\\n",sum);} 结果:

c语言编程 求解简单表达式,输入一个形式如“操作数 运算符 操作数”的...
有没有括号的,有的话很烦喔,要用栈来写,没的话一个switch函数和一个循环函数就可以搞定了。用getchar()函数一个个读入所有字符,然后操作数的话用ASCII换成十进制的,switch里面分别就是+-*\/四个运算,然后循环两次4个数读完就printf()出来,思路够清晰了吧,代码自己实践。。。无法实现我再...

C语言:从键盘输入一个人简单的数学表达式,计算并显示该表达式的值_百度...
int num1; \/\/存放第一个操作数值 int num2; \/\/存放第二个操作数值 double result0; \/\/存放运算结果 char current_char;int i = 0;printf("请输入简单表达式!\\n");scanf("%c", ¤t_char);while (current_char >= '0' && current_char <= '9'){\/\/输入第一个变量 va...

用C语言编写一个抽牌游戏,游戏规则为总共21张牌,人先抽牌机器后抽,每次...
\/\/标准输入输出头文件int main() \/\/主函数 {int n=21,a,b,i; \/\/n总数,a人抽,b电脑抽,i第几轮 for(i=1;n>=0;i++) \/\/用一个循环,使游戏可以一直进行下去 {printf("\\n第%d轮,还剩%d张牌。",i,n); \/\/显示第几轮和剩几张 do{printf("\\n你想抽几张(1...

用c语言编写一个计算两个日期之间相差天数的小程序。语法尽量简单...
dayArr[month]+1 : dayArr[month] ;}\/\/获取两个日期之间的天数,第一个日期必须比第二个日期小 :) int getTotalDay(int year1,int month1,int day1,int year2,int month2,int day2){long total = 0;for(int year = year1; year < year2 ; year ++ ){total = total + 365;...

丹凤县13417452581: 用C语言编写一个简单的计算器1 -
欧闹蛾贞: #include<stdio.h> int main() {double num1 = 0; //输入1double num2 = 0; //输入2char ch; //操作double ret = 0; //结果 printf( "输入第一个数:" );scanf( "%lf", &num1 );printf( "输入第二个数:" );scanf( "%lf", &num2 ); ...

丹凤县13417452581: c语言程序编写(初学)(1) 编写一个简单计算器程序,输入格式为:data1 op data2.其中data1和data2是参加运算的两个数,op为运算符,其取值只能是+... -
欧闹蛾贞:[答案] #includemain(){ char op; float result,data1,data2; float OP(float x,float y,char t); printf("input op(+,-,*,/):\n",op); scanf("%c",&op); printf("input data1:\n",data1); scanf("%f",&data1); pr...

丹凤县13417452581: 怎么用C语言编写一个计算器程序? -
欧闹蛾贞: #include<stdio.h> #include<math.h> main() { float a,b; char c; printf("please input the expression:"); scanf("%f%c%f",&a,&c,&b); swich(c) { case'+': printf("%f",a+b); case'-': printf("%f",a-b); case'*': printf("%f",a*b); case'/': if(b==0)printf("wrong!"); elseprintf("%f",a/b); break; default: printf("wrong!"); } }

丹凤县13417452581: 怎样用C语言编个简单的计算器 ?
欧闹蛾贞: #include <stdio.h> #include <stdlib.h> void min() { float a,b; char op; printf("输入第一个数:"); scanf("%f",&a); fflush(stdin); printf("输入一个运算符+-* /:"); scanf("%c",&op); fflush(stdin); printf("输入第二个数:"); scanf("...

丹凤县13417452581: C语言实现的简易计算器 -
欧闹蛾贞: 展开全部#include void main() { float a,b,i=0;char yun;mama: printf("\n请输入运算符和要计算的两个数:"); main:if(i<3) {fflush(stdin); scanf("%c%f%f",&yun,&a,&b);switch(yun) { case '+':printf("%.2f+%.2f=%.2f",a,b,a+b); break; case '-':...

丹凤县13417452581: C语言的简单计算器怎么做 -
欧闹蛾贞:#include int main(void){long a,b;long max;char c;printf("请输入a,b的数值.\n");scanf("%ld%c%ld",&a,&c,&b);switch (c) {case '+':max=a+b;break;case '-':max=a-b;break;case '*':max=a*b;break;case '/':max=(float)...

丹凤县13417452581: 用C语言编写一个简单的计算器 -
欧闹蛾贞: #include int main() { double num1 = 0; //输入1 double num2 = 0; //输入2 char ch; //操作 double ret = 0; //结果 printf( "输入第一个数:" ); scanf( "%lf", &num1 ); printf( "输入第二个数:" ); scanf( "%lf", &num2 ); printf( "操...

丹凤县13417452581: c语言设计一个简单的计算器程序
欧闹蛾贞: /* 2013年12月23日 12:43:46 目的:计算器的实现 */ # include <stdio.h> # include <ctype.h> # include <math.h> char get_choice(void); //获取用户输入的选项,并建立目 char get_first(void); //获取用户输入的选项,并剔除错误输入 float get_int(...

丹凤县13417452581: 用C语言写一个简单的计算器 -
欧闹蛾贞: #includeint do_Calculate(int a[],char b[],int j,int k) { int m=0,n=0; int result=a[m++]; while(j--&&k--) { switch(b[n++]) { case '*':result=result*a[m++];break; case '/':result=result/a[m++];break; case '+':result=result+a[m++];break; case '-':result=result-a[m++];...

丹凤县13417452581: 如何使用C语言做一个简单的计算器 -
欧闹蛾贞: #include <iostream> using namespace std; int main() { int a,b; char c; cin>>a; cin>>c; cin>>b; switch(c) { case '+':printf("%d+%d=%d\n",a,b,a+b);break; case '-':printf("%d-%d=%d\n",a,b,a-b);break; case '*':printf("%d*%d=%d\n",a,b,a*b);...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网