c语言中如何对字符型进行加减乘除 C语言编程中,怎样使输出的加减乘除是运算符?
C\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\uff1f\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a
\u8bfb\u5165\u7684\u65f6\u5019\u7528%x\u8bfb\u5165\uff0c\u7136\u540e\u5728\u7a0b\u5e8f\u91cc\u9762\u76f4\u63a5\u7528+\uff0c\uff0d\uff0c*\uff0c/\u8fd0\u7b97\u5c31\u884c\u4e86\uff0c\u8f93\u51fa\u7684\u65f6\u5019\u518d\u7528%x\u3002
\u793a\u4f8b\u4e00\uff1a
int a,b;
scanf("%x%x",&a,&b);
printf("%x\n%x\n",a+b,a-b);
\u8f93\u5165:9 7
\u8f93\u51fa:
10
2
\u793a\u4f8b\u4e8c\uff1a
int a = 0, b = 0;
char char1 = 0;
int val = 0;
printf("\u8f93\u5165\uff1a");
scanf("%d%c%d", &a, &char1, &b);
switch (char1) {
case '+': val = a + b;
break;
case '-': val = a - b;
break;
case '*': val = a * b;
break;
case '/': val = a / b;
break;
}
printf("%d", val);
#include
#include
#define N 100
typedef struct{ //\u64cd\u4f5c\u6570\u6808
float d[N];
int len; //len\u76f8\u5f53\u4e8etop
}stacks,*lists;
typedef struct{ //\u8fd0\u7b97\u7b26\u6808
char c[N];
int len; //len\u76f8\u5f53\u4e8etop
}stack,*list;
int init(list p); //stack \u521d\u59cb\u5316
int inits(lists p); //stacks \u521d\u59cb\u5316
int pushs(lists p,float m); // stacks \u5165\u6808
int push(list p,char c); //stack \u5165\u6808
int pops(lists p,float *m); //stacks \u51fa\u6808
int pop(list p,char *c); //stack \u51fa\u6808
int in(char c); //\u5224\u65adC\u662f\u5426\u4e3a\u6570\u636e \u662f\u5219\u8fd4\u56de 1 \u5426\u5219 \u8fd4\u56de 0
char pre(char s,char c); //\u5224\u65ads c\u7684\u4f18\u5148\u7ea7 s>c return '>'; s<c return '<';s=c return '=' ;
float operate(float a,char t,float b); //\u5bf9a b\u8fdb\u884ct\uff08+-*/\uff09\u8fd0\u7b97 return a+b or a-b, a*b ,a/b
float value(char s[]); //\u6c42\u8868\u8fbe\u5f0fS \u7684\u503c
void main(){
char s[N]; int n; float f;
printf("*******************Express Evaluate***********************\n");
while(1){
printf("please input a express\n");
scanf("%s",s); //\u8f93\u5165\u8868\u8fbe\u5f0f
n=strlen(s); //\u6c42\u8868\u8fbe\u5f0f\u7684\u957f\u5ea6
printf("express length=%d\n",n); //\u8f93\u51fa\u8868\u8fbe\u5f0f\u7684\u957f\u5ea6
s[n]='#'; //\u5728\u8868\u8fbe\u5f0f\u5c3e\u90e8\u63d2\u5165'#' \u6ce8: # \u8868\u793a\u64cd\u4f5c\u7ed3\u675f
f=value(s); //\u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u957f\u5ea6
printf("f=%.5f\n",f); //\u8f93\u51fa\u8868\u8fbe\u65f6\u7684\u503c \u6ce8 \u53ea\u8f93\u51fa\u524d\u4e94\u4f4d\u5c0f\u6570
printf("continue input 1,break input 0\n");
scanf("%d",&n); printf("\n"); //\u5224\u65ad\u662f\u5426\u7ee7\u7eed\u8fdb\u64cd\u4f5c \u662f\u5219\u8f93\u5165 1\uff0c\u5426\u5219\u8f93\u5165 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){ //\u5224\u65adC\u662f\u5426\u4e3a\u6570\u636e \u662f\u5219\u8fd4\u56de 1 \u5426\u5219 \u8fd4\u56de 0
if(c>='0'&&c<='9'||c=='.') return 1; return 0;}
char pre(char s,char c){ //\u5224\u65ads c\u7684\u4f18\u5148\u7ea7 s>c return '>'; s<c return '<';s=c return '=' ;
char m= '' , r;
if((s=='-')||(s=='+')){
if((c=='*')||(c=='(')||(c=='/'))
r=m;
else r=k; }
if((s=='*')||(s=='/')){
if((c=='(')) r=m;
else r=k; }
if(s=='('){
if(c==')') r=n;
else r=m; }
if(s==')')
r=k;
if(s=='#') {
if(c=='#') r=n;
else r=m; }
return r;
}
float operate(float a,char t,float b){ //\u5bf9a b\u8fdb\u884ct\uff08+-*/\uff09\u8fd0\u7b97 return a+b or a-b, a*b ,a/b
if(t=='-') return a-b; if(t=='+') return a+b;
if(t=='*') return a*b; if(t=='/') return a/b; }
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,'#'); //\u5c06'#'\u538b\u5165\u6808optr\u4e2d\uff0c\u4f5c\u7ed3\u675f\u6807\u5fd7
p=pr=s; //\u521d\u59cb\u5316 \u6307\u9488 p pr \u4f7fpr p \u6307\u5411 \u8868\u8fbe\u5f0f
while(*p!='#'||optr.c[optr.len-1]!='#'){//\u5f53p\u6307\u5411\u7684\u64cd\u4f5c\u7b26 \u548coptr \u4e2d\u7684\u6808\u9876\u5143\u7d20\u90fd\u4e3a'#'\u65f6 \u64cd\u4f5c\u7ed3\u675f
if(in(*p)) //\u5982\u679cp\u6307\u5411\u6570\u5b57 \u5219\u628a \u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5b9e\u578b\uff08float\uff09\u6570\u636e \u5e76\u5b58\u5165\u6808opnd\u4e2d
{
m=0; k=0; tp=10;
while(in(*p)) p++; //\u4f7fp\u6307\u5411\u4e0b\u4e00\u4e2a\u8fd0\u7b97\u7b26
while(pr!=p)
{
if(*pr!='.') //\u5982\u679cpr\u6307\u5411\u5c0f\u6570\u70b9 \u5219\u8df3\u8fc7
m=(m+*(pr++)-'0')*10; //\u5c06\u5355\u4e2a\u5b57\u6570\u5b57\u7b26\u8f6c\u5316\u4e3a\u6570\u636e \u5e76\u9010\u4f4d\u6c42\u548c
else{ //\u5982 12.3 -> 1230
k=p-pr;pr++; }
}
for(i=0,tp=10;i<k;i++) //\u6c42\u51fa\u9664\u6570tp \u4f7f1230\u9664\u4ee5tp\uff08100\uff09\u7b49\u4e8e 12.3
{
tp=tp*10;
if(i==k-1)
tp=tp/10;
}
pushs(&opnd,m/tp); //\u8f93\u5165\u6808opnd\u4e2d
}
else switch(pre(optr.c[optr.len-1],*p)){
case'<':push(&optr,*p);p++;pr=p;break;
case'=':pop(&optr,&x);p++;pr=p;break;
case'>':pop(&optr,&t); pops(&opnd,&b); pops(&opnd,&a); //\u8f93\u51fa\u4e00\u4e2a\u64cd\u4f5c\u7b26\u548c\u4e24\u4e2a\u6570\u636e
pushs(&opnd,operate(a,t,b));break;} //\u8fd0\u7b97
}
return opnd.d[opnd.len-1]; //\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u503c
}
加减可以,乘除应该也可以。其实字符型做四则运算就是对这个字符的ASCII码进行运算,所以不用转换
你可以先把char转换为string
再把string 转换为int
程序代码:
#include <stdio.h>
#define MAXINT 1000
int compare(int a[],int b[]);
int bigplus(int a[],int b[],int c[]);
int bigsub(int a[],int b[],int c[]);
int bigmult(int a[],unsigned int b,int c[]);
int bigmult2(int a[],int b[],int c[]);
int bigdiv(int a[],unsigned int b,int c[],int *d);
int bigdiv2(int a[],int b[],int c[],int d[]);
int main(int argc, char *argv[])
{
int a[MAXINT]={10,5,4,6,5,4,3,2,1,1,1}; //被乘数或被除数
int b[MAXINT]={7,7,6,5,4,3,2,1}; //乘数或除数
int c[MAXINT],d[MAXINT]; //c[]存放商,d[]存放余数
int div=1234; //小乘数或小除数
int k=0;
int *res=&k; //小余数整数指针
bigplus(a,b,c);
bigsub(a,b,c);
bigmult(a,div,c);
bigmult2(a,b,c);
bigdiv(a,div,c,res);
bigdiv2(a,b,c,d);
getchar();
return 0;
}
int compare(int a[],int b[]) //比较大整数的大小
{
int i;
if (a[0]>b[0]) return 1; //比较a,b的位数确定返回值
else if (a[0]<b[0]) return -1;
else //位数相等时的比较
{
i=a[0];
while (a[i]==b[i]) //逐位比较
i--;
if (i==0) return 0;
else if (a[i]>b[i]) return 1;
else return -1;
}
}
int bigplus(int a[],int b[],int c[]) //大整数加法
{
int i,len;
len=(a[0]>b[0]?a[0]:b[0]); //a[0] b[0]保存数组长度,len为较长的一个
for(i=0;i<MAXINT;i++) //将数组清0
c[i]=0;
for (i=1;i<=len;i++) //计算每一位的值
{
c[i]+=(a[i]+b[i]);
if (c[i]>=10)
{
c[i]-=10; //大于10的取个位
c[i+1]++; //高位加1
}
}
if (c[i+1]>0) len++;
c[0]=len; //c[0]保存结果数组实际长度
printf("Big integers add: ";
for (i=len;i>=1;i--)
printf("%d",c[i]); //打印结果
printf("\n";
return 0;
}
int bigsub(int a[],int b[],int c[]) //大整数减法
{
int i,len;
len=(a[0]>b[0]?a[0]:b[0]); //a[0]保存数字长度,len为较长的一个
for(i=0;i<MAXINT;i++) //将数组清0
c[i]=0;
if (compare(a,b)==0) //比较a,b大小
{
printf("Result:0";
return 0;
}
else if (compare(a,b)>0)
for (i=1;i<=len;i++) //计算每一位的值
{
c[i]+=(a[i]-b[i]);
if (c[i]<0)
{
c[i]+=10; //小于0的原位加10
c[i+1]--; //高位减1
}
}
else
for (i=1;i<=len;i++) //计算每一位的值
{
c[i]+=(b[i]-a[i]);
if (c[i]<0)
{
c[i]+=10; //小于0原位加10
c[i+1]--; //高位减1
}
}
while (len>1 && c[len]==0) //去掉高位的0
len--;
c[0]=len;
printf("Big integers sub= ";
if (a[0]<b[0]) printf("-";
for(i=len;i>=1;i--) //打印结果
printf("%d",c[i]);
printf("\n";
return 0;
}
int bigmult(int a[],unsigned int b,int c[])//高精度乘以低精度
{
int len,i;
for (i=0;i<MAXINT;i++) //数组清0
c[i]=0;
len=a[0];
for(i=1;i<=len;i++) //对每一位计算
{
c[i]+=a[i]*b;
c[i+1]+=c[i]/10;
c[i]%=10;
}
while (c[++len]>=10) //处理高位
{
c[len+1]=c[len]/10;
c[len]%=10;
}
if (c[len]==0) len--; //处理高进位为0情况
printf("Big integrs multi small integer: ";
for (i=len;i>=1;i--)
printf("%d",c[i]);
printf("\n";
}
int bigmult2(int a[],int b[],int c[]) //高精度乘以高精度
{
int i,j,len;
for (i=0;i<MAXINT;i++) //数组清0
c[i]=0;
for (i=1;i<=a[0];i++) //被乘数循环
for (j=1;j<=b[0];j++) //乘数循环
{
c[i+j-1]+=a[i]*b[j]; //将每一位计算累加
c[i+j]+=c[i+j-1]/10; //将每一次结果累加到高一位
c[i+j-1]%=10; //计算每一次的个位
}
len=a[0]+b[0]; //取最大长度
while (len>1 && c[len]==0) //去掉高位0
len--;
c[0]=len;
printf("Big integers multi: ";
for (i=len;i>=1;i--) //打印结果
printf("%d",c[i]);
printf("\n";
}
int bigdiv(int a[],unsigned int b,int c[],int *d) //高精度除以低精度
{ //a[] 为被乘数,b为除数,c[]为结果,d为余数
int i,len;
len=a[0]; //len为a[0]的数组长度
for (i=len;i>=1;i--)
{
(*d)=10*(*d)+a[i]; //计算每一步余数
c[i]=(*d)/b; //计算每一步结果
(*d)=(*d)%b; //求模余数
}
while (len>1 && c[len]==0) len--; //去高位0
printf("Big integer div small integer: ";
for (i=len;i>=1;i--) //打印结果
printf("%d",c[i]);
printf("\tArithmetic compliment:%d",*d);
printf("\n";
}
int bigdiv2(int a[],int b[],int c[],int d[]) //高精度除以高精度
{
int i,j,len;
if (compare(a,b)<0) //被除数较小直接打印结果
{
printf("Result:0";
printf("Arithmetic compliment:";
for (i=a[0];i>=1;i--) printf("%d",a[i]);
printf("\n";
return -1;
}
for (i=0;i<MAXINT;i++) //商和余数清0
{
c[i]=0;
d[i]=0;
}
len=a[0];d[0]=0;
for (i=len;i>=1;i--) //逐位相除
{
for (j=d[0];j>=1;j--)
d[j+1]=d[j];
d[1]=a[i]; //高位*10+各位
d[0]++; //数组d长度增1
while (compare(d,b)>=0) //比较d,b大小
{
for (j=1;j<=d[0];j++) //做减法d-b
{
d[j]-=b[j];
if (d[j]<0)
{
d[j]+=10;
d[j+1]--;
}
}
while (j>0 && d[j]==0) //去掉高位0
j--;
d[0]=j;
c[i]++; //商所在位值加1
}
}
j=b[0];
while (c[j]==0 && j>0) j--; //求商数组c长度
c[0]=j;
printf("Big integers div result: ";
for (i=c[0];i>=1;i--) //打印商
printf("%d",c[i]);
printf("\tArithmetic compliment: "; //打印余数
for (i=d[0];i>=1;i--)
printf("%d",d[i]);
printf("\n");
}
绛旓細c璇█涓暣褰㈠拰瀛楃鍨嬪彉閲忔槸閫氱敤鐨勶紝鍗冲瓧绗﹀瀷鍙橀噺鏄敤ASCII鐮佽〃绀虹殑锛屾槸鏁村舰鐨勶紝鍙互鐩存帴鍋氬姞鍑忎箻闄よ繍绠锛屽彧鏄渶鍚庤緭鍑鸿鐢ㄥ瓧绗﹀瀷杈撳嚭锛屽锛歝har a;a=98;a++;printf("%c",a);
绛旓細璁$畻鏈烘墽琛岀殑鎿嶄綔鏄皢c2鐨凙SCII鐮佸煎噺鍘籧1鐨凙SCII鐮佸硷紝鍗66锛'B'鐨凙SCII鐮佸硷級鍑忓幓65锛'A'鐨凙SCII鐮佸硷級锛屽緱鍒扮粨鏋1銆傝繖涓粨鏋滀篃浼氳浆鎹负涓涓瓧绗︼紝瀛樺偍鍦╟4涓傚湪杩欑鎯呭喌涓嬶紝1瀵瑰簲鐨凙SCII瀛楃鏄疭OH锛圫tart of Header锛夛紝鍚屾牱鏄竴涓潪鎵撳嵃瀛楃銆傜患涓婃墍杩帮紝C璇█鐨勫瓧绗﹀瀷鍙互杩涜鏁板艰繍绠楁槸鍥犱负瀛...
绛旓細鍏ヤ竴涓瓧绗︿覆锛屽寘鍚暟瀛楀拰鍔(+)銆佸噺(-)銆佷箻(*)銆侀櫎(/)绛夊洓鍒欒繍绠楃鍙凤紝绗︿覆鐨勫洓鍒欒繍绠椼傜粰鍑轰竴涓瓧绗︿覆锛屽寘鍚0~9鐨勬暟瀛楀拰+-*/()鐨勮繍绠楃锛-浠呬唬琛ㄥ噺鍙蜂笉浠h〃璐熸暟銆備妇渚嬪涓嬶細杈撳叆锛1+2*(3-4)锛岃緭鍑猴細-1銆傛濊矾锛氬垎鏋瀛楃涓茬殑鍚堟硶鎬э紝 妯℃嫙鍥涘垯杩愮畻瑙勫垯锛屾寜鐓у厛鎷彿鍚庝箻闄ゆ渶鍚鍔犲噺鐨椤...
绛旓細瀹炵幇鏂规硶濡備笅锛氳鍏ョ殑鏃跺欑敤%x璇诲叆锛岀劧鍚庡湪绋嬪簭閲岄潰鐩存帴鐢+锛岋紞锛*锛/杩愮畻灏辫浜嗭紝杈撳嚭鐨勬椂鍊欏啀鐢%x銆傜ず渚嬩竴锛歩nt a,b;scanf("%x%x",&a,&b);printf("%x\n%x\n",a+b,a-b);杈撳叆:9 7 杈撳嚭:10 2 绀轰緥浜岋細int a = 0, b = 0;char char1 = 0;int val = 0;printf("杈撳叆锛");...
绛旓細涓嬮潰灏忕紪缁欏ぇ瀹跺垎浜竴涓嬨1銆侀鍏堟墦寮Code Blocks杞欢锛屾柊寤轰竴涓C璇█鏂囦欢锛屽涓嬪浘鎵绀 2銆佹帴涓嬫潵鍦–璇█鏂囦欢涓紪鍐鍔犲噺涔闄よ繍绠椾唬鐮侊紝濡備笅鍥炬墍绀 3銆佺劧鍚庣偣鍑绘瀯寤鸿彍鍗曚笅闈㈢殑缂栬瘧褰撳墠鏂囦欢閫夐」锛屽涓嬪浘鎵绀 4銆佹渶鍚庤繍琛岀▼搴忓氨鍙互寰楀埌鍔犲噺涔橀櫎杩愮畻鐨勭粨鏋滀簡锛屽涓嬪浘鎵绀 宸ュ叿/鏉愭枡 Code Blocks ...
绛旓細int main(){ int a,b;scanf("%d %d",&a,&b);printf("%d\t",a + b);printf("%d\t",a - b);printf("%d\t",a * b);printf("%d\t",a / b);return 0;} 杩愯鎴浘 鍒嗘瀽C璇█涓鐨鍔犲噺涔闄ゅ拰鏁板涓殑鍔犲噺涔橀櫎涓鏍凤紝涓嶅悓鍦ㄤ簬绗﹀彿鐨勮〃绀洪棶棰橈紝涔樺彿闇瑕佺敤鈥*鈥濊〃绀恒傞櫎鍙烽渶瑕佺敤...
绛旓細c璇█鐨勫姞鍑忎箻闄ゅ彲浠ヨ繖鏍峰啓锛屽亣璁炬湁int鍙橀噺a,b锛宑 鍔犳硶:c=a+b 鍑忔硶锛歝=a-b 涔樻硶锛歝=a*b 闄ゆ硶锛歝=a/b
绛旓細鏄殑锛屽彲浠ヤ娇鐢C璇█缂栧啓涓涓绠楀櫒绋嬪簭锛岃兘澶瀹炵幇鍔犮佸噺銆佷箻銆侀櫎绛夋贩鍚堣繍绠椼備笅闈㈡槸涓涓畝鍗曠殑绀轰緥绋嬪簭锛歚``c include <stdio.h> int main() { char operator;double num1, num2, result;printf("Enter an operator (+, -, *, /): ");scanf("%c", &operator);printf("Enter two ...
绛旓細int a,b, i;char c;for(i=0;;i++){ printf("璇疯緭鍏ユ墍瑕佽绠楃殑涓や釜鏁,浠ュ強鎵瑕佹墽琛岀殑璁$畻绗﹀彿\n");scanf("%d %d %c", &a,&b,&c);switch (c){ case '+':printf("鎵瑕佽绠楃殑寮忓瓙锛%d+%d\n",a,b);a = a + b;printf("璁$畻缁撴灉涓猴細%d\n\n",a);break;case '-':prin...
绛旓細1銆佹墦寮C-Free5.0鏂板缓涓涓┖鐧介〉闈紝鐒跺悗灏C璇█鐨鍩虹鏍煎紡鍐欏畬锛屾敞鎰忔牸寮忕缉杩涖傚涓嬪浘鎵绀恒2銆佺劧鍚庤緭鍏モ int a=10; float b=5,c; 鈥濇敞鎰忥細 float 鏄诞鐐瑰瀷锛宨nt 鏄暣鍨嬨傝繖涓槸鐢ㄦ潵瀹氫箟C璇█涓鐨勬暟鍊肩殑绫诲瀷锛岃繕鏈夊鏋滀竴涓鍙ョ粨鏉熼偅灏辫鎵撲笂鈥滐紱鈥濓紝杩欎釜寰堥噸瑕侊紝涓嶈蹇樿浜嗐備笂闈㈢殑璇彞...