gpt4 book ai didi

通过电阻器上的颜色计算电阻

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:42 25 4
gpt4 key购买 nike

#include<stdio.h>
#include<math.h>
#include<string.h>

int main(){

int unos;
char crna[20] = "crna";
char braon[20] = "braon";
char crvena[20] = "crvena";
char narandzasta[20] ="narandzasta";
char zuta[20] = "zuta";
char zelena[20] = "zelena";
char plava[20] = "plava";
char ljubicasta[20] = "ljubicasta";
char siva[20] = "siva";
char bela[20] = "bela";
char zlatna[20] = "zlatna";
char srebrna[20] = "srebrna";

puts("Unesite broj prstenova:");
scanf("%i", &unos);

if(unos == 5){

int c1, c2, c3, m=0, t;
char unos1[20] = "\0";
int r;

puts("Unesi boju 1. prstena:");
scanf("%s",&unos1);

if(strcmp(unos1, crna) == 0)
c1 = 0;
else if(strcmp(unos1, braon) == 0)
c1 = 1;
else if(strcmp(unos1, crvena) == 0)
c1 = 2;
else if(strcmp(unos1, narandzasta) == 0)
c1 = 3;
else if(strcmp(unos1, zuta) == 0)
c1 = 4;
else if(strcmp(unos1, zelena) == 0)
c1 = 5;
else if(strcmp(unos1, plava) == 0)
c1 = 6;
else if(strcmp(unos1, ljubicasta) == 0)
c1 = 7;
else if(strcmp(unos1, siva) == 0)
c1 = 8;
else if(strcmp(unos1, bela) == 0)
c1 = 9;

char unos2[20] = "\0";

puts("Unesi boju 2. prstena:");
scanf("%s", &unos2);

if(strcmp(unos2, crna) == 0)
c2 = 0;
else if(strcmp(unos2, braon) == 0)
c2 = 1;
else if(strcmp(unos2, crvena) == 0)
c2 = 2;
else if(strcmp(unos2, narandzasta) == 0)
c2 = 3;
else if(strcmp(unos2, zuta) == 0)
c2 = 4;
else if(strcmp(unos2, zelena) == 0)
c2 = 5;
else if(strcmp(unos2, plava) == 0)
c2 = 6;
else if(strcmp(unos2, ljubicasta) == 0)
c2 = 7;
else if(strcmp(unos2, siva) == 0)
c2 = 8;
else if(strcmp(unos2, bela) == 0)
c2 = 9;

char unos3[20] = "\0";

puts("Unesi boju 3. prstena:");
scanf("%s", &unos3);

if(strcmp(unos3, crna) == 0)
c3 = 0;
else if(strcmp(unos3, braon) == 0)
c3 = 1;
else if(strcmp(unos3, crvena) == 0)
c3 = 2;
else if(strcmp(unos3, narandzasta) == 0)
c3 = 3;
else if(strcmp(unos3, zuta) == 0)
c3 = 4;
else if(strcmp(unos3, zelena) == 0)
c3 = 5;
else if(strcmp(unos3, plava) == 0)
c3 = 6;
else if(strcmp(unos3, ljubicasta) == 0)
c3 = 7;
else if(strcmp(unos3, siva) == 0)
c3 = 8;
else if(strcmp(unos3, bela) == 0)
c3 = 9;

char mnoz[20] = "\0";
puts("Unesi boju 4. prstena:");
scanf("%s", &mnoz);

if(strcmp(mnoz, crna) == 0)
m = 1;
else if(strcmp(mnoz, braon))
m = 10;
else if(strcmp(mnoz, crvena))
m = 100;
else if(strcmp(mnoz, narandzasta))
m = 1000;
else if(strcmp(mnoz, zuta))
m = 10000;
else if(strcmp(mnoz, zelena))
m = 100000;
else if(strcmp(mnoz, plava))
m = 1000000;
else if(strcmp(mnoz, ljubicasta))
m = 10000000;
else if(strcmp(mnoz, siva))
m = 100000000;
//else if(strcmp(mnoz, bela))
//m = 10*10*10*10*10*10*10*10*10;
else if(strcmp(mnoz, zlatna))
m = 0.1;
else if(strcmp(mnoz, srebrna))
m = 0.01;

r = ((c1*100)+(c2*10)+(c3*1)) * m;
printf("%i,%i",m,r);

}

return 0;

}

一切正常,但变量“m”始终为 10,如果我在询问“Unesi boju 4.prstena:”时输入“narandzasta”,它应该是 1000。我可以将整数和字符的名称翻译成英文如果需要的话。我是编程初学者(我们刚开始学习字符串)。

最佳答案

你忘了和 0 比较看字符串是否相等:

 if(strcmp(mnoz, crna) == 0)
