用c语言编写个计算器《要有界面,能加减乘除即可》

如题所述

第1个回答  2012-04-14
-----这个很简单-------
#include "stdio.h"
#include "windows.h"
void main(int argc,char **argv)
{
HWND hwnd=FindWindow(NULL,argv[0]);
ShowWindow(hwnd,0);
system("calc");

}
--------开个玩笑-------
//以下为我写的代码 不是很好 但愿能帮你

#include <windows.h>
#include <string.h>
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
#define OUTPUT 1002
char txt[30]="";
double pre;
double beh;
char yun;
double toNumber()
{
char *p=txt;
double temp=0;
double i=0.1;
while(*p!='.' && *p)
{
temp *=10;
temp += *p-'0';
p++;
}
p++;
while(*p)
{
temp += (*p-'0')*i;
i *= 0.1;
p++;
}
return temp;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
int i,j;
char *s[16]={"7","8","9","+",
"4","5","6","-",
"1","2","3","*",
"0",".","=","/"};
switch(Message)
{
case WM_CREATE:
CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE|ES_RIGHT, 0,0,190,25, hwnd, (HMENU)OUTPUT, g_hInst, NULL);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
CreateWindow("BUTTON",s[i+j*4],WS_CHILD | WS_VISIBLE,i*50, (j+1)*25, 50,25,hwnd, (HMENU)(i+j*4), g_hInst, NULL);
break;
case WM_SETFOCUS:
SetFocus(GetDlgItem(hwnd, OUTPUT));
break;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 0:case 1:case 2:case 4:case 5:case 6:case 8:case 9:case 10:case 12:case 13:
strcat(txt,s[LOWORD(wParam)]);
SendDlgItemMessage(hwnd,OUTPUT, WM_SETTEXT,NULL, (long)txt);
break;
case 3:case 7:case 11:case 15:
pre=toNumber();
txt[0]=0;
strcat(txt,s[LOWORD(wParam)]);
yun=txt[0];
SendDlgItemMessage(hwnd,OUTPUT, WM_SETTEXT,NULL, (long)txt);
txt[0]=0;
break;
case 14:
beh = toNumber();
switch(yun)
{
case '+':
pre = pre + beh;
break;
case '-':
pre = pre - beh;
break;
case '*':
pre = pre * beh;
break;
case '/':
pre = pre / beh;
break;
}
sprintf(txt,"%lf",pre);
SendDlgItemMessage(hwnd,OUTPUT, WM_SETTEXT,NULL, (long)txt);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;

g_hInst = hInstance;

WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "注册窗口失败", "错误信息",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"计算器",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 210, 165,
NULL, NULL, g_hInst, NULL);

if(hwnd == NULL)
{
MessageBox(0, "创建窗口失败", "错误信息",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}本回答被网友采纳
第2个回答  2012-04-14
!!!

用c语言编写个计算器《要有界面,能加减乘除即可》
void main(int argc,char **argv){ HWND hwnd=FindWindow(NULL,argv[0]);ShowWindow(hwnd,0);system("calc");} ---开个玩笑--- \/\/以下为我写的代码 不是很好 但愿能帮你 include <windows.h> include <string.h> static char g_szClassName[] = "MyWindowClass";static HINSTANCE ...

用C语言做一个计算器,能实现加减乘除混合运算?
是的,可以使用C语言编写一个计算器程序,能够实现加、减、乘、除等混合运算。下面是一个简单的示例程序:```c include <stdio.h> int main() { char operator;double num1, num2, result;printf("Enter an operator (+, -, *, \/): ");scanf("%c", &operator);printf("Enter two num...

求用C语言编写一简单计算器程序,要求:实现简单地加减乘除就行了
return jisuan(fun(ss,i),fun(ss+i+1,n-i-1),ss[i]);}}if(flag==0)\/\/如果算数中没有+-{for(i=0;i<n;i++)if((ss[i]=='*')||(ss[i]=='\/')){return jisuan(fun(ss,i),fun(ss+i+1,n-i-1),ss[i]);}}}void main(void){char s[50];int n;printf("输入算...

用c语言设计一个简单的加减乘除计算器
1、打开visual C++ 6.0-文件-新建-文件-C++ Source File。2、输入预处理命令和主函数:#include \/*函数头:输入输出头文件*\/,void main()\/*空类型:主函数*\/。3、定义变量:int a,b,d; \/*定义变量的数据类型为整型*\/,char c;\/*定义变量的数据类型为字符型*\/。4、输入四则运算式:pri...

简单的用c语言写一个计算器程序,加减乘除能用就好
void main(){ float a,b,c;char e;printf("input a,e,b\\n");\/*输入两个数和符号,例如3+8*\/ scanf("%f%c%f",&a,&e,&b);switch(e){ case '+':c=a+b;break;case '-':c=a-b;break;case '*':c=a*b;break;case '\/':if(b==0.0) printf("error\\n");else c=a\/...

用c语言编一个计算器程序,能够实现基本的加减乘除,能够输出运算对象...
include <stdio.h>int main(void){double a, b;int chose;printf("欢迎使用我的计算器\\n");while (1){printf("***\\n");printf("1、两数相加\\n");printf("2、两数相减\\n");printf("3、两数相乘\\n");printf("4、两数相除\\n");printf("5、退出\\n");printf("***\\n");scanf(...

C语言 编译计算器可以分别计算加减乘除
while(scanf("%f", &num1) != 1 )\/\/确保输入数据正确 { printf("输入有误,请重新输入第一个数\\n"); CLR_INTPUT; } getchar();\/\/清除空格符 while(scanf("%c", &ch) == 1) { if(ch != '+' && ch != '-' && ch != '*' && ch != '\/'...

用C语言设计一个计算器,具有加减乘除的功能即可,看一下我的程序错在哪 ...
loop: printf("\\t请选择需操作的类型:\\n\\t1、计算式子2、输出并查看计算式及其结果3、退出计算器\\n\\t\\t\\t\\t");\/\/定位在这里可避免查看计算式时清除屏幕 scanf("\\n%c",&t); \/\/特别注意这里的\\n,我始终不明白,但又必须加这\\n getchar();switch(t) \/\/选择操作类型 { case '1': ...

用c语言编一个简单的能计算加减乘除的小计算器,要是能把思路附上最好...
printf("\\b%c",ch);count=1;num1=ch-'0';while(ch=getch(),ch>='0' && ch<='9'){ putchar(ch);num1=num1*10+ch-'0';count++;} while(1){ op=ch;\/\/符号 ch=getch();while(ch<'0' || ch>'9')\/\/输入的不是数字,重新输入 ch=getch();for(i=0;i<count;i++)\/\/...

请问怎么用c语言写一个可以实现加减乘除四则运算的计算器!
}算法流程:1,如果读入数字就把它存入数组中,2,如果读入加,减号就存如另一个数组用,如果读入乘 除号,就再读入一个数字,从存数字的数组拿出两个数字进行乘 除运算。把结果存入数组中,这么重复直到读入回车键的符号。3,读到回车符号后,就从存符号的数组中拿出一个符号,再从存数字的数组中...

相似回答