请高手帮忙看看这个简单的求圆周和面积的程序
以下是一个简单的求圆面积和圆周的程序,具体的要求是:1. 按照输入的半径来计算面积和圆周;
2. 输入的半径必须大于0,否侧,系统会要求重新输入有效半径值;
3. 当输入-1为半径值的时候,系统结束计算。并且结束之后统计输入有效半径次数和一共输入的有效半径的总和。
现在遇到的难题是:关于计算和统计都没有问题,只是在输入-1来结束程序的时候,程序总是要先计算-1为半径的圆周和面积,再进行总体的统计。
怎样才能让系统跳过计算半径为-1的圆周和面积?
我刚开始学C语言不到2周,实在是没法解决这个问题。请高手帮忙解惑。先谢过了。
#include "stdafx.h"
#include "stdio.h"
#define PI 3.14159265
int _tmain(int argc, _TCHAR* argv[])
{
double radius=0.0;
double circumference =0.0;
double area = 0.0;
int calculation_counted = -1; // adjustment because of the circule which has the radius=-1 (to end) will be counted
double sum_radiuses= 1.0; // adjustment because of the radius=-1 will be entered to end
while (radius != -1)
{
printf ("Please enter a radius value (-1 to end): ");
scanf_s ("%lf", &radius);
while ((radius<-1) || (-1 < radius && radius< 0))// radius must be bigger than 0
{
printf ("The radius must >=0, enterd a valid radius (or -1 to end): ");
scanf_s ("%lf", &radius);
}
circumference = 2*PI*radius;// circumference equation
area = PI*radius*radius; // area equation
calculation_counted = calculation_counted + 1;
sum_radiuses = sum_radiuses + radius;
printf ("The ciucumference is %lf \n", circumference); // %lf, %#.#f.... depends on the requirement
printf ("The area is %lf\n\n", area);
}
printf ("%d valid radiuses have been counted,\n ", calculation_counted);
printf ("The sum of valid radiuses is %5.2f\n ", sum_radiuses);
printf ("\n\n");
return 0;
}