m = 1;
else if(strcmp(mnoz, braon) == 0)
m = 10;
else if(strcmp(mnoz, crvena) == 0)
m = 100;
else if(strcmp(mnoz, narandzasta) == 0)
m = 1000;
else if(strcmp(mnoz, zuta)==0)
m = 10000;
else if(strcmp(mnoz, zelena) == 0)
m = 100000;
else if(strcmp(mnoz, plava) == 0)
m = 1000000;
else if(strcmp(mnoz, ljubicasta) == 0)
m = 10000000;
else if(strcmp(mnoz, siva) == 0)
m = 100000000;

并且您将 m 声明为 int 并在最终使用中声明为 double,您的代码已更正:

#include<stdio.h>
#include<math.h>
#include<string.h>

int main(){

int unos;
char crna[20] = "crna";
char braon[20] = "braon";
char crvena[20] = "crvena";
char narandzasta[20] ="narandzasta";
char zuta[20] = "zuta";
char zelena[20] = "zelena";
char plava[20] = "plava";
char ljubicasta[20] = "ljubicasta";
char siva[20] = "siva";
char bela[20] = "bela";
char zlatna[20] = "zlatna";
char srebrna[20] = "srebrna";

puts("Unesite broj prstenova:");
scanf("%i", &unos);

if(unos == 5){

int c1, c2, c3, t;
double m=0;
char unos1[20] = "\0";
int r;

puts("Unesi boju 1. prstena:");
scanf("%s", unos1);

if(strcmp(unos1, crna) == 0)
c1 = 0;
else if(strcmp(unos1, braon) == 0)
c1 = 1;
else if(strcmp(unos1, crvena) == 0)
c1 = 2;
else if(strcmp(unos1, narandzasta) == 0)
c1 = 3;
else if(strcmp(unos1, zuta) == 0)
c1 = 4;
else if(strcmp(unos1, zelena) == 0)
c1 = 5;
else if(strcmp(unos1, plava) == 0)
c1 = 6;
else if(strcmp(unos1, ljubicasta) == 0)
c1 = 7;
else if(strcmp(unos1, siva) == 0)
c1 = 8;
else if(strcmp(unos1, bela) == 0)
c1 = 9;

char unos2[20] = "\0";

puts("Unesi boju 2. prstena:");
scanf("%s", unos2);

if(strcmp(unos2, crna) == 0)
c2 = 0;
else if(strcmp(unos2, braon) == 0)
c2 = 1;
else if(strcmp(unos2, crvena) == 0)
c2 = 2;
else if(strcmp(unos2, narandzasta) == 0)
c2 = 3;
else if(strcmp(unos2, zuta) == 0)
c2 = 4;
else if(strcmp(unos2, zelena) == 0)
c2 = 5;
else if(strcmp(unos2, plava) == 0)
c2 = 6;
else if(strcmp(unos2, ljubicasta) == 0)
c2 = 7;
else if(strcmp(unos2, siva) == 0)
c2 = 8;
else if(strcmp(unos2, bela) == 0)
c2 = 9;

char unos3[20] = "\0";

puts("Unesi boju 3. prstena:");
scanf("%s", unos3);

if(strcmp(unos3, crna) == 0)
c3 = 0;
else if(strcmp(unos3, braon) == 0)
c3 = 1;
else if(strcmp(unos3, crvena) == 0)
c3 = 2;
else if(strcmp(unos3, narandzasta) == 0)
c3 = 3;
else if(strcmp(unos3, zuta) == 0)
c3 = 4;
else if(strcmp(unos3, zelena) == 0)
c3 = 5;
else if(strcmp(unos3, plava) == 0)
c3 = 6;
else if(strcmp(unos3, ljubicasta) == 0)
c3 = 7;
else if(strcmp(unos3, siva) == 0)
c3 = 8;
else if(strcmp(unos3, bela) == 0)
c3 = 9;

char mnoz[20] = "\0";
puts("Unesi boju 4. prstena:");
scanf("%s", mnoz);

if(strcmp(mnoz, crna) == 0)
m = 1;
else if(strcmp(mnoz, braon) == 0)
m = 10;
else if(strcmp(mnoz, crvena) == 0)
m = 100;
else if(strcmp(mnoz, narandzasta) == 0)
m = 1000;
else if(strcmp(mnoz, zuta) == 0)
m = 10000;
else if(strcmp(mnoz, zelena) == 0)
m = 100000;
else if(strcmp(mnoz, plava) == 0)
m = 1000000;
else if(strcmp(mnoz, ljubicasta) == 0)
m = 10000000;
else if(strcmp(mnoz, siva) == 0)
m = 100000000;
//else if(strcmp(mnoz, bela))
//m = 10*10*10*10*10*10*10*10*10;
else if(strcmp(mnoz, zlatna))
m = 0.1;
else if(strcmp(mnoz, srebrna))
m = 0.01;

r = ((c1*100)+(c2*10)+(c3*1)) * m;
printf("%lf,%i",m,r);

}

return 0;

}

关于通过电阻器上的颜色计算电阻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55249321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com