❶ c語言編寫計算器程序
C語言編寫計算器
我們可以用printf和scanf函數輸出結果和獲取用戶的輸入。需要<stdio.h>頭文件。scanf函數在讀取數據的時候不需要再一行上輸入每個數據,只要數據和數據之間留出空白就可以了。先聲明兩個變數number1和number2,operation變數用來存儲運算符。用scanf函數獲取這兩個數字和運算符。分別用%lf %c %lf
❷ 求一道C語言中關於計算器的程序代碼
這個已經有計算功能了,但記事本還沒有,給你急用。因為C沒有專門的字元串類型:有事再聯系405719864// C語言計算器.cpp : Defines the entry point for the console application.
//
/*注意!本程序用C編寫,在VC++6.0中運行通過,如果要有記事本功能,可能要用C++寫,因為C沒有專門的字元串類型*/
#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>char c,operate;//c用於保存計算符號,operate表示當前輸入
double n,m;//記錄輸入的數字
bool check_c,check_n,check_m;//各個輸入的開關,當輸入發生改變時,可以有根據地操作double count(double n,char ch,double m)//根據不同的計算符號計算結果
{
if(ch == '+') return n+m;
if(ch == '-') return n-m;
if(ch == '*') return n*m;
if(ch == '/')
{
if(m != 0) return n/m;
else printf("0不能作除數!\n");
}
return n;
}int main()
{
n=m=0;
check_c=check_n=check_m=false;
printf("0\n"); printf("若要結束,請輸入任意字母!\n");
while(scanf("%c",&operate) != EOF)//表示輸入以EOF結束
{
if(operate == 0x0d || operate==' ' || operate=='\n') continue;//表示排除無效字元
if(operate == '#')
{
n=m=0;
check_c=check_n=check_m=false;
printf("0\n");
}
else
{
if(isdigit(operate))
{
if(!check_n) { n=n*10+operate-'0'; printf("%.0lf\n",n); }
else { m=m*10+operate-'0'; check_m=true; check_c=true; printf("%.0lf\n",m); }
}
else
{
if(operate=='+' || operate=='-' || operate=='*' || operate=='/' || operate=='=')//出現運算符號
{
if(operate != '=')//非「=」,則第一個數字輸入結束,並記錄當前計算符號
{
check_n=true;
if(!check_c) c=operate;
}
if(check_n && check_m)//兩個數字都有輸入,就進行計算
{
n=count(n,c,m);
printf("%.0lf\n",n);
if(operate != '=')//出現連續等號,連續計算,非等號,則記錄並從新輸入第二個數字
{
c=operate;
check_c=false;
m=0;
check_m=false;
}
}
else printf("%.0lf\n",n);
}
else
{
printf("輸入數據無效!\n");//表示以任意字母字元結束
break;
}
}
}
}
return 0;
}
❸ 計算器c語言代碼
#include<dos.h>/*DOS介面函數*/
#include<math.h>/*數學函數的定義*/
#include<conio.h>/*屏幕操作函數*/
#include<stdio.h>/*I/O函數*/
#include<stdlib.h>/*庫函數*/
#include<stdarg.h>/*變數長度參數表*/
#include<graphics.h>/*圖形函數*/
#include<string.h>/*字元串函數*/
#include<ctype.h>/*字元操作函數*/
#defineUP0x48/*游標上移鍵*/
#defineDOWN0x50/*游標下移鍵*/
#defineLEFT0x4b/*游標左移鍵*/
#defineRIGHT0x4d/*游標右移鍵*/
#defineENTER0x0d/*回車鍵*/
void*rar;/*全局變數,保存游標圖象*/
structpalettetypepalette;/*使用調色板信息*/
intGraphDriver;/*圖形設備驅動*/
intGraphMode;/*圖形模式值*/
intErrorCode;/*錯誤代碼*/
intMaxColors;/*可用顏色的最大數值*/
intMaxX,MaxY;/*屏幕的最大解析度*/
doubleAspectRatio;/*屏幕的像素比*/
voiddrawboder(void);/*畫邊框函數*/
voidinitialize(void);/*初始化函數*/
voidcomputer(void);/*計算器計算函數*/
voidchangetextstyle(intfont,intdirection,intcharsize);/*改變文本樣式函數*/
voidmwindow(char*header);/*窗口函數*/
intspecialkey(void);/*獲取特殊鍵函數*/
intarrow();/*設置箭頭游標函數*/
/*主函數*/
intmain()
{
initialize();/*設置系統進入圖形模式*/
computer();/*運行計算器*/
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0);/*結束程序*/
}
/*設置系統進入圖形模式*/
voidinitialize(void)
{
intxasp,yasp;/*用於讀x和y方向縱橫比*/
GraphDriver=DETECT;/*自動檢測顯示器*/
initgraph(&GraphDriver,&GraphMode,"");
/*初始化圖形系統*/
ErrorCode=graphresult();/*讀初始化結果*/
if(ErrorCode!=grOk)/*如果初始化時出現錯誤*/
{
printf("GraphicsSystemError:%s\n",
grapherrormsg(ErrorCode));/*顯示錯誤代碼*/
exit(1);/*退出*/
}
getpalette(&palette);/*讀面板信息*/
MaxColors=getmaxcolor()+1;/*讀取顏色的最大值*/
MaxX=getmaxx();/*讀屏幕尺寸*/
MaxY=getmaxy();/*讀屏幕尺寸*/
getaspectratio(&xasp,&yasp);/*拷貝縱橫比到變數中*/
AspectRatio=(double)xasp/(double)yasp;/*計算縱橫比值*/
}
/*計算器函數*/
voidcomputer(void)
{
structviewporttypevp;/*定義視口類型變數*/
intcolor,height,width;
intx,y,x0,y0,i,j,v,m,n,act,flag=1;
floatnum1=0,num2=0,result;/*操作數和計算結果變數*/
charcnum[5],str2[20]={""},c,temp[20]={""};
charstr1[]="1230.456+-789*/Qc=^%";/*定義字元串在按鈕圖形上顯示的符號*/
mwindow("Calculator");/*顯示主窗口*/
color=7;/*設置灰顏色值*/
getviewsettings(&vp);/*讀取當前窗口的大小*/
width=(vp.right+1)/10;/*設置按鈕寬度*/
height=(vp.bottom-10)/10;/*設置按鈕高度*/
x=width/2;/*設置x的坐標值*/
y=height/2;/*設置y的坐標值*/
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.");/*輸出字元串"0."*/
x=2*width-width/2;/*設置x的坐標值*/
y=2*height+height/2;/*設置y的坐標值*/
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]);
/*將字元保存到str2中*/
outtextxy(x+(width/2),y+height/2,str2);
x=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);/*移動游標到x,y位置*/
arrow();/*顯示游標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,"");/*設置str2為空串*/
while((v=specialkey())!=45)/*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER)/*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT);/*顯示游標圖象*/
if(v==RIGHT)/*右移箭頭時新位置計算*/
if(x>=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字元位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
}/*否則,右移到下一個字元位置*/
if(v==LEFT)/*左移箭頭時新位置計算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
}/*如果移到頭,再左移,則移動到最右邊字元位置*/
else
{
x=x-width-width/2;
m--;
}/*否則,左移到前一個字元位置*/
if(v==UP)/*上移箭頭時新位置計算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
}/*如果移到頭,再上移,則移動到最下邊字元位置*/
else
{
y=y-height-height/2;
n--;
}/*否則,移到上邊一個字元位置*/
if(v==DOWN)/*下移箭頭時新位置計算*/
if(y>=7*height)
{
y=y0;
n=0;
}/*如果移到尾,再下移,則移動到最上邊字元位置*/
else
{
y=y+height+height/2;
n++;
}/*否則,移到下邊一個字元位置*/
putimage(x,y,rar,XOR_PUT);/*在新的位置顯示游標箭頭*/
}
c=str1[n*5+m];/*將字元保存到變數c中*/
if(isdigit(c)||c=='.')/*判斷是否是數字或小數點*/
{
if(flag==-1)/*如果標志為-1,表明為負數*/
{
strcpy(str2,"-");/*將負號連接到字元串中*/
flag=1;
}/*將標志值恢復為1*/
sprintf(temp,"%c",c);/*將字元保存到字元串變數temp中*/
strcat(str2,temp);/*將temp中的字元串連接到str2中*/
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,"");/*將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)/*如果str2為空,說明是負號,而不是減號*/
flag=-1;/*設置負數標志*/
else
{
num1=atof(str2);/*將第二個操作數轉換為浮點數*/
strcpy(str2,"");/*將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,"");/*將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,"");/*將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,"");/*將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.");/*顯示字元串*/
}
if(c=='%')
{
num1=atof(str2);/*將第二個操作數轉換為浮點數*/
strcpy(str2,"");/*將str2清空*/
act=6;/*做計算模運算乘方標志值*/
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=='=')
{
num2=atof(str2);/*將第二個操作數轉換為浮點數*/
switch(act)/*根據運算符號計算*/
{
case1:result=num1+num2;break;/*做加法*/
case2:result=num1-num2;break;/*做減法*/
case3:result=num1*num2;break;/*做乘法*/
case4:result=num1/num2;break;/*做除法*/
case5:result=pow(num1,num2);break;/*做x的y次方*/
case6: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);/*將結果保存到temp中*/
outtextxy(5*width,height,temp);/*顯示結果*/
}
if(c=='c')
{
num1=0;/*將兩個操作數復位0,符號標志為1*/
num2=0;
flag=1;
strcpy(str2,"");/*將str2清空*/
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=='Q')exit(0);/*如果選擇了q回車,結束計算程序*/
}
putimage(x,y,rar,XOR_PUT);/*在退出之前消去游標箭頭*/
return;/*返回*/
}
/*窗口函數*/
voidmwindow(char*header)
{
intheight;
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();/*畫邊框*/
}
voiddrawboder(void)/*畫邊框*/
{
structviewporttypevp;/*定義視口類型變數*/
setcolor(MaxColors-1);/*設置當前顏色為白色*/
setlinestyle(SOLID_LINE,0,NORM_WIDTH);/*設置畫線方式*/
getviewsettings(&vp);/*將當前視口信息裝入vp所指的結構中*/
rectangle(0,0,vp.right-vp.left,vp.bottom-vp.top);/*畫矩形邊框*/
}
/*設計滑鼠圖形函數*/
intarrow()
{
intsize;
intraw[]={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);/*消去游標箭頭圖象*/
return0;
}
/*按鍵函數*/
intspecialkey(void)
{
intkey;
while(bioskey(1)==0);/*等待鍵盤輸入*/
key=bioskey(0);/*鍵盤輸入*/
key=key&0xff?key&0xff:key>>8;/*只取特殊鍵的掃描值,其餘為0*/
return(key);/*返回鍵值*/
}
❹ c語言設計簡單計算器代碼
#include <iostream.h>
#include <stack>
using namespace std;
int precede(char op1,char op2)//> 1,= 0,<-1
{
switch(op1)
{
case '+':
if(op2=='+' || op2=='-' || op2==')' || op2=='#')
return 1;
return -1;
case '-':
if(op2=='+' || op2=='-' || op2==')' || op2=='#')
return 1;
return -1;
case '*':
if(op2=='(')
return -1;
return 1;
case '/':
if(op2=='(')
return -1;
return 1;
case '(':
if(op2==')')
return 0;
return -1;
case ')':
return 1;
case '#':
return -1;
default:
break;
}
return 0;
}
int compute(int num1,char op,int num2)
{
int num=0;
switch(op)
{
case '+':
num=num1+num2;
break;
case '-':
num=num1-num2;
break;
case '*':
num=num1*num2;
break;
case '/':
num=num1/num2;
break;
}
return num;
}
int calculator(char str[205])
{
int i,pre,num,num1,num2;
char op1;
bool flag;
stack<char> charsta;
stack<int> intsta;
charsta.push('#');
i=strlen(str);
str[i]='#';
str[i+1]='\0';
i=0,num=0;
flag=false;
while(str[i])
{
if(str[i]>='0' && str[i]<='9')
{
flag=true;
num=num*10+str[i]-'0';
i++;
}
else
{
if(flag)
{
flag=false;
intsta.push(num);
num=0;
}
op1=charsta.top();
if(op1=='#' && str[i]=='#')
break;
pre=precede(op1,str[i]);
switch(pre)
{
case 0:
charsta.pop();
i++;
break;
case 1:
charsta.pop();
num2=intsta.top();
intsta.pop();
num1=intsta.top();
intsta.pop();
intsta.push(compute(num1,op1,num2));
break;
case -1:
charsta.push(str[i]);
i++;
break;
}
}
}
num=intsta.top();
intsta.pop();
return num;
}
int main()
{
char str[205];
int i,n;
cin>>n;//數據組數
for(i=0;i<n;i++)
{
cin>>str; //輸入表達式,如((5-3)*2-1)
printf("%d\n",calculator(str));//計算結果
}
return 0;
}
❺ 如何用C語言設計一個程序模擬有圖形界面的計算器
首先,打開Vs 2010,如圖
❻ C語言程序設計簡易計算器
1、首先,打開Vs 2010,如圖。
2、找到左上角的新建並點擊,給文件為簡單計算器,單擊確定。
3、點擊下一步,注意勾選空項目,點擊下一步,點擊完成。
4、點擊左側的源文件,右擊選擇「添加—>項目」,選擇C++文件,命名為簡單計算器,因為是C程序,注意後綴名要加上.c,點擊確定完成文件新建工作。
5、輸入以下代碼,好了,一個簡單的計算器便做好了
❼ C語言程序設計 簡單計算器(急急急!!!!)
#include <dos.h> /*DOS介面函數*/
#include <math.h> /*數學函數的定義*/
#include <conio.h> /*屏幕操作函數*/
#include <stdio.h> /*I/O函數*/
#include <stdlib.h> /*庫函數*/
#include <stdarg.h> /*變數長度參數表*/
#include <graphics.h> /*圖形函數*/
#include <string.h> /*字元串函數*/
#include <ctype.h> /*字元操作函數*/
#define UP 0x48 /*游標上移鍵*/
#define DOWN 0x50 /*游標下移鍵*/
#define LEFT 0x4b /*游標左移鍵*/
#define RIGHT 0x4d /*游標右移鍵*/
#define ENTER 0x0d /*回車鍵*/
void *rar; /*全局變數,保存游標圖象*/
struct palettetype palette; /*使用調色板信息*/
int GraphDriver; /* 圖形設備驅動*/
int GraphMode; /* 圖形模式值*/
int ErrorCode; /* 錯誤代碼*/
int MaxColors; /* 可用顏色的最大數值*/
int MaxX, MaxY; /* 屏幕的最大解析度*/
double AspectRatio; /* 屏幕的像素比*/
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(); /*設置箭頭游標函數*/
/*主函數*/
int main()
{
initialize();/* 設置系統進入圖形模式 */
computer(); /*運行計算器 */
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0); /*結束程序*/
}
/* 設置系統進入圖形模式 */
void initialize(void)
{
int xasp, yasp; /* 用於讀x和y方向縱橫比*/
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)
{
struct viewporttype vp; /*定義視口類型變數*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作數和計算結果變數*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";/* 定義字元串在按鈕圖形上顯示的符號 */
mwindow( "Calculator" ); /* 顯示主窗口 */
color = 7; /*設置灰顏色值*/
getviewsettings( &vp ); /* 讀取當前窗口的大小*/
width=(vp.right+1)/10; /* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; /*設置按鈕高度 */
x = width /2; /*設置x的坐標值*/
y = height/2; /*設置y的坐標值*/
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."); /*輸出字元串"0."*/
x =2*width-width/2; /*設置x的坐標值*/
y =2*height+height/2; /*設置y的坐標值*/
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]);
/*將字元保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =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); /*移動游標到x,y位置*/
arrow(); /*顯示游標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*設置str2為空串*/
while((v=specialkey())!=45) /*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER) /*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); /*顯示游標圖象*/
if(v==RIGHT) /*右移箭頭時新位置計算*/
if(x>=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字元位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否則,右移到下一個字元位置*/
if(v==LEFT) /*左移箭頭時新位置計算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
} /*如果移到頭,再左移,則移動到最右邊字元位置*/
else
{
x=x-width-width/2;
m--;
} /*否則,左移到前一個字元位置*/
if(v==UP) /*上移箭頭時新位置計算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到頭,再上移,則移動到最下邊字元位置*/
else
{
y=y-height-height/2;
n--;
} /*否則,移到上邊一個字元位置*/
if(v==DOWN) /*下移箭頭時新位置計算*/
if(y>=7*height)
{
y=y0;
n=0;
❽ c語言一個簡單計算器的設計程序,代碼如下,不是很看得懂,求解釋!
先說頭文件
從第一個開始 基本輸入輸出
第二個 第三個 都是 輸入輸出,都差不多,
定義了 一個 整形 a 數組 裡面有10000個數
定義了 一個字元型 b 裡面有10000個數
然後 定義 i,num,c 沒有賦值
其餘的那幾個 就是定義給一個值
在 do while語句里
叫你輸入一個整數 一個字元,值 傳遞給了 num和c
這里是先做後循環,然後a是數組加上 cnt1加1的值給num,另一個給 c,開始循環,並判斷 c如果不等於 =號 ,就做下面的賦值,
下面接上for循環,i的循環條件來自於cnt,做一次 j減一次i,剩下的就都是判斷數組的符號,看起來像是個計算器,他們的條件就是 條件1和條件2和條件3和條件4這樣,判斷完給一個返回,把值返回給數組,然後再顯示出來,如果有輸入錯誤 或判斷錯誤 就會從最下面error那句列印出來
不懂可以再來追問我
❾ 求助用C語言程序設計一個計算器
已經在TC下編譯通過,有界面
#include <dos.h> /*DOS介面函數*/
#include <math.h> /*數學函數的定義*/
#include <conio.h> /*屏幕操作函數*/
#include <stdio.h> /*I/O函數*/
#include <stdlib.h> /*庫函數*/
#include <stdarg.h> /*變數長度參數表*/
#include <graphics.h> /*圖形函數*/
#include <string.h> /*字元串函數*/
#include <ctype.h> /*字元操作函數*/
#define UP 0x48 /*游標上移鍵*/
#define DOWN 0x50 /*游標下移鍵*/
#define LEFT 0x4b /*游標左移鍵*/
#define RIGHT 0x4d /*游標右移鍵*/
#define ENTER 0x0d /*回車鍵*/
void *rar; /*全局變數,保存游標圖象*/
struct palettetype palette; /*使用調色板信息*/
int GraphDriver; /* 圖形設備驅動*/
int GraphMode; /* 圖形模式值*/
int ErrorCode; /* 錯誤代碼*/
int MaxColors; /* 可用顏色的最大數值*/
int MaxX, MaxY; /* 屏幕的最大解析度*/
double AspectRatio; /* 屏幕的像素比*/
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(); /*設置箭頭游標函數*/
/*主函數*/
int main()
{
initialize();/* 設置系統進入圖形模式 */
computer(); /*運行計算器 */
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0); /*結束程序*/
}
/* 設置系統進入圖形模式 */
void initialize(void)
{
int xasp, yasp; /* 用於讀x和y方向縱橫比*/
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)
{
struct viewporttype vp; /*定義視口類型變數*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作數和計算結果變數*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";/* 定義字元串在按鈕圖形上顯示的符號 */
mwindow( "Calculator" ); /* 顯示主窗口 */
color = 7; /*設置灰顏色值*/
getviewsettings( &vp ); /* 讀取當前窗口的大小*/
width=(vp.right+1)/10; /* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; /*設置按鈕高度 */
x = width /2; /*設置x的坐標值*/
y = height/2; /*設置y的坐標值*/
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."); /*輸出字元串"0."*/
x =2*width-width/2; /*設置x的坐標值*/
y =2*height+height/2; /*設置y的坐標值*/
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]);
/*將字元保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =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); /*移動游標到x,y位置*/
arrow(); /*顯示游標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*設置str2為空串*/
while((v=specialkey())!=45) /*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER) /*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); /*顯示游標圖象*/
if(v==RIGHT) /*右移箭頭時新位置計算*/
if(x>=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字元位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否則,右移到下一個字元位置*/
if(v==LEFT) /*左移箭頭時新位置計算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
} /*如果移到頭,再左移,則移動到最右邊字元位置*/
else
{
x=x-width-width/2;
m--;
} /*否則,左移到前一個字元位置*/
if(v==UP) /*上移箭頭時新位置計算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到頭,再上移,則移動到最下邊字元位置*/
else
{
y=y-height-height/2;
n--;
} /*否則,移到上邊一個字元位置*/
if(v==DOWN) /*下移箭頭時新位置計算*/
if(y>=7*height)
{
y=y0;
n=0;
} /*如果移到尾,再下移,則移動到最上邊字元位置*/
else
{
y=y+height+height/2;
n++;
} /*否則,移到下邊一個字元位置*/
putimage(x,y,rar,XOR_PUT); /*在新的位置顯示游標箭頭*/
}
c=str1[n*5+m]; /*將字元保存到變數c中*/
if(isdigit(c)||c=='.') /*判斷是否是數字或小數點*/
{
if(flag==-1) /*如果標志為-1,表明為負數*/
{
strcpy(str2,"-"); /*將負號連接到字元串中*/
flag=1;
} /*將標志值恢復為1*/
sprintf(temp,"%c",c); /*將字元保存到字元串變數temp中*/
strcat(str2,temp); /*將temp中的字元串連接到str2中*/
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,""); /*將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) /*如果str2為空,說明是負號,而不是減號*/
flag=-1; /*設置負數標志*/
else
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,""); /*將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,""); /*將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,""); /*將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,""); /*將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."); /*顯示字元串*/
}
if(c=='%')
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,""); /*將str2清空*/
act=6; /*做計算模運算乘方標志值*/
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=='=')
{
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; /*做x的y次方*/
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); /*將結果保存到temp中*/
outtextxy(5*width,height,temp); /*顯示結果*/
}
if(c=='c')
{
num1=0; /*將兩個操作數復位0,符號標志為1*/
num2=0;
flag=1;
strcpy(str2,""); /*將str2清空*/
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=='Q')exit(0); /*如果選擇了q回車,結束計算程序*/
}
putimage(x,y,rar,XOR_PUT); /*在退出之前消去游標箭頭*/
return; /*返回*/
}
/*窗口函數*/
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 );/*將當前視口信息裝入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; /*只取特殊鍵的掃描值,其餘為0*/
return(key); /*返回鍵值*/
}
❿ 怎麼用C語言程序設計一個簡單計算器
#include<<a href="https://www..com/s?wd=stdio.h&tn=44039180_cpr&fenlei=-bIi4WUvYETgN-TLwGUv3EPH6srjc4rH61" target="_blank" class="-highlight">stdio.h</a>>
void main() { float x,y,z; char c;
scanf("%f%c%f",&x,&c,&y);
switch ( c ) {
case '+': z=x+y; break;
case '-': z=x-y; break;
case '*': z=x*y; break;
case '/': z=( y==0 )?(0):(x/y); break;
default: z=0; break;
}
printf("%f%c%f=%f ",x,c,y,z);
}