当前位置:文档之家› C语言编写的计算器源代码

C语言编写的计算器源代码

C语言编写的计算器源代码

```c

#include

#include

#include

#include

#define MAX_EXPRESSION_SIZE 100

//栈结构定义

typedef struct

int top;

double data[MAX_EXPRESSION_SIZE];

} Stack;

//初始化栈

void initStack(Stack *s)

s->top = -1;

//入栈

void push(Stack *s, double value)

if (s->top == MAX_EXPRESSION_SIZE - 1)

printf("Stack is full. Cannot push element.\n");

} else

s->data[++(s->top)] = value;

}

//出栈

double pop(Stack *s)

if (s->top == -1)

printf("Stack is empty. Cannot pop element.\n"); return -1;

} else

return s->data[(s->top)--];

}

//获取栈顶元素

double peek(Stack *s)

if (s->top == -1)

return -1;

} else

return s->data[s->top];

}

//判断运算符的优先级

int getPriority(char operator)

switch (operator)

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

default:

return -1;

}

//执行四则运算

double performOperation(double operand1, double operand2, char operator)

switch (operator)

case '+':

return operand1 + operand2;

case '-':

return operand1 - operand2;

case '*':

return operand1 * operand2;

case '/':

if (operand2 != 0)

return operand1 / operand2;

} else

printf("Error: Division by zero.\n");

exit(1);

}

case '^':

return pow(operand1, operand2);

default:

return 0;

}

//计算表达式结果

double evaluateExpression(char *expression) Stack operandStack;

Stack operatorStack;

initStack(&operandStack);

initStack(&operatorStack);

int length = strlen(expression);

for (int i = 0; i < length; i++)

//忽略空格

if (expression[i] == ' ')

continue;

}

//数字直接入栈

if (isdigit(expression[i]))

double num = 0;

while (i < length && (isdigit(expression[i]) , expression[i] == '.'))

num = num * 10 + (expression[i] - '0');

i++;

}

i--;

push(&operandStack, num);

}

//左括号入栈

else if (expression[i] == '(')

push(&operatorStack, expression[i]);

}

//右括号出栈并执行运算,直到遇到左括号

else if (expression[i] == ')')

while (peek(&operatorStack) != '(')

double operand2 = pop(&operandStack);

double operand1 = pop(&operandStack);

char operator = pop(&operatorStack);

double result = performOperation(operand1, operand2, operator);

push(&operandStack, result);

}

pop(&operatorStack);

}

//运算符出栈并执行运算,直到栈空或者遇到优先级较低的运算符else

while (peek(&operatorStack) != -1 &&

getPriority(expression[i]) <= getPriority(peek(&operatorStack))) double operand2 = pop(&operandStack);

double operand1 = pop(&operandStack);

char operator = pop(&operatorStack);

double result = performOperation(operand1, operand2, operator);

push(&operandStack, result);

}

push(&operatorStack, expression[i]);

}

}

//处理剩下的运算符

while (peek(&operatorStack) != -1)

double operand2 = pop(&operandStack);

double operand1 = pop(&operandStack);

char operator = pop(&operatorStack);

double result = performOperation(operand1, operand2, operator);

push(&operandStack, result);

}

return pop(&operandStack); // 返回最终结果

int mai

char expression[MAX_EXPRESSION_SIZE];

printf("Enter an arithmetic expression: ");

fgets(expression, MAX_EXPRESSION_SIZE, stdin);

double result = evaluateExpression(expression);

printf("Result = %.2f\n", result);

return 0;

```

这个计算器可以实现基本的四则运算,支持括号和浮点数。运行时,用户需要输入一个算术表达式,然后计算器会输出结果。计算器使用两个栈来实现表达式的求值,一个栈用于存储操作数,另一个栈用于存储运算符。通过比较运算符的优先级,可以确保表达式求值的正确顺序。

速算,简易计算器c语言代码,可实现加减乘除

