cjweffort

博客园 首页 联系 订阅 管理
// 1060_Are_They_Equal.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

const int N = 303;

void simplifyFloatNum(int n, char *str, int *dig, int &exp){
	int len = strlen(str);
	bool tag = false;
	int firstNotZeroNum, pointNum;
	for(int i = 0; str[i]; i++){
		if(str[i] >= '1' && str[i] <= '9'){
			firstNotZeroNum = i;
			tag = true;
			break;
		}
	}
	if(!tag){
		exp = 0;
		return;
	}
	tag = false;
	for(int i = 0; str[i]; i++){
		if(str[i] == '.'){
			pointNum = i;
			tag = true;
			break;
		}
	}
	if(!tag)
		pointNum = len;
	exp = firstNotZeroNum < pointNum ? (pointNum - firstNotZeroNum) : (pointNum - firstNotZeroNum + 1);
	for(int i = firstNotZeroNum, cnt = 0; str[i] && cnt < n; i++){
		if(str[i] != '.')
			dig[++cnt] = str[i] - '0';
	}
}

void print(int n, int *dig, int exp){
	printf("0.");
	for(int i = 1; i <= n; i++){
		printf("%d", dig[i]);	
	}
	printf("*10^");
	printf("%d", exp);
}

void isEqual(int n, int *dig1, int exp1, int *dig2, int exp2){
	if(exp1 != exp2){
		printf("NO ");
		print(n, dig1, exp1);
		printf(" ");
		print(n, dig2, exp2);
		printf("\n");
	}
	else{
		bool tag =true;
		for(int i = 1; i <= n; i++){
			if(dig1[i] != dig2[i]){
				tag=false;
				break;
			}
		}
		if(tag == false){
			printf("NO ");
			print(n, dig1, exp1);
			printf(" ");
			print(n, dig2, exp2);
			printf("\n");
		}
		else{
			printf("YES ");
			print(n, dig1, exp1);
			printf("\n");
		}
	}
}

int main(int argc, char* argv[])
{
	int n;
	char str1[N], str2[N];
	int dig1[N], dig2[N];
	int exp1, exp2;
	while(~scanf("%d%s%s", &n, str1, str2)){
		memset(dig1, 0, sizeof(dig1));
		memset(dig2, 0, sizeof(dig2));
		simplifyFloatNum(n, str1, dig1, exp1);
		simplifyFloatNum(n, str2, dig2, exp2);
		isEqual(n, dig1, exp1, dig2, exp2);
	}
	return 0;
}

posted on 2013-07-07 16:44  cjweffort  阅读(154)  评论(0编辑  收藏  举报