繁体   English   中英

如何计算程序在C中循环的次数

[英]How to count how many times the program goes through a loop in C

所以我是一个总 C 菜鸟,这是我的第一个问题,请温柔。 我有这个程序可以扫描患者的体重(磅)和他们的华氏温度,并将信息转换为千克和摄氏度。 这一切都很好,但我应该添加一个循环,以便用户可以扫描他们希望的尽可能多的患者(我想我这样做了)并以某种方式计算和打印有多少患者接受了治疗。 我正在努力的那部分,我认为那将是“后增量”,但我无法理解我为我的生活找到的解释。 如果有人有耐心用简单的话由我来运行它,那将非常感激:)

这是代码的样子:(它是法语的,但我想你会明白的)

#include <stdio.h>

int main()
{

/* 1 livre = 0.454 kg */
const float LBS_EN_KG = 0.454;
int poids, /* le poids en livres */ patients; 
/* la température en Fahrenheit */
float fahrenheit, celsius;
char reponse;



do
{

fflush(stdin);
/* Saisie de données tapées au clavier */
   printf("Entrez le poids en nombre de livres et la temperature en  degre Fahrenheit \n");

   scanf("%d%f", &poids, &fahrenheit);
   celsius=(fahrenheit - 32)*5/9;


    /* Affichage de ces informations */
    printf("Le poids en kg est : %5.2fkg)\n", (poids * LBS_EN_KG));

    printf("La temperature en Celsius est : %5.2f Degre Celsius \n",celsius );

    printf("Voulez-vous continuer ? (O/N) \n");    
    fflush(stdin);
    reponse = toupper(getchar());



} while (reponse == 'O');

return 0;

}

您必须使用变量来存储迭代,例如:

int iterations=0;//very important equals 0 otherwise we dont know what iterations store and we need a 0 (cause i start counting on 0)
do {
       //your code
       iterations++;
} while (response=='0');
//here iterations stores number of times program go through loop

如果您使用像 i++ 或像 ++i 这样的增量,则这些增量在这种情况下是相同的,它优先于其他操作数的操作,例如:

term=term2++;

这里 term 将分配给 term2,然后 term2 递增

term=++term2;

这里 term2 递增,然后 term 也取了它的值

请记住,前置增量和后置增量优先于其他操作。 我希望能帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM