求助啊80分---c语言
查看原帖
求助啊80分---c语言
238203
跟你沟通楼主2022/10/28 22:10
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cal_len(int num)
{
	int t=0;
	if(num==0)
		return 0;
	else if(num>0)
		while(num)
		{
			t++;
			num/=10;
		}
	else
	{
		++t;
		num*=-1;
		while(num)
		{
			t++;
			num/=10;
		}
	}
	return t;
}
int main()
{
	char str[100],c;
	int a=0,b=0,n,x;
	scanf("%d",&n);getchar();
	while(n-->0)
	{
		memset(str,0,100);
		scanf("%s",str);
		switch(str[0])
		{
			case 'a':
				scanf("%d%d",&a,&b);
				printf("%d+%d=%d\n",a,b,a+b);
				x=cal_len(a+b)+2;
				c=str[0];
				break;
			case 'b':
				scanf("%d%d",&a,&b);
				printf("%d-%d=%d\n",a,b,a-b);
				x=cal_len(a-b)+2;
				c=str[0];
				break;
			case 'c':
				scanf("%d%d",&a,&b);
				printf("%d*%d=%d\n",a,b,a*b);
				x=cal_len(a*b)+2;
				c=str[0];
				break;
			default:
				int k=strlen(str);
				a=0;
				for(int i=0;i<k;++i)
					a=a*10+str[i]-'0';
				scanf("%d",&b);
				switch(c)
				{
					case 'a':
						printf("%d+%d=%d\n",a,b,a+b);
						x=cal_len(a+b)+2;
						break;
					case 'b':
						printf("%d-%d=%d\n",a,b,a-b);
						x=cal_len(a-b)+2;
						break;
					case 'c':
						printf("%d*%d=%d\n",a,b,a*b);
						x=cal_len(a*b)+2;
						break;
				}
				break;
		}
		x+=cal_len(a)+cal_len(b);
		printf("%d\n",x);
	}
	return 0;
}
2022/10/28 22:10
加载中...