资源描述
P168
源程序:
#include<stdio.h>
void main()
{
float A,B,X;
scanf(“%f%f%f”,&A,&B,&X);
if((A>1)&&(B==0))
X=X/A;
if((A==2)||(X>1))
X=X+1;
printf(“%f”,X);
}
P171
源程序:
1. int testing(int x, int y)
2. {
3. int software=0;
4. if((x>0) && (y>0))
5. {
6. software = x+y+10;
7. }
8. else
9. {
10. software = x+y-10;
11. }
12.
13. if(software < 0)
14. {
15. software = 0;
16. }
17. return software;
18. }
P173
源程序:
1 int Test(int i_count, int i_flag)
2 {
3 int i_temp = 1;
4 while (i_count>0)
5 {
6 if (0 == i_flag)
7 {
8 i_temp = i_count + 100;
9 break;
10 }
11 else
12 {
13 if (1 == i_flag)
14 {
15 i_temp = i_temp * 10;
16 }
17 else
18 {
19 i_temp = i_temp * 20;
20 }
21 }
22 i_count--;
23 }
24 return i_temp;
25 }
P174
源程序:
1 int _tmain(int argc, _TCHAR* argv[])
2 {
3 int x,y;
4 scanf("%d%d",&x,&y);
5 if(x > 0 && y > 0)
6 {
7 int i = 1;
8 if(x > y)
9 {
10 while((x * i)% y != 0)
11 i++;
12 printf("%d\n",x * i);
13 }
14 else
15 {
16 while((y * i)% x != 0)
17 i++;
18 printf("%d\n",y * i);
19 }
20 }
21 return 0;
22 }
P178
源程序:
int GetMaxDay( int year, int month )
{
int maxday = 0;
if ( month >= 1 && month <= 12 )
{
if ( month == 2 )
{
if ( year % 4 == 0 )
{
if ( year % 100 == 0 )
{
if ( year % 400 == 0 )
maxday = 29;
else
maxday = 28;
}
else
maxday = 29;
}
else
maxday = 28;
}
else
{
if ( month == 4 || month == 6 || month == 9 || month == 11 )
maxday = 30;
else
maxday = 31;
}
}
return maxday;
}
P180
源程序:
Int IsLeap(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if ( year % 400 == 0)
leap = 1;
else
leap = 0;
}
else
leap = 1;
}
else
leap = 0;
return leap;
}
P181
源程序:
/*
作用:计算软件学院教师薪水
说明:节选自软件学院教师管理系统源代码
*/
1 void CaculateTeacherSalary()
2 {
3 int i;
4 int j=0;
5 printf("输入要计算的软件学院教师编号:\n");
6 fflush(stdin);
7 scanf("%d",&num);
8 for(i=0;i<MAXNUM;i++)
9 {
10 if(Teacher[i].TeacherNo==num) //确定是否为输入的教师号
11 {
12 j=1; //先赋值,在后面让j同0比较
13 printf("输入保险金额:");
14 fflush(stdin);
15 scanf("%f",&baoxianjin);
16 printf("输入月效益:");
17 fflush(stdin);
18 scanf("%f",&xiaoyi);
19 TeacherSalary[i]=(Teacher[i].TeacherBaseSalary+2*Teacher[i].TeacherMonthWorkDays+xiaoyi*Teacher[i].TeacherWorkYears/100)*0.5-baoxianjin;
20 printf("%04d号软件学院教师的薪水为:%lf元每月\n",Teacher[i].TeacherNo,TeacherSalary[i]);
21 break; //找到该教师后,直接跳出循环
22 }
23 }
24 if(j==0)
25 printf("未找到!\n");
26 }
P182
源程序:
/*
作用:输出软件学院教师信息
说明:节选自软件学院教师管理系统源代码
*/
1 void PrintTeacherInformation()
2 {
3 unsigned int i;
4 if(ActualNum!=0)
5 {
6 printf("共有%d条软件学院教师信息\n",ActualNum);
7 printf("\n");
8 for(i=0;i<ActualNum;i++)
9 {
10 printf("第%d个软件学院教师的信息:\n",i+1);
11 printf("编号:%04d\n",Teacher[i].TeacherNo);
12 printf("姓名:%s\n",Teacher[i].TeacherName);
13 printf("籍贯:%s\n",Teacher[i].TeacherHometown);
14 printf("地址:%s\n",Teacher[i].TeacherAddress);
15 printf("电话:%s\n",Teacher[i].TeacherPhone);
16 printf("生日:%d年%d月%d日\n",Teacher[i].TeacherBirth.year,Teacher[i].TeacherBirth.month,Teacher[i].TeacherBirth.day);
17 printf("工龄:%d\n",Teacher[i].TeacherWorkYears);
18 if(Teacher[i].TeacherSex==0)
19 printf("性别:男\n");
20 else if(Teacher[i].TeacherSex==1)
21 printf("性别:女\n");
22 else
23 printf("性别:无\n");
24 printf("基本工资:%f\n",Teacher[i].TeacherBaseSalary);
25 printf("月工作天数:%d\n",Teacher[i].TeacherMonthWorkDays);
26 switch(Teacher[i].TeacherEducation)
27 {
28 case 1:
29 printf("教育背景: 高中\n");
30 break;
31 case 2:
32 printf("教育背景: 学士\n");
33 break;
34 case 3:
35 printf("教育背景: 硕士\n");
36 break;
37 case 4:
38 printf("教育背景: 其它\n");
39 break;
40 case 5:
41 printf("教育背景:无\n");
42 }
43 printf("********************************************************\n");
44 }
45 }
46 else printf("暂无软件学院教师信息!请重新选择!\n");
47 }
P187
源程序:
getit(int m)
{
int I,k;
k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0) break;
if(i>=k+1)
printf(“%d is a selected number\n”,m);
else
printf(“%d is not a selected number\n”,m);
}
P218
源程序:
#include<stdio.h>
#include<stdlib.h>
void bhdy(int s,int b);
void prt();
int a[4],flag,count;
void main()
{
int b1,b2,i,j,k=0,p,c;
printf("Game guess your number in mind is # # # #.\\n");
for(i=1;i<10&&k<4;i++) //分别显示四个1~9确定四个数字的组成
{
printf("No.%d:your number may be:%d%d%d%d\\n",++count,i,i,i,i);
printf("How many digits have bad correctly guessed:");
scanf("%d",&p); //人输入包含几位数字
for(j=0;j<p;j++)
a[k+j]=i; //a[]:存放已确定数字的数组
k+=p; //k:已确定的数字个数
}
if(k<4) //自动算出四位中包的个数
for(j=k;j<4;j++)
a[j]=0;
i=0;
printf("No.%d:your number may be:%d%d%d%d\\n",++count,a[0],a[1],a[2],a[3]);
printf("How many are in exact positions:"); //顺序显示四位数字
scanf("%d",&b1); //人输入有几位位置是正确的
if(b1==4){prt();exit(0);} //四位正确,打印结果,结束游戏
for(flag=1,j=0;j<3&&flag;j++) //实现四个数字的两两(a[j],a[k]交换
for(k=j+1;k<4&&flag;k++)
if(a[j]!=a[k])
{
c=a[j];a[j]=a[k];a[k]=c; //交换a[j],a[k]
printf("No.%d:Your number may be: %d%d%d%d\\n",++count,a[0],a[1],a[2],a[3]);
printf("How many are in exact positins:");
scanf("%d",&b2); //输入有几个位置正确
if(b2==4){prt();flag=0;} //若全部正确,结束游戏
else if(b2-b1==2)bhdy(j,k);
else if(b2-b1==-2)
{
c=a[j];a[j]=a[k];a[k]=c;
bhdy(j,k);
}
else if(b2<=b1)
{
c=a[j];a[j]=a[k];a[k]=c;
}
else b1=b2;
}
if(flag) printf("You input error!\\n");
}
void prt()
{
printf("Now your number must be %d%d%d%d.\\n",a[0],a[1],a[2],a[3]);
printf("Game Over\\n");
}
void bhdy(int s,int b)
{
int i,c=0,d[2];
for(i=0;i<4;i++) //查找s和b以外的两个元素下标
if(i!=s&&i!=b)
d[c++]=i;
i=a[d[1]];a[d[1]]=a[d[0]]; a[d[0]]=i; //交换除a[s]和a以外的两个元素
prt();
flag=0;
}
P265
源代码:
Public class shuzu {
public int getlargest(int[] array) throws Exception{
if(0 == array.length)
{
throw new Exception("数组不能为空!");
}
int result = array[0];
for(int i = 0; i < array.length; i++ )
{
if(result < array[i])
{
result = array[i];
}
}
return result;
}
}
P280
源代码:
package metroSaleTicket;
public class MetroSaleTicket {
private int inputTotalMoney, countOfOneYuan;
//定义允许的地铁路线的“类型”:A类2元 B类4元
private String[] typeOfTickets = {"TypeA", "TypeB"};
private String resultOfDeal;
public MetroSaleTicket()
{
initial();
}
private void initial()
{
countOfOneYuan = 100; //1元的数量,初始为100个
}
public MetroSaleTicket(int oneYuan)
{
countOfOneYuan = oneYuan;
}
public String currentState() //当前状态
{
String state = "Current State\n" +
"1 Yuan: " + countOfOneYuan;
return state;
}
public String operation(String type,String money)
//type是用户选择的路线类型,money是用户投币的种类
{
if (money.equalsIgnoreCase("1yuan")) //若投入1元
{ inputTotalMoney= inputTotalMoney+1; countOfOneYuan= countOfOneYuan+1;}
else if (money.equalsIgnoreCase("5yuan")) //若投入5元
{ inputTotalMoney= inputTotalMoney+5;}
else if (money.equalsIgnoreCase("10yuan")) //若投入10元
{ inputTotalMoney= inputTotalMoney+10;}
if(inputTotalMoney<2 )
{
resultOfDeal = "Not enough money!"; //投入少于2元,返回钱不足
return resultOfDeal;
}
else if (type.equals(typeOfTickets[0]) &&(countOfOneYuan >=inputTotalMoney-2)) //若选择A类票且系统足够找零
{
countOfOneYuan= countOfOneYuan- (inputTotalMoney-2);
resultOfDeal = "Input Information\n" +
"Type: A; Money: 2Yuan \n" + currentState();
return resultOfDeal;
}
else if (type.equals(typeOfTickets[0]) &&(countOfOneYuan < inputTotalMoney-2)) //若选择A类票且系统不够找零
{
resultOfDeal = " Not enough Change!"; return resultOfDeal;
}
else if (type.equals(typeOfTickets[1]) &&(inputTotalMoney<4)) //若选择B类票且投入少于4元,返回钱不足
{
resultOfDeal = " Not enough Money";
return resultOfDeal;
}
else if (type.equals(typeOfTickets[1]) &&(countOfOneYuan >= inputTotalMoney-4)) //若选择B类票且系统足够找零
{
countOfOneYuan = countOfOneYuan- (inputTotalMoney-4);
resultOfDeal = "Input Information\n" +
"Type: B; Money: 2Yuan \n" + currentState();
return resultOfDeal;
}
else if (type.equals(typeOfTickets[1]) &&(countOfOneYuan < inputTotalMoney-4)) //若选择B类票且系统不够找零
{
resultOfDeal = " Not enough Change!"; return resultOfDeal;
}
else
{ // 其他状态 返回异常
resultOfDeal = "Failure Information\n" + "Money Error";
return resultOfDeal;
}
}
}
P293
源代码:
package metroSaleTicket;
import junit.framework.Assert;
import junit.framework.TestCase;
public class MetroSaleTicketTest extends TestCase {
private MetroSaleTicket obj;
protected void setUp() throws Exception {
obj = new MetroSaleTicket();
//super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
//基本路径1
public void testOperation1() {
String except = "Not enough money!";
Assert.assertEquals(except,obj.operation("TypeA", "1yuan"));
}
//基本路径2
public void testOperation2() {
String except = "Input Information\n" +
"Type: A; Money: 2Yuan \n" + "Current State\n" +
"1 Yuan: " + 97;
Assert.assertEquals(except,obj.operation("TypeA", "5yuan"));
}
//基本路径2
public void testOperation3() {
String except = "Input Information\n" +
"Type: A; Money: 2Yuan \n" + "Current State\n" +
"1 Yuan: " + 92;
Assert.assertEquals(except,obj.operation("TypeA", "10yuan"));
}
//基本路径4
public void testOperation4() {
String except = "Not enough money!";
Assert.assertEquals(except,obj.operation("TypeB", "3yuan"));
}
//基本路径5
public void testOperation5() {
String except = "Input Information\n" +
"Type: B; Money: 4Yuan \n" + "Current State\n" +
"1 Yuan: " + 99;
Assert.assertEquals(except,obj.operation("TypeB", "5yuan"));
}
//基本路径5
public void testOperation6() {
String except = "Input Information\n" +
"Type: B; Money: 4Yuan \n" + "Current State\n" +
"1 Yuan: " + 94;
Assert.assertEquals(except,obj.operation("TypeB", "10yuan"));
}
//基本路径7
public void testOperation7() {
String except = "Failure Information\n" + "Type Error";
Assert.assertEquals(except,obj.operation("TypeC", "10yuan"));
}
}
展开阅读全文