//
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAXN 100+10
char s[MAXN];
void decode_prefix(char s[],double *d) //单位转换函数
{
int i=0;
while (isdigit(s[i]) || s[i]=='.') i++;
switch(s[i])
{
case 'm': *d/=1000; break;
case 'k': *d*=1000; break;
case 'M': *d*=1000000; break;
default: break;
}
}
int main()
{
int i,j,n,is_P,is_I,is_U;
double P,I,U;
scanf("%d",&n);
getchar(); //读取之前输入的字符n,后面fgets函数读入时从流中继续读取一行
for(i=0;i<n;i++)
{
is_P=is_I=is_U=0;
fgets(s,MAXN,stdin);
for(j=0;j<(int)strlen(s);j++)
{
if(s[j]=='P' && s[j+1]=='=')
{
is_P=1;
sscanf(&s[j+2],"%lf",&P); //读取是数字的字符,转换成浮点型
decode_prefix(&s[j+2],&P); //单位转换函数,检查数据后的单位
}
if(s[j]=='I' && s[j+1]=='=')
{
is_I=1;
sscanf(&s[j+2],"%lf",&I);
decode_prefix(&s[j+2],&I);
}
if(s[j]=='U' && s[j+1]=='=')
{
is_U=1;
sscanf(&s[j+2],"%lf",&U);
decode_prefix(&s[j+2],&U);
}
}
printf("Problem #%d\n",i+1);
if(is_P!=1) printf("P=%.2lfW\n",U*I);
if(is_I!=1) printf("I=%.2lfA\n",P/U);
if(is_U!=1) printf("U=%.2lfV\n",P/I);
printf("\n");
}
return 0;
}
相关函数 | isdigit |
表头文件 | #include<,ctype,h> |
定义函数 | int,isdigit(,char,c) |
函数说明 | 检查参数c是否为,阿拉伯数字,0到9 |
返回值 | 若参数c为阿拉伯数字,则返回TRUE,否则返回NULL(0) |
附加说明 | 此为,宏定义,非真正函数 |
范例 | /* 找出str字符串中为阿拉伯数字的字符*/ #include<ctype.h> main() { char str[]="123@#FDsP[e?"; int i; for(i=0;str[i]!=0;i++) { if( isdigit (str[i]) ) printf( "%c is an digit character\n",str[i] ); } } |
执行 | 1 is an digit character 2 is an digit character 3 is an digit character |