一、 顺序结构程序设计========================================1 已知三角形的三边长为a,b,c,计算三角形面积的公式为:area = ,s=要求编写程序,从键盘输入a,b,c的值,计算并输出三角形的面积。
#include#includevoid main(){ float a,b,c,s,area; scanf("%f,%f,%f",&a,&b,&c); s=1.0/2*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("a=%7.2f, b=%7.2f, c=%7.2f, s=%7.2f\n",a,b,c,s); printf("area=%7.2f\n",area);}
2 编程从键盘输入圆的半径r,计算并输出圆的周长和面积。#include "stdio.h"#define Pi 3.14void main(){float r,c,area;printf("请输入圆的半径:");scanf("%f",&r);c=2*Pi*r;area=Pi*r*r;printf("该圆的周长是%.2f,面积是%.2f\n",c,area);}
二、 选择结构程序设计========================================== 1 从键盘任意输入一个年号,判断它是否是闰年。若是闰年,输出“Yes”,否则输出“No”。已??符合下列条件之一者是闰年:能被4整除,但不能被100整除。能被400整除。#include main(){int year, flag;
printf("Enter year:");scanf("%d",&year );
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ flag = 1; /*如果year是闰年,则标志变量flag置1*/}else{flag = 0; /*否则,标志变量flag置0*/}
if (flag){ printf("%d is a leap year!\n",year); /*打印“是闰年”*/}else{printf("%d is not a leap year!\n",year); /* 打印“不是闰年”*/}}
2 通过键盘输入一个字符,判断该字符是数字字符、大写字母、小写字母、空格还是其他字符。#include main(){char ch;
scanf("%c",ch);
if ((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 98)){printf("It is an English character!\n");}else if (ch <= 57 && ch >= 48){ printf("It is a digit character!\n");}else if (ch == 95){ printf("It is a space character!\n");}else{printf("It is other character!\n");}}
3 华氏和摄氏温度的转换公式为C=5/9×(F-32)。其中,C表示摄氏温度,F表示华氏温度。要求:华氏0℉~300℉,每隔20℉输出一个华氏温度对应的摄氏温度值。#include #include main(){int upper,step;float fahr = 0,celsius;
upper = 300;step = 20;while ( fahr< upper){ celsius = 5.0 / 9 * (fahr - 32); printf("%4.0f\t%6.1f\n", fahr, celsius); fahr = fahr + step;}}
4 编程判断输入整数的正负性和奇偶性。 #include main(){int m;
printf("Input m: ");scanf("%d", &m);