Code Funktion: Es ist ein russischer Bauer Multiplikation Rechner.
Code:Der Code wird nicht ausgegeben, aber er läuft. [c]
#include <stdio.h>
#include <math.h>
main(){
int ier, product = 0, check, i = 1; //var to store numbers
float cand; //Var to store decimal numbers
while(i != 0){
printf("\nInput only whole numbers!"); //Ask user for number to be multiplied
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
while(i != 0){
if((ier == 0 && cand > 0) || (ier > 0 && cand == 0) || (ier == 0 && cand < 0) || (ier < 0 && cand == 0)){//Regulations for the code
printf("Product: 0");
printf("\n\nInput only whole numbers!");
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
}
if((ier > 0 && cand > 0) || (ier < 0 && cand > 0) || (ier > 0 && cand < 0) || (ier < 0 && cand < 0)){
break;
}
if(ier == 0 && cand == 0){//If user inputs 0 and 0, end code
return 0;
}
}
while(i != 0){
if((ier < 0 && cand > 0) || (ier > 0 && cand < 0) || (ier < 0 && cand < 0)){//Regulations for the code
printf("Values must not be negative");
printf("\n\nInput only whole numbers!");
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
}
if((ier > 0 && cand > 0) || (ier == 0 && cand > 0) || (ier > 0 && cand == 0) || (ier == 0 && cand < 0) || (ier < 0 && cand == 0)){
break;
}
if(ier == 0 && cand == 0){//If user inputs 0 and 0, end code
return 0;
}
}
if(ier > 0 && cand > 0){
printf("Calculating Product:");//It should print this, but it isn't for some reason.
while(i != 0){
if(fmod(cand, 2) != 0){
if(fmod(cand, 1) != 0){
product = product + floor(cand);
cand = floor(cand);
printf("%d \t%f", ier, cand);
}
else{
product = product + cand;
printf("%d \t%f", ier, cand);
}
}
if(cand == 1){
printf("Product: %d", product);
break;
}
ier *= 2;
cand /= 2;
}
}
}
}
Problem: Wenn ich Code und I-Eingang laufe beispiel 5 und 2, hält es ohne etwas Ausgeben auf Laufen. Ich denke, das Problem im Code ist die fmod()
. Ich verwende nur fmod
, weil Sie den Modulo-Operator %
nicht für Float-Variablen verwenden können.
Thing, das ich getan habe: Ich änderte die fmod zu dem Modulo-Operator %
und machte cand in eine ganze Zahl. Das hat funktioniert, aber jetzt habe ich ein Problem mit der Dezimalzahl, da ganze Zahlen die Dezimalzahl haben. Also ging ich zurück zu fmod
.
Eingang:
gcc version 4.6.3
Input only whole numbers!
Multiplier: 5
Multiplicand: 2
/*Runtime: infinite*/
Ausgang Ich möchte
Multiplier: 57
Multiplicand: 86
Calculating product:
114 43
228 21
912 5
3648 1
Product: 4902
Multiplier: 48
Multiplicand: -36
Values must not be negative
Multiplier: 27
Multiplicand: 0
Product: 0
Multiplier: 0
Multiplicand: 0
P. S: Es ist mein erstes Mal fmod
und floor()
verwenden.
Kommentare sind nicht für längere Diskussion; Diese Konversation wurde [in den Chat verschoben] (http://chat.stackoverflow.com/rooms/138405/discussion-on-question-by-manav-dubey-streetsville-ss-the-code-isnt-outputti). –