當前位置:首頁 » 課程大全 » c語言課程設計計算器程序分析

c語言課程設計計算器程序分析

發布時間: 2021-02-17 20:39:39

① c語言計算器程序和實驗報告怎麼寫啊

#include <stdio.h>
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;

link push(link stack,int value)
{
link newnode;

newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}

link pop(link stack,int *value)
{
link top;
if(stack !=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}

int empty(link stack)
{
if(stack==NULL)
return 1;
else
return 0;

}

int is_operator(char operator)
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}

int priority(char operator)
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0;
}
}

int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}

void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;

printf("\nPlease input the inorder expression:");
gets(expression);

while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);

operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}
理工大?

② 用C語言設計一個簡單計算器的課程設計(希望能盡可能的詳細,多一些)

//名字記不太清了,這個叫遞歸下降演算法,但這個演算法肯定是首先在編譯原理中的,主要用在
//各種編譯器中。就是現掃描整個表達式字元串,把其中的運算符找出來,判斷它們的優先順序
//然後按從左到右的順序先計算把優先順序低的運算符和它兩邊的數據壓入,這樣循環做過以後
//再從頭取出一個一個計算,表達式的結構類似與二叉樹,遍歷二叉樹後把結果存在連表中供
//計算。你這個程序問題好像比較多啊。用的數據結構類型和函數名根本就不配套
#include <stdio.h>
struct s_node //節點結構體
{
int data;

struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;

link push(link stack,int value) //向鏈表添加數據
{
link newnode;

newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}

link pop(link stack,int *value) //從鏈表取出數據
{
link top;
if(stack !=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}

int empty(link stack) //判斷鏈表是否為空
{
if(stack==NULL)
return 1;
else
return 0;

}

int is_operator(char operator) //判斷是否是運算符號
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}

int priority(char operator) //判斷運算符優先順序
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0;
}
}

int two_result(int operator,int operand1,int operand2) //計算數值,計算器的核心
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}

void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;

printf("\nPlease input the inorder expression:");
gets(expression);

while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);

operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}

③ 誰給個C語言課程設計計算器的源程序和完整的實驗報告啊謝謝啦!~~~

[email protected]我也是要這個 有人回答的話 分享一下 謝謝了
我有下載的 但是呵呵 學計算機的 不是很明白 先

④ c語言課程設計一個簡單計算器

//名字記不太清了,這個叫遞歸下降演算法,但這個演算法肯定是首先在編回譯原理中的,主要答用在
//各種編譯器中。就是現掃描整個表達式字元串,把其中的運算符找出來,判斷它們的優先順序
//然後按從左到右的順序先計算把優先順序低的運算符和它兩邊的數據壓入,這樣循環做過以後
//再從頭取出一個一個計算,表達式的結構類似與二叉樹,遍歷二叉樹後把結果存在連表中供
//計算。

⑤ C語言課程設計,簡單計算器

#include<stdio.h>
voidmain()
{
inta,b;
charch;
scanf("%d%c%d",&a,&ch,&b);
switch(ch)
{
case'+':printf("%d",a+b);break;
case'-':printf("%d",a-b);break;
case'*':printf("%d",a*b);break;
case'/':
{
if(b==0)printf("算式無抄意義");
elseprintf("%f",float(a)/b);
break;
}
default:printf("輸入的運算符有誤!");
}
}

⑥ "C語言課程設計--算術計算器的實現"怎麼做

#include<stdio.h>
#include<malloc.h>
#include<iostream.h>

struct node
{
int date;
struct node *next;
};
typedef struct node *link;
link number=NULL;
link symble=NULL;
//把數據放入棧中//
link push(link stack ,int num,int k)
{ link newnode;
newnode=(link)malloc(sizeof(struct node));
if(newnode==NULL)
cout<<"out of space"<<endl;
else
{ if(k>=0 && k<=9)//判斷前一個字元是否為數字
{
stack->date=stack->date*10+num;
return stack;
}
else
{
newnode->date=num;
newnode->next=stack;
stack=newnode;
return stack;

}
}
}

//從棧上取出數據//
link pull(link stack,int *num)
{
link top;//去棧頂元素
if(stack!=NULL)
{
top=stack;
stack=stack->next;
*num=top->date;
free(top);
return stack;

}
else
*num=0;

}
//判斷棧是否為空
int empty(link stack)
{
if(stack==NULL)
return 1;
else
return 0;

}
//判斷是否為運算符
int is_symble(char ch)
{
switch(ch)
{ case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
return 1;
default:
return 0;
}

}
// 判斷運算符的優先順序
int priority(char ch)
{
switch(ch)
{ case '(':
return 1;
case '+':
case '-':return 2;
case '*':
case '/': return 3;//'+','-'的優先順序小於'*','/'
default:return 0;
}

}
//計算兩個操作數的值
int tuo_result(int m,int num1,int num2 )
{ switch(m)
{ case '+':
return(num2 + num1);
case '-':
return (num2-num1);
case '*':
return(num2 * num1);
case '/':
return(num2 / num1);
}

}

