简单字符串处理题目
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
char buffer[MAX_LEN];
int case_count;
void func()
{
double u, i, p;
char temp[100];
char *start, *pp;
u = i = p = -1.0;
start = buffer;
pp = strstr(start, "U=");
if(pp)
{
start = pp;
pp = strstr(start, "V");
memcpy(temp, start, pp-start+1);
temp[pp-start+1] = '\0';
if(pp[-1] == 'm')
{
sscanf(temp, "U=%lfmV", &u);
u /= 1000;
}
else if(pp[-1] == 'k')
{
sscanf(temp, "U=%lfkV", &u);
u *= 1000;
}
else if(pp[-1] == 'M')
{
sscanf(temp, "U=%lfMV", &u);
u *= 1000000;
}
else
{
sscanf(temp, "U=%lfV", &u);
}
}
start = buffer;
pp = strstr(start, "I=");
if(pp)
{
start = pp;
pp = strstr(start, "A");
memcpy(temp, start, pp-start+1);
temp[pp-start+1] = '\0';
if(pp[-1] == 'm')
{
sscanf(start, "I=%lfmA", &i);
i /= 1000;
}
else if(pp[-1] == 'k')
{
sscanf(start, "I=%lfkA", &i);
i *= 1000;
}
else if(pp[-1] == 'M')
{
sscanf(start, "I=%lfMA", &i);
i *= 1000000;
}
else
{
sscanf(start, "I=%lfA", &i);
}
}
start = buffer;
pp = strstr(start, "P=");
if(pp)
{
start = pp;
pp = strstr(start, "W");
memcpy(temp, start, pp-start+1);
temp[pp-start+1] = '\0';
if(pp[-1] == 'm')
{
sscanf(start, "P=%lfmW", &p);
p /= 1000;
}
else if(pp[-1] == 'k')
{
sscanf(start, "P=%lfkW", &p);
p *= 1000;
}
else if(pp[-1] == 'M')
{
sscanf(start, "P=%lfMW", &p);
p *= 1000000;
}
else
{
sscanf(start, "P=%lfW", &p);
}
}
printf("Problem #%d\n", case_count);
if(-1.0 == u)
printf("U=%.2lfV\n\n", p/i);
else if(-1.0 == i)
printf("I=%.2lfA\n\n", p/u);
else
printf("P=%.2lfW\n\n", i*u);
}
int main(void)
{
int n;
gets(buffer);
sscanf(buffer, "%d", &n);
case_count = 0;
while(n--)
{
gets(buffer);
case_count ++;
func();
}
return 0;
}