#include"stdio.h" #include"math.h" #include"stdlib.h" struct optrstyle { int top; char stack[20]; } struct opndstyle { int top; int stack[20]; } void main() { char ch; while(1) { printf("-----------------------------------------\n"); printf("|-----------欢迎来到速算24--------------|\n"); printf("|----------是否开始游戏?????------------|\n"); printf("|--------- 开始游戏请按'Y'-------------|\n"); printf("|--------- 退出游戏请按其他键-------------|\n"); printf("-----------------------------------------\n"); scanf("%c",&ch); if(ch=='Y' or ch=='y') { } else break; } } int EvaluateExpression() { // 算术表达式求值的算符优先算法。设OPTR 和OPND 分别为运算符栈和运算数栈,OP 为运算符集合。 InitStack(OPTR); Push(OPTR,'#'); InitStack(OPND); c = getchar(); while(c!='#' || GetTop(OPTR)!='#') { if(!In(c,OP))

c语言计算器源代码

# include # include # include # define maxsize 100 typedef double datatype1; typedef char datatype2; typedef struct stack1 { datatype1 data1[maxsize]; int top1; /*栈顶元素*/ }seqstack1,*pseqstack1; /* 顺序栈*/ typedef struct stack2 { datatype2 data2[maxsize]; int top2; /*栈顶元素*/ }seqstack2,*pseqstack2; /* 顺序栈*/ /*栈的初始化*/ pseqstack1 init_seqstack1(void) { pseqstack1 S; S=(pseqstack1)malloc(sizeof(pseqstack1)); if(S) S->top1=-1; return S; } pseqstack2 init_seqstack2(void) { pseqstack2 S; S=(pseqstack2)malloc(sizeof(pseqstack2)); if(S) S->top2=-1; return S; } /*判断栈空*/ int empty_seqstack1(pseqstack1 S) { if(S->top1==-1) return 1; else return 0; } int empty_seqstack2(pseqstack2 S) { if(S->top2==-1)

C语言表达式计算器代码

#include #include #include typedef struct Expression //定义表达式结构体 { int num[64]; char ch[64]; }Exper; typedef struct node //定义字栈 { char e[100]; int top; }linkstack; typedef struct Node //定义整数栈 { int c[100]; int top; }link; char precede(char a,char b) //符号判断 { int i=0,j=0; char sign[]="+-*/()^%"; char prec[8][8]={">><<()<<",">><<()<<",">>>>()<<",">>>>()<<","((((~~((","))))~~))",">>>>()>>",">>>>()> >"}; if (b=='=') { return 0; } while(sign[i]!=a) { i++; } while (sign[j]!=b) { j++;

} return(prec[i][j]); } void push1(linkstack *p,char c) //字符入栈 { p->e[p->top++]=c; } void push2(link *p,int num) //整数入栈 { p->c[p->top++]=num; } char gettop(linkstack *s) //读字符栈顶的元素{ char c; c=s->e[s->top-1]; return(c); } char pop1(linkstack *s) //字符出栈 { char c; c=s->e[--s->top]; return(c); } int pop2(link *N) //整数入栈 { int num; num=N->c[--N->top]; return(num); } int operate(int a, char oper,int b)//进行运算 { int num; switch (oper) {

用C语言编写计算器

用c编写的,有各种检错的功能,用栈和优先级实现。头文件是:ss.h typedef union{float a;char b;} eletype; typedef struct{ eletype *base; eletype *top; int stacksize; }stack; stack * createStack(int); int isempty(stack *); int gettopStack(stack *,eletype *); int push(stack *,eletype); int pop(stack *,eletype *data); stack * createStack(int m) { stack *p; p=(stack *)malloc(sizeof(stack)); if(!p)return 0; p->base=(eletype *)malloc(m*sizeof(eletype)); if(!p->base)return 0; p->top=p->base; p->stacksize=m; return p; } int isempty(stack *lp) { if(lp->top > lp->base)return 0; else return 1; } int gettopStack(stack *lp,eletype *data) { while(isempty(lp))return 0; *data = *(lp->top - 1); return 1; }

