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

问题描述 投票:0回答:1

所以我总的来说是C菜鸟,这是我的第一个问题,请保持温柔。我有一个程序,该程序以lbs为单位扫描患者体重,以华氏度为单位扫描患者的温度,并以kg和摄氏度为单位转换信息。一切都很好,但我应该添加一个循环,以便用户可以根据需要扫描任意数量的患者(我想我已经这样做了),并以某种方式计数和打印了接受治疗的患者数。我一直在努力的那一部分,我认为那是“职位增加”,但是我无法理解我一生中发现的解释。如果有人有耐心由我来用简单的话来运行它,将不胜感激:)

这是代码的样子:(它是法文,但我想您会明白的。]

#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;

}
c loops post-increment
1个回答
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的增量,则没有关系,因为在这种情况下,此类增量是相同的,因此它的操作优先于其他操作数,例如:assigments

term=term2++;

这里术语将是对term2的归属,然后对term2进行递增

term=++term2;

这里term2递增,然后term也取其值

记住前增量和后增量优先于其他操作。希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.