当前位置:首页 » 编程软件 » 简易计算器的编程

简易计算器的编程

发布时间: 2023-02-10 14:38:58

‘壹’ 怎样用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

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:750
制作脚本网站 发布:2025-10-20 08:17:34 浏览:1012
python中的init方法 发布:2025-10-20 08:17:33 浏览:719
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:879
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:774
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1127
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:351
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:229
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:911
python股票数据获取 发布:2025-10-20 07:39:44 浏览:876