您的当前位置:首页正文

《软件测试技术及用例设计实训》源程序

2023-01-05 来源:步旅网
P168

源砸

#in clude void main() {

float A,B,X;

scanf( “%f%f%f”,&A,&B,&X); if((A>l)&&(B==0))

X=X/A; if((A==2)||(X>l))

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. 11. 12. 13. 14. 15. 16. 17. 18. }

software = x+y-10; }

if (software < 0)

{

software = 0;

}

return software;

P173

源砸

1 2 3 4 5 6 7 8 9

int Test(int i_count, int i_flag) {

int i_temp = 1; while (i_count>0) {

讦(0 == i_flag) {

i_temp = i_count + 100; break;

10 } 11 else

12 {

13 if (1 == i_flag) 14 { 15

i_temp = i_temp * 10;

16 }

else 17

{ 18

i_temp = i_temp * 20;

19

} 20

} 21

i_cou nt—; 22

}

23

return i_temp;

24

}

25 P174 源程序: 1 2 3 4

int _tmain(int argc, _TCHAR* argv[]) {

int x,y;

scanf(,,%d%d,,/&x,&y);

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 }

P178

if(x > 0 && y > 0) {

int i = 1; if(x > y) {

while((x*i)%y !=0) i++;

printf(,,%d\\n,,,x * i); } else {

while((y * i)% x != 0)

i++; printf(,,%d\\n,,,y * i); } }

return 0;

源砸

int GetMaxDay( int year, int month )

int maxday = 0;

讦(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 11 month == 6 11 month == 9 11 month == 11) maxday = 30; else

maxday = 31;

} }

return maxday;

}