int push(stack *lp,eletype data) { if(lp->top - lp->base >= lp->stacksize) { lp->base=(eletype *)realloc(lp->base,(lp->stacksize+1)*sizeof(eletype)); if(!lp->base)return 0; lp->stacksize++; } lp->top++; *(lp->top-1)=data; return 1; } int pop(stack *lp,eletype *data) { while(isempty(lp))return 0; *data = *(--lp->top); return 1; } Main函数的编写: #include "ss.h" #define SHU printf("fuck!! biao da shi cuowu!!!"); int n,x,y=0; float t=0; char c; char panduan(char,char); float yunsuan(float,char,float); int jiexi(char a[100]); float zhuan(char c[20]); main() { char a[100]; int m; char d,l; stack *optr; stack *open; eletype data; eletype * data1; char e[20]; float j,k;

用c语言编写的加减乘除计算器程序

#include<> double jjcc(double st[],int k){ for(int j=1;j47&&st1[i]<58){

sum1=(st1[i]-48)+sum1*10; i++; if(st1[i]=='.'){ i++; while(st1[i]>47&&st1[i]<58){ k=k*; sum2=(st1[i]-48)*k+sum2; i++; } } st2[c]=sum1+sum2; } else{ st2[++c]=st1[i]; i++; sum1=0; c++; } } printf("%.4f\n",jjcc(st2,c+1)); }

C语言编写的计算器源代码

C语言编写的计算器源代码 ```c #include #include #include #include #define MAX_EXPRESSION_SIZE 100 //栈结构定义 typedef struct int top; double data[MAX_EXPRESSION_SIZE]; } Stack; //初始化栈 void initStack(Stack *s) s->top = -1; //入栈 void push(Stack *s, double value) if (s->top == MAX_EXPRESSION_SIZE - 1) printf("Stack is full. Cannot push element.\n");

} else s->data[++(s->top)] = value; } //出栈 double pop(Stack *s) if (s->top == -1) printf("Stack is empty. Cannot pop element.\n"); return -1; } else return s->data[(s->top)--]; } //获取栈顶元素 double peek(Stack *s) if (s->top == -1) return -1; } else return s->data[s->top]; } //判断运算符的优先级

int getPriority(char operator) switch (operator) case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; default: return -1; } //执行四则运算 double performOperation(double operand1, double operand2, char operator) switch (operator) case '+': return operand1 + operand2;

C语言编写一个科学计算器

C语言编写一个科学计算器 科学计算器是一种功能强大的工具,可以进行各种复杂的科学计算。下面是一个使用C语言编写的科学计算器的示例代码,用于演示其基本功能。 ```c #include #include void mainMen printf("------------------------\n"); printf(" 科学计算器 \n"); printf("------------------------\n"); printf("1. 四则运算\n"); printf("2. 求平方根\n"); printf("3. 求立方根\n"); printf("4. 求幂次方\n"); printf("5. 求对数\n"); printf("6. 退出\n"); void arithmeticOperation float num1, num2;

printf("请输入两个操作数:\n"); scanf("%f %f", &num1, &num2); printf("1. 加法结果:%f\n", num1 + num2); printf("2. 减法结果:%f\n", num1 - num2); printf("3. 乘法结果:%f\n", num1 * num2); if (num2 != 0) printf("4. 除法结果:%f\n", num1 / num2); } else printf("操作数2不能为0!\n"); } void squareRoo float num; printf("请输入一个数字:\n"); scanf("%f", &num); printf("平方根:%f\n", sqrt(num)); void cubeRoo float num; printf("请输入一个数字:\n"); scanf("%f", &num);

计算器源代码

