❶ c語言計算器 功能要求: 可以輸入0-9數字、小數點、正負數 可以進行加減乘除運算及簡單的數學函數運算
下面是使用 C 語言設計簡易科學計算器的示例代碼:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
char op;
double num1, num2;
printf("請輸入數學表達式(如:1 + 2):");
scanf("%lf %c %lf", &num1, &op, &num2);
switch (op)
{
case '+':
printf("結果為:%.2lf\n", num1 + num2);
break;
case '-':
printf("結果為:%.2lf\n", num1 - num2);
break;
case '*':
printf("結果為:%.2lf\n", num1 * num2);
break;
case '/':
if (num2 == 0)
{
printf("除數不能為 0!\n");
}
else
{
printf("結果為:%.2lf\n", num1 / num2);
}
break;
case '^':
printf("結果為畝茄:%.2lf\n", pow(num1, num2));
break;
case 's':
printf("結果為:%.2lf\n", sin(num1));
break;
case 'c':
printf("結果為:%.2lf\n", cos(num1));
break;
case 't':
printf("結果為:%.2lf\n", tan(num1));
break;
case 'l':
printf("結果為:%.2lf\n", log(num1));
break;
case 'e':
printf("結果為:%.2lf\n", exp(num1));
break;
default:
printf("山哪無效的運算符!逗耐碼\n");
break;
}
return 0;
}
❷ c語言編寫 編寫一個簡單的計算器,實現兩個整型數的四則運算。
#include<stdio.h>
int main()
{int a,b,c,err;
char op;
do
{scanf("%d%c%d",&a,&op,&b);
err=0;
if(a==0&&b==0)break;
if(op=='+')c=a+b;
if(op=='-')c=a-b;
if(op=='*')c=a*b;
if(op=='/')
if(b)c=a/b;else err=1;
if(op=='%')
if(b)c=a%b;else err=1;
if(err)printf("%d%c%d error! ",a,b,c);
else printf("%d%c%d=%d ",a,op,b,c);
}while(1);
return 0;
}
❸ 怎麼用C語言設計一個簡單計算器
怎麼用C語言設計一個簡單計算器 #include<iostream>
#include<math.h>
using namespace std;
main ()
{
char k;
double s,g;
k='-';
cout<<"求絕對值請按A;求平方根請按B;加減乘除清分別按+-*/" <<endl;
cin>>g;
while (1)
{
cin>>k;
if (k=='=')
break;
if (k=='A')
{
g=fabs (g);
break;
}
if (k=='B')
{
g=sqrt (g);
break;
}
cin>>s;
if (k=='+')
g+=s;
if (k=='-')
g-=s;
if (k=='*')
g*=s;
if (k=='/')
{
if (s==0)
cout<<"wrong";
else
g/=s;
}
}
cout<<g;
}
#include<stdio.h> void add(int a,int b,int c) { c=a+b; printf("%d\t",c); printf("\n"); } void minus(int a,int b,int c) { c=a-b; printf("%d\t",c); printf("\n"); } void multiplication(int a,int b,int c) { c=a*b; printf("%d\t",c); printf("\n"); } void div(int a,int b,int c) { c=(float)a/(float)b; printf("%f\t",c); printf("\n"); } main() { int a,b,c; char p; puts("input A:\n"); scanf("%d",&a); puts("input B:\n"); scanf("%d",&b); puts("input operation:\n"); getchar(); p=getchar(); if(p=='+') add(a,b,c);else if(p=='-') minus(a,b,c);else if(p=='*') multiplication(a,b,c);else if(p=='/') div(a,b,c);else puts("沒有注冊這個運運算元號\n"); } 以上是設計的一個簡易計算器。可以進行相應的加減乘除。 簡介:
C語言是一種計算機程式設計語言,它既具有高階語言的特點,又具有組合語言的特點。它由美國貝爾研究所的D.M.Ritchie於1972年推出,1978年後,C語言已先後被移植到大、中、小及微型機上,它可以作為工作系統設計語言,編寫系統應用程式,也可以作為應用程式設計語言,編寫不依賴計算機硬體的應用程式。它的應用范圍廣泛,具備很強的資料處理能力,不僅僅是在軟體開發上,而且各類科研都需要用到C語言,適於編寫系統軟體,三維,二維圖形和動畫,具體應用比如微控制器以及嵌入式系統開發。
求用C語言設計一個簡單計算器
應該不難,就是按鈕的訊息傳遞處理函式等,
用C語言怎麼設計一個簡單計算器?
#include<stdio.h>
void add(int a,int b,int c)
{
c=a+b;
printf("%d\t",c);
printf("\n");
}
void minus(int a,int b,int c)
{
c=a-b;
printf("%d\t",c);
printf("\n");
}
void multiplication(int a,int b,int c)
{
c=a*b;
printf("%d\t",c);
printf("\n");
}
void div(int a,int b,int c)
{
c=(float)a/(float)b;
printf("%f\t",c);
printf("\n");
}
main()
{
int a,b,c;
char p;
puts("input A:\n");
scanf("%d",&a);
puts("input B:\n");
scanf("%d",&b);
puts("input operation:\n");
getchar();
p=getchar();
if(p=='+') add(a,b,c);else
if(p=='-') minus(a,b,c);else
if(p=='*') multiplication(a,b,c);else
if(p=='/') div(a,b,c);else
puts("沒有注冊這個運運算元號\n");
}
怎麼用C語言程式設計一個簡單計算器?
#include<<a href=":./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\n",x,c,y,z);
}
C語言是一門通用計算機程式語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低階儲存器、產生少量的機器碼以及不需要任何執行環境支援便能執行的程式語言。
盡管C語言提供了許多低階處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程式可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(微控制器或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國家標准語法,稱為ANSI C,作為C語言最初的標准。[1] 目前2011年12月8日,國際標准化組織(ISO)和國際電工委員會(IEC)釋出的C11標準是C語言的第三個官方標准,也是C語言的最新標准,該標准更好的支援了漢字函式名和漢字識別符號,一定程度上實現了漢字程式設計。
用C語言編一個簡單計算器
已經為你實現了加減乘除了。 main.c 簡易計算器 Created by shengan on 15/6/29. Copyright (c) 2015年 shengan. All rights reserved.#include <stdio.h>double plus();double minus();double multi();double divide();int main(int argc, const char * argv[]) { insert code here... double a, b; char c; while(1 == 1) { printf("簡易計算器\n"); printf("輸入第一個數:\n"); scanf("%lf", &a); printf("輸入運運算元:\n"); scanf("%s", &c); printf("輸入第二個數:\n"); scanf("%lf", &b); switch (c) { case '+': printf("%lf %c %lf = %lf", a, c, b, plus(a, b)); break; case '-': printf("%lf %c %lf = %lf", a, c, b, minus(a, b)); break; case '*': printf("%lf %c %lf = %lf", a, c, b, minus(a, b)); break; case '/': printf("%lf %c %lf = %lf", a, c, b, minus(a, b)); break; default: printf("運運算元號輸入錯誤!\n"); break; } } return 0;}加法double plus(double a, double b){ return a + b;}double minus(double a, double b){ return a - b;}double multi(double a, double b){ return a * b;}double divide(double a, double b){ return a / b;}
設計一個簡單計算器
#include <stdio.h>
int main(void)
{
int data1, data2;
char op;
printf("please input data1 op data2:");
scanf("%d %c %d", &data1 , &op , &data2);
switch (op)
{
case '+':
printf("%d + %d = %.0f\n", data1, data2, (double)data1 + (double)data2);
break;
case '-':
printf("%d - %d = %.0f\n", data1, data2, (double)data1 - (double)data2);
break;
case '*':
printf("%d * %d = %.0f\n", data1, data2, (double)data1 * (double)data2);
break;
case '/':
if (data2 == 0)
{
printf("Error! chu shu wei 0.");
}
else
{
printf("%d / %d = %.0f\n", data1, data2, (double)data1 / (double)data2);
}
break;
case '%':
if (data2 == 0)
{
printf("Error! chu shu wei 0.");
}
else
{
printf("%d %% %d=%d3\n", data1, data2, data1 % data2);
}
break;
default:
printf("Unknown operator\n");
}
return 0;
}如果要求是浮點數的話換一下資料型別就可以了
用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(void); 獲取使用者輸入的計算值
float add(void); 定義加法函式
float subtraction(void); 定義減法函式
float multiplication(void); 定義乘法函式
float division(void); 定義除法函式
float extract(void); 定義開方函式
float square(void); 定義平方函式
float cube(void); 定義立方函式
int count = 0;
int main(void)
{
char choice;
printf("***歡迎使用由小錢製作的計算器***\n");
choice = get_choice();
while(choice != 'q')
{
switch(choice)
{
case 'a':
add(); break;
case 'b':
subtraction(); break;
case 'c':
multiplication(); break;
case 'd':
division(); break;
case 'e':
extract(); break;
case 'f':
square(); break;
case 'g':
cube(); break;
default :
printf("您輸入有誤,請重新輸入:"); break;
}
fflush(stdin);
choice = get_choice();
}
printf("bye");
return 0;
}
獲取使用者輸入的選項,並建立目錄
char get_choice(void)
{
char ch;
int a = 0;
建立目錄
printf("\n--------------------------------\n");
printf("a. 加法\t\t\tb. 減法\nc. 乘法\t\t\td. 除法\n");
printf("e. 開方\t\t\tf. 平方\ng. 立方\t\t\tq. 退出\n");
printf("--------------------------------\n");
printf("請輸入你的選項:");
ch = get_first();
while(ch == ' ' || ch == '\n' || ch == '\t')
ch = get_first();
判斷使用者輸入的選項是否有誤
while((ch<'a' || ch>'g') && ch !='q')
{
putchar(ch);
printf(" 你輸入的選項有誤,請重新輸入:");
ch = get_first();
}
return ch;
}
獲取使用者輸入的選項,並剔除錯誤輸入
char get_first(void)
{
char ch;
ch = getchar();
剔除由使用者輸入選項時產生的換行符
while(ch == '\n')
{
ch = getchar();
}
return ch;
}
獲取使用者輸入的計算值
float get_int(void)
{
float input;
char ch;
int a;
if(count == 0)
printf("親!請輸入數值:");
if(count == 1)
printf("親!請輸入第一個數值:");
if(count == 2)
printf("親!請輸入第二個數值:");
a = scanf("%f", &input);
判斷使用者的輸入是否為一個數值
while(a != 1)
{
剔除使用者輸入錯誤的字元
while((ch = getchar()) != '\n')
{
putchar(ch);
printf(" 不是一個數值,請輸入例如3、111.2、或者-1");
a = scanf("%f", &input);
}
}
return input;
}
定義加法函式
float add(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
sum = i + j;
printf("%.2f + %.2f = %.2f\n", i, j, sum);
return sum;
}
定義減法函式
float subtraction(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
sum = i - j;
printf("%.2f - %.2f = %.2f\n", i, j, sum);
return sum;
}
定義乘法函式
float multiplication(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
sum = i * j;
printf("%.2f * %.2f = %.2f\n", i, j, sum);
return sum;
}
定義除法函式
float division(void)
{
float i, j, sum;
count = 0;
count = count+1;
i = get_int();
count = count+1;
j = get_int();
判斷除數是否為0
while(j == 0)
{
printf("除數不能為0\n請重新輸入!!!\n");
j = get_int();
}
sum = i / j;
printf("%.2f / %.2f = %.2f\n", i, j, sum);
return sum;
}
定義開方函式
float extract(void)
{
float i, sum;
count = 0;
i = get_int();
判斷開方數是否小於0,如果小於0,則讓使用者重新輸入
while(i < 0)
{
printf("請輸入大於0的數值\n");
i = get_int();
}
sum = sqrt(i);
printf("%.2f的開方等於%.2f\n", i, sum);
return sum;
}
定義平方函式
float square(void)
{
float i, sum;
count = 0;
i = get_int();
sum = i * i;
printf("%.2f的平方等於%.2f\n", i, sum);
return sum;
}
定義立方函式
float cube(void)
{
float i, sum;
count = 0;
i = get_int();
sum = i * i * i;
printf("%f的立方等於%.3f\n", i, sum);
return sum;
}
❹ 簡單的用c語言寫一個計算器程式,加減乘除能用就好
簡單的用c語言寫一個計算器程式,加減乘除能用就好 #include"stdio.h"
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/b;break;
}
printf("%f%c%f=%f",a,e,b,c);
}
如何用vc++編寫一個簡單的(只有加減乘除)計算器程式?
先設定介面如下
加法按鈕程式碼
void CMy03Dlg::OnBnClickedButton1()
{
TODO:在此新增控制元件通知處理程式程式碼
UpdateData(TRUE);
m_Nub3=m_Nub1+m_Nub2;
UpdateData(FALSE);
}
減法按鈕程式碼
void CMy03Dlg::OnBnClickedButton2()
{
TODO:在此新增控制元件通知處理程式程式碼
UpdateData(TRUE);
m_Nub3=m_Nub1-m_Nub2;
UpdateData(FALSE);
}
乘法按鈕程式碼
void CMy03Dlg::OnBnClickedButton3()
{
TODO:在此新增控制元件通知處理程式程式碼
UpdateData(TRUE);
m_Nub3=m_Nub1*m_Nub2;
UpdateData(FALSE);
}
除法按鈕程式碼
void CMy03Dlg::OnBnClickedButton4()
{
TODO:在此新增控制元件通知處理程式程式碼
UpdateData(TRUE);
if(m_Nub2!=0)
m_Nub3=m_Nub1 / m_Nub2;
else
AfxMessageBox("被除數不能為0");
UpdateData(FALSE);
}
清除按鈕程式碼
void CMy03Dlg::OnBnClickedButton5()
{
TODO:在此新增控制元件通知處理程式程式碼
UpdateData(TRUE);
m_Nub3=0;
m_Nub1=0;
m_Nub2=0;
UpdateData(FALSE);
}
結束按鈕程式碼
void CMy03Dlg::OnBnClickedButton6()
{
TODO:在此新增控制元件通知處理程式程式碼
CDialog::OnOK();
}
如果只允許在輸入框中輸入資料應該怎樣處理?
製作托盤程式
目的:在工作列中建立一個圖示,使該程式永遠駐留在記憶體中。例如郵件檢查程式可以作為駐留程式,一旦有郵件來了,就可以接收郵件。
Shell_NotifyIcon函式傳送訊息來增加、刪除、修改工作列的圖示
BOOL TrayMessage(HWND hWnd, DWORD dwMessage, HICON hIcon, PSTR pszTip)
{
BOOL res;
NOTIFYICONDATA tnd;
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = hWnd;
tnd.uID = IDI_ICON1;
tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.uCallbackMessage = WM_MY_TRAY_NOTIFICATION;
tnd.hIcon = hIcon;
lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
res = Shell_NotifyIcon(dwMessage, &tnd); dwMessage為NIM_ADD從工作列中新增圖示、NIM_DELETE從工作列中刪除圖示、NIM_MODIFY改變工作列中圖示
if (hIcon)
DestroyIcon(hIcon);
return res;
}
定義一個回撥訊息:WM_MY_TRAY_NOTIFICATION
在DLG的CPP檔案中,
#define WM_MY_TRAY_NOTIFICATION WM_USER+100
為對話方塊新增訊息對映ON_MESSAGE(WM_MY_TRAY_NOTIFICATION,OnTrayNotification)
在DLG的標頭檔案中應該有
public:
long m_Nub1;
float m_Nub3;
CBitmapButton Button;
afx_msg void OnBnClickedButton1();
long m_Nub2;
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton4();
afx_msg void OnBnClickedButton5();
afx_msg void OnBnClickedButton3();
afx_msg void OnBnClickedButton6();
afx_msg void OnBnClickedButton7();
afx_msg LRESULT OnTrayNotification(WPARAM wparam, LPARAM lparam);
在DLG的CPP檔案中應該有
BEGIN_MESSAGE_MAP(CMailCheckDlg, CDialog)
……
ON_MESSAGE(WM_MY_TRAY_NOTIFICATION,OnTrayNotification)
……
END_MESSAGE_MAP()
並定義一個回撥訊息函式
LRESULT CMailCheckDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)
{
switch (lparam )
{
case WM_RBUTTONUP:
case WM_LBUTTONDBLCLK:修改不同的按鈕處理事件,以觀察圖示退出效果。
ShowWindow(SW_SHOW);
TrayMessage(m_hWnd, NIM_DELETE, NULL, "");從工作列中刪除圖示
}
return 0;
}
在對話方塊視窗上新增「駐留」按鈕,雙擊按鈕新增程式碼
void CMailCheckDlg::OnBnClickedButton1()
{
TODO:在此新增控制元件通知處理程式程式碼
下面程式向工作列新增圖示
TrayMessage(m_hWnd, NIM_ADD, NULL, "計算器程式");
TrayMessage(m_hWnd, NIM_MODIFY, m_hIcon, "計算器程式");
ShowWindow(SW_HIDE);
用MFC編寫一個簡單的加減乘除計算器
我有程式,加31782771群
c語言計算器程式設計包含加減乘除簡單的函式運算
實用計算器之程式設計
[摘 要]多用計算器的構思及設計程式碼
[關鍵詞]多用計算器;設計
數值計算可以說是日常最頻繁的工作了,WIN98提供了「計算器」軟體供使用者使用,該軟體可以處理一般的一步四則運算,例如:3+2、5/3等等,但在日常中使用者經常遇到多步四則運算問題,例如:3+4*5-4/2,45*34/2+18*7等等,那麼該個計算器就無法勝任了,作者製作了一個實用的計算器,該計算器新增不少功能:(程式介面如圖)
1.可以實現連續的四則運算
2.可以實現輸入式子的顯示
3.可以方便計算個人所得稅
4.滑鼠、鍵盤均可輸入資料
5.操作介面友好
6.擊鍵可發聲
構建該個計算器所需研究及解決的核心問題有如下幾個:1、連乘求值?2、字元顯示 3、鍵盤輸入?4、擊鍵發聲?5、個人所得稅法規,為了使大家對程式有更一步認識,現將程式碼提供給讀者參考:
*定義陣列及窗體變數
Dim number2(0 To 50) As Double
Dim number(0 To 50) As Double
Dim z As Integer
Dim k As Integer, r As Integer
Dim j As Integer
Dim str As String
*呼叫名為「playsound」的API函式
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hMole As Long, ByVal dwFlags As Long) As Long
Private Const SND_FILENAME = &H20000?
Private Const SND_ASYNC = &H1?
Private Const SND_SYNC = &H0
*判斷通用過程
Sub pianan(p As String)
r = 0
Dim i As Integer, l As Integer, h As Integer
h = 0
i = 1
If InStr(Trim$(p), "*") <> 0 Then
k = k + 1
End If
If InStr(Trim$(p), "/") <> 0 Then
r = r + 1
End If
End Sub
*連乘通用過程(略)
*各按鈕事件過程
Private sub Command1_Click(Index As Integer)
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Text1.Text = Text1.Text + Command1(Index).Caption
Text2.Text = Text2.Text + Command1(Index).Caption
Text1.SetFocus
End Sub
rivate sub Command10_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
str = Text3.Text
End Sub
Private sub Command11_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Text3.Text = str
End Sub
rivate sub Command2_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianan(Text1.Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "+"
z = z + 1
Text1.SetFocus
End Sub
rivate sub Command3_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianan(Text1.Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "-"
Text1.Text = Text1.Text & "-"
z = z + 1
Text1.SetFocus
End Sub
Private sub Command4_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Text2.Text = Text2.Text + "*"
Text1.Text = Text1.Text + "*"
Text1.SetFocus
End Sub
rivate sub Command5_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Text2.Text = Text2 + "/"
Text1.Text = Text1 + "/"
Text1.SetFocus
End Sub
Private sub Command6_Click()
PlaySound App.Path & "\sound.wav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianan(Text1.Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
z = z + 1
Dim dengyu As Double
Dim v As Integer
For v = 0 To 50
dengyu = dengyu + number2(v)
Next v
If dengyu < 0 Then
Text3.ForeColor = &HFF&
Else
Text3.ForeColor = &HFF0000
End If
Text3.Text = dengyu
Text1.SetFocus
If Len(Text3.Text) >= 14 Then
calcresult.Show
End If
End Sub
rivate sub Command7_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
z = 0: k = 0: r = 0: j = 0
Dim i As Integer
For i = 0 To 50
number(i) = 0
number2(i) = 0
Next i
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End Sub
rivate sub Command8_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
If Val(Text3.Text) = 0 Then
MsgBox "除數不能為0!"
Exit Sub
End If
Text3.Text = 1 / Val(Text3.Text)
End Sub
Private sub Command9_Click()
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Text3.ForeColor = &HFF0000
Text3.Text = Val(Text3.Text) * Val(Text3.Text)
End Sub
rivate sub muninter_Click()
Dim i
i = Shell("C:\Program Files\InterExplorer\iexplore.exe", vbMaximizedFocus)
End Sub
rivate sub munmp3_Click()
Dim i
i = Shell("C:\Program Files\Windows Media Player\mplayer2", vbNormalNoFocus)
End Sub
Private sub mun *** _Click()
Dialog.Show
End Sub
rivate sub muntax_Click()
tax.Show
End Sub
rivate sub munver_Click()
ver.Show
End Sub
rivate sub notepad_Click()
Dim i
i = Shell("c:\windows\notepad", vbNormalFocus)
End Sub
Private sub Text1_KeyPress(KeyAscii As Integer)
PlaySound App.Path & "\start.wav", 0, SND_SYNC
Dim num As Integer
num = Val(KeyAscii)
If num > 47 And num < 58 Then
Text1.Text = Text1.Text + CStr(num - 48)
Text2.Text = Text2.Text + CStr(num - 48)
End If
If num = 46 Then
Text1.Text = Text1.Text + "."
Text2.Text = Text2.Text + "."
End If
If KeyAscii = 43 Then
Dim totle As Double
Dim n As Integer
Call pianan(Text1.Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "+"
z = z + 1
End If
If KeyAscii = 45 Then
Call pianan(Text1.Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "-"
Text1.Text = Text1.Text & "-"
z = z + 1
End If
If KeyAscii = 42 Then
Text2.Text = Text2.Text + "*"
Text1.Text = Text1.Text + "*"
End If
If KeyAscii = 47 Then
Text2.Text = Text2.Text + "/"
Text1.Text = Text1.Text + "/"
End If
If KeyAscii = vbKeyReturn Then
PlaySound App.Path & "\sound.wav", 0, SND_SYNC
Call pianan(Text1.Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
z = z + 1
Dim dengyu As Double
Dim v As Integer
For v = 0 To 50
dengyu = dengyu + number2(v)
Next v
If dengyu < 0 Then
Text3.ForeColor = &HFF&
Else
Text3.ForeColor = &HFF0000
End If
Text3.Text = dengyu
End If
If KeyAscii = vbKeyEscape Then
z = 0: k = 0: r = 0: j = 0
Dim i As Integer
For i = 0 To 50
number(i) = 0
number2(i) = 0
Next i
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End If
If Len(Text3.Text) >= 14 Then
calcresult.Show
End If
End Sub
rivate sub Text3_Change()
tax2.Text1 = Text3.Text
End Sub
用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 iPush(float);
float iPop();
float StaOperand[MaxSize];
int iTop=-1;
char Srcexp[MaxSize];
char Capaexp[MaxSize];
char RevPolishexp[MaxSize];
float NumCapaTab[26];
char validexp[]="*/+-()";
char NumSets[]="0123456789";
char StackSymb[MaxSize];
int operands;
void NumsToCapas(char [], int , char [], float []);
int CheckExpress(char);
int PriorChar(char,char);
int GetOperator(char [], char);
void counterPolishexp(char INexp[], int slen, char Outexp[]);
float CalcRevPolishexp(char [], float [], char [], int);
void main()
{
int ilen;
float iResult=0.0;
printf("enter a valid number string:\n");
memset(StackSymb,0,MaxSize);
memset(NumCapaTab,0,26); A--NO.1, B--NO.2, etc.
gets(Srcexp);
ilen=strlen(Srcexp);
printf("source expression:%s\n",Srcexp);
NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);
printf("Numbers listed as follows:\n");
int i;
for (i=0; i<operands; ++i)
printf("%.2f ",NumCapaTab[i]);
printf("\nCapaexp listed in the following:\n");
printf("%s\n",Capaexp);
ilen=strlen(Capaexp);
counterPolishexp(Capaexp,ilen,RevPolishexp);
printf("RevPolishexp:\n%s\n",RevPolishexp);
ilen=strlen(RevPolishexp);
iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);
printf("\ncounterPolish expression:\n%s%.6f\n",Srcexp,iResult);
}
void iPush(float value)
{
if(iTop<MaxSize) StaOperand[++iTop]=value;
}
float iPop()
{
if(iTop>-1)
return StaOperand[iTop--];
return -1.0;
}
void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[])
{
char ch;
int i, j, k, flg=0;
int sign;
float val=0.0,power=10.0;
i=0; j=0; k=0;
while (i<slen)
{
ch=Srcexp[i];
if (i==0)
{
sign=(ch=='-')?-1:1;
if(ch=='+'||ch=='-')
{
ch=Srcexp[++i];
flg=1;
}
}
if (isdigit(ch))
{
val=ch-'0';
while (isdigit(ch=Srcexp[++i]))
{
val=val*10.0+ch-'0';
}
if (ch=='.')
{
while(isdigit(ch=Srcexp[++i]))
{
val=val+(ch-'0')/power;
power*=10;
}
} end if
if(flg)
{
val*=sign;
flg=0;
}
} end if
write Capaexp array
write NO.j to array
if(val)
{
Capaexp[k++]='A'+j;
Capaexp[k++]=ch;
NumCapaTab[j++]=val; A--0, B--1,and C, etc.
}
else
{
Capaexp[k++]=ch;
}
val=0.0;
power=10.0;
i++;
}
Capaexp[k]='\0';
operands=j;
}
float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen)
{
float sval=0.0, op1,op2;
int i, rt;
char ch;
recursive stack
i=0;
while((ch=RevPolishexp[i]) && i<slen)
{
switch(rt=GetOperator(validexp, ch))
{
case iMUL: op2=iPop(); op1=iPop();
sval=op1*op2;
iPush(sval);
break;
case iDIV: op2=iPop(); op1=iPop();
if(!fabs(op2))
{
printf("overflow\n");
iPush(0);
break;
}
sval=op1/op2;
iPush(sval);
break;
case iADD: op2=iPop(); op1=iPop();
sval=op1+op2;
iPush(sval);
break;
case iSUB: op2=iPop(); op1=iPop();
sval=op1-op2;
iPush(sval);
break;
case iCap: iPush(NumCapaTab[ch-'A']);
break;
default: ;
}
++i;
}
while(iTop>-1)
{
sval=iPop();
}
return sval;
}
int GetOperator(char validexp[],char oper)
{
int oplen,i=0;
oplen=strlen(validexp);
if (!oplen) return -1;
if(isalpha(oper)) return 4;
while(i<oplen && validexp[i]!=oper) ++i;
if(i==oplen || i>=4) return -1;
return i;
}
int CheckExpress(char ch)
{
int i=0;
char ;
while((=validexp[i]) && ch!=) ++i;
if (!)
return 0;
return 1;
}
int PriorChar(char curch, char stach)
{
棧外優先順序高於(>)棧頂優先順序時,才入棧
否則(<=),一律出棧
if (curch==stach) return 0; 等於時應該出棧
else if (curch=='*' || curch=='/')
{
if(stach!='*' && stach!='/')
return 1;
}
else if (curch=='+' || curch=='-')
{
if (stach=='(' || stach==')')
return 1;
}
else if (curch=='(')
{
if (stach==')')
return 1;
}
return 0;
}
void counterPolishexp(char INexp[], int slen, char Outexp[])
{
int i, j, k,pr;
char t;
i=0;
j=k=0;
while (INexp[i]!='=' && i<slen)
{
if (INexp[i]=='(')
StackSymb[k++]=INexp[i];
iPush(*(INexp+i));
else if(INexp[i]==')')
{
if((t=iPop())!=-1)
while((t=StackSymb[k-1])!='(')
{
Outexp[j++]=t;
k--;
}
k--;
}
else if (CheckExpress(INexp[i])) is oparator
{
printf("operator %c k=%d\n",INexp[i],k);
while (k)
{
iPush(*(INexp+i));
if(pr=PriorChar(INexp[i],StackSymb[k-1]))
break;
else
{
if ((t=iPop())!=-1)
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
} end while
StackSymb[k++]=INexp[i]; mon process
}
else if() 變數名
{
printf("operand %c k=%d\n",INexp[i],k);
Outexp[j++]=INexp[i];
}
i++;
}
while (k)
{
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
Outexp[j]='\0';
}
註:程式源於「網路知道」
用verilog編寫一個最簡單的加減乘除的計算器的程式
verilog是有加法器乘法器的。也直接識別 + - * / 符號。
mole kjasdja(a,option,b,result);
input option,a,b;
output result;
always @(a,b,option)
begin
result_r=0; 結果暫存器清零
case(option)
+:result_r=a+b;
-:result_r=a-b;
*:result_r=a*b;
/:result_r=a/b;
assign result =result_r;
endmole
大概演演算法就這樣。寫的倉促,語法可能有誤。另外除法reg型別只能儲存整數部分,小數通過移位操作實現,比較麻煩。比如3/5=0.6
做的時候先3=30,然後30/5=6,然後對6在數碼管的顯示進行調整就好。把6顯示在小數點後面1位就好
用vb編寫一個計算器程式,實現加減乘除,
Dim v As Boolean
Dim s As Integer
Dim X As Double
Dim Y As Double
Private Sub Command1_Click(Index As Integer)
If Form1.Tag = "T" Then
If Index = 10 Then
Text1.Text = "0"
Else
Text1.Text = Command1(Index).Caption
End If
Form1.Tag = ""
Else
Text1.Text = Text1.Text & Command1(Index).Caption
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Form1.Tag = "T"
If v Then
X = Val(Text1.Text)
v = Not v
Else
Y = Val(Text1.Text)
Select Case s
Case 0
Text1.Text = X + Y
Case 1
Text1.Text = X - Y
Case 2
Text1.Text = X * Y
Case 3
If Y <> 0 Then
Text1.Text = X / Y
Else
MsgBox ("不能以0為除數")
Text1.Text = X
v = False
End If
Case 4
Y = 0
v = False
End Select
X = Val(Text1.Text)
End If
s = Index
End Sub
Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
End Sub
控制元件自己新增吧,空間名要和程式碼名一致
求一簡單的加減乘除計算器c++程式
#include<stdio.h>
#include<math.h>
void main()
{
float a,b;
char C;
while(1)
{
scanf("%f%c%f",&a,&C,&b);
if((C!='+')&&(C!='-')&&(C!='*')&&(C!='/'))
break;
switch(C)
{
case '+': printf("%f+%f=%f",a,b,a+b);
break;
case '-': printf("%f-%f=%f",a,b,a-b);
break;
case '*': printf("%f*%f=%f",a,b,a*b);
break;
case '/': printf("%f/%f=%f",a,b,a/b);
break;
}
}
}
想改成按1 2 3 4分別為加減乘除,只需要將程式中的+ - * / 改成1 2 3 4即可。按除了+ - * / 以外的鍵就會退出。
用c++語言編寫一個簡單的計算器程式,會加減乘除就行,本人初學不太會,特訓求幫助
這個是最簡單,簡陋的計算器。很多情況沒考慮進去,例如除數不能為0之類的,真要寫完整的話程式碼還要更多。
程式碼如下: #include <iostream>using namespace std;int main(){ float a, b, result; char operation; cout << "請輸入算式,如1+2並回車:" << endl; cin >> a >> operation >> b; switch(operation) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; default: cout << "輸入非法,程式退出!" << endl; return -1; } cout << endl << "結果為:" << endl << a << operation << b << "=" << result << endl; return 0;}
知道switch函式 嗎 用這個就行
建倆個int型變數 一個字元型變數
❺ 大一c語言編程實現計算器功能實驗報告
#include"stdio.h"
intmain()
{
inta,b,c;
do
{
system("cls");
printf("計算器菜單 ");
printf("======================== ");
printf("1:計算a+b ");
printf("2:計算a-b ");
printf("3:計算a*b ");
printf("4:計算a/b ");
printf("5:計算a%b ");
printf("0:退出 ");
printf("請選擇(0-5):");
scanf("%d",&c);
if(c!=0)
{
printf("請輸入a:");
scanf("%d",&a);
printf("請輸入b:");
scanf("%d",&b);
switch(c)
{
case1:printf("a+b=%d",a+b);break;
case2:printf("a-b=%d",a-b);break;
case3:printf("a*b=%d",a*b);break;
case4:printf("a/b=%d",a/b);break;
case5:printf("a%b=%d",a%b);break;
default:break;
}
printf(" 按任意鍵繼續......");
getch();
}
}
while(c!=0);
return0;
}