P180 源 SJT: Int lsLeap(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 源®^ /*

作用:计算软件学院教师薪水

说明:节选自软件学院教师管理系统源代码 void CaculateTeacherSalary()

int i; */ 1 2 3 4 5

int j=0;

printfC*输入要计算的软件学院教师编号:\\n\"); fflush(stdin); scanf(,,%d,,,&num);

for(i=0;iTeacherSalary[i]=(Teacher[i].TeacherBaseSalary+2*Teacher[i].TeacherM on

6 thWorkDays+xiaoyi *Teacher[i].TeacherWorkYears/100)*0.5-baoxianjin;

20 printf(\"%04d号软件学院教师的薪水为:%lf元每月 7

8 \\n\"zTeacher[i].TeacherNo,TeacherSalary[i]); 21 break; 〃找到该教师后,直接跳出循坏 9 10 11 12

13 14 15 16 17 18 19 22 23 24

if(Teacher[i].TeacherNo==n um) 〃确定是否为输入的教师号 {

j=l;

printf(\"输入保险金额:”); fflush(stdin);

//!

,在后面让j同0比较

scanf(\"%f:&baoxianjin); printf(“输入月效益门; fflush(stdin); scanf(\"%f: &xiaoyi); } } if(j==O)

25 26 }

printf(H未找到! \\nM);

P182 源程序:

/*

作用:输出软件学院教师信息

说明:节选自软件学院教师管理系统源代码 */

1 void PrintTeacherlnformation() 2 3 4 5 6 7 8 9 10 11 12 13 14 15 {

unsigned int i; if(ActualNum!=O) {

printf(“共有%4条软件学院教师信息\\n\printf(H\\n\");

for(i=0;i{

printf(”第%d个软件学院教师的信息:Xn-J+l); printf(N 编号:%04d\\n,,/Teacher[i].TeacherNo); printf(\"姓名:%s\\n,,,Teacher[i].TeacherName); printf(N 籍贯:%s\\n,,,Teacher[i].TeacherHometown); printf(N 地址:%s\\nn,Teacher[i].TeacherAddress); printf(\"电话:%s\\n,,,Teacher[i].TeacherPhone);

printf(\"生 口:%d 年%01 月 %d 口 16

\\n \"JeacherliJ.TeacherBirth.yearJeacherfil.TeacherBirth.m on th,Teacher[i].TeacherBirth.day);

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

printf(H K龄:%d\\n\"/Teacher[i]・TeacherWorkYea⑸; if(Teacher[i].TeacherSex==O)

printf(”性别:男\\n“); else if(Teacher[i].TeacherSex==l)

printf(”性别:女\\n“);

else

printf(\"性别:无\\n\");

printf(H 基本工资:%fXn'^TeacherliJ.TeacherBaseSalary); printf(\"月工作天数:%d\\nnzTeacher[i].TeacherMonthWorkDays) switch(Teacher[i].TeacherEducatio n) {

case 1:

printff墩育背景:高中\\n“);

break; case 2:

printff墩育背景:学士\\n“); break;

32 33

34 35 36 37 38 39 40 41 42

case 3:

printff教育背景:硕士\\n“); break; case 4:

printff教育背景:其它\\n“); break; case 5:

printfC1教育背景:无\\n“); 44 } 45 }

\\nH);

43 printf( 46 else printf(“暂无软件学院教师信息!请重新选择! \\n“); 47 P187

}

源鉀

getit(int m) {

int l,k;

k=sqrt(m); for(i=2;i<=k;i++) 讦(m%i==0) break; if(i>=k+l)

printf(\"%d is a selected number\\n:m); else printf(\"%d is not a selected numberXn^m);

P218

源砸

#in clude #include void bhdy(int s」nt b); void prt(); int a[4],flag,count; void main() {

int bl,b2J丄k=O,p,c;

printf(HGame guess your n umber in mind is## # #・\\\\n”); for(i=l;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(nHow many digits have bad correctly guessed:\"); scanf(“%d\〃人输入包含几位数字 for(j=0;ja[k+j]=i; k+=p; }

if(k<4)

for(j=k;j<4;j++)

a[j]=O; i=0;

printf(HNo.%d:your number may be:%d%d%d%d\\\\n,,,++count/a[0]/a[l]/a[2]/a[3]); printf(\"How many are in exact positions:11); scanf(”%d”,&bl);

〃顺序显示四位数字

〃人输入有几位位置是正确的 〃四

〃a[]:存放已确定数字的数组 〃k:已确定的数字个数 〃自动算出四位中包的个数

if(bl==4){prt();exit(0);} for(flag=lj=0;j<3&&flag;j++) 位正确,打印结果,结束游戏 〃实

for(k=j+l;k<4&&flag;k++) if(aU]!=a[k]) 现四个数字的两两(aU],a[k]交换

{

c=a[j];a[j]=a[k] ;a[k]=c; printf(HNo.%d:Your number may

be: %d%d%d%d\\\\n,,/++count/a[0]/a[l],a[2]/a[3]);

printf(HHow many are in exact

positins:”); scanf(”%d\〃输入有几个位置正确 if(b2==4){prt();flag=0;} else if(b2-bl==2)bhdy(j,k); else if(b2-bl==-2)

c=aO];a[j]=a[k];a[k]=c; bhdy(j,k); }

else if(b2<=bl) {

c=aO];a[j]=a[k];a[k]=c; else bl=b2;

}

if(flag) printf(\"You input error!\\\\n\");

〃若全部正确,结束游戏

〃交换 a[jLa[k]

void prt() {

printf(HNow your number must be %d%d%d%d.\\\\n,,/a[0]/a[l]/a[2],a[3]); printf(HGame Over\\\\nH);

void bhdy(int s」nt b)

int izc=0,d[2];

for(i=0;i<4;i++)

if(i!=s&&i!=b)

d[c++]=i;

〃查找s和b以外的两个元素下标

i=a[d[l]];a[d[l]]=a[d[O]]; a[d[0]]=i; //交换除 a[s]和 a 以外的两个元素 prt(); flag=O;

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 = arrayfi]; } }

return result; }

P280 源代码:

package metroSaleTicket;

public class MetroSaleTicket {

private int inputTotalMoney, countOfOneYuan;

//定义允许的地铁路线的、'类型〃:A类2元B类4元

private String[] typeOfTickets = {nTypeAnz nTypeBn}; private String

resultOfDeal;

public MetroSaleTicket()

initial ();

}

private void initial ()

{

countOf One Yuan = 100;

}

public MetroSaleTicket(int oneYuan)

//I 元的数量,初始为 100 个

{

countOfOneYuan = oneYuan; }

public St ring curren

//当前状态

{

String state = \"Current State\\nu +

,fl Yuan: H + coun tOf One Yuan;

return state;

}

public String operation(String type,String money)

//type是用户选择的路线类型,money是用户投币的种类 {

if (mone y・ equal signor eCase (H lyuanH) ) //若投入 1 元

{ inputTotalMoney= inputTotalMoney+1; countOfOneYuan=

countOfOneYuan+1;}

else if (money ・ equals I gnor eCase ( H5yuanH ) ) //若投入 5 元

{ inputTotalMoney= inputTotalMoney+5;}

else if (mone y・ equal si gnor eCase ( H10yuanK) ) //若投入 10 元

{ inputTotalMoney= inputTotalMoney+10;}

if (inputTotalMoney<2 )

{

resultOfDeal = \"Not enough money! u;

return resultOfDeal;

//投入少于2元,返回钱不足

}

else if (type ・ equals(typeOfTickets[0])

&& (countOf One Yuan >=inputTotalMoney-2) )

{

//若选择 A 类票且系统足够找零

countOfOneYuan= countOfOneYuan- (inputTotalMoney-2); resultOfDeal = nInput Information\\nH + \"Type: A; Money: 2Yuan \\nH + currentState();

return resultOfDeal;

}

else if (type ・equals(typeOfTickets[0])

&& (countOf One Yuan < inputTotalMoney-2) )

//若选择 A 类票且系统不够找零

resultOf Deal = ,f Not enough Change ! H; else if (type.equals(typeOfTickets[1])

return resultOfDeal;

&& (inputTotalMoney<4) )

{

//若选择B类票且投入少于4元,返回钱不足

resultOfDeal = n Not enough Money,f; return resultOfDeal; }

else if (type.equals(typeOfTickets[1])

&& (countOf One Yuan >= inputTotalMoney-4 ) ) //若选择 B 类票且系统足够找零

{

countOfOneYuan = countOfOneYuan-

(inputTotalMoney-4);

resultOfDeal = MInput Information\\nu +

MType: B; Money: 2Yuan \\n,f + currents return

resultOfDeal; }

else if (type ・equals(typeOfTickets[1])

&& (countOf One Yuan < inputTotalMoney-4) )

{

//若选择 B 类票且系统不够找零

resultOfDeal = H Not enough Change !,f; return resultOfDeal; }

else

{

\"Money Error\";

//其他状态返回异常

resultOfDeal = ''Failure Information\\nH +

return resultOfDeal;

}

} }

P293

源代码:

package metroSaleTicket; import jun it.framework.Assert; import jun it.framework.TestCase;

public class MetroSaleTicketTest extends TestCase {

private MetroSaleTicket obj;

protected void setllp() throws Exception {

obj = new MetroSaleTicket();

//super.setUp(); }

protected void tearDown() throws Exception { super.tearDown(); }

〃基本路径1

public void testOperationl() {

String except = \"Not enough money「';

Assert.assertEquals(except,obj.operation(”TypeA: Ulyuan\")); }

〃基本路径2

public void testOperation2() {

String except = \"Input Information\\nH +

\"Type: A; Money: 2Yuan \\nH + \"Current State\\nH +

MlYuan: ” + 97;

Assert.assertEquals(except,obj.operation(”TypeA: H5yuan\")); }

〃基本路径2

public void testOperation3() {

String except = \"Input Information\\nH +

\"Type: A; Money: 2Yuan \\nH + \"Current State\\nH +

M1 Yuan: ” + 92;

Assert.assertEquals(except,obj.operation「TypeA蔦 M10yuan”)); }

〃基本路径4

public void testOperation4() {

String except = \"Not enough money「';

Assert.assertEquals(except,obj.operationClypeB\; }

〃基本路径5

public void testOperation5() {

String except = \"Input Information\\nH + \"Type: B; Money: 4Yuan \\nH + \"Current State\\nn +

nl Yuan: ” + 99;

Assert.assertEquals(exceptzobj.operationC'TypeB\n5yuan\")); } 〃基本路径5

public void testOperation6() {

String except = Hlnput Information\\nH +

\"Type: B; Money: 4Yuan \\nH + HCurrent State\\nn + nl Yuan: ” + 94; Assert.assertEquals(exceptzobj.operationC'TypeB\H10yuan\")); } 〃基本路径7

public void testOperation7() {

String except = \"Failure Information\\n” + \"Type Error\";

Assert.assertEquals(exceptzobj.operation(”TypeC蔦,,10yuanH)); }

}

因篇幅问题不能全部显示,请点此查看更多更全内容