//主函數
void main ()
{
char expression[50];//存放表達式
int position=0;//表達式的位置
int sym=0;//運算符
int num1=0;//後操作數
int num2=0;//前操作數
int sum=0;//運算結果
cout<<"請輸入表達式:"<<endl;
gets(expression);
while(expression[position]!='\0' && expression[position]!='\n')
{ if(is_symble(expression[position]))//判斷運算符
{
if(expression[position]=='(')
symble=push(symble,expression[position],-1);
else if(expression[position]==')')
{ while(symble->date!='(')
{ number=pull(number,&num1);
number=pull(number,&num2);
symble=pull(symble,&sym);
number=push(number,tuo_result(sym,num1,num2),-1);
}
//if(symble->date='(')symble=pull(symble,&sym);
symble=pull(symble,&sym);

}

else if(!empty(symble))//判斷放運算符的棧是否為空

{ if(priority(expression[position]) <= priority(symble->date) &&
!empty(symble))
{
//取出一個運算符和兩個操作數
number=pull(number,&num1);
number=pull(number,&num2);
symble=pull(symble,&sym);
number=push(number,tuo_result(sym,num1,num2),-1);
//cout<<tuo_result(sym,num1,num2)<<endl;
//把運算符放在棧里
//symble=push(symble,expression[position],-1);
}
symble=push(symble,expression[position],-1);
}

//把運算符放在棧里
else
symble=push(symble,expression[position],-1);

}
else if(!is_symble(expression[position]))//把操作數放在棧里
number=push(number,expression[position]-48,expression[position-1]-48);
//cout<<expression[position]<<endl;

position++;
}

//取出運算符棧的運算符
while(!empty(symble))
{ //cout<<symble->date<<endl;

symble=pull(symble,&sym);
if(!empty(symble))//判斷是否為空
{
if(sym=='+' && symble->date=='-' )//判斷符號問題 ,2-2*3+5=1不然就是-1
sym='-';
}
number=pull(number,&num1);
number=pull(number,&num2);

//cout<<num1<<" "<<num2<<endl;
//計算的結果放在棧中
number=push(number, tuo_result(sym,num1,num2),-1);
//cout<<number->date<<endl;

}
//取出運算結果
number=pull(number,&sum);
cout<<expression<<"="<<sum<<endl;

}

⑦ 求一C語言課程設計簡易計算器的程序代碼

都沒有懸賞

⑧ c語言課程設計 計算器

分也太少了。我這程序我到是寫了一個(貌似一年前寫的)。主要是鏈表與堆棧的操作。
看回去能不能翻出來~

⑨ c語言設計一個簡單的計算器程序

#include<.h>//計算器

voidmenu()//自定義的菜單界面

printf("--------------------\n");

printf("請輸入你的選擇\n");

printf("1.+\n");

printf("2.-\n");

printf("3.*\n");

printf("4./\n");

printf("--------------------\n");

intmain()

inti=0;

intj=0;

intnum=0;//計算結果存放在nun

intselect=0;//選擇的選項存放在select

do//do-while先執行再判斷循環條件,即可實現重復計算功能

menu();//列印出菜單界面

scanf("%d",&select);//輸入你的選項

printf("請輸入計算值:");

scanf("%d%d",&i,&j);//輸入要計算的數值

switch(select)

case1:

printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能

break;

case2:

printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能

break;

case3:

printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能

break;

case4:

printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能

break;

default:

printf("輸入有誤重新選擇");

break;

}while(select);

return0;

運行結果:

(9)c語言課程設計計算器程序分析擴展閱讀:

return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。

return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。

⑩ c語言課程設計(設計一個簡單的計算器)

KTV魔女咯摸摸哦哦弄

熱點內容
武漢大學學生會輔導員寄語 發布:2021-03-16 21:44:16 瀏覽:612
七年級學生作文輔導學案 發布:2021-03-16 21:42:09 瀏覽:1
不屑弟高考成績 發布:2021-03-16 21:40:59 瀏覽:754
大學畢業證會有成績單 發布:2021-03-16 21:40:07 瀏覽:756
2017信陽學院輔導員招聘名單 發布:2021-03-16 21:40:02 瀏覽:800
查詢重慶2018中考成績查詢 發布:2021-03-16 21:39:58 瀏覽:21
結業考試成績怎麼查詢 發布:2021-03-16 21:28:40 瀏覽:679
14中醫醫師資格筆試考試成績查分 發布:2021-03-16 21:28:39 瀏覽:655
名著賞析課程標准 發布:2021-03-16 21:27:57 瀏覽:881
北京大學商業領袖高端培訓課程 發布:2021-03-16 21:27:41 瀏覽:919