用C语言能编出一个有计算器界面的计算器么?

作者&投稿:油具 (若有异议请与网页底部的电邮联系)
~ 这段代码是一个使用C语言编写的计算器程序,旨在图形界面下运行。不过,代码中存在一些问题,例如变量未初始化、语法错误、逻辑错误以及一些不正确的函数调用。以下是修改后的代码,我已经纠正了这些问题,并且改进了代码的结构和可读性。
```c
#include
#include
#include
#include
#include
#include
#include
/* 定义颜色等 */
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4B
#define RIGHT 0x4D
#define ENTER 0x0D
/* 计算器函数 */
void computer(void);
void drawboder(void);
void initialize(void);
void changetextstyle(int font, int direction, int charsize);
void mwindow(char *header);
int specialkey(void);
int arrow();
/* 主函数 */
int main() {
initialize();
computer();
closegraph();
return 0;
}
/* 初始化图形模式 */
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;
}
/* 计算器函数 */
void computer(void) {
/* 省略了部分代码,只展示了修改后的部分 */
/* 设置光标位置 */
gotoxy(x, y);
arrow();
putimage(x, y, rar, XOR_PUT);
m = 0;
n = 0;
strcpy(str2, "");
while ((v = specialkey()) != 45) {
/* 省略了部分代码 */
if (c == '=') {
num2 = atof(str2);
switch (act) {
case 1: result = num1 + num2; break;
case 2: result = num1 - num2; break;
case 3: result = num1 * num2; break;
case 4: result = num1 / num2; break;
case 5: result = pow(num1, num2); break;
case 6: result = fmod(num1, num2); break;
}
setfillstyle(SOLID_FILL, color+3);
bar(2*width+width/2, height/2, 15*width/2, 3*height/2);
sprintf(temp, "%f", result);
outtextxy(5*width, height, temp);
}
/* 省略了部分代码 */
}
/* 省略了部分代码 */
}
/* 窗口函数 */
void mwindow(char *header) {
int height;
cleardevice();
setcolor(MaxColors - 1);
setviewport(20, 20, MaxX/2, MaxY/2, 1);
height = textheight("H");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
settextjustify(CENTER_TEXT, TOP_TEXT);
outtextxy(MaxX/4, 2, header);
setviewport(20, 20+height+4, MaxX/2+4, MaxY/2+20, 1);
drawboder();
}
/* 画边框 */
void drawboder(void) {
struct viewporttype vp;
setcolor(MaxColors - 1);
setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
getviewsettings(&vp);
rectangle(0, 0, vp.right-vp.left, vp.bottom-vp.top);
}
/* 设计鼠标图形函数 */
int arrow() {
int size;
int raw[] = {4, 4, 4, 8, 6, 8, 14, 16, 16, 16, 8, 6, 8, 4, 4, 4};
setfillstyle(SOLID_FILL, 2);
fillpoly(8, raw);
size = imagesize(4, 4, 16, 16);
rar = malloc(size);
getimage(4, 4, 16, 16, rar);
putimage(4, 4, rar, XOR_PUT);
return 0;
}
/* 按键函数 */
int specialkey(void) {
int key;
while (bioskey(1) == 0);
key = bioskey(0);
key = key & 0xFF ? key & 0xFF : key >> 8;
return key;
}
```
请注意,这段代码可能仍然在某些平台上无法正常运行,因为它们使用了特定的图形库和BIOS功能,这些可能在现代操作系统上不可用。此外,代码中的一些部分(如`arrow`函数和`mwindow`函数)在实际使用中可能需要进一步的调整和优化。


c语言编写程序,输入一个不多于3位的正整数,要求,求出它有几位数,并逆 ...
include <stdio.h>int main(){char a[3]={0,0,0};int i,num=0,cnt=0;printf("input:");scanf("%d",&num);if(num>999) printf("input data must:0~999\\n");a[0] = (num\/100)%10;\/\/百位a[1] = (num\/10)%10; \/\/十位a[2] = num%10; \/\/个位 if(a[0]>...

用C语言编写:有一个分数序列2\/1,3\/2,5\/3,8\/5,13\/8,...求出这个数列前20...
include <stdio.h> double sum(int n){ int i;double part = 0;for( i = 1.0; i <= n; i++ )part += (1.0 \/ i);return 2 * n - part;} int main(void){ printf( "%.18f\\n", sum(20) );return 0;} 楼主,小数点后的精度你可以根据需要自己调,我选的是18位。

C语言 编写一个程序,用户输入一个整数n后打印出由* '号组成的n行n列的...
int n,i,j;printf("请输入数字:");scanf("d%",&n);for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("* ");} printf("\\n");} 插进去就ok了

