第二章 数据的存储与运算
1、鸡兔同笼
设x代表鸡的数量,y代表兔的数量,总头数为 h,总脚数为 f ,根据代数公式,得出下列公式:
① x+y=h;
② 2x+4y=f.
解方程:② - ① × 2
得:y=(f-2h)/ 2 x=h-y
#include <stdio.h>
int main()
{
int x,y,h,f;
printf("请输入鸡兔的总头数:");
scanf("%d",&h);
printf("请输入鸡兔的总脚数:");
scanf("%d",&f);
y=(f-2*h)/2;
x=h-y;
printf("鸡的数量为:%d\n兔的数量为:%d\n",x,y);
}
2、分期付款的计算
某人向银行贷款,324500元,准备每月还3450元,月利率为0.8%,求需要多少个月(m)才能还淸?
m计算公式:m=log (p) - log (p-d×r) / log (1+r)
其中,d是贷款额;p是每月还款数;r是月利率;m是还清贷款需月数。给定d=34500元,p=3245,r=0.8%。
#include <stdio.h>
#include <math.h>
int main()
{
int d=324500,p=3245;
double r=0.008,m;
m=(log10(p)-log10(p-d*r))/log10(1+r);
printf("month=%f\n",m);
printf("totoal=%lf\n",m*p);
return 0;
}
3、实型变量(单精度和双精度的区别)
#include <stdio.h>
#include <math.h>
int main()
{
float a;
a=1234.1415926; // a=1234.141602 float 单精度(4字节) 第八位数字后开始不准确
printf("a=%f\n",a);
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
double a;
a=1234.1415926; // double 双精度(8字节)可存储15-16位有效数字
printf("a=%lf\n",a);
return 0;
}
4、字符常量和字符变量
/*字符串常量赋值*/
#include <stdio.h>
int main()
{
char a='C',b='H',c='I',d='N',e='A';
printf("%c%c%c%c%c\n",a,b,c,d,e); // CHINA
printf("%c%c%c%c%c\n",e,d,c,b,a); // ANIHC
return 0;
}
/*字符常量赋值*/
#include <stdio.h>
int main()
{
char c='a';
char d=97;
printf("'a'=%c\n97=%c\n",c,d); // a a
}
/*用字符形式和整型形式输出字符数据*/
#include <stdio.h>
int main()
{
char c1=97,c2=98;
printf("%c,%c\n",c1,c2); // a b
printf("%d,%d\n",c1,c2); // 97 98
}
5、将两个小写字母转换为大写字母:
#include <stdio.h>
int main()
{
char c1='a',c2='b';
printf("%c,%c\n",c1-32,c2-32); // A B
return 0;
}
注意:C语言中没有专门的字符串变量,有字符数组,因而一个字符变量只能存储一个字符。
6、计算圆的周长c、面积s和圆球体积:
圆的周长:2πR; 圆的面积:πR2; 圆球体积:4/3 (πR3);
#include <stdio.h>
int main()
{
double r,c,s,v,PI;
PI=3.1415926;
printf("请输入圆的半径:");
scanf("%lf",&r);
c=2*PI*r;
s=PI*r*r;
v=4.0/3.0*PI*r*r*r; // 这里4和3,必须为4.0和3.0,因为整型和整型相除为整型,没有小数点。
printf("周长为:%lf\n面积为:%lf\n体积为:%lf\n",c,s,v);
return 0;
}
引用POW函数和使用符号常量实现
#define PI 3.1415926 // PI为符号常量,不占存储单元,一改全改,后面没有分号
#include <stdio.h>
#include <math.h>
int main()
{
double r,c,s,v;
printf("请输入圆的半径:");
scanf("%lf",&r);
c=2*PI*r;
s=PI*pow(r,2); // 调用pow函数
v=4.0/3.0*PI*pow(r,3); // 这里4和3,必须为4.0和3.0,因为整型和整型相除为整型,没有小数点。
printf("周长为:%lf\n面积为:%lf\n体积为:%lf\n",c,s,v);
return 0;
}
7、用强制类型转换进行不同类型数据的运算
#include <stdio.h>
int main()
{
float f=3.6;
int i;
i =(int)f; // 将 float型的f转换为 int型并赋值为 i ;
printf("float型的f:%f\n整型的i:%d\n",f,i);
}
8、判断数据类型的字节数
#include <stdio.h>
int main()
{
printf("int型的字节数:%d;\nfloat型的字节数:%d;\ndouble型的字节数:%d;\nlong型的字节数:%d;\n",sizeof(int),sizeof(float),sizeof(double),sizeof(long));
}
/*
int型的字节数:4;
float型的字节数:4;
double型的字节数:8;
long型的字节数:4;
Press any key to continue
*/
习题部分:
2.1 假如我国国民生产总值的年增长率为10%,计算10年后我国国民生产总值与现在相比增长多少百分比。
计算公式:P=(1+r)n
r为年增长率,n为年数,P为与现在相比的百分比。
#include <stdio.h>
#include <math.h>
int main()
{
double r,P;
int n;
r=0.01;
n=10;
P=pow(1+r,n);
printf("%lf\n",P);
}
2.2 此处不列举题目
#include <stdio.h>
#include <math.h>
int main()
{
double b=1000; // b为本金
double n=5,n1=1,n2=2,n3=3,n5=5; //n为季度,其他为年份
double r,r1,r2,r3,r5;
double P1,P2,P3,P4,P5;
r1=0.0414;
r2=0.0468;
r3=0.054;
r5=0.0585;
r=0.0072; // 活期存款利息
P1=b*(1+n5*r5); // 一次存5年期
P2=b*(1+n2*r2); // 先存两年
P2=P2*(1+n3*r3); // 再存三年
P3=b*(1+n3*r3); // 先存两年
P3=P3*(1+n2*r2); // 再存三年
P4=b*pow((1+r1),n5); // 存1年期,到期后将本息存再存1年期,连续存5次
P5=b*pow((1+r/4),4*n); // 存活期存款。活期利息每一季度结算一次
printf("P1=%lf\nP2=%lf\nP3=%lf\nP4=%lf\nP5=%lf\n",P1,P2,P3,P4,P5);
}
2.3 简单的字符串加密
#include <stdio.h>
int main()
{
char c1='C',c2='h',c3='i',c4='n',c5='a';
printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);
printf("%c%c%c%c%c\n",c1+=4,c2+=4,c3+=4,c4+=4,c5+=4);
return 0;
}

浙公网安备 33010602011771号