C语言实训课程设计——大一

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一、题目
Writes a C program that a small insurance company can use to determine the cost of car insurance .The base cost of insurance is based on the car’s price .Then the driver’s age , the number of years the driver has been licensed, and the number of at-fault accidents the driver has had in the 3 years are used to calculate the final cost of insurance.
二、变量列表
1)Base:车辆的基础保险价格
2)Cost:车辆总的保险价格
3)Age:司机的年龄
4)Year:司机的驾龄
5)Accident:司机违章次数
三、流程图
四、程序代码
/* Assignment #8 */
/* Course: IP C144 */
/* Family Name: Liao */
/*Student Number: 20 */
/*Professor: Chen xianzhong */
/*Due Date: July 1, 2011 */
/* */
#include <stdio.h>
void main()
{
float base;
int age,year,accident,i;
float price; /*程序数据*/
float cost;
printf("Welcome to IPC insurance.\n");
printf("Enter the price of your car:\n"); /*提示用户输入信息*/
scanf("%f",&price);
printf("Enter your age:\n"); /*提示用户输入信息*/
scanf("%d",&age);
if(age<16)
{
for(i=0;i<5;i++)
{
printf("Your age: %d is less than 16.\nPlease enter it again.\n",age);
printf("Enter your age:\n"); /*提示用户输入信息*/
scanf("%d",&age);
if(age>=16)
break;
}
if(i==5)
{
printf("Sorry,we cannot insure you because you aren't old enough.\n");
return;
}
}
printf("Enter the number of years you have been licensed:\n");
/*提示用户输入信息*/
scanf("%d",&year);
printf("Enter the number of at-fault accidents you were in during the late 3 years:\n"); /*提示用户输入信息*/
scanf("%d",&accident);
printf("====================================\n");
printf("Car Price:\t\t$%.2f\n",price);
printf("Driver Age:\t\t%d\n",age); /*输出结果*/
printf("Year licensed:\t\t%d\n",year);
printf("Number of Accidents:\t%d\n",accident);
printf("====================================\n");
if(price<9000)
{
cost=900;
base=900;
}
else if(0.1*price>=2500) /*if函数计算*/
{
cost=2500;
base=2500;
}
else
{
cost=0.1*price;
base=0.1*price;
}
if((age>=16)&&(age<=20))
cost=cost+base*0.25; /*if函数计算*/
else if ((20<age)&&(age<25))
cost=cost+base*0.1;
if((year<3) && (accident>0))
cost=cost+base;
/*输出测试相关结果*/
/*switch语句计算*/
switch(accident)
{
case 0:
printf("Cost of insurance:\t$%.2f\n",cost);
break;
case 1:
cost=cost+0.25*base;
printf("Cost of insurance:\t$%.2f\n",cost);
break;
case 2:
cost=cost+0.75*base;
printf("Cost of insurance:\t$%.2f\n",cost);
break;
case 3:
cost=cost+1.5*base;
printf("Cost of insurance:\t$%.2f\n",cost);
break;
default:
printf("Sorry,we cannot insure you because you have been in too many accidents.\n");
break;
}
}
五、运行界面
Sample run #1
六、心得体会
转眼之间,为期两周的C语言实训也即将结束,当在几周前刚刚得知我们有实训的时候,大家都很为之兴奋。

但是当我们真正的拿到实训题目,大家都傻眼了。

因为实训远没有我们一开始想的这么简单。

在程序刚开始编写调试的时侯,我们发现了很多问题,但是switch二级菜单是我们遇到的最大的问题。

由于因为在平时的上课时间,没有好好听课,所以对一些编程用的格式之类的有很多的不解。

包括像对switch的运用不了解,不知道如何将if和switch结合等问题。

因为最初我们的程序是用if编写的,但是在编写的过程中,发现如果单用if编写的话,不能让程序完全正常的运行。

所以,我们决定用if加switch相结合的方式来编写程序。

与此同此,在编写这个程序的时候,也同时对do…while,for 循环也有了进一步的认识。

在这次的实训过程中,我发现自己的知识结构很不完整,知道的只是不够用。

尽管,在翻书查阅,上网查询之后被逐渐解决,但也从此学到了很多,以前不知道的知识。

与此同时,我还明白了,在编写程序的时候,一定要细心,因为很有可能由于多一个符号,而使程序无法运行。

我相信在以后的日子里,我一定会努力学习,让自己学到更多的东西。

相关文档
最新文档