c语言编程 编写一个函数,求出一个给定数字的所有因子。如72=2*2*2*...
include<stdio.h>int main(void){ int n, i; scanf("%d", &n); printf("%d = ", n); for(i = 2; n > 1; i++) { while(n % i == 0) { n \/= i; if(n == 1) printf("%d\\n", i); else printf("%d * ", i); } } ...

c程序的一个完整的程序是如何组成的?
1、一个C语言源程序可以由一个或多个源文件组成。2、每个源文件可由一个或多个函数组成。3、一个源程序不论由多少个文件组成,都有一个且只能有一个main函数,即主函数。是整个程序的入口。4、源程序中可以有预处理命令(包括include 命令,ifdef、ifndef命令、define命令),预处理命令通常应放在源...

C语言:①题目:编写程序,定义一个含有15个元素的数组,并编写函数分别完成...
include<stdio.h> include<stdlib.h> define M 15 void getx(int *s);void putx(int s[]);int sum(int x,int s[]);main(){ int x[M]={0},i;getx(x);putx(x);for(i=0;i<15;i++)printf("%d ",x[i]);printf("\\n");for(i=0;i<15;i++)printf("%d ",sum(i,x...

用C语言程序编写“输入一个四位整数(如1234),使其倒序输出(如4321...
include<stdio.h>int main(){int n,s=0; scanf("%d",&n); while(n>0) {printf("%d",n%10); s+=n%10; n\/=10; } printf("\\n各位之和=%d\\n",s); return 0; }

C语言中用结构体设计一个可以显示花色和编号的扑克牌,并实现对这副扑克...
include <iostream> include <ctime> using namespace std;\/\/全局变量,一副牌 \/\/ int g_cards[54] = { 0,1,2,3,\/\/ 3 10,11,12,13,\/\/ 4 20,21,22,23,\/\/ 5 30,31,32,33,\/\/ 6 40,41,42,43,\/\/ 7 50,51,52,53,\/\/ 8 60,61,62,63,\/\/ 9 70,71,72,73,\/\/ 10 80...

如何用C语言编一个发出声音的程序?
这个唱 多来米法少 include <stdio.h> include <stdlib.h> include <windows.h> main(){ Beep(523,500);Beep(587,500);Beep(659,500);Beep(698,500);Beep(784,500);Beep(880,500);Beep(980,500);Beep(1060,500);Sleep(500);Beep(523,500);Beep(587,500);Beep(659,500);Beep(698,...

C语言编程:有n个人围成一圈,按顺序从1到n编号。从第一个人开始,报到3...
include<stdio.h> int main(int argc,char*argv[]){ int i,j=0,k=0,n;int a[30]={0};printf("请输入有几个人玩游戏:");scanf("%d",&n);for(i=0;i<n;i++){ a=1;\/\/1代表活着,0代表出局 } for(i=1;i<4;i=i%3+1)\/\/控制i的值在[0,3]{ if(3==i&&a[j]!=0...

图木舒克市19441604334: 用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 ); ...

图木舒克市19441604334: 用C语言编辑计算器(要有计算器界面!+ - */即可) -
藩晓欧来: #include "stdio.h"#include "string.h" #include "ctype.h" #include "math.h" //expression evaluate #define iMUL 0 #define iDIV 1 #define iADD 2#define iSUB 3 #define iCap 4 //#define LtKH 5//#define RtKH 6#define MaxSize 100 void ...

图木舒克市19441604334: 怎么用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!"); } }

图木舒克市19441604334: C语言的简单计算器怎么做 -
藩晓欧来:#include int main(void){long a,b;long max;char c;printf("请输入a,b的数值.\n");scanf("%ld%c%ld",&a,&c,&b);switch (c) {case '+':max=a+b;break;case '-':max=a-b;break;case '*':max=a*b;break;case '/':max=(float)...

图木舒克市19441604334: C语言设计一个多功能计算器 -
藩晓欧来: 利用栈吧.先入后出.这是一位牛人编的.#include<stdio.h> /*库文件包含*/ #include<string.h> /*用于字符串操作*/ #include<stdlib.h> /*用于exit函数*//************************************************************************** int check(char *c) 输入参数:...

图木舒克市19441604334: 怎样用C语言实现计算器功能 -
藩晓欧来: #include <stdio.h>int main() {float data1, data2;char op; while (3 == scanf("%f%c%f", &data1, &op, &data2) ){float result; printf("%.6lf%c%.6lf=", data1, op, data2); switch (op){case '+':result = data1 + data2;break;case '-':result = ...

图木舒克市19441604334: 急!!!求一个简单的C语言计算器程序..... -
藩晓欧来: //实现计算机功能的程序 a program which can work the functions as a computer.#include <stdio.h>//头文件#include <conio.h> void menu();//声明部分 void add(); void sub(); void mul(); void div(); void remain(); void add_n_to_m(); void factor(); ...

图木舒克市19441604334: 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(!...

图木舒克市19441604334: c语言里的简单计算器怎么做?
藩晓欧来: 我提供一点思路吧. 首先这是一个两个数之间的计算器.(1)运行后显示一段提示信息,让用户选择 + - * / 其中一种运算;然后分别输入值,最后计算输出.(2)复杂点的就是,显示提示信息,用户直接输出一段运算(例如:9+2, 20-1*8)回车输出结果,值就存放在一个字符串,然后通过字符转换、判断、分割之类的方法,把值和运算符分离出来,最后设置算法判断优先级进行运算,输出结果. 第二种是多数多运算的思路,当然你也可以删减的

图木舒克市19441604334: 急求一个计算器的c语言程序 -
藩晓欧来: 给,已编译确认:#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <ctype.h>#include <bios.h>#define End 0X4F00 char token; /*全局标志变量*//*递归调用的函数原型*/ int exp( void ); int term( void ); int factor( void ); void error( void...

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