当前位置:首页 » 编程语言 » c语言程序设计计算器

c语言程序设计计算器

发布时间: 2023-01-17 16:36:25

Ⅰ 用c语言 (c++) 编写计算器程序

我们平时进行数学运算都是用计算器完成的,那么如何用C语言编写一个计算器呢?下面我给大家分享一下。

工具/材料

Dev C++

  • 01

    首先我们需要在Dev C++软件中创建一个C语言项目,项目类型选择控制台程序,如下图所示

  • 02

    接下来我们在项目下面新建C语言文件,如下图所示

  • 03

    然后我们在C文件中写入计算器逻辑代码,主要是让用户输入计算方式,然后程序自动计算,如下图所示

  • 04

    接下来我们点击运行菜单,选择下拉菜单中的运行选项,如下图所示

  • 05

    最后在弹出的界面中我们输入要计算的公式,程序就会自动计算,如下图所示

Ⅱ 用c语言编写计算器

#include <stdio.h>
struct s_node
{
int data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;

link push(link stack,int value)
{
link newnode;

newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack;
}

link pop(link stack,int *value)
{
link top;
if(stack !=NULL)
{
top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack;
}
else
*value=-1;
}

int empty(link stack)
{
if(stack==NULL)
return 1;
else
return 0;

}

int is_operator(char operator)
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}

int priority(char operator)
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0;
}
}

int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}

void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;

printf("\nPlease input the inorder expression:");
gets(expression);

while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);

operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}

Ⅲ 用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语言设计一个简单计算器

怎么用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语言程序设计,做一个简单计算器。

1、首先,打开Vs 2010,如图。

Ⅶ 用c语言设计一个简单的加减乘除计算器 具体需要这样做

1、打开visual C++ 6.0-文件-新建-文件-C++ Source File。

2、输入预处理命令和主函数:#include /*函数头:输入输出头文件*/,void main()/*空类型:主函数*/。

3、定义变量:int a,b,d; /*定义变量的数据类型为整型*/,char c;/*定义变量的数据类型为字符型*/。

4、输入四则运算式:printf(输入如“3*4”或“5+2”的四则运算式:);/*输出文字提示*/scanf(%d%c%d,&a,&c,&b);/*输入四则运算式*/。

5、判断运算符号:switch(c) /*判断运算符号*/{case'+':d=a+b;break;/*进行加法6、运算*/case'-':d=a-b;break;/*进行减法运算*/case'*':d=a*b;break;/*进行乘法运算*/case'/':d=a/b;break; /*进行除法运算*/}。

7、输出结果:printf(%d%c%d=%d\n,a,c,b,d);/*输出结果*/。

热点内容
乐视1s锁屏密码是12的有多少 发布:2025-07-16 18:45:12 浏览:98
战队宣传片拍摄脚本 发布:2025-07-16 18:42:23 浏览:462
疫情源码 发布:2025-07-16 18:34:53 浏览:794
安卓开发平台怎么样 发布:2025-07-16 18:30:35 浏览:345
电话加密码 发布:2025-07-16 18:29:12 浏览:66
河马云脚本 发布:2025-07-16 18:29:03 浏览:148
格物致知编程 发布:2025-07-16 18:07:54 浏览:950
戴尔服务器系统设置如何设置 发布:2025-07-16 18:02:09 浏览:961
为什么换安卓这么难 发布:2025-07-16 17:14:44 浏览:423
转动密码锁怎么开 发布:2025-07-16 17:14:37 浏览:613