用c语言制作计算器,如何计算2个以上的数字
我必须用c语言制作一个计算器,程序读取第一行中的数学运算,并在下一行打印结果。此外,如果数学运算中的字符不是数字,则必须写入错误消息。现在我的程序只在一行中读取一个数学运算(例如2+5)。你知道它如何在一行中读取更多的操作吗?(例2+5+7+8) 我的代码如下:用c语言制作计算器,如何计算2个以上的数字,c,C,我必须用c语言制作一个计算器,程序读取第一行中的数学运算,并在下一行打印结果。此外,如果数学运算中的字符不是数字,则必须写入错误消息。现在我的程序只在一行中读取一个数学运算(例如2+5)。你知道它如何在一行中读取更多的操作吗?(例2+5+7+8) 我的代码如下: #include <stdio.h> int main(void) { int ch; int input1 = 0, input2 = 0, flag = 0, flag1 = 0; char o
#include <stdio.h>
int main(void) {
int ch;
int input1 = 0, input2 = 0, flag = 0, flag1 = 0;
char oper;
int i = 1;
while ((ch = getchar()) != EOF){
int result = 0;
if (ch != '\n'){ /* If user didnt change line */
if (ch >= '0' && ch <= '9'){ /* Checks if ch is a number */
if(flag == 0) /* flag is used to change to the second number */
input1 = input1*10 + ch - '0'; /* Converts ASCII to decimal */
else
input2 = input2*10 + ch - '0'; /* Converts ASCII to decimal*/
}
if (ch == '+' || ch == '-' || ch == '*'){ /* Operator has been detected */
oper = ch;
flag = 1;
}
if (ch >= 'A'){
flag1 = 1; /* flag1 is used to determine if a non-number character was written */
}
}
else{
switch(oper){
case '+': /* if operator is "+" add the numbers */
result = input1 + input2;
break;
case '-': /* if operator is "-" subtract the numbers */
result = input1 - input2;
break;
case '*': /* if operator is "*" multiply the numbers */
result = input1 * input2;
break;
}
if (flag1 == 0){
printf("Result %d: %d\n", i, result);
i++;
input1 = 0;
input2 = 0;
flag = 0;
}
else if (flag1 == 1){
printf("Result %d: Error!\n", i);
i++;
input1 = 0;
input2 = 0;
flag = 0;
flag1 = 0;
}
}
}
return 0;
}
#包括
内部主(空){
int-ch;
int input1=0,input2=0,flag=0,flag1=0;
字符操作器;
int i=1;
而((ch=getchar())!=EOF){
int结果=0;
if(ch!='\n'){/*如果用户未更改行*/
如果(ch>='0'&&ch='A'){
flag1=1;/*flag1用于确定是否写入了非数字字符*/
}
}
否则{
开关(操作){
案例“+”:/*如果运算符为“+”,则添加数字*/
结果=输入1+输入2;
打破
案例'-':/*如果运算符为“-”,则减去数字*/
结果=输入1-输入2;
打破
案例“*”:/*如果运算符为“*”,则将数字相乘*/
结果=输入1*输入2;
打破
}
如果(flag1==0){
printf(“结果%d:%d\n”,i,结果);
i++;
输入1=0;
输入2=0;
flag=0;
}
else if(flag1==1){
printf(“结果%d:错误!\n”,i);
i++;
输入1=0;
输入2=0;
flag=0;
flag1=0;
}
}
}
返回0;
}
您可以使用前缀表示法来简化计算。您有很多关于中缀前缀符号的在线资料和代码。
基本思想是,您的输入(例如:(1+2)*(3*4))表示中缀符号,可以转换为前缀符号(*+12*34)。使用堆栈数据结构计算给定的前缀结构 评估前缀(字符串): 假设你有:*+12*34就像我们前面的例子一样
我建议您看看前缀、中缀和后缀表达式。理解它不会花费很长时间,它应该为您的问题提供一个很好的解决方案。希望有帮助。:) 为了便于计算,您可以使用前缀符号。
您有很多关于中缀前缀符号的在线资料和代码。
基本思想是,您的输入(例如:(1+2)*(3*4))表示中缀符号,可以转换为前缀符号(*+12*34)。使用堆栈数据结构计算给定的前缀结构 评估前缀(字符串): 假设你有:*+12*34就像我们前面的例子一样
我建议您看看前缀、中缀和后缀表达式。理解它不会花费很长时间,它应该为您的问题提供一个很好的解决方案。希望有帮助。:) 我发现这对我自己来说是一个很好的锻炼 建议如下:
- 状态机可以帮助获取输入
- 在这种情况下(计算器),一个链表应该足以促进多个操作
- 在收集输入时填写列表,然后按照操作员优先级的顺序清空列表
#include <stdio.h>
#include <stdlib.h>
#define MAX_INPUT_SIZE 1000;
struct Node
{
char operator;
int result;
struct Node* left;
struct Node* right;
};
enum InputState{
START = 0,
NUMBER,
OPERATOR
};
void pushNodeToList( struct Node**head, struct Node**tail, char op, int result)
{
struct Node* nodePtr = malloc(sizeof(*nodePtr));
nodePtr->operator = op;
result = result;
if(!head)
{
*head = nodePtr;
*tail = nodePtr;
nodePtr->left = 0;
nodePtr->right = 0;
}
else{
nodePtr->left = *tail;
(*tail)->right = nodePtr;
*tail = nodePtr;
}
}
void calculateNode( struct Node* nodePtr)
{
if(nodePtr->left != 0 && nodePtr->right != 0)
{
if(nodePtr->left->operator == 'n' && nodePtr->right->operator == 'n' )
{
//calculate result
switch(nodePtr->operator)
{
case '+':
nodePtr->result = nodePtr->left->result + nodePtr->right->result;
break;
case '-':
nodePtr->result = nodePtr->left->result - nodePtr->right->result;
break;
case '*':
nodePtr->result = nodePtr->left->result * nodePtr->right->result;
break;
default:
printf("Calculation Error: %d \n", 5);
return;
}
//change type of node to 'n'
nodePtr->operator == 'n';
//reduce the numbers consumed
struct Node* tempLeft = nodePtr->left;
struct Node* tempRight = nodePtr->right;
nodePtr->left = tempLeft->left;
nodePtr->right = tempRight->right;
free(tempLeft);
free(tempRight);
}
else
{
printf("Calculation Error: %d \n", 4);
return;
}
}
else{
printf("Calculation Error: %d \n", 3);
return;
}
}
int main(void) {
int ch;
struct Node* head = 0;
struct Node* tail = 0;
//have a state machine to handle the logics related to parsing input
int num = 0;
enum InputState mState = START;
int i = 1;
while ((ch = getchar()) != EOF)
{
switch(mState)
{
case START:
if (ch >= '0' && ch <= '9'){
mState = NUMBER;
num = 0;
//initialize state to number
}
else if(ch == '+' || ch == '-' || ch == '*'){
mState = OPERATOR;
//initilize state to operator
}
else{
//your error code
printf("Input Error: %d \n", 1);
return 0;
}
break;
case NUMBER:
if (ch >= '0' && ch <= '9'){
num = num * 10 + ch - '0';
}
else if(ch == '+' || ch == '-' || ch == '*'){
mState = OPERATOR;
//we just got a number recorded
pushNodeToList(&head,&tail,'n',num);//'n' for number
}
else{
printf("Input Error: %d \n", 2);
return 0;
}
break;
case OPERATOR:
if (ch >= '0' && ch <= '9'){
mState = NUMBER;
num = ch - '0';
}
else if(ch == '+' || ch == '-' || ch == '*'){
pushNodeToList(&head,&tail,ch,0);//push in operator
}
else{
printf("Input Error: %d \n", 3);
return 0;
}
break;
}
}
//terminal condition to push-in last number
if(mState == NUMBER)
{
pushNodeToList(&head,&tail,'n',num);//'n' for number
}
//higher prioriety operation
struct Node* workingPtr = head;
while(workingPtr !=tail)//assuming the last input is number (not operator)
{
if(workingPtr->operator == '*')
{
calculateNode(workingPtr);
}
}
//lower prioriety operations
workingPtr = head;
while(workingPtr !=tail)
{
if(workingPtr->operator == '+' || workingPtr->operator == '-' )
{
calculateNode(workingPtr);
}
}
//print result
if(head == tail && head->operator == 'n')
{
printf("Result : %d\n", head->result);
}
else
{
printf("Error: %d \n", 7);
return 0;
}
return 0;
}
#包括
#包括
#定义最大输入大小1000;
结构体类型
{
字符算子;
int结果;
结构节点*左;
结构节点*右;
};
枚举输入状态{
开始=0,
数字,
操作人员
};
void pushNodeToList(结构节点**头部,结构节点**尾部,字符操作,int结果)
{
结构节点*nodePtr=malloc(sizeof(*nodePtr));
nodePtr->operator=op;
结果=结果;
如果(!头)
{
*head=nodePtr;
*tail=nodePtr;
nodePtr->left=0;
nodePtr->right=0;
}
否则{
nodePtr->left=*尾部;
(*tail)->右=nodePtr;
*tail=nodePtr;
}
}
void calculateNode(结构节点*nodePtr)
{
如果(nodePtr->left!=0&&nodePtr->right!=0)
{
if(nodePtr->left->operator='n'&&nodePtr->right->operator='n')
{
//计算结果
开关(nodePtr->operator)
{
格“+”:
nodePtr->result=nodePtr->left->result+nodePtr->right->result;
打破
案例'-':
诺德普-