* 程式撰寫:
本程式使用 Turbo C++ 3.0 編譯
* 操作說明
輸入與題目要求相同, 若輸入錯誤或 error (譬如 1/0)則暫存數歸零,
畫面雖然畫一個計算機, 但並沒有特殊功能, 只有加上對 0^0, 1/0 的判斷
* 程式說明
程式中用了四個自己寫的 function, 其中 scan_data(), do_next_op() 如
題目要求一樣, 分別負責處理輸入及運算. 另外還有 DrawCalculator(), 及
Error(). 前者於程式開始時顯示畫面, 後者處理使用者輸入不合法的情形.
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <conio.h>
void DrawCalculator(void)
{
clrscr();
printf(
"+------------------------------------------------------+\n"
"| |\n"
"| |\n"
"+------------------------------------------------------+\n"
"| |\n"
"| CASIO electronic calculator |\n"
"| |\n"
"| +---+ +---+ +---+ +---+ +-----+ |\n"
"| | 7 | | 8 | | 9 | | + | | Sin | |\n"
"| +---+ +---+ +---+ +---+ +-----+ |\n"
"| +---+ +---+ +---+ +---+ +-----+ |\n"
"| | 4 | | 5 | | 6 | | - | | Cos | |\n"
"| +---+ +---+ +---+ +---+ +-----+ |\n"
"| +---+ +---+ +---+ +---+ +-----+ |\n"
"| | 1 | | 2 | | 3 | | * | | Tan | |\n"
"| +---+ +---+ +---+ +---+ +-----+ |\n"
"| +---------+ +---+ +---+ +-----+ +------+ |\n"
"| | 0 | | . | | / | | 1/R | | Quit | |\n"
"| +---------+ +---+ +---+ +-----+ +------+ |\n"
"| |\n"
"+------------------------------------------------------+\n");
}
void scan_data(char *op,double *operand)
{
gotoxy(3,3);
printf(" ");
gotoxy(3,3);
scanf(" %c",op);
if(*op=='+' || *op=='-' || *op=='*' || *op=='/' || *op=='^')
scanf("%lf",operand);
}
void Error(double *number)
{
gotoxy(3,2);
printf(" ");
gotoxy(3,2);
printf("Operation Error, press any key to continue . . .");
getch();
fflush(stdin);
*number=0;
}
void do_next_op(char op,double operand,double *number)
{
switch(toupper(op)) {
case '+':
*number+=operand;
break;
case '-':
*number-=operand;
break;
case '*':
*number*=operand;
break;
case '/':
if(operand==0)
Error(number);
else
*number/=operand;
break;
case '^':
if(*number==0.0 && operand==0.0)
Error(number);
else
*number=pow(*number,operand);
break;
case 'S':
*number=sin(*number);
break;
case 'C':
*number=cos(*number);
case 'R':
if(*number==0.0)
Error(number);
else
*number=1.0/ *number;
case 'T':
*number=tan(*number);
break;
case 'Q':
break;
default:
Error(number);
break;
}
}
int main(void)
{
char op;
double number=0.0,operand;
DrawCalculator();
do {
gotoxy(3,2);
printf(" ");
gotoxy(3,2);
printf("%f",number);
scan_data(&op,&operand);
do_next_op(op,operand,&number);
} while(toupper(op)!='Q');
gotoxy(3,2);
printf(" ");
gotoxy(3,2);
printf("Final value: %f",number);
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: feb.csie.ntu.edu