电子秤---乱码转格式

 
// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
char* GetResult(char strlast[]);
long Char2long(char strres[]);
char* getWeight(char result[]);

char  res[8],result[30], weight[30];
long sum;
//假设乱码的格式是 233+34243434+4324,提取出+号中间的那段数
int _tmain(int argc, _TCHAR* argv[])
{
	char str[30], *temp;
	while(scanf("%s", str)!=EOF)
	{
		printf("result = %s\n", GetResult(str));
		printf("long = %ld\n", Char2long(GetResult(str)));
		printf("weight = %s\n", getWeight(result));
	}
	return 0;
}

char* GetResult(char strlast[])
{
	int len = strlen(strlast);
	int i, j = 0;
	for(i = 0; i < len; i++)
	{
		while(strlast[i] != '+' && strlast[i] != '\0') i++;
		i++;
		while(strlast[i] != '+' && strlast[i] != '\0')
		{
			result[j++] = strlast[i++];
		}
		result[j] = '\0';
		
		break;
	}
	return result;
}

long Char2long(char* strres)
{
	sum = 0;
	printf("strres = %s\n", strres);
	int len = strlen(strres);
	for(int i = 0; i < len; i++)
	{
		long temp = strres[i] - 48;
		sum = sum * 10 + temp;
	}
	printf("\n");
	return sum;
}

char* getWeight(char result[])
{
	int j = 0;
	for(int k=0; k<4; k++)
	{
		if(result[k] == '0') continue; //去掉开头的0
		weight[j++] = result[k];
	}
	if(j == 0) weight[j++] = '0'; //如果前面4位都是0,则自动添加一个0
	weight[j++] = '.';
	weight[j++] = result[4];
	weight[j++] = result[5];
	weight[j] = '\0';

	return weight;
}

你可能感兴趣的:(c++,MFC)