/*
題目二
運算式求值問題
給定一個算術運算式,對該運算式進行四則運算。
基本要求:
(1)以堆疊作為存盤結構,一個堆疊存放運算子,一個堆疊存放運算元;
(2)按字串形式輸入運算式;
(3)最終結果放在運算元堆疊中并輸出。
測驗資料要求:
給定的運算式中,必須有包含兩位數的數值。例如,給定的運算式為:(10+2)*6-12/3
*/
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 30
typedef struct{
char *base;
int top;
int stacksize;
}SqStack;
typedef struct{
int *base;
int top;
int stacksize;
}NumStack;
void Init_SqStack(SqStack &S){
S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S.base){
return ;
}
S.top=0;
S.stacksize=STACK_INIT_SIZE;
}
int SqStackEmpty(SqStack S){
if(S.top==0){
return 1;
}else{
return 0;
}
}
void Push_Sq(SqStack &S,char e){
if(S.top>=S.stacksize){
S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
if(!S.base){
exit(0);
}
S.stacksize+=STACKINCREMENT;
}
S.base[S.top++]=e;
return ;
}
void Pop_Sq(SqStack &S,char &e){
if(S.top==0){
exit(0);
}
e=S.base[--S.top];
return ;
}
char GetTop_Sq(SqStack S,char &e){
if(S.top==0){
return NULL;
}
e=S.base[S.top-1];
return e;
}
void Init_NumStack(NumStack &S){
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base){
return ;
}
S.top=0;
S.stacksize=STACK_INIT_SIZE;
}
int NumStackEmpty(NumStack S){
if(S.top==0){
return 1;
}else{
return 0;
}
}
void Push_Num(NumStack &S,int e){
if(S.top>=S.stacksize){
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
if(!S.base){
exit(0);
}
S.stacksize+=STACKINCREMENT;
}
S.base[S.top++]=e;
return ;
}
void Pop_Num(NumStack &S,int &e){
if(S.top==0){
exit(0);
}
e=S.base[--S.top];
return ;
}
void GetTop_Num(NumStack S,int &e){
if(S.top==0){
return ;
}
e=S.base[S.top-1];
return ;
}
char Priority(char a,char b){
if(a=='*'||a=='/'){
if(b=='+'||b=='-'){
return '>';
}else if(b=='*'||b=='/'){
return '=';
}else{
return '<';
}
}
if(a=='+'||a=='-'){
if(b=='+'||b=='-'){
return '=';
}
else{
return '<';
}
}
}
int calculate(int num1,char x,int num2){
if(x=='+'){
return num1+num2;
}
else if(x=='-'){
return num1-num2;
}
else if(x=='*'){
return num1*num2;
}
else if(x=='/'){
return num1/num2;
}
}
void Init_Storage(char a[]){
NumStack Num;
SqStack Oper;
Init_SqStack(Oper);
Init_NumStack(Num);
Push_Sq(Oper,'#');
int i=0;
char x,jud;
int num1,num2,result=0;
while(a[i]!='\0'){
if(a[i]>='0'&&a[i]<='9'){
Push_Num(Num,(int)a[i]);
}
if(a[i+1]=='\0'){
Pop_Sq(Oper,x);
Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);
}
else if(a[i]==')'){
while(jud=GetTop_Sq(Oper,jud)!='('){
Pop_Sq(Oper,x);
Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);
}
Pop_Sq(Oper,x);
Push_Num(Num,result);
}
else {
if(Priority(a[i],jud=GetTop_Sq(Oper,jud))=='>'){ //當前運算子優先級高
Push_Sq(Oper,a[i]);
}
else if(Priority(a[i],jud=GetTop_Sq(Oper,jud))=='<'||Priority(a[i],jud=GetTop_Sq(Oper,jud))=='='){
Pop_Sq(Oper,x);
Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);
Push_Num(Num,result);
Push_Sq(Oper,a[i]);
}
}
i++;
}
//Pop_Sq(Oper,x);
/*Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);*/
printf("最終結果為%d",result);
}
int main(){
char a[5];
for(int i=0;i<5;i++){
scanf("%c",a[i]);
}
Init_Storage(a);
return 0;
}
代碼如上,做四則運算 只算7*2+3類似 但運行直接崩潰求解
uj5u.com熱心網友回復:
for(int i=0;i<5;i++){
scanf("%c", &a[i]);
}少個&
uj5u.com熱心網友回復:
/*
題目二
運算式求值問題
給定一個算術運算式,對該運算式進行四則運算。
基本要求:
(1)以堆疊作為存盤結構,一個堆疊存放運算子,一個堆疊存放運算元;
(2)按字串形式輸入運算式;
(3)最終結果放在運算元堆疊中并輸出。
測驗資料要求:
給定的運算式中,必須有包含兩位數的數值。例如,給定的運算式為:(10+2)*6-12/3
*/
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 30
typedef struct{
char *base;
int top;
int stacksize;
}SqStack;
typedef struct{
int *base;
int top;
int stacksize;
}NumStack;
void Init_SqStack(SqStack &S)
{
S.base = (char *)malloc(STACK_INIT_SIZE*sizeof(char));
if (!S.base) {
exit(0);
}
S.top = 0;
S.stacksize = STACK_INIT_SIZE;
}
int SqStackEmpty(SqStack S)
{
if(S.top == 0)
return 1;
else
return 0;
}
void Push_Sq(SqStack &S,char e)
{
if (S.top >= S.stacksize) {
S.base = (char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
if(!S.base)
exit(0);
S.stacksize += STACKINCREMENT;
}
S.base[S.top++] = e;
}
void Pop_Sq(SqStack &S,char &e)
{
if(S.top==0){
exit(0);
}
e = S.base[--S.top];
}
char GetTop_Sq(SqStack S,char &e)
{
if(S.top == 0)
return -1;
e = S.base[S.top-1];
return e;
}
void Init_NumStack(NumStack &S)
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)
exit(0);
S.top = 0;
S.stacksize = STACK_INIT_SIZE;
}
int NumStackEmpty(NumStack S)
{
if(S.top==0)
return 1;
else
return 0;
}
void Push_Num(NumStack &S,int e)
{
if(S.top>=S.stacksize){
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
if(!S.base){
exit(0);
}
S.stacksize+=STACKINCREMENT;
}
S.base[S.top++]=e;
return ;
}
void Pop_Num(NumStack &S,int &e){
if(S.top==0)
return;
e = S.base[--S.top];
}
void GetTop_Num(NumStack S,int &e)
{
if(S.top==0){
return ;
}
e=S.base[S.top-1];
return ;
}
char Priority(char a,char b)
{
if(a=='*'||a=='/'){
if(b=='+'||b=='-'){
return '>';
}else if(b=='*'||b=='/'){
return '=';
}else{
return '<';
}
}
if(a=='+'||a=='-'){
if(b=='+'||b=='-'){
return '=';
}
else{
return '<';
}
}
}
int calculate(int num1,char x,int num2)
{
if(x=='+'){
return num1+num2;
}
else if(x=='-'){
return num1-num2;
}
else if(x=='*'){
return num1*num2;
}
else if(x=='/'){
return num1/num2;
}
}
void Init_Storage(char a[])
{
NumStack Num;
SqStack Oper;
Init_SqStack(Oper);
Init_NumStack(Num);
Push_Sq(Oper,'#');
int i=0;
char x,jud;
int num1,num2,result=0;
while (a[i]) {
if(a[i]>='0'&&a[i]<='9')
Push_Num(Num,a[i] - '0'); /*不能直接強制型別轉換*/
if(a[i+1] == 0){
Pop_Sq(Oper,x);
Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);
}
else if(a[i]==')') {
while((jud=GetTop_Sq(Oper,jud))!='('){
Pop_Sq(Oper,x);
Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);
}
Pop_Sq(Oper,x);
Push_Num(Num,result);
}
else {
if(Priority(a[i],jud=GetTop_Sq(Oper,jud))=='>'){ //當前運算子優先級高
Push_Sq(Oper,a[i]);
}
else if(Priority(a[i],jud=GetTop_Sq(Oper,jud))=='<'||Priority(a[i],jud = GetTop_Sq(Oper,jud))=='='){
Pop_Sq(Oper,x);
Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);
Push_Num(Num,result);
Push_Sq(Oper,a[i]);
}
}
i++;
}
//Pop_Sq(Oper,x);
/*Pop_Num(Num,num1);
Pop_Num(Num,num2);
result=calculate(num1,x,num2);*/
printf("最終結果為%d",result);
}
int main()
{
char a[80];
scanf("%s", a);
Init_Storage(a);
return 0;
}
參考一下吧
注意若a定義成5,很容易越界。
程式很接近答案了,建議你在這個基礎上繼續除錯
uj5u.com熱心網友回復:
僅供參考:/*---------------------------------------
函式型計算器(VC++6.0,Win32 Console)
功能:
目前提供了10多個常用數學函式:
⑴正弦sin
⑵余弦cos
⑶正切tan
⑷開平方sqrt
⑸反正弦arcsin
⑹反余弦arccos
⑺反正切arctan
⑻常用對數lg
⑼自然對數ln
⑽e指數exp
⑾乘冪函式^
⑿向上取整ceil
⒀向下取整floor
⒁四舍五入取整round
用法:
如果要求2的32次冪,可以打入2^32<回車>
如果要求30度角的正切可鍵入tan(Pi/6)<回車>
注意不能打入:tan(30)<Enter>
如果要求1.23弧度的正弦,有幾種方法都有效:
sin(1.23)<Enter>
sin 1.23 <Enter>
sin1.23 <Enter>
如果驗證正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2 <Enter>或sin1.23^2+cos1.23^2 <Enter>
此外兩函式運算式連在一起,自動理解為相乘如:sin1.23cos0.77+cos1.23sin0.77就等價于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)
當然你還可以依據三角變換,再用sin(1.23+0.77)也即sin2驗證一下。
本計算器充分考慮了運算子的優先級因此諸如:2+3*4^2 實際上相當于:2+(3*(4*4))
另外函式名前面如果是數字,那么自動認為二者相乘.
同理,如果某數的右側是左括號,則自動認為該數與括弧項之間隱含一乘號。
如:3sin1.2^2+5cos2.1^2 相當于3*sin2(1.2)+5*cos2(2.1)
又如:4(3-2(sqrt5-1)+ln2)+lg5 相當于4*(3-2*(√5 -1)+loge(2))+log10(5)
此外,本計算器提供了圓周率Pi鍵入字母時不區分大小寫,以方便使用。
16進制整數以0x或0X開頭。
----------------------------------------*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;
const char Tab=0x9;
const int DIGIT=1;
const int MAXLEN=16384;
char s[MAXLEN],*endss;
int pcs=15;
double round(double dVal, short iPlaces) {//iPlaces>=0
char s[30];
double dRetval;
sprintf(s,"%.*lf",iPlaces,dVal);
sscanf(s,"%lf",&dRetval);
return (dRetval);
}
double fun(double x,char op[],int *iop) {
while (op[*iop-1]<32) //本行使得函式嵌套呼叫時不必加括號,如 arc sin(sin(1.234)) 只需鍵入arc sin sin 1.234<Enter>
switch (op[*iop-1]) {
case 7: x=sin(x); (*iop)--;break;
case 8: x=cos(x); (*iop)--;break;
case 9: x=tan(x); (*iop)--;break;
case 10: x=sqrt(x); (*iop)--;break;
case 11: x=asin(x); (*iop)--;break;
case 12: x=acos(x); (*iop)--;break;
case 13: x=atan(x); (*iop)--;break;
case 14: x=log10(x); (*iop)--;break;
case 15: x=log(x); (*iop)--;break;
case 16: x=exp(x); (*iop)--;break;
case 17: x=ceil(x); (*iop)--;break;
case 18: x=floor(x); (*iop)--;break;
case 19: x=round(x,0);(*iop)--;break;
}
return x;
}
double calc(char *expr,char **addr) {
static int deep; //遞回深度
static char *fname[]={"sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp","ceil","floor","round",NULL};
double ST[10]={0.0}; //數字堆疊
char op[10]={'+'}; //運算子堆疊
char c,*rexp,*pp,*pf;
int ist=1,iop=1,last,i,n;
__int64 i64;
if (!deep) {
pp=pf=expr;
do {
c = *pp++;
if (c!=' '&& c!=Tab)
*pf++ = c;
} while (c!='\0');
}
pp=expr;
if ((c=*pp)=='-'||c=='+') {
op[0] = c;
pp++;
}
last = !DIGIT;
while ((c=*pp)!='\0') {
if (c=='(') {//左圓括弧
deep++;
ST[ist++]=calc(++pp,addr);
deep--;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp = *addr;
last = DIGIT;
if (*pp == '('||isalpha(*pp) && strnicmp(pp,"Pi",2)) {//目的是:當右圓括弧的右惻為左圓括弧或函式名字時,默認其為乘法
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
else if (c==')') {//右圓括弧
pp++;
break;
} else if (isalpha(c)) {
if (!strnicmp(pp,"Pi",2)) {
if (last==DIGIT) {
cout<< "π左側遇)" <<endl;exit(1);
}
ST[ist++]=3.14159265358979323846264338328;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp += 2;
last = DIGIT;
if (!strnicmp(pp,"Pi",2)) {
cout<< "兩個π相連" <<endl;exit(2);
}
if (*pp=='(') {
cout<< "π右側遇(" <<endl;exit(3);
}
} else {
for (i=0; (pf=fname[i])!=NULL; i++)
if (!strnicmp(pp,pf,strlen(pf))) break;
if (pf!=NULL) {
op[iop++] = 07+i;
pp += strlen(pf);
} else {
cout<< "陌生函式名" <<endl;exit(4);
}
}
} else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^') {
char cc;
if (last != DIGIT) {
cout<< "運算子粘連" <<endl;exit(5);
}
pp++;
if (c=='+'||c=='-') {
do {
cc = op[--iop];
--ist;
switch (cc) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '%': ST[ist-1] = fmod(ST[ist-1],ST[ist]);break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
} while (iop);
op[iop++] = c;
} else if (c=='*'||c=='/'||c=='%') {
operate: cc = op[iop-1];
if (cc=='+'||cc=='-') {
op[iop++] = c;
} else {
--ist;
op[iop-1] = c;
switch (cc) {
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '%': ST[ist-1] = fmod(ST[ist-1],ST[ist]);break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
} else {
cc = op[iop-1];
if (cc=='^') {
cout<< "乘冪符連用" <<endl;exit(6);
}
op[iop++] = c;
}
last = !DIGIT;
} else {
if (last == DIGIT) {
cout<< "兩數字粘連" <<endl;exit(7);
}
if (pp[0]=='0' && (pp[1]=='x'||pp[1]=='X')) {
sscanf(pp+2,"%I64x%n",&i64,&n);
rexp=pp+2+n;
ST[ist++]=(double)i64;
} else ST[ist++]=strtod(pp,&rexp);
ST[ist-1]=fun(ST[ist-1],op,&iop);
if (pp == rexp) {
cout<< "非法字符" <<endl;exit(8);
}
pp = rexp;
last = DIGIT;
if (*pp == '('||isalpha(*pp)) {
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
}
*addr=pp;
if (iop>=ist) {
cout<< "運算式有誤" <<endl;exit(9);
}
while (iop) {
--ist;
switch (op[--iop]) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '%': ST[ist-1] = fmod(ST[ist-1],ST[ist]);break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
return ST[0];
}
int main(int argc,char **argv) {
int a;
if (argc<2) {
if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代碼頁
cout << "計算函式運算式的值。"<<endl<<"支持(),+,-,*,/,%,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp,ceil,floor,round"<<endl;
while (1) {
cout << "請輸入運算式:";
gets(s);
if (s[0]==0) break;//
cout << s <<"=";
cout << setprecision(15) << calc(s,&endss) << endl;
}
} else if (argc==2 && 0==strcmp(argv[1],"/?")) {
if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代碼頁
cout << "計算由≥1個命令列引數給出的函式運算式的值。\n"
"最后一個引數是.0~.15表示將計算結果保留小數0~15位\n"
"最后一個引數是x表示將計算結果以16進制正整數格式輸出\n"
"支持(),+,-,*,/,%,^^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp,ceil,floor,round\n"
"16進制整數以0x或0X開頭\n";
} else {
strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;
if (argc>2) {
for (a=2;a<argc-1;a++) strncat(s,argv[a],MAXLEN-1);//將空格間隔的各引數連接到s
if (1==sscanf(argv[a],".%d",&pcs) && 0<=pcs && pcs<=15) {//最后一個引數是.0~.15表示將計算結果保留小數0~15位
printf("%.*lf\n",pcs,calc(s,&endss));
} else if (argv[a][0]=='x'||argv[a][0]=='X') {//最后一個引數是x表示將計算結果以16進制正整數格式輸出
printf("0x%016I64x\n",(__int64)calc(s,&endss));
} else {
strncat(s,argv[a],MAXLEN-1);
printf("%.15lg\n",calc(s,&endss));
}
} else {
printf("%.15lg\n",calc(s,&endss));
}
}
return 0;
}
uj5u.com熱心網友回復:
老師能大概講講思路嗎uj5u.com熱心網友回復:
代碼功能歸根結底不是別人幫自己看或講解或注釋出來的;而是被自己靜下心來花足夠長的時間和精力親自動手單步或設斷點或對執行到某步獲得的中間結果顯示或寫到日志檔案中一步一步分析出來的。提醒:再牛×的老師也無法代替學生自己領悟和上廁所!
單步除錯和設斷點除錯(VS IDE中編譯連接通過以后,按F10或F11鍵單步執行,按Shift+F11退出當前函式;在某行按F9設斷點后按F5執行停在該斷點處。)是程式員必須掌握的技能之一。
uj5u.com熱心網友回復:
這個程式運算沒有結果輸出uj5u.com熱心網友回復:
你是c語言還是c++,如果是c是沒有&參考的轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84704.html
標籤:C語言
