苏小红C语言程序设计

阅读: 评论:0

苏小红C语言程序设计

苏小红C语言程序设计

5.3:

  1. 若a,b,c为整型数据时,s=1/2(a+b+c):1/2结果为0!!!应用1.0/2.0书写;s=(float)((a+b+c)/2):应为s=(float)(a+b+c)/2;

  1. area=(float)sqrt(s*(s-a)*(s-b)*(s-c));其中sqrt()的返回值默认为double,需要类型转换,若本来就是double类型,则无需转换。

  1. 浮点数并非真正意义上的实数,只是其在某种范围内的近似。故不能使用==或者!=来判断两个浮点数是否相等,或判断一个浮点数是否等于0。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>int main(void)
{float a, b, c;scanf("%f %f %f", &a, &b, &c);if (a + b > c&&a + c > b&&b + c > a){float s = (a + b + c)/2;float area = (float)sqrt(s*(s - a)*(s - b)*(s - c));printf("三角形面积为%fn", area);}else{printf("不能构成三角形n");}system("pause");return 0;
}

5.4

1、格式化输出实数,无论是单、双精度,即float和double都用%f;加l/L是用于long型数据

格式化输入实数,l加在f、e之前,即%lf才是输入double类型。

2、程序中使用pow()函数计算的本息和是采用滚存计算。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>int main(void)
{int n;double rate, capital, deposit;scanf("%lf %d", &capital, &n);switch (n){case 1:rate = 0.0225; break;case 2:rate = 0.0243; break;case 3:rate = 0.0270; break;case 5:rate = 0.0288; break;case 8:rate = 0.0300; break;default:printf("Error rate!n");exit(0);}deposit = capital*pow(1 + rate, n);printf("deposit=%fn", deposit);system("pause");return 0;
}

5.7

  1. 大小字母ASCII码值相差32

  1. 用十进制整型格式(%d)输出的是char型变量的ASCII码值。

int main(void)
{char ch;//scanf("%c", &ch);ch = getchar();if (ch >= 'A'&&ch <= 'Z')ch += 32;else if (ch >= 'a'&&ch <= 'z')ch -= 32;printf("%c----%dn", ch, ch);system("pause");return 0;
}

5.10

int main(void)
{int year, month;scanf("%d %d", &year, &month);switch (month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("31daysn"); break;case 4:case 6:case 9:case 11:printf("30daysn"); break;case 2:if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)printf("29daysn");elseprintf("28daysn");break;default:printf("Input error!n");}system("pause");return 0;
}

5.11

  1. %c输入字符存在问题:空格字符和转义字符(包括回车)都会被当做有效字符读入

解决方法:用函数getchar();将数据输入时存入缓冲区中的回车符读入;或在%c前面加一个空格

对程序进行优化时,一定要注意单个输入时前面加空格再接%c。

  1. 注意或使用格式修饰符,如域宽、显示精度等。

int main(void)
{char sex, sports, diet;float faHeight, moHeight,Height;printf("input your sex, sports, diet,faHeight, moHeightn");scanf("%c %c %c %f %f", &sex, &sports, &diet, &faHeight, &moHeight);if (sex == 'M')Height = (faHeight + moHeight)*0.54;if (sex == 'F')Height = (faHeight*0.923 + moHeight)/2;if (sports == 'Y')Height = Height*(1 + 0.02);if (diet == 'Y')Height = Height*(1 + 0.015);printf("Your Height will be %.0fcmn", Height);system("pause");return 0;
}

5.12

int main(void)
{float h, w, t;printf("Input your height(m) and weight(kg)n");scanf("%f %f", &h, &w);t = w / pow(h,2);if (t < 18)printf("Lown");if (t >= 18 && t < 25)printf("Normaln");if (t >= 25 && t < 27)printf("Overweightn");if (t >= 27)printf("Obesityn");system("pause");return 0;
}

本文发布于:2024-01-30 03:50:54,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170655785619026.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23