发布网友 发布时间:2022-04-19 10:10
共5个回答
热心网友 时间:2023-08-28 19:29
#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();
}
热心网友 时间:2023-08-28 19:30
#include"stdio.h"
/*预处理命令*/
void main()
/*主函数*/
{
double a,b;
/*双精度实型变量说明*/
char c,d;
/*变量说明*/
do
/*循环体*/
{
printf("input a (-*/)b\n");
/*输入提示*/
scanf("%lf%c%lf",&a,&c,&b);
/*输入算术表达式*/
if(c==' ')
/*判断 */
printf("=%0.2f",a b);
/*输出a b的值*/
else if(c=='-')
/*判断-*/
printf("=%0.2f",a-b);
/*输出a-b的值*/
else if(c=='*')
/*判断**/
printf("=%0.2f",a*b);
/*输出a*b的值*/
else if(c=='/')
/*判断/*/
printf("=%0.3f",a/b);
/*输出a/b*/
else
/*不满足以上条件*/
printf("error");
/*输出错误*/
printf("\n\ninput\n");
/*输入\n*/
scanf("%c",&d);
/*输入符号给d*/
}
/*循环体结束*/
while(d=='\n');
/*循环条件语句*/
}
热心网友 时间:2023-08-28 19:30
简单计算器实现:
#include<stdio.h>
int main() {
double num1 = 0; //输入1
double num2 = 0; //输入2
char ch; //操作
double ret = 0; //结果 printf( "输入第一个数:" );
scanf( "%lf", &num1 );
printf( "输入第二个数:" );
scanf( "%lf", &num2 );
printf( "操作[+ - * /]:" );
getchar();
scanf( "%c", &ch ); switch( ch ) {
case '+':
ret = num1 + num2;
break;
case '-':
ret = num1 - num2;
break;
case '*':
ret = num1 * num2;
break;
case '/':
ret = num1 / num2;
break;
default:
break;
}
printf( "结果:%.2lf\n", ret ); return 0;
}
热心网友 时间:2023-08-28 19:31
堆栈是数据结构的内容,对初学者来说是很难的。可以不用这个,我写一个吧
#include"stdio.h"
void
main(){
int
a,
b;
int
s;
printf("输入2个数");
scanf("%d",&a);
scanf("%d",&b);
char
c;
printf("输入计算符号");
scanf("%c",&c);
switch(c)
case
'+':s=a+b;
case
'-':s=a-b;
case
'*':s=a*b;
case
'/':s=a/b;
printf("计算结果是%d",s);}
//这个计算器比较简单。
热心网友 时间:2023-08-28 19:32
不知道是不是你喜欢的格式。建议自己修改成自己喜欢的格式。#include<stdio.h>
#include<math.h>
void
menu()
{
printf("
|============================|\n");
printf("
|
1.算术运算;
2.进制转换;
|\n");
printf("
|============================|\n\n");
}
void
fun1()//算术运算
{
int
a,b;
char
oper; printf("请输入算式:");
scanf("%d%c%d",&a,&oper,&b);
switch(oper)
{
case
'+':printf("结果为:%d\n",a+b);break;
case
'-':printf("结果为:%d\n",a-b);break;
case
'*':printf("结果为:%d\n",a*b);break;
case
'/':printf("结果为:%.2f\n",(float)a/b);break;
case
'%':printf("结果为:%d\n",a%b);break;
default:printf("输入有误!\n");break;
}
}
void
fun2()//进制转换
{
int
choice,value,i,j=0,k,t;
char
s[50];
int
a[4];
printf("
********************************************************\n");
printf("
1.十进制转换成二进制;
2.十进制转换成十六进制;\n");
printf("
3.二进制转换成十进制;
4.二进制转换成十六进制;\n");
printf("
5.十六进制转换成二进制;
6.十六进制转换成十进制;\n");
printf("
********************************************************\n");
printf("请选择:");
scanf("%d",&choice);
switch(choice)
{
case
1:printf("请输入十进制数值:");scanf("%d",&value);
while(value>=2)
{
if(value%2!=0)
s[j++]='1';
else
s[j++]='0';
value=value/2;
}
if(value==1)
s[j]='1';
printf("结果为:");
for(i=j;i>=0;i--)
printf("%c",s[i]);
break;
case
2:printf("请输入十进制数值:");scanf("%d",&value);
printf("结果为:%x",value);break;
case
3:printf("请输入二进制数:");scanf("%s",s);
for(i=0;s[i]!='\0';i++);
i--;value=0;
for(j=0;j<=i;j++)
{
t=1;
if(s[j]=='1')
{
for(k=i-j;k>0;k--)
t*=2;
value+=t;
}
}
printf("结果为:%d\n",value);break;
case
4:printf("请输入二进制数:");scanf("%s",s);
for(i=0;s[i]!='\0';i++);
i--;value=0;
for(j=0;j<=i;j++)
{
t=1;
if(s[j]=='1')
{
for(k=i-j;k>0;k--)
t*=2;
value+=t;
}
}
printf("结果为:%x\n",value);break;
case
5:printf("请输入十六进制数值:");scanf("%x",&value);
while(value>=2)
{
if(value%2!=0)
s[j++]='1';
else
s[j++]='0';
value=value/2;
}
if(value==1)
s[j]='1';
printf("结果为:");
for(i=j;i>=0;i--)
printf("%c",s[i]);
break;
case
6:printf("请输入十六进制数值:");scanf("%x",value);
printf("结果为:%d\n",value);break;
default:printf("选择有误!\n");
break;
}
}
void
main()
{
int
choice;
while(choice!=0)
{
menu();
printf("请选择(0退出):");
scanf("%d",&choice);
if(choice==1)
fun1();
else
if(choice==2)
fun2();
printf("\n");
}
}