#include #include #include using namespace std; const double pi = 3.14159265; const double e = 2.718281828459; const int SIZE = 1000; typedef struct node//为了处理符号而建立的链表(如: 1+(-2)) { char data; node *next; }node; typedef struct stack_num//存储数的栈 { double *top; double *base; }stack_num; typedef struct stack_char//存储运算符号的栈 { char *top; char *base; }stack_char; stack_num S_num;//定义 stack_char S_char;//定义 char fu[18] = {'\n', ')', '+', '-', '*', '/', '%', '^', 'Q', 'L', 'C', 'S', 'T', 'c', 's', 't', '('}; int compare[1000];//表现出各运算符号的优先级 double shu[1000];//存储"数" 的数组 double dai_result;//运算的结果,是为了处理M 运算(简介函数里有M的定义) int biao = 0;//和dia_result 一样,为了处理M 运算 char line[SIZE];//输入的所要计算的表达式 void init()//初始化 { compare[fu[0]] = -2;//用数字的大小表现出符号的优先级 compare[fu[1]] = -1; compare[fu[2]] = 2; compare[fu[3]] = 2; compare[fu[4]] = 4;

C语言计算器源代码

C++语言编写;; include include include using namespace std; const double pi = 3.; const double e = ; const int SIZE = 1000; typedef struct node { i++; ge = 0; biao_dian = 1; } iflinei == 'P' {

shu++p = pi; i++; break; } iflinei == 'E' { shu++p = e; i++; break; } ifflag1 { h = h 10 + linei - '0'; flag = 1; i++; ifbiao_dian

ge++; } else break; } ifflag { ifbiao_dian { int r = 1; forint k = 1; k <= ge; k++ r = 10; h /= r; } shu++p = h; + ^乘方 Foff Enter= "<

cout<<"M在前面结果的基础上继续计算,如:上次结果为10,现输入+2"<>start

C语言实现计算器功能

C语言实现计算器功能 计算机科学中的计算器功能是一个非常常见的问题。在C语言中,我们可以使用各种技术和算法来实现这个功能。在本文中,我将介绍一种简单的实现方法,该方法可以处理基本的四则运算。 首先,我们需要定义我们的计算器结构。在C语言中,我们可以使用结构来组织相关的数据和函数。我们可以定义一个名为 Calculator 的结构体,它包含两个浮点数类型的操作数和一个字符类型的操作符。 ```c typedef struct float operand1; float operand2; char operator; } Calculator; ``` 接下来,我们可以定义一些辅助函数来执行各种计算。我们可以使用switch 语句来根据操作符执行相应的操作。我们可以使用 scanf 函数来读取用户输入的操作数和操作符。 ```c float add(float operand1, float operand2) return operand1 + operand2;

float subtract(float operand1, float operand2) return operand1 - operand2; float multiply(float operand1, float operand2) return operand1 * operand2; float divide(float operand1, float operand2) return operand1 / operand2; void calculate(Calculator* calculator) switch(calculator->operator) case '+': printf("Result: %f\n", add(calculator->operand1, calculator->operand2)); break; case '-': printf("Result: %f\n", subtract(calculator->operand1, calculator->operand2)); break; case '*': printf("Result: %f\n", multiply(calculator->operand1, calculator->operand2)); break;

c语言计算器加减乘除开方混合计算和清零代码

C语言计算器加减乘除开方混合计算和清零代码 一、介绍 计算器是人们日常生活中常用的工具之一,通过计算器可以进行加减乘除等基本运算,还可以进行开方等复杂运算。本文将介绍使用C语言编写计算器的加减乘除开方混合计算和清零代码。 二、加法运算代码 加法运算是计算器最基本的运算之一,以下是C语言中加法运算的代码示例: ```c #include int m本人n() { int num1, num2, sum; printf("请输入两个整数:"); scanf("d d", num1, num2); sum = num1 + num2; printf("它们的和是:d\n", sum); return 0; } ```

三、减法运算代码 接下来是减法运算的C语言代码示例: ```c #include int m本人n() { int num1, num2, diff; printf("请输入两个整数:"); scanf("d d", num1, num2); diff = num1 - num2; printf("它们的差是:d\n", diff); return 0; } ``` 四、乘法运算代码 乘法运算是计算器中常用的运算之一,以下是C语言中乘法运算的代码示例: ```c #include

int m本人n() { int num1, num2, product; printf("请输入两个整数:"); scanf("d d", num1, num2); product = num1 * num2; printf("它们的积是:d\n", product); return 0; } ``` 五、除法运算代码 除法运算涉及到除数不能为0的情况,以下是C语言中除法运算的代码示例: ```c #include int m本人n() { float num1, num2, quotient; printf("请输入两个数:"); scanf("f f", num1, num2); if(num2 != 0) {

c语言小项目开发实例

c语言小项目开发实例 C语言是一种广泛应用于计算机科学领域的编程语言,它具有高效、简洁、可移植等特点,因此在各种小项目开发中都有着广泛的应用。本文将介绍几个基于C语言的小项目开发实例,希望能够对初学者有所帮助。 一、简单计算器 计算器是我们日常生活中经常使用的工具,而用C语言编写一个简单的计算器也是一个不错的练手项目。下面是一个简单的计算器代码: ``` #include int main() { char op; float num1, num2, result; printf("请输入运算符:+、-、*、/\n"); scanf("%c", &op); printf("请输入两个数字:\n"); scanf("%f %f", &num1, &num2);

switch(op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: printf("输入的运算符有误!\n"); return 0; } printf("计算结果为:%f\n", result);

return 0; } ``` 这个计算器可以进行加、减、乘、除四种基本运算,用户只需要输入运算符和两个数字即可得到计算结果。这个小项目可以帮助初学者熟悉C语言的基本语法和流程控制语句。 二、猜数字游戏 猜数字游戏是一种简单有趣的游戏,玩家需要根据提示猜出一个随机生成的数字。下面是一个简单的猜数字游戏代码: ``` #include #include #include int main() { int num, guess, count = 0; srand(time(0)); num = rand() % 100 + 1;

