① 怎樣運用c語言調用電腦里的計算器
電腦計算器:你具體指的是什麼?
如果是電腦系統自帶的計算器軟體的話,以xp系統,附件中的計算器為例:
1、計算器-》菜單-》查看->選中:科學型
2、選中:十六進制單選框
3、輸入:cd(h是十六進制的表示符,不用輸入,也輸入不了)
4、選中:十進制單選框
原輸入cd的地方就變成205。十六進制自動轉化為對應的十進制了。
② 用c語言實現科學計算器要求有計算器界面 可以加減乘除平方開方
#include "stdafx.h"
#define STACK_INIT_SIZE 10
#define STACKINCRESIZE 5
typedef struct
{
SElemType *base;
SElemType *top;
int stack_size;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stack_size=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
if((S.top-S.base)>=STACK_INIT_SIZE){
S.base=(ElemType*)realloc(S.base,(S.stack_size+STACKINCRESIZE)*(sizeof(SqStack)));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stack_size;
S.stack_size+=STACK_INIT_SIZE;
}
*(S.top)++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return ERROR;
e=*--S.top;
return OK;
}
Status StackEmpty(SqStack &S)
{
if(S.top==S.base)
return TRUE;
else return FALSE;
}
int GetTop(SqStack S,ElemType &e)
{
if(S.top >S.base )
{
e=*(S.top-1) ;
return OK;
}
else return ERROR;
}
/*#include "stdafx.h"
#include "MyStack.h"
int InitStack(SqStack &S)
{
S.base =(ElemType *)malloc(STACKSIZE*sizeof(ElemType));
S.top =S.base ;
S.stacksize =STACKSIZE;
return OK;
}
int GetTop(SqStack S,ElemType &e)
{
if(S.top >S.base )
{
e=*(S.top-1) ;
return OK;
}
else return ERROR;
}
int Push(SqStack &S,Elemtype e)
{
if(S.top -S.base >=S.stacksize )
{
S.base=(ElemType *)realloc(S.base ,(S.stacksize +ADDSIZE)sizeof(ElemType));
S.top =S.stacksize +S.base ;
S.stacksize =S.stacksize +ADDSIZE;
}
*(S.top)++=e;
return OK;
}
int Pop(SqStack &S,ElemType &e)
{
if(S.base !=S.top )
{
e=*(--S.top );
return OK;
}
else return ERROR;
}*/
#include "stdafx.h"
#include "stdafx.h"
#include "MyStack.h"
//#include "MyStack.cpp"
ElemType Precede(ElemType t1,ElemType t2)
{
ElemType f='0';
switch(t2)
{
case '+':
case '-':
if(t1=='('||t1=='=')
f='<';
else f='>';
break;
case '*':
case '/':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else f='<';
break;
case '(':
if(t1==')')
{
cout<<"ERROR"<<endl;
exit(ERROR);
}
else f='<';
break;
case ')':switch(t1)
{
case '(':f='=';
break;
case '=':printf("ERROR2\n");
exit(ERROR);
default: f='>';
}
break;
case'=':switch(t1)
{
case '=':f='=';
break;
case '(':cout<<"ERROR"<<endl;
default:f='>';
}
break;
}
return f;
}
int In(ElemType e)
{
switch(e)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'=':return TRUE;
default:return FALSE;
}
}
ElemType Operate(ElemType a,ElemType theta,ElemType b)
{
ElemType re=0;
switch(theta)
{
case'+':re=a+b;
break;
case'-':re=a-b;
break;
case'*':re=a*b;
break;
case'/':re=a/b;
break;
}
return re;
}
ElemType EvaluateExpression()
{
ElemType x,a,b,theta,d;
char c;
char z[6];
SqStack OPTR,OPND;
InitStack(OPTR);Push(OPTR,'=');
InitStack(OPND);
c=getchar();
GetTop(OPTR,x);
while(c!='='||x!='=')
{
if(In(c)) // 是7種運算符之一
switch(Precede(x,c))
{
case'<':Push(OPTR,c); // 棧頂元素優先權低
c=getchar();
break;
case'=':Pop(OPTR,x); // 脫括弧並接收下一字元
c=getchar();
break;
case'>':Pop(OPTR,theta); // 退棧並將運算結果入棧
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
}
else if(c>='0'&&c<='9') // c是操作數
{
int i=0;
do
{
z[i]=c;
i++;
c=getchar();
}while(c>='0'&&c<='9');
z[i]=0;
d=atoi(z);
Push(OPND,d);
}
else // c是非法字元
{
printf("ERROR4\n");
exit(ERROR);
}
GetTop(OPTR,x);
}
GetTop(OPND,x);
return x;
}
// Calculator.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include "Calculator.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("請輸入算術表達式,負數要用(0-正數)表示,並以=結束\n");
printf("%d\n",EvaluateExpression());
getchar();
system("pause");
return 0;
}
③ 怎樣用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器
用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器的方法:
1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;
④ 如何用C語言設計一個科學計算器
搞兩個棧, 一個放數據,一個放運算符
然後按照優先順序 就可以搞了
⑤ 科學計算器如何用C程序……
#include <iostream.h>
#include <string>
using namespace std;
/**************Simple Calculator Code*************************
This program is abstracted from <C++ Programming Language>
all the ideas belong to original author
you can study from it
how to use ?
input a line of expression which is ended with ";"
Examples:
you put :
3+(5+6)*(-1+8);
the reult :
80
and you can continue ...
***********************************************************/
enum Token_value{
NAME, NUMBER, END,
PLUS = '+', MINUS = '-',MUL = '*',DIV = '/',
PRINT = ';',ASSIGN = '=',LP = '(',RP = ')'
};
Token_value curr_tok = PRINT;
// fuction list//////////////////////////////////////
double expr(bool get);
double term(bool get);
double prim(bool get);
Token_value get_token();
double error(const string &s);
//////////////////////////////////////////////////////////
double expr(bool get) //plus and minus
{
double left = term(get);
for(;;)
{
switch(curr_tok){
case PLUS:
left+=term(true);
break;
case MINUS:
left-=term(true);
break;
default:
return left;
}
}
}
double term(bool get) // multiply and divide
{
double left = prim(get);
for(;;)
{
switch(curr_tok){
case MUL:
left*=prim(true);
break;
case DIV:
if(double d = prim(true)){ // no zero!!
left/= d;
break;
}
return error("divide by zero!!\n");
default :
return left;
}
}
}
double number_value;
double string_value;
double prim(bool get)
{
if(get) get_token();
switch(curr_tok){
case NUMBER:
{
double v = number_value;
get_token();
return v;
}
case NAME:
{
double v;
//double &v = table[string_value];
//this table reserved the name mapped with variable
//now we don't use it!
if(get_token()==ASSIGN)
v = expr(true);
return v;
}
case MINUS: // negative
{
return -prim(true);
}
case LP:
{
double e = expr(true);
if(curr_tok!=RP)return error(")expected");
get_token(); // absorb )
return e;
}
default:
return error("primary expected"); // no primary
}
}
Token_value get_token()
{
char ch =0;
cin>>ch;
switch(ch){
case 0:
return curr_tok=END; //return and assignment
case ';':
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok = Token_value(ch);
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
case '.':
cin.putback(ch);
cin>>number_value;
return curr_tok = NUMBER;
default:
if(isalpha(ch))
{
cin.putback(ch);
cin>>string_value;
return curr_tok = NAME;
}
error("bad token!");
return curr_tok = PRINT;
}
}
int no_of_error;
double error(const string &s)
{
no_of_error++;
cout<<"error:"<<s.data()<<"\n";
return 1;
}
int main()
{
while(cin)
{
get_token();
if(curr_tok==END)break;
if(curr_tok==PRINT) continue;
cout<<expr(false)<<'\n';
}
return no_of_error;
}
不知道為什麼,在VC下的漢字在這里是亂碼,沒辦法,程序里我只能用English
程序的主要思想:
利用自頂向下的分析方法:(編譯原理)
E= E(+|-)T
T =T(*|/)P
P = i|(E)
不用定義太復雜的數據結構,我是從Bjarne Stroustrup那本書上的代碼稍做修改寫的這個,給你一個參考。
⑥ 用C語言編一個科學計算器的程序
需要詞法分析、語法分析,還不夠你麻煩的
要想計算表達式最簡單的用EXCEL,如果你實在喜歡編程序就直接用記事本寫一個.vbs程序運行就是:
wscript.echo (9+4)*5*sin(3+7)
執行,得到
P:\>notepad abc.vbs
P:\>abc.vbs
P:\>cscript abc.vbs
Microsoft (R) Windows Script Host Version 5.7
版權所有(C) Microsoft Corporation 1996-2001。保留所有權利。
-35.361372207809
⑦ 用C語言做一個計算器,能實現加減乘除混合運算
用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器的方法:
1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;
⑧ c語言編寫簡單的科學計算器
/*/////////////////////////////////////////////////////////////
* 功能:實現一個計算器程序
* 作者:
* 時間:
*////////////////////////////////////////////////////////////*/
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;
enum types { DELIMITER = 1, VARIABLE, NUMBER};//定義枚舉類型把DELIMITER初試化為1後面
//的相應的是前面值+1
class Info
{
public:
void displayInfo();
};
class parser
{
char *exp_ptr; // 定義一個指向表達式的指針
char token[80]; // 存儲表達式中的字元變數
char tok_type; // 存儲具體是什麼類型
void eval_exp2(double &result);
void eval_exp3(double &result);
void eval_exp4(double &result);
void eval_exp5(double &result);
void eval_exp6(double &result);
void atom(double &result);
void get_token();
void serror(int error);
int isdelim(char c);
public:
parser();//類的構照函數
double eval_exp(char *exp);
};
void Info::displayInfo()
{
cout<<"==============================================================\n";
cout<<"====================這是一個計算器程序========================\n";
cout<<"==============================================================\n";
cout<<"* 說明:可以進行+ - * \ % ^ 操作您還可以用括弧輸入您的表達式 *\n";
cout<<"* 您輸入表達式不需要輸=號 例:您可輸入(6+7)*5\\7+4 然後回車 *\n";
cout<<"==============================================================\n\n";
cout << "\t\t輸入點號(.)回車,結束程序運行\n\n";
}
// 類構造函數的具體實現
parser::parser()
{
exp_ptr = NULL;//把exp_ptr的指針初始化為空
}
// 方法eval_exp的具體實現過程
double parser::eval_exp(char *exp)
{
double result;
exp_ptr = exp;//把傳遞過來的參數exp賦給exp_ptr
get_token();
if(!*token)
{
serror(2); //輸出沒有任何錶達式的錯誤信息
return 0.0;
}
eval_exp2(result);
if(*token) serror(0); // last token must be null
return result;
}
// 兩個變數字元是加或者減操作
void parser::eval_exp2(double &result)
{
register char op;
double temp;
eval_exp3(result);
while((op = *token) == '+' || op == '-')
{
get_token();
eval_exp3(temp);
switch(op)
{
case '-':
result = result - temp;
break;
case '+':
result = result + temp;
break;
}
}
}
// 兩個變數是乘或者除操作 或者是取余
void parser::eval_exp3(double &result)
{
register char op;
double temp;
eval_exp4(result);
while((op = *token) == '*' || op == '/' || op == '%')
{
get_token();
eval_exp4(temp);
switch(op)
{
case '*':
result = result * temp;
break;
case '/':
result = result / temp;
break;
case '%':
result = (int) result % (int) temp;
break;
}
}
}
// 進行^運算
void parser::eval_exp4(double &result)
{
double temp, ex;
register int t;
eval_exp5(result);
if(*token== '^')
{
get_token();
eval_exp4(temp);
ex = result;
if(temp==0.0)
{
result = 1.0;
return;
}
for(t=(int)temp-1; t>0; --t) result = result * (double)ex;
}
}
void parser::eval_exp5(double &result)
{
register char op;
op = 0;
if((tok_type == DELIMITER) && *token=='+' || *token == '-')
{
op = *token;
get_token();
}
eval_exp6(result);
if(op=='-') result = -result;
}
// 進行有括弧的演算法
void parser::eval_exp6(double &result)
{
if((*token == '('))
{
get_token();
eval_exp2(result);
if(*token != ')')
serror(1);
get_token();
}
else atom(result);
}
// 取得數值
void parser::atom(double &result)
{
switch(tok_type)
{
case NUMBER:
result = atof(token);
get_token();
return;
default:
serror(0);
}
}
// 輸出出錯信息.
void parser::serror(int error)
{
static char *e[]=
{
"語法錯誤",
"不對稱的括弧",
"您沒有輸入任何的表達式!"
};
cout << e[error] << endl;
}
// 取得下一個token值
void parser::get_token()
{
register char *temp;
tok_type = 0;
temp = token;
*temp = '\0';
if(!*exp_ptr) return; // 如果到表達式末尾則返回
while(isspace(*exp_ptr)) ++exp_ptr; //
if(strchr("+-*/%^=()", *exp_ptr))
{ //用strchr函數在字元串"+-*/%^=()"中查找*exp_ptr指向的字元所在位置,判斷返回值(非0為真就執行)以便是否執行以下語句
tok_type = DELIMITER;//把tok_type置為DELIMITER
// advance to next char
*temp++ = *exp_ptr++;
}
else if(isalpha(*exp_ptr))
{
while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++; //isdelim判斷是否是分隔符,當*exp_ptr指向的字元不為分隔符時把值負給*temp
tok_type = VARIABLE;//把tok_type置為VARIABLE
}
else if(isdigit(*exp_ptr))
{ // isdigit判斷*exp_ptr指向的字元是否為數字
while(!isdelim(*exp_ptr)) *temp++ = *exp_ptr++;
tok_type = NUMBER;
}
*temp = '\0';
}
// isdelim函數的具體實現,如果參數是一個分割符則返回真
int parser::isdelim(char c)
{
if(strchr(" +-/*%^=()", c) || c==9 || c=='\r' || c==0)//把"+-/*%^=()09\r定義非分隔符
return 1;
return 0;
}
int main()
{
char expstr[80];
Info prgInfo;
parser ob; // 創建一個parser類型的實例
prgInfo.displayInfo();
for(;;)
{
cout << "請輸入你要計算的表達式: ";
cin.getline(expstr, 79);//讀入表達式到名為expstr的數組中,數組大小為79
if(*expstr=='.') break;
cout << "計數的結果是: " << ob.eval_exp(expstr) << "\n\n";
};
return 0;
}
⑨ 如何用C語言編寫一個科學計算器
用棧 就可以辦到了。。。這個很詳細的, lz 隨便輸入一個表達式,中間的計算過程全部輸出了,lz試兩個 就知道怎麼回事了。 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 4000;
typedef struct
{
char data[10];
int top;//頭地址
int base;//基地址
int length;//長度
}Stack;
void init(Stack *st)//初始化棧
{
st->base=0;
st->top=0;
st->length=0;
}
int isEmpty(Stack *st)
{
int n=0,top,base;
top =st->top;
base =st->base;
if(top==base)
{
return 1;
}
return n;
}
int isFull(Stack *st)
{
int n=0,top,base;
top =st->top;
if(top>=4000)
{
return 1;
}
return n;
}
char getTop(Stack *st)// 返回top值,不改變棧的結構
{
char n;
if(isEmpty(st))
{
printf("棧為空\n");
return 0;
}
int positon= st->top-1;
n= st->data[positon];//取出數據;
return n;
}
char pop(Stack *st)// 出棧,返回
{
char n;
if(isEmpty(st))
{
printf("棧為空\n");
return 0;
}
int positon= st->top-1;
n= st->data[positon];//取出數據;
st->top--;
st->length--;
st->data[positon]='\0';//消除數據
return n;
}
void push(char n,Stack *st)//入棧
{
int positon ;
if(isFull(st))
{
printf("棧滿\n");
}
else
{
positon= st->top;//獲取位置
st->data[positon]=n;//存入數據
st->top++;//改變位置
}
}
void show(Stack *m1)//輸出棧中的數據
{
int top,base;
top=m1->top;
base=m1->base;
while(top>base)
{
printf("%c,",m1->data[--top]);
}
printf("\n");
}
int isOperate(char temp)//是否是操作符
{
if(temp=='+'||temp=='-'||temp=='*'||temp=='/'||temp=='('||temp==')'||temp=='#')
{
return 1;
}
return 0;
}
int isValue(char temp)//是否是數值
{
if(temp>='0'&&temp<='9')//
{
return 1;
}
else
{
return 0;
}
}
int isAvail(char temp)//是否有效字元
{
if(isOperate(temp)||isValue(temp))//如果temp既不是操作符和數值的話,則它是非法的
{
return 1;
}
return 0;
}
int detect(char temp)//搜索矩陣位置
{
int i=0;
char oper[7]={'+','-','*','/','(',')','#'};
for(i=0;i<7;i++)
{
if(temp==oper[i])
{
return i;
}
}
}
char Priority(char temp,char optr)//判斷優先順序
{
/**//*
+ - * / ( ) #
1 2 3 4 5 6 7
+ 1 < < < < > > >
- 2 < < < < > > >
* 3 > > < < > > >
/ 4 > > < < > > >
( 5 > > > > > = 0
) 6 < < < < = 0 >
# 7 < < < < > 0 =
*/
int row ,col;
char priority[7][7]={/**//* + - * / ( ) # */
{'<','<','<','<','>','>','>'},
{'<','<','<','<','>','>','>'},
{'>','>','<','<','>','>','>'},
{'>','>','<','<','>','>','>'},
{'>','>','>','>','>','=','>'},
{'<','<','<','<','=','0','>'},
{'<','<','<','<','>','<','='},
};
row = detect(temp);//找出對應的矩陣下標;
col = detect(optr);
// printf("%d,%d",row,col);
//優先順序存儲在一個7x7的矩陣中,對應關繫上圖;
return priority[row][col];
}
char evaluate(int a,int b,char oper)
{
switch(oper)
{
case '+': return a+b+'0';
case '-': return a-b+'0';
case '*': return a*b+'0';
case '/': return a/b+'0';
default : return 0+'0';
}
}
int calculateExpress(char *express)//計算表達式
{
int result=0;
int a,b;
// char oper,result;
Stack OPTR,OPND;//OPTR存儲操作符,OPND操作數值
init(&OPTR);
init(&OPND);
push('#',&OPTR);//默認第一個位'#'
////////////////////-演算法-////////////////////////////
while(*express!='\0')
{
char temp;
temp= *(express);
printf("---------------------------------\n");
printf("當前的符號為%c\n",temp);
if(isAvail(temp))//是否是有效字元
{
if(isOperate(temp) )//輸入的是操作符
{
char oper,result;
char optr = getTop(&OPTR);//棧中top位的操作符
printf("棧頂操作符位:%c\n",optr);
char prior = Priority(temp,optr);//判斷優先順序
switch(prior)
{
case '>':
push(temp,&OPTR);
printf("將符號位%c入棧\n",temp);
express++;
break;
case '<':
//int a,b;
//char oper,result;
a=pop(&OPND)-'0';//存在棧中的都是char字元
b=pop(&OPND)-'0';
oper=pop(&OPTR);
result=evaluate(b,a,oper);//出棧一個操作符,計算結果
//printf("%d",result-'0');
push(result,&OPND);//結果入OPND
printf("%d%c%d結果為:%d\n",b,oper,a,result-'0');
break;
case '=':
//消除括弧
pop(&OPTR);
printf("消除括弧\n");
express++;
break;
}
}
if(isValue(temp))//輸入的是數值
{
push(temp,&OPND);//將數值位入棧;
express++;
printf("將數值%c壓入棧\n",temp);
//show(&OPND);
}
}
else //表達式中有非法字元
{
printf("表達式中有非法字元\n");
exit(-1);//退出程序
}
}
// show(&OPND);
// show(&OPTR);
return getTop(&OPND)-'0';
}
void inputExpress(char *express)//輸入表達式
{
int length=0;
printf("請輸入一個表達式:");
scanf("%s",express);
int len =strlen(express);
express[len]='#';//表達式最後一位默認為'#';
express[len+1]='\0';
}
void output(char *express,int result)//輸出表達式
{
int i=0;
printf("----------------------------------------\n表達式:");
while(express[i]!='#')
{
printf("%c",express[i]);
i++;
}
printf("=%d\n",result);
}
int main()
{
char express[100];//表達式
int result =0;
inputExpress(express);//輸入表達式
result = calculateExpress(express);//計算表達式;
output(express,result); //輸出表達式
//、、、、、、、、、、、、、測試優先順序。
/**//*
char m='7' ;
m=Priority('+','*');
printf("優先順序為%c",m);
int m=evaluate(5,6,'m');
printf("%d",m);
*/
return 0;
}