Code Fonction: C'est une calculatrice russe de multiplication paysanne.
code:Le code ne sort pas, mais il est en cours d'exécution. [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;
}
}
}
}
Problème: Quand je lance le code et je entrée 5 et 2 par exemple, il continue à fonctionner sans sortir quoi que ce soit. Je pense que le problème dans le code est le fmod()
. J'utilise seulement fmod
parce que vous ne pouvez pas utiliser l'opérateur de module %
sur des variables flottantes.
Chose que j'ai fait: J'ai changé le fmod à l'opérateur de module %
et fait cand dans un nombre entier. Cela a fonctionné, mais maintenant j'ai un problème avec la décimale puisque les entiers arrondissent le nombre décimal. Je suis donc retourné à fmod
.
Entrée:
gcc version 4.6.3
Input only whole numbers!
Multiplier: 5
Multiplicand: 2
/*Runtime: infinite*/
sortie Je veux
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: Il est ma première fois en utilisant fmod
et floor()
.
Les commentaires ne sont pas pour une discussion prolongée; cette conversation a été [déplacée pour discuter] (http://chat.stackoverflow.com/rooms/138405/discussion-on-question-by-manav-dubey-streetsville-ss-the-code-isnt-outputti). –