c 语言的计算(电阻)
发布网友
发布时间:2022-05-04 23:16
我来回答
共3个回答
热心网友
时间:2022-06-27 01:48
下面是正文:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char danwei[3]={'K','M','G'};
int i=-1; bool flag=false;
int x, y, z, p;
double pp;
//explanation
printf("\nThis program is to calculate the roots of a quadratic equation.\n");
printf("1=宗 2=红 3=澄 4=黄 5=绿 6=蓝 7=紫 8=灰 9=白 0=黑\n");
//prompt for input
printf("Please choose the first colours you want:\n");
scanf(" %d", &x);
printf("Please choose the second colours you want:\n");
scanf(" %d", &y);
printf("Please choose the third colours you want:\n");
scanf(" %d", &z);
//calculation
if(x<0 || x>9) { printf("input x error!"); return -1; }
if(y<0 || y>9) { printf("input y error!"); return -1; }
if(z<0 || z>9) { printf("input z error!"); return -1; }
while(z>=3 && i<2)
{ i++; z-=3;
}
p=(x*10+y)*pow(10,z);
pp=(double)p;
while(pp>=1000.0 && i<2)
{
i++; pp=pp/1000.0;
flag=true;
}
if(i!=-1 && i<3)
{
if(flag) printf("The value of reisitance is %f%c\n", pp,danwei[i]);
else printf("The value of reisitance is %d%c\n", p,danwei[i]);
}
else printf("The value of reisitance is %d\n",p);
system("PAUSE");
return 0;
}
我试验上面代码是可以的。
热心网友
时间:2022-06-27 01:49
类型转换要写(double)z,不是double(x)
----------------
你程序的思想就是将输入的字母转换为相应的指数,但是非要输入字母不可吗?不可以用数字代替吗?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int x, y, z, p;
printf("\nThis program is to calculate the roots of a quadratic equation.\n");
printf("1=宗 2=红 3=澄 4=黄 5=绿 6=蓝 7=紫 8=灰 9=白 0=黑\n");
printf("Please choose the first colours you want:\n");
scanf(" %d", &x);
printf("Please choosethe second colours you want:\n");
scanf(" %d", &y);
printf("Please choose the third colours you want:\n");
scanf(" %d", &z);
p=(x*10+y)*pow(10,z);
printf("The vlaue of reisitance is %d\n", p);
system("PAUSE");
return 0;
}
热心网友
时间:2022-06-27 01:49
你P定义的不是int吗