实现代码(原创):
#include<stdio.h> #include<string.h> #include<stdlib.h> char* itoa(int value, char* str, int radix) { static char dig[] = "0123456789" "abcdefghijklmnopqrstuvwxyz"; int n = 0, neg = 0; unsigned int v; char* p, *q; char c; /* 负数:十进制直接在前面加负号,其它进制用补码 */ if (radix == 10 && value < 0) { v = -value; neg = 1; }else { v = value; } /* 其它进制的负数转为unsigned int类型,即取了补码 */ do { str[n++] = dig[v%radix]; v /= radix; } while (v); if (neg) str[n++] = '-'; str[n] = '\0'; /* 字符串反转 */ for (p = str, q = p + (n-1); p < q; ++p, --q) c = *p, *p = *q, *q = c; return str; } int main() { int nums[60]; char str[60][32]; int n; scanf("%d",&n); getchar(); char sym[6]; char s2; int i,j,k; int m=0; while(n) { scanf("%s",sym); //getchar(); if(strlen(sym)==1 && (sym[0]=='a'||sym[0]=='b'||sym[0]=='c')) { char ss1[20],ss2[20],ss3[20]; int l1,l2,l3; int x,y,z; s2 = sym[0]; scanf("%d%d",&x,&y); itoa(x,ss1,10); itoa(y,ss2,10); l1 = strlen(ss1); l2 = strlen(ss2); if(s2=='a') { z = x+y; itoa(z,ss3,10); l3 = strlen(ss3); s2='+'; }else if(s2=='b') { z = x-y; itoa(z,ss3,10); l3 = strlen(ss3); s2='-'; }else if(s2=='c') { z = x*y; itoa(z,ss3,10); l3 = strlen(ss3); s2='*'; } for(i=0;i<l1;i++) str[m][i] = ss1[i]; str[m][i] = s2; i++; for(k=0;k<l2;k++,i++) str[m][i] = ss2[k]; str[m][i] = '='; i++; for(j=0;j<l3;j++,i++) str[m][i] = ss3[j]; nums[m]=l1+l2+l3+2; m++; }else { char ss2[20],ss3[20]; int l1,l2,l3; int x,y,z; x = atoi(sym); scanf("%d",&y); itoa(y,ss2,10); l1 = strlen(sym); l2 = strlen(ss2); if(s2=='+') { z = x+y; itoa(z,ss3,10); l3 = strlen(ss3); }else if(s2=='-') { z = x-y; itoa(z,ss3,10); l3 = strlen(ss3); }else if(s2=='*') { z = x*y; itoa(z,ss3,10); l3 = strlen(ss3); } for(i=0;i<l1;i++) str[m][i] = sym[i]; str[m][i] = s2; i++; for(k=0;k<l2;k++,i++) str[m][i] = ss2[k]; str[m][i] = '='; i++; for(j=0;j<l3;j++,i++) str[m][i] = ss3[j]; nums[m]=l1+l2+l3+2; m++; } n--; } for(i=0;i<m;i++) { for(j=0;j<nums[i];j++) printf("%c",str[i][j]); printf("\n"); printf("%d\n",nums[i]); } return 0; }
标签:练习题,P1957,洛谷,int,s2,ss3,char,sym,str From: https://www.cnblogs.com/kmxojer/p/16955463.html