簡易計算器的編程
『壹』 怎樣用vb編程一部簡單的計算器呢
1、打開VB新建一個EXE。

『貳』 用c語言編程一個計算器
#include
<stdio.h>
#include
<conio.h>
#include
<stdlib.h>
int
func_1(
int
n1,
int
n2
)
{
int
sum=0;/*存放最後結果*/
int
i;
for(i=n1;
i<=n2;
i++)
sum
+=
i;
return
sum;
}
long
func_2(
int
n
)
{
if(1==n
||
0==n)
return
1;
else
return
n*func_2(n-1);
}
int
func_3(
int
x,
int
y
)
{
if(0==y)
return
x;
else
return
func_3(y,x%y);
}
void
func_4(
)
{
system("cls");
puts("=======簡易計算器========");
puts("1.累加和");
puts("2.階乘");
puts("3.最大公約數");
puts("4.退出");
puts("=========================");
}
int
main()
{
int
num1,num2;
char
ch;
while(ch!='4')
{
/*讀入用戶的選擇*/
do
{
system("cls");
func_4();
printf("請選擇(1-4):");
ch=getchar();
}while(ch>'4'
||
ch<'1');
switch(ch)
{
case
'1':
printf("請輸入兩個整數:");
scanf("%d%d",&num1,&num2);
printf("%d和%d的累計和為:%d\n",num1,num2,func_1(num1,num2));
getch();
break;
case
'2':
printf("請輸入一個正整數:");
scanf("%d",&num1);
printf("%d的階乘為%d\n",num1,func_2(num1));
getch();
break;
case
'3':
printf("請輸入兩個整數:");
scanf("%d%d",&num1,&num2);
printf("%d和%d的最大公約數為:%d\n",num1,num2,func_3(num1,num2));
getch();
break;
case
'4':
puts("請按任意鍵結束程序!");
getch();
}
}
return
0;
}
『叄』 用C語言設計一個簡單計算器
#include<stdio.h>
voidadd(inta,intb,intc)
{
c=a+b;
printf("%d ",c);
printf(" ");
}
voidminus(inta,intb,intc)
{
c=a-b;
printf("%d ",c);
printf(" ");
}
voidmultiplication(inta,intb,intc)
{
c=a*b;
printf("%d ",c);
printf(" ");
}
voiddiv(inta,intb,intc)
{
c=(float)a/(float)b;
printf("%f ",c);
printf(" ");
}
main()
{
inta,b,c;
charp;
puts("inputA: ");
scanf("%d",&a);
puts("inputB: ");
scanf("%d",&b);
puts("inputoperation: ");
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("沒有注冊這個運算符號 ");
}
以上是設計的一個簡易計算器。可以進行相應的加減乘除。
『肆』 c語言編寫 編寫一個簡單的計算器,實現兩個整型數的四則運算。
1、打開CodeBlocks,新建一個空白文件,先定義頭文件和主函數,接著寫程序多大的主體:

