C语言编程中,怎样使输出的加减乘除是运算符?
答案:6 信息版本:手机版
解决时间 2019-10-06 06:12
已解决
2019-10-05 05:31
题目是编程简易计算器。但试了几次,没有错误没有警告,却就是运算不出来。我想应该是运算符格式有错误。求高人指点啊!
最佳答案
2019-10-05 06:59
#include
#include
#define N 100
typedef struct{ //操作数栈
float d[N];
int len; //len相当于top
}stacks,*lists;
typedef struct{ //运算符栈
char c[N];
int len; //len相当于top
}stack,*list;
int init(list p); //stack 初始化
int inits(lists p); //stacks 初始化
int pushs(lists p,float m); // stacks 入栈
int push(list p,char c); //stack 入栈
int pops(lists p,float *m); //stacks 出栈
int pop(list p,char *c); //stack 出栈
int in(char c); //判断C是否为数据 是则返回 1 否则 返回 0
char pre(char s,char c); //判断s c的优先级 s>c return '>'; slen=0;
return 1;
}
int inits(lists p){
if(!p) {printf("Error,init stacks NULL\n"); return 0;}
p->len=0;
return 1;
}
int pushs(lists p,float m){
if(!p) {printf("Error,pushs stacks NULL\n"); return 0;}
p->d[p->len++]=m; return 1;
}
int push(list p,char c){
if(!p) {printf("Error,push stack NULL\n"); return 0;}
p->c[p->len++]=c; return 1;
}
int pops(lists p,float *m){
if(!p) {printf("Error,push stacks NULL\n"); return 0;}
*m=p->d[--p->len]; return 1;
}
int pop(list p,char *c){
if(!p) {printf("Error,push stack NULL\n"); return 0;}
*c=p->c[--p->len]; return 1;
}
int in(char c){ //判断C是否为数据 是则返回 1 否则 返回 0
if(c>='0'&&c<='9'||c=='.') return 1; return 0;}
char pre(char s,char c){ //判断s c的优先级 s>c return '>'; s 1230
k=p-pr;pr++; }
}
for(i=0,tp=10;i':pop(&optr,&t); pops(&opnd,&b); pops(&opnd,&a); //输出一个操作符和两个数据
pushs(&opnd,operate(a,t,b));break;} //运算
}
return opnd.d[opnd.len-1]; //返回表达式的值
}
全部回答
1楼
2019-10-05 11:35
本人专写烂程序...呵呵#include
#include
int main()
{
int a=0,b=0,i,flag=0;
char c,s[100];
printf("please input :");
scanf("%s",s);
for(i=0;i= 48)
{
if(flag == 0)
a=a*10+s[i] - 48;
else
b=b*10+s[i] - 48;
}
else
{
flag++;
c=s[i];
}
}
switch(c)
{
case '+' : printf("%d%c%d=%5.2f\n",a,c,b,(double)a+b); break;
case '-' : printf("%d%c%d=%5.2f\n",a,c,b,(double)a-b); break;
case '*' : printf("%d%c%d=%5.2f\n",a,c,b,(double)a*b); break;
case '/' : printf("%d%c%d=%5.2f\n",a,c,b,(double)a/b); break;
}
return 0;
}
2楼
2019-10-05 10:15
printf("+ - * /");
就是写在引号里
3楼
2019-10-05 10:00
#include#include#define n 100
typedef struct{ //操作数栈 float d[n]; int len; //len相当于top}stacks,*lists;
typedef struct{ //运算符栈 char c[n]; int len; //len相当于top}stack,*list;
int init(list p); //stack 初始化int inits(lists p); //stacks 初始化int pushs(lists p,float m); // stacks 入栈int push(list p,char c); //stack 入栈int pops(lists p,float *m); //stacks 出栈int pop(list p,char *c); //stack 出栈int in(char c); //判断c是否为数据 是则返回 1 否则 返回 0char pre(char s,char c); //判断s c的优先级 s>c return '>'; s
void main(){ char s[n]; int n; float f; printf("*******************express evaluate***********************\n"); while(1){ printf("please input a express\n"); scanf("%s",s); //输入表达式 n=strlen(s); //求表达式的长度 printf("express length=%d\n",n); //输出表达式的长度 s[n]='#'; //在表达式尾部插入'#' 注: # 表示操作结束 f=value(s); //计算表达式的长度 printf("f=%.5f\n",f); //输出表达时的值 注 只输出前五位小数 printf("continue input 1,break input 0\n"); scanf("%d",&n); printf("\n"); //判断是否继续进操作 是则输入 1,否则输入 0 if(!n) break; }}
int init(list p){ if(!p) {printf("error,init stack null\n"); return 0;} p->len=0; return 1; }int inits(lists p){ if(!p) {printf("error,init stacks null\n"); return 0;} p->len=0; return 1;}int pushs(lists p,float m){ if(!p) {printf("error,pushs stacks null\n"); return 0;} p->d[p->len++]=m; return 1;}int push(list p,char c){ if(!p) {printf("error,push stack null\n"); return 0;} p->c[p->len++]=c; return 1;}int pops(lists p,float *m){ if(!p) {printf("error,push stacks null\n"); return 0;} *m=p->d[--p->len]; return 1;}int pop(list p,char *c){ if(!p) {printf("error,push stack null\n"); return 0;} *c=p->c[--p->len]; return 1;}int in(char c){ //判断c是否为数据 是则返回 1 否则 返回 0 if(c>='0'&&c<='9'||c=='.') return 1; return 0;}char pre(char s,char c){ //判断s c的优先级 s>c return '>'; s
float value(char s[]){ stacks opnd; stack optr; char *p,*pr,x,t; float a,b,m=0; inits(&opnd); init(&optr); int k=0,i,tp=10; push(&optr,'#'); //将'#'压入栈optr中,作结束标志 p=pr=s; //初始化 指针 p pr 使pr p 指向 表达式 while(*p!='#'||optr.c[optr.len-1]!='#'){//当p指向的操作符 和optr 中的栈顶元素都为'#'时 操作结束 if(in(*p)) //如果p指向数字 则把 数字字符串转化为实型(float)数据 并存入栈opnd中 { m=0; k=0; tp=10; while(in(*p)) p++; //使p指向下一个运算符 while(pr!=p) { if(*pr!='.') //如果pr指向小数点 则跳过 m=(m+*(pr++)-'0')*10; //将单个字数字符转化为数据 并逐位求和 else{ //如 12.3 -> 1230 k=p-pr; pr++; } } for(i=0,tp=10;i':pop(&optr,&t); pops(&opnd,&b); pops(&opnd,&a); //输出一个操作符和两个数据 pushs(&opnd,operate(a,t,b));break;} //运算 } return opnd.d[opnd.len-1]; //返回表达式的值}
4楼
2019-10-05 08:44
printf("a+b=%d",a+b);
显示的结果就是a+b=值
5楼
2019-10-05 08:25
应该是输入加减乘除运算符吧就是直接输入计算表达式吧这个需要你自己定义,检测表达式中的字符,是数字正常,是加减乘除则变为加减乘除,你输入的加减乘除号,系统是不认的,只有让系统自己输入就行了,不过这个有点难,要涉及到宏定义,还要判断符号优先级……
我要举报
如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
大家都在看
推荐资讯