c语言制作简单的计算器

c语言制作简单的计算器 在计算机编程领域中,C语言是一种广泛使用的编程语言之一。它具有简单易学的特点,并且能够进行复杂的计算和逻辑处理。今天,我们就来学习一下如何使用C语言来制作一个简单的计算器。 首先,我们需要明确我们计算器的功能和界面设计。本次制作的计算器将具有基本的四则运算功能,即加法、减法、乘法和除法。我们将在控制台中实现计算器的操作,并通过简单的用户界面进行交互。 接下来,我们需要创建一个C语言程序的框架。首先,我们需要包含头文件,以便在程序中使用各种函数和变量。我们可以使用 "#include" 来引入头文件,例如: ```c #include ``` 然后,我们需要定义主函数 `main()`。在这个函数中,我们将编写计算器的逻辑代码。让我们开始编写函数吧! 首先,我们需要声明一些变量来存储用户输入的数字和运算符。我们可以使用 `int` 来定义整数变量,并用 `char` 定义字符变量。例如:```c int num1, num2, result; char operator;

``` 接下来,我们需要提示用户输入要进行的运算,并读取用户输入的数字和运算符。我们可以使用 `printf` 和 `scanf` 函数来完成这个任务,例如: ```c printf("请输入第一个数字: "); scanf("%d", &num1); printf("请输入运算符(+, -, *, /): "); scanf(" %c", &operator); printf("请输入第二个数字: "); scanf("%d", &num2); ``` 注意,我们在读取运算符之前添加了空格,这是为了消除输入缓冲区中可能残留的换行符等字符。 接下来,我们需要编写代码来进行实际的计算操作。根据用户输入的运算符,我们可以使用 `switch` 语句来选择不同的操作。例如:```c switch(operator) { case '+':

C语言实现科学计算器的加减乘除平方开放运算

C语言实现科学计算器的加减乘除平方开放运算 科学计算器是一种能够进行各种高级数学运算的工具。在这个项目中,我们将使用C语言实现一个简单的科学计算器,它能够执行加法、减法、 乘法、除法、平方和开方等运算。 首先,让我们来设计计算器的基本框架。我们可以使用一个循环来接 受用户的输入,并根据输入的指令执行相应的运算。我们可以使用一个变 量来存储当前的结果,并在每次运算完成后更新该变量。以下是一个简单 的代码框架: ```c #include #include int mai double result = 0; // 存储当前的结果 char operation; // 存储用户的指令 while (1) //显示当前的结果 printf("当前结果:%lf\n", result); //获取用户的指令 printf("请输入指令:"); scanf(" %c", &operation);

