姓名:陈裕坤 学号:120705213 班级:12电信2班
任务一:显示美元和人民币的汇率
实验内容:编写一个“显示美元和人民币的汇率”C程序
实验目的:进一步掌握简单C程序的编写和运行的过程
说明:假设100美元=621人民币,程序显示100、200、300···1000美元与人民币汇率表
要求:独立思考和编写
我的程序
//****************************************
//对=100、200、300...1000
//编写一个“显示美元和人民币汇率”的C程序
//Code by 陈裕坤 120705213 2013-3-15
//****************************************
#include <stdio.h>
int main()
{
int dollar,yuan;
int lower,upper,step;
lower=100;
upper=1000;
step=100;
dollar=lower;
printf("陈裕坤,120705213\n", "");
while(dollar<=upper){
yuan=dollar*6.21;
printf("%d\t %d\n", dollar, yuan);
dollar=dollar+step;
}
return 100;
}
运行结果如图
设计思考过程:对照温度转换程序,发现只需把输入的fahr,celsius,改成dollar,yuan,step 改成100,再将转换公式改成汇率公式yuan=dollar*6.21即可完成任务
-----------------------------------------任务分割线-----------------------------------------------
任务二:了解int 和float变量类型
实验内容:修改“华氏温度与摄氏温度对照表”
实验目的:了解“同一变量”定义不同数据类型,如何影响程序运行结果
我的程序
//****************************************
//对f=0、20、40...100
//编写一个“显示华氏温度与摄氏温度对照表”的C程序
//Code by 陈裕坤 120705213 2013-3-15
//****************************************
#include <stdio.h>
int main()
{
int f;
int c;
int lower=0;
int upper=300;
int step=20;
f=lower;
printf("陈裕坤,120705213\n","");
while (f<=upper)
{
c=(5/9)*(f-32);
printf("%d\t%d\n", f, c);
f=f+step;
}
return 0;
}
运行结果如图
运行结果异常,因为根据运算公式,其结果不是整型,而代码中的输出数据类型为整型,两者互相矛盾,所以不能得到正常结果。
修改代码,得到程序
//****************************************
//对f=0、20、40...100
//编写一个“显示华氏温度与摄氏温度对照表”的C程序
//Code by 陈裕坤 120705213 2013-3-15
//****************************************
#include <stdio.h>
int main()
{
float f;
float c;
float lower=0;
float upper=300;
float step=20;
f=lower;
printf("陈裕坤,120705213\n","");
while (f<=upper)
{
c=(5.0/9.0)*(f-32.0);
printf("%f\t%f\n", f, c);
f=f+step;
}
return 0;
}
运行结果如图
![](https://img-my.csdn.net/uploads/201303/16/1363402268_1944.png)