『伍』 簡單計算器編程
嘿嘿,C++實現個Console環境中的計算器很簡單,鑒於你沒給懸賞分,我也懶得打字了,從別處粘貼過來的代碼,非常簡單,你可以參考一下。
// Ex6_09Extended.cpp
// A program to implement a calculator accepting parentheses
#include <iostream> // For stream input/output
#include <cstdlib> // For the exit() function
#include <cctype> // For the isdigit() function
#include <cstring> // For the strcpy() function
using std::cin;
using std::cout;
using std::endl;
void eatspaces(char* str); // Function to eliminate blanks
double expr(char* str); // Function evaluating an expression
double term(char* str, int& index); // Function analyzing a term
double number(char* str, int& index); // Function to recognize a number
char* extract(char* str, int& index); // Function to extract a substring
const int MAX = 80; // Maximum expression length,
// including '\0'
int main()
{
char buffer[MAX] = {0}; // Input area for expression to be evaluated
cout << endl
<< "Welcome to your friendly calculator."
<< endl
<< "Enter an expression, or an empty line to quit."
<< endl;
for(;;)
{
cin.getline(buffer, sizeof buffer); // Read an input line
eatspaces(buffer); // Remove blanks from input
if(!buffer[0]) // Empty line ends calculator
return 0;
cout << "\t= " << expr(buffer) // Output value of expression
<< endl << endl;
}
}
// Function to eliminate spaces from a string
void eatspaces(char* str)
{
int i = 0; // 'Copy to' index to string
int j = 0; // 'Copy from' index to string
while((*(str + i) = *(str + j++)) != '\0') // Loop while character
// copied is not \0
if(*(str + i) != ' ') // Increment i as long as
i++; // character is not a space
return;
}
// Function to evaluate an arithmetic expression
double expr(char* str)
{
double value = 0.0; // Store result here
int index = 0; // Keeps track of current character position
value = term(str, index); // Get first term
for(;;) // Indefinite loop, all exits inside
{
switch(*(str + index++)) // Choose action based on current character
{
case '\0': // We're at the end of the string
return value; // so return what we have got
case '+': // + found so add in the
value += term(str, index); // next term
break;
case '-': // - found so subtract
value -= term(str, index); // the next term
break;
default: // If we reach here the string
cout << endl // is junk
<< "Arrrgh!*#!! There's an error"
<< endl;
exit(1);
}
}
}
// Function to get the value of a term
double term(char* str, int& index)
{
double value = 0.0; // Somewhere to accumulate
// the result
value = number(str, index); // Get the first number in the term
// Loop as long as we have a good operator
while((*(str + index) == '*') || (*(str + index) == '/'))
{
if(*(str + index) == '*') // If it's multiply,
value *= number(str, ++index); // multiply by next number
if(*(str + index) == '/') // If it's divide,
value /= number(str, ++index); // divide by next number
}
return value; // We've finished, so return what
// we've got
}
// Function to recognize a number in a string
double number(char* str, int& index)
{
double value = 0.0; // Store the resulting value
if(*(str + index) == '(') // Start of parentheses
{
char* psubstr = 0; // Pointer for substring
psubstr = extract(str, ++index); // Extract substring in brackets
value = expr(psubstr); // Get the value of the substring
delete[]psubstr; // Clean up the free store
return value; // Return substring value
}
while(isdigit(*(str + index))) // Loop accumulating leading digits
value = 10*value + (*(str + index++) - '0');
// Not a digit when we get to here
if(*(str + index) != '.') // so check for decimal point
return value; // and if not, return value
double factor = 1.0; // Factor for decimal places
while(isdigit(*(str + (++index)))) // Loop as long as we have digits
{
factor *= 0.1; // Decrease factor by factor of 10
value = value + (*(str + index) - '0')*factor; // Add decimal place
}
return value; // On loop exit we are done
}
// Function to extract a substring between parentheses
// (requires cstring)
char* extract(char* str, int& index)
{
char buffer[MAX]; // Temporary space for substring
char* pstr = 0; // Pointer to new string for return
int numL = 0; // Count of left parentheses found
int bufindex = index; // Save starting value for index
do
{
buffer[index - bufindex] = *(str + index);
switch(buffer[index - bufindex])
{
case ')':
if(numL == 0)
{
size_t size = index - bufindex;
buffer[index - bufindex] = '\0'; // Replace ')' with '\0'
++index;
pstr = new char[index - bufindex];
if(!pstr)
{
cout << "Memory allocation failed,"
<< " program terminated.";
exit(1);
}
strcpy_s(pstr, index-bufindex, buffer); // Copy substring to new memory
return pstr; // Return substring in new memory
}
else
numL--; // Rece count of '(' to be matched
break;
case '(':
numL++; // Increase count of '(' to be
// matched
break;
}
} while(*(str + index++) != '\0'); // Loop - don't overrun end of string
cout << "Ran off the end of the expression, must be bad input."
<< endl;
exit(1);
return pstr;
}
上面的代碼來自《Lvor Horton's Begining Visual C++ 2008》一書,非常適合C++初學者。用上面代碼實現的計算器可以進行諸如(1+3)*6/2之類的加減乘除和帶括弧的表達式運算。
此外,在Linux系統的Shell環境中,有個bc計算器程序,功能更強一些,有興趣的話可以找來源代碼看看。
『陸』 如何在計算器上編程
可以在手機上安裝可編程的計算器。例如使用易歷知食軟體內部的可編程計算器,就可以在計算器上編程,下面示例是編寫一個計算圓面積的函數c,並在計算器中用函數c來計算半徑為6的圓的面積,如下圖所示:

『柒』 c語言設計一個簡單的計算器程序
#include<stdio.h>//計算器
voidmenu()//自定義的菜單界面
{
printf("--------------------\n");
printf("請輸入你的選擇\n");
printf("1.+\n");
printf("2.-\n");
printf("3.*\n");
printf("4./\n");
printf("--------------------\n");
}
intmain()
{
inti=0;
intj=0;
intnum=0;//計算結果存放在nun
intselect=0;//選擇的選項存放在select
do//do-while先執行再判斷循環條件,即可實現重復計算功能
{
menu();//列印出菜單界面
scanf("%d",&select);//輸入你的選項
printf("請輸入計算值:");
scanf("%d%d",&i,&j);//輸入要計算的數值
switch(select)
{
case1:
printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能
break;
case2:
printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能
break;
case3:
printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能
break;
case4:
printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能
break;
default:
printf("輸入有誤重新選擇");
break;
}
}while(select);
return0;
}
運行結果:

(7)簡易計算器的編程擴展閱讀:
return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。
return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。
『捌』 C語言編寫簡易計算器程序
C語言編寫計算器
我們可以用printf和scanf函數輸出結果和獲取用戶的輸入。需要<stdio.h>頭文件。scanf函數在讀取數據的時候不需要再一行上輸入每個數據,只要數據和數據之間留出空白就可以了。先聲明兩個變數number1和number2,operation變數用來存儲運算符。用scanf函數獲取這兩個數字和運算符。分別用%lf %c %lf