//根据用户的指令执行相应的运算switch (operation) case '+': //执行加法运算 break; case '-': //执行减法运算 break; case '*': //执行乘法运算 break; case '/': //执行除法运算 break; case 's': //执行平方运算 break; case 'r': //执行开方运算

break; case 'q': //退出计算器 return 0; default: printf("无效的指令\n"); } } return 0; ``` 在代码中,我们使用了`switch`语句来根据用户的指令执行相应的运算。接下来,我们将详细说明每个运算的实现。 1.加法运算: 用户可以输入两个数,并将它们相加。我们可以使用`scanf`函数来获取用户输入,并使用加法运算符`+`执行相加操作。 ```c printf("请输入两个数:"); double num1, num2; scanf("%lf %lf", &num1, &num2);

c语言计算器程序编写代码

c语言计算器程序编写代码 C语言是一门广泛应用于计算机程序设计中的高级编程语言,计 算器是我们日常生活中必不可少的工具之一。今天,我们将探讨如何 使用C语言编写计算器程序。 步骤一:确定计算器的基本功能 在开始编写任何计算器程序之前,我们需要先决定它的基本功能。例如,我们需要让计算器能够执行四种基本算术运算 -- 加、减、乘、除。我们可能还需要添加其他一些功能,例如计算百分数、开根号、 求幂等。 步骤二:编写C语言代码 接下来,我们可以开始编写计算器程序的C代码。此时,我们需 要确定程序的最外层框架。这包括用于接收用户输入、调用所需的函 数以及输出结果的代码。 例如,我们可以使用如下的代码框架: ``` #include int main() { // 接收输入 // 调用对应的函数进行计算 // 输出结果 return 0; } ``` 步骤三:编写函数 我们需要编写函数来执行各种计算。例如,我们可以编写一个 add函数来执行加法,一个Subtract函数来执行减法,以此类推。 对于每个函数,我们需要指定它所需的输入,以及它返回的输出。例如,对于add函数,我们期望它将两个数字相加,并返回结果:

``` int add(int x, int y) { return x + y; } ``` 步骤四:解析用户输入 为了使计算器程序更加友好,我们希望程序能够解析用户输入并 检查其是否有效。例如,如果用户输入了两个不能相互转换为数字的 字符串,则程序应该输出错误消息。 步骤五:测试代码 最后,我们需要测试我们的代码以确保它能够正常工作。这意味 着我们应该使用各种输入和边缘情况进行测试,例如“0除以任何数字”和“无效输入”的情况。 总结 编写C语言计算器程序可能看起来很困难,但实际上它是一项非 常有趣的任务,因为它要求我们配备基本的编程知识并创造性地思考 解决方案。通过这篇文章,我们了解了如何确定计算器的基本功能, 如何编写C语言代码并编写函数,如何解析用户输入以及如何测试我 们的代码。虽然这是一项基础的项目,但它是学习C语言编程的很好 的起点,尤其是对于那些希望开始编写自己的应用程序的人来说。

简易计算器C语言代码

