C語言實(shí)現(xiàn)簡單計(jì)算器程序
這兩天在看一個(gè)C語言寫的計(jì)算器程序,做了不少的功夫,跟著作者一步步的進(jìn)行完善,了解了許多細(xì)節(jié)性的東西,在此自己做個(gè)總結(jié),加深自己對(duì)程序的印象,也算是梳理。
在該計(jì)算器程序,能進(jìn)行加減乘除、sin、cos、exp等操作,同時(shí)能進(jìn)行數(shù)值保存功能。而該計(jì)算器使用逆波蘭表示法。即所有運(yùn)算符都跟在操作數(shù)的后面,比如下列表達(dá)式:
(1 - 2) * (4 + 5)采用逆波蘭表示法表示為:1 2 - 4 5 + *
逆波蘭表達(dá)法中不需要圓括號(hào),只要知道每個(gè)運(yùn)算符需要幾個(gè)操作數(shù)就不會(huì)引起歧義。
計(jì)算器程序?qū)崿F(xiàn)很簡單,具體原理如下:
while(/* 下一個(gè)運(yùn)算符或操作數(shù)不是文件結(jié)束指示符 */) if(/* 是數(shù) */) /* 將該數(shù)壓入到棧中 */ else if (/* 是運(yùn)算符 */) /* 彈出所需數(shù)目的操作數(shù) */ /* 執(zhí)行運(yùn)算 */ /* 將結(jié)果壓入到棧中 */ else if (/* 是換行符 */) /* 彈出并打印棧頂?shù)闹?*/ else /* 出錯(cuò) */
在程序設(shè)計(jì)中,使用模塊化思想,getop函數(shù)來進(jìn)行讀入,該函數(shù)返回一個(gè)標(biāo)識(shí),用來標(biāo)識(shí)讀入的是什么類型。主循環(huán)體中根據(jù)該標(biāo)識(shí)執(zhí)行相應(yīng)的動(dòng)作。
以下是該程序: (我將所有函數(shù)和變量放在同一文件)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXOP 100
#define NUMBER '0' //標(biāo)識(shí)讀入的是數(shù)字
#define NAME 'n' //標(biāo)識(shí)讀入的是字符串(函數(shù)名或非法字符串)
#define ALPHA 26
int getop(char []);
void push (double); //壓棧
double pop(void); //出棧
void clear(void); //清空棧
void mathfnc(char []); //執(zhí)行相應(yīng)的數(shù)學(xué)函數(shù)sin、cos、exp等
int main(void)
{
int type;
int i, var = 0;
double op1, op2,v;
char s[MAXOP];
double variable[ALPHA];
for (i = 0; i < ALPHA; i++) //初始化用于保存數(shù)值的變量數(shù)組
variable[i] = 0.0;
while ((type = getop(s)) != EOF) //讀取輸入
{
switch (type)
{
case NUMBER:
push (atof(s));
break;
case NAME:
mathfnc(s);
break;
case '+':
push (pop() + pop());
break;
case '*':
push (pop() * pop());
break;
case '-':
op2 = pop();
push (pop() - op2);
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push (pop() / op2);
else
printf ("error: zero divisor\n");
break;
case '%':
op2 = pop();
if (op2 != 0.0)
push (fmod(pop(), op2));
else
printf ("error: zero divisor\n");
break;
case '?': //打印棧頂元素
op2 = pop();
printf ("\t%.8g\n", op2);
push (op2);
break;
case '=': //保存數(shù)值
pop();
if (var >= 'A' && var <= 'Z')
variable[var - 'A'] = pop();
else
printf ("error: no variable name\n");
break;
case 'c':
clear();
break;
case 'd': //復(fù)制棧頂元素
op2 = pop();
push(op2);
push(op2);
break;
case 's': //交換棧元素
op1 = pop();
op2 = pop();
push(op1);
push(op2);
case '\n':
v = pop(); //v保存最后的一次結(jié)果
printf ("\t%.8g\n", v);
break;
default:
if (type >= 'A' && type <= 'Z')
push(variable[type - 'A']);
else if (type == '@') //輸入的字符@表示最近一次結(jié)果值
push(v);
else
printf ("error: unknown command %s\n", s);
break;
}
var = type;
}
return 0;
}
/* ----------------------------------------------------------- */
#define MAXVAL 100
int sp = 0; //標(biāo)識(shí)棧頂
double val[MAXVAL];
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
else
printf ("error: stack full, can't push %g\n", f);
}
double pop(void)
{
if (sp > 0)
return val[--sp];
else
{
printf ("error: statck empty\n");
return 0.0;
}
}
void clear(void)
{
sp = 0;
}
void mathfnc (char s[])
{
double op2;
if (strcmp (s, "sin") == 0)
push(sin(pop()));
else if(strcmp (s, "cos") == 0)
push(cos(pop()));
else if(strcmp (s, "exp") == 0)
push(exp(pop()));
else if(strcmp (s, "pow") == 0)
{
op2 = pop();
push (pow(pop(), op2));
}
else
printf ("error: %s not supported\n", s);
}
/* ----------------------------------------------------------- */
#include <ctype.h>
int getch(void);
void ungetch(int);
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t') //過濾開頭的空白字符
;
s[1] = '\0';
i = 0;
if (islower(c)) //判斷是否為小寫字母,也即讀取由小寫字母組成的字符串
{
while (islower(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
if (strlen (s) > 1)
return NAME;
else
return c;
}
if (!isdigit(c) && c != '.' && c != '-')
return c;
if (c == '-') //用于判斷是負(fù)數(shù)還是減操作
{
if (isdigit(c = getch()) || c == '.')
s[++i] = c;
else
{
if (c != EOF)
ungetch(c);
return '-';
}
}
if (isdigit(c)) //收集整數(shù)部分
while (isdigit(s[++i] = c = getch()))
;
if (c == '.') //收集小數(shù)部分
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
/* ----------------------------------------------------------- */
/*
* 引用以下兩個(gè)函數(shù)是因?yàn)椋撼绦虿荒艽_定它已經(jīng)讀入的輸入是否足夠 *
* 除非超前多讀入一些輸入,在本程序中,讀入一些字符合成一個(gè)數(shù)字 *
* 所以在看到第一個(gè)非數(shù)字字符之前,已經(jīng)讀入的數(shù)的完整性是不能確定的
* 由于程序要超前讀入一個(gè)字符,這樣就導(dǎo)致最后又一個(gè)字符不屬于當(dāng)前所要讀入的數(shù)
*/
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch (int c)
{
if (bufp >= BUFSIZE)
printf ("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
該程序雖然簡單,但是還是存在一些小小的問題,比如沒有數(shù)據(jù)時(shí)進(jìn)行pop的話,會(huì)打印棧中無數(shù)據(jù)同時(shí)返回?cái)?shù)值0.0,在循環(huán)體中許多執(zhí)行操作會(huì)將該數(shù)值保存到棧中,之后打印該值,用戶體驗(yàn)度比較差。程序設(shè)計(jì)方面,模塊化設(shè)計(jì)使得該程序容易增加功能而不影響其他模塊,比如增加一些數(shù)學(xué)函數(shù)處理,在mathfnc函數(shù)中去添加,或增加一些運(yùn)算符操作,可以在主循環(huán)體中增加。
總之,這次學(xué)習(xí)還是頗有收獲。
關(guān)于計(jì)算器的精彩文章請(qǐng)查看《計(jì)算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語言數(shù)據(jù)結(jié)構(gòu)之簡易計(jì)算器
- C語言實(shí)現(xiàn)簡單計(jì)算器功能(1)
- C語言實(shí)現(xiàn)簡單的計(jì)算器
- C語言實(shí)現(xiàn)簡單計(jì)算器功能(2)
- C語言實(shí)現(xiàn)簡單計(jì)算器
- C語言結(jié)課設(shè)計(jì)之計(jì)算器功能
- 用C語言實(shí)現(xiàn)計(jì)算器功能
- C語言實(shí)現(xiàn)個(gè)稅計(jì)算器
- C語言運(yùn)用函數(shù)指針數(shù)組實(shí)現(xiàn)計(jì)算器功能
- 用C語言實(shí)現(xiàn)簡單的計(jì)算器功能
相關(guān)文章
VisualStudio2019解決scanf函數(shù)報(bào)錯(cuò)問題
在 Visual Studio 2019 編輯代碼時(shí),前期剛剛接觸到VS編譯器時(shí)存在的困惑,當(dāng)用scanf()函數(shù),進(jìn)行輸入時(shí),在運(yùn)行的時(shí)候編譯器會(huì)出現(xiàn)警告報(bào)錯(cuò),本文就來介紹一下解決方法2023-08-08
C/C++?Qt數(shù)據(jù)庫與SqlTableModel組件應(yīng)用教程
SqlTableModel?組件可以將數(shù)據(jù)庫中的特定字段動(dòng)態(tài)顯示在TableView表格組件中,這篇文章將主要介紹SqlTableModel組件一些常用的操作,需要的朋友可以參考一下2021-12-12
C語言用封裝方法實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲
這篇文章主要為大家詳細(xì)介紹了C語言用封裝方法實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

