用c语言程序设计一个简单计算器,求其源代码

作者&投稿:颛厚 (若有异议请与网页底部的电邮联系)
~ #include
#include
#include
#include
#include
#include
#include
#include
#include
/* Define constants for the calculator */
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4B
#define RIGHT 0x4D
#define ENTER 0x0D
/* Global variables */
double num1 = 0, num2 = 0, result = 0;
char str1[] = "1230.456+-789*/知消扒Qc=^%";
char cnum[5], str2[20] = "", c;
int x, y, x0, y0, i, j, v, m, n, act, flag = 1;
/* Function prototypes */
void drawboder(void);
void initialize(void);
void computer(void);
void changetextstyle(int font, int direction, int charsize);
void mwindow(char *header);
int specialkey(void);
int arrow();
/* Main function */
int main() {
initialize();
computer();
closegraph();
return 0;
}
/* Initialize the graphics system */
void initialize(void) {
int xasp, yasp;
GraphDriver = DETECT;
initgraph( &GraphDriver, &GraphMode, "" );
ErrorCode = graphresult();
if (ErrorCode != grOk) {
printf("Graphics System Error: %s\n", grapherrormsg(ErrorCode));
exit(1);
}
getpalette( &palette );
MaxColors = getmaxcolor() + 1;
MaxX = getmaxx();
MaxY = getmaxy();
getaspectratio( &xasp, &yasp );
AspectRatio = (double)xasp / (double)yasp;
}
/* Main calculator function */
void computer(void) {
struct viewporttype vp;
int color, height, width;
mwindow("Calculator");
color = 7;
getviewsettings( &vp );
width = (vp.right + 1) / 10;
height = (vp.bottom - 10) / 10;
x = width / 2;
y = height / 2;
setfillstyle(SOLID_FILL, color + 3);
bar( x + width * 2, y, x + 7 * width, y + height );
setcolor( color + 3 );
rectangle( x + width * 2, y, x + 7 * width, y + height );
setcolor(RED);
outtextxy(x + 3 * width, y + height / 2, "0.");
x = 2 * width - width / 2;
y = 2 * height + height / 2;
for (j = 0; j < 4; ++j) {
for (i = 0; i < 5; ++i) {
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x + width, y + height );
rectangle( x, y, x + width, y + height );
sprintf(str2, "%c", str1[j * 5 + i]);
outtextxy( x + (width / 2), y + height / 2, str2);
x += width + (width / 2);
}
y += (height / 2) * 3;
x = 2 * width - width / 2;
}
x0 = 2 * width;
y0 = 3 * height;
x = x0;
y = y0;
gotoxy(x, y);
arrow();
m = 0;
n = 0;
strcpy(str2, "");
while ((v = specialkey()) != 45) {
while ((v = specialkey()) != ENTER) {
putimage(x, y, rar, XOR_PUT);
if (v == RIGHT) {
if (x >= x0 + 6 * width)
x = x0;
else
x += width + width / 2;
m++;
}
if (v == LEFT) {
if (x <= x0)
x = x0 + 6 * width;
else
x -= width - width / 2;
m--;
}
if (v == UP) {
if (y <= y0)
y = y0 + 4 * height + height / 2;
else
y -= height - height / 2;
n--;
}
if (v == DOWN) {
if (y >= 7 * height)
y = y0;
else
y += height + height / 2;
n++;
}
putimage(x, y, rar, XOR_PUT);
}
c = str1[n * 5 + m];
if (isdigit(c) || c == '.') {
if (flag == -1) {
strcpy(str2, "-");
flag = 1;
}
sprintf(temp, "%c", c);
strcat(str2, temp);
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, 15 * width / 2, 3 * height / 2);
outtextxy(5 * width, height, str2);
}
if (c == '+') {
num1 = atof(str2);
strcpy(str2, "");
act = 1;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, 15 * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
if (c == '-') {
if (strcmp(str2, "") == 0)
flag = -1;
else {
num1 = atof(str2);
strcpy(str2, "");
act = 2;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, 15 * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
}
if (c == '*') {
num1 = atof(str2);
strcpy(str2, "");
act = 3;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, 15 * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
if (c == '/') {
num1 = atof(str2);
strcpy(str2, "");
act = 4;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, 15 * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");
}
if (c == '^') {
num1 = atof(str2);
strcpy(str2, "");
act = 5;
setfillstyle(SOLID_FILL, color + 3);
bar(2 * width + width / 2, height / 2, 15 * width / 2, 3 * height / 2);
outtextxy(5 * width, height, "0.");


用c语言程序设计一个简单计算器,求其源代码
double num1 = 0, num2 = 0, result = 0;char str1[] = "1230.456+-789*\/知消扒Qc=^%";char cnum[5], str2[20] = "", c;int x, y, x0, y0, i, j, v, m, n, act, flag = 1;\/* Function prototypes *\/ void drawboder(void);void initialize(void);void computer...

C语言编写一个程序,求x> y的运行结果
以下是一个简单的C语言程序,可以比较两个整数x和y,并打印出相应的结果:在这个程序中,我们首先声明了两个整数变量x和y,然后使用printf函数提示用户输入两个整数。接下来,我们使用scanf函数读取用户输入的整数,并将它们存储在变量x和y中。然后,我们使用一个if-else语句来比较x和y的值。如果x大于y...

设计一个C语言程序,输入0 100内的5个整数,将这5个数按从大到小顺寻输...
include<stdio.h> int main(){ int i,j,t,arr[5];printf("请输入5个0-100的整数:");scanf("%d",arr);for(i=1;i<5;i++){ scanf("%d",arr+i);if(arr[i]>arr[i-1]){ t=arr[i];for(j=i-1;j>=0&&t>arr[j];j--)arr[j+1]=arr[j];arr[j+1]=t;} } printf(...

C语言程序设计,做一个简单计算器。
1、首先,打开Vs 2010,如图。2、找到左上角的新建并点击,给文件为简单计算器,单击确定。3、点击下一步,注意勾选空项目,点击下一步,点击完成。4、点击左侧的源文件,右击选择“添加—>项目”,选择C++文件,命名为简单计算器,因为是C程序,注意后缀名要加上.c,点击确定完成文件新建工作。5、...

如何用C语言编程一个计算圆的面积的程序?
int iR;cin >> iR;cout << "半径为" << iR << "的圆面积为:" << 3.14*iR*iR << endl;

用c语言编程:编写一个程序,输入4个数,求其平均值
程序代码如下:直接编译,程序输出结果中任意输入4个数字,程序执行结果如下图所示:

编程一个c语言程序?
用选择结构实现分段函数的程序如下 include <stdio.h> int main(){ double x,y;scanf("%lf",&x);if(x>0)y=x-1;else if(x==0)y=0;else y=x+1;printf("%.2lf",y);return 0;}

设计一个c语言程序完成以下功能:若输入英文字母,则原样输出;输入其他...
include<stdio.h> main(){ int c=0;while (c!='Q' && c!='q'){ c=getchar();if (c>='a' && c<='z' || c>='A' && c<='Z') putchar(c);} }

设计一个c语言程序, 输入某月某日,判断这-天是这一 年的第几天? 输入...
h> int main(){ int m, d, cnt, i;int a[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };scanf("%d%*[^0-9]%d", &m, &d);for (cnt = 0, i = 0; i < m; ++i)cnt += a[i];cnt += d;printf("%d\\n", cnt);return 0;} ...

C语言:设计一个收银程序,要求程序实现这样的功能:
int price=0,guestpay=0,change=0;void app(){printf("输入顾客付款:");scanf("%d",&guestpay);if(price==guestpay){printf("交易成功!");}else if(guestpay>price){change=guestpay-price;printf("找给顾客%d,交易成功!",change);}else{printf("付款不足,交易失败!\\n");app();}}...

彰武县13610258095: 用C语言编写一个简单的计算器1 -
揣复斯特: #include<stdio.h> int main() {double num1 = 0; //输入1double num2 = 0; //输入2char ch; //操作double ret = 0; //结果 printf( "输入第一个数:" );scanf( "%lf", &num1 );printf( "输入第二个数:" );scanf( "%lf", &num2 ); ...

彰武县13610258095: c语言设计一个简单的计算器程序
揣复斯特: /* 2013年12月23日 12:43:46 目的:计算器的实现 */ # include <stdio.h> # include <ctype.h> # include <math.h> char get_choice(void); //获取用户输入的选项,并建立目 char get_first(void); //获取用户输入的选项,并剔除错误输入 float get_int(...

彰武县13610258095: 用C语言编写一个简单的计算器 -
揣复斯特: #include int main() { double num1 = 0; //输入1 double num2 = 0; //输入2 char ch; //操作 double ret = 0; //结果 printf( "输入第一个数:" ); scanf( "%lf", &num1 ); printf( "输入第二个数:" ); scanf( "%lf", &num2 ); printf( "操...

彰武县13610258095: C语言实现的简易计算器 -
揣复斯特: 展开全部#include void main() { float a,b,i=0;char yun;mama: printf("\n请输入运算符和要计算的两个数:"); main:if(i<3) {fflush(stdin); scanf("%c%f%f",&yun,&a,&b);switch(yun) { case '+':printf("%.2f+%.2f=%.2f",a,b,a+b); break; case '-':...

彰武县13610258095: 题目一:用C语言设计一个简单计算器 -
揣复斯特: #include<stdio.h> int sum(int n); int main(){ int s; int n=100; s=sum(n); printf("%d",s); } int sum(int n){ if(n==0) return 0; else { n=n+sum(n-1); return n; } }

彰武县13610258095: 简单的计算器 C语言 程序设计 -
揣复斯特: #include <stdio.h>void min(){int a;int b;int c;char op;scanf("%d",&a);scanf("%c",&op);scanf("%d",&b); switch(op){case '+':c=a+b;break;case '-':c=a-b;break;case '*':c=a*b;break; case '/':c=a/b;break;default:printf(...

彰武县13610258095: 怎么用C语言编写一个计算器程序? -
揣复斯特: #include<stdio.h> #include<math.h> main() { float a,b; char c; printf("please input the expression:"); scanf("%f%c%f",&a,&c,&b); swich(c) { case'+': printf("%f",a+b); case'-': printf("%f",a-b); case'*': printf("%f",a*b); case'/': if(b==0)printf("wrong!"); elseprintf("%f",a/b); break; default: printf("wrong!"); } }

彰武县13610258095: 求大神给一个C语言的程序代码做简易计算器? -
揣复斯特: 更好看的代码 #include <stdio.h> #include <math.h> double sum(double a, double b) { return a + b; } double minu(double a, double b) { return a - b; } double mult(double a, double b) { return a * b; } double div(double a, double b) { return a / b; } int mod...

彰武县13610258095: 急!!!求一个简单的C语言计算器程序.....
揣复斯特: 只要+ - * /的是吧? #include&lt;stdio.h&gt; int main(void) { char ch; int a,b; printf("Enter formula:"); scanf("%d%c%d",&amp;a,&amp;ch,&amp;b); switch(ch) { case '+':printf("%d+%d=%d\n",a,b,a+b);break; case '-':printf("%d-%d=%d\n",a,b,...

彰武县13610258095: C语言 要求编写一个简单计算器的程序 -
揣复斯特: #include<stdio.h> void add(int a,int b,int c) {c=a+b;printf("%d+%d = %d",a,b,c);printf("\n"); } void minus(int a,int b,int c) {c=a-b;printf("%d-%d=%d",a,b,c);printf("\n"); } void multiplication(int a,int b,int c) {c=a*b;printf("%d*%d=%d...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网