#include #include int main(void) { int choice,t=1; printf("**********************计算器*****************************************\n"); printf(" 1.加法运算2.减法运算\n"); printf(" 3.乘法运算4.除法运算\n"); printf(" 5.平方运算6.开方运算\n"); printf(" 7.解一元二次方程By:_一念成殇、\n"); printf("**********************************************************************\n"); printf("请选择您即将进行的运算方式(输入对应数字):"); while(t==1) { scanf("%d",&choice); if(choice>7) printf("请您输入正确的选项编号!\n"); //加法 if(choice==1) { float addend_1,addend_2,sum; printf("请输入两个加数:\n"); scanf("%f",&addend_1); scanf("%f",&addend_2); sum=addend_1+addend_2; printf("结果为:%.2f",sum); } //减法 if(choice==2) { float reduction,minuend,difference; printf("请输入被减数和减数:\n"); scanf("%f",&reduction); scanf("%f",&minuend ); difference=reduction-minuend ; printf("结果为:%.2f",difference); } //乘法 if(choice==3) { float multiplier_1,multiplier_2,product; printf("请输入两个乘数:\n"); scanf("%f",&multiplier_1); scanf("%f",&multiplier_2); product= multiplier_1 * multiplier_2; printf("结果为:%.2f",product);

用c语言编写的计算器源代码

作品:科学计算器 作者:欧宗龙 编写环境:vc++6.0 语言:c #include "stdafx.h" #include #include #include #include "resource.h" #include "MainDlg.h" #include #include #define PI 3.141593 BOOL A_Op=FALSE; BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog); HANDLE_MSG(hWnd, WM_MAND, Main_Onmand); HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose); } return FALSE; } BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) { return TRUE; } void TrimNumber(char a[])//判断并删除小数点后无用的零

for(unsigned i=0;i=i;j--) { if(a[j]=='0') { a[j]='\0'; } else if(a[j]=='.') { a[j]='\0'; } else break; } } } } double Operate(char Operator,double n1,double n2) //判断符号,进行相应的运算{ if(Operator=='0') { } if(Operator=='+') { n2+=n1; } if(Operator=='-') { n2=n1-n2; } if(Operator=='*') { n2*=n1; } if(Operator=='/') { n2=n1/n2; } if(Operator=='^') { n2=pow(n1,n2); } return n2; }

用c语言编写的加减乘除计算器程序

#include double jjcc(double st[],int k){ for(int j=1;j47&&st1[i]<58){

sum1=(st1[i]-48)+sum1*10; i++; if(st1[i]=='.'){ i++; while(st1[i]>47&&st1[i]<58){ k=k*0.1; sum2=(st1[i]-48)*k+sum2; i++; } } st2[c]=sum1+sum2; } else{ st2[++c]=st1[i]; i++; sum1=0; c++; } } printf("%.4f\n",jjcc(st2,c+1)); } 如有侵权请联系告知删除,感谢你们的配合!

计算器c语言代码

计算器c语言代码 计算器C语言代码 计算器是一种常见的工具,用于进行数值计算和数学运算。计算器通常具有基本的四则运算功能,可以执行加法、减法、乘法和除法运算,同时还可以进行其他高级计算,比如开方、求幂、取余等。在本文中,我们将介绍如何使用C语言编写一个简单的计算器。 我们需要定义计算器的基本功能。我们可以使用函数来实现不同的计算操作。下面是一个示例代码,实现了加法、减法、乘法和除法四种功能: ```c #include // 加法函数 double add(double a, double b) { return a + b; } // 减法函数 double subtract(double a, double b) { return a - b; }

// 乘法函数 double multiply(double a, double b) { return a * b; } // 除法函数 double divide(double a, double b) { if (b == 0) { printf("除数不能为零!\n"); return 0; } return a / b; } int main() { double num1, num2; char operator; printf("请输入两个操作数:"); scanf("%lf %lf", &num1, &num2); printf("请输入操作符:"); scanf(" %c", &operator); switch (operator) {

case '+': printf("结果:%lf\n", add(num1, num2)); break; case '-': printf("结果:%lf\n", subtract(num1, num2)); break; case '*': printf("结果:%lf\n", multiply(num1, num2)); break; case '/': printf("结果:%lf\n", divide(num1, num2)); break; default: printf("无效的操作符!\n"); break; } return 0; } ``` 在上面的代码中,我们使用了四个函数来实现不同的计算操作。通过调用这些函数,我们可以实现加法、减法、乘法和除法运算。在

相关主题
文本预览
相关文档 最新文档