您的位置:首页 > 编程语言

Loadrunner中 C 语言编程 简单例子

2013-09-15 10:14 246 查看
Action()

{

int a,b;

char c;

a=1;

b=2;

c='A';

lr_output_message("值分别是:a=%d ,b=%d,c=%c ",a,b,c);

return 0;

}

------------关于数组的例子----------

Action()

{

int i;

char cp[]={'a','b','c'};

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

{

lr_message("%c",toupper(cp[i]));

}

return 0;

}

--------------------------------------------------------------

Action()

{

int a=80;

if(a<75)

{lr_output_message("你输入的值太小");}

else if ((a>=75) &&(a<=80))

{lr_output_message("你输入了一个正确的值");}

else if(a>80)

{lr_output_message("你输入的值太大");}

else

{lr_output_message("不可能出现的值");}

return 0;

}

---------------------------------------------------------------

Action()

{

int vuser,commodity;

lr_whoami(&vuser,NULL,NULL);

commodity=vuser%5+1;

switch(commodity)

{

case 1:

lr_vuser_status_message("I buy the commodity1");

break;

case 2:

lr_vuser_status_message("I buy thecommodity2");

break;

case 3:

lr_vuser_status_message("I'll buy the commodity3");

break;

case 4:

lr_vuser_status_message("I'll buy the commodity4");

break;

case 5:

lr_vuser_status_message("I'll buy the commodity5");

break;

default:

lr_vuser_status_message("Invalid commodity%d",commodity);

}

return 0;

}

--------------------------------------------------------------------

Action()

{

int i;

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

lr_output_message("%d=%c",i,i);

return 0;

}

---------------------------------------------------------------------

Action()

{

int i,j;

for(i=1;i<=3;i++)

{

for(j=1;j<=3;j++)

{

lr_output_message("i=%d,j=%d",i,j);

}

lr_output_message("end of j loop!");

}

lr_output_message("end of i loop");

return 0;

}

-----------------------------------------------------------------------

Action()

{

int i=0;

while (i<=50)

lr_output_message("Current value of i=%d",i++);

-----------------------------------------------------------------------

Action()

{

int i;

int sum,avg,multiple,substract;

int array[]={99,1,47,53,19,81};

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

sum=sum+array[i];

avg=sum/5;

multiple=sum*10;

substract=sum-111;

lr_message("%d",sum);

lr_message("%d",avg);

lr_message("%d",multiple);

lr_message("%d",substract);

return 0;

}

------------------------------------------------------------------------

Action()

{

int i;

char array[]={'M','e','r','c','u','r','y'};

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

lr_output_message(">>>>>>>>>>>>>>>>%c",array[i]);

lr_output_message(">>>>>>>>>>>>>>>>%s",array);

return 0;

}

-------------------------------------------------------------------------

Action()

{

char mystring1[20]="";

char mystring2[20]="";

char mystring3[20]="mercury2";

char cstring[10]="12345";

int cint;

// MyString1 is empty

//

lr_output_message("mystring1 is %s",mystring1);

// copy "Mercury1" into MyString1

//

strcpy(mystring1,"mercury1");

// Now MyString1 contains "Mercury1"

//

lr_output_message("mystring1 is %s",mystring1);

// Copy MyString3 into MyString2

//

strcpy(mystring2,mystring3);

lr_output_message("mysrting2 is %s",mystring2);

// Catenate MyString2 to MyString1

//

strcat(mystring1,mystring2);

lr_output_message("mystring1 is %s",mystring1);

// Cstring is converted to integer Cint

//

lr_output_message("cstring=%s",cstring);

cint=atoi(cstring);

lr_output_message("cint=%d",cint);

// Cint is converted to string

cint=100;

itoa(cint,cstring,10);

lr_output_message("cstring=%s",cstring);

return 0;

}

------------------------------------------------------------------------------

Action()

{

char MyArray[5][20]={"Doors","LedZepplin","Grateful Dead",

"BobDylan","Police"};

int i;

//

// We declared 5 rows of characters.

// Each row is a string in itself;

// So each row can be printed like a regularstring.

//

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

lr_output_message("String %d:%s",i,MyArray[i]);

return 0;

}

-----------------------------------------------------------------------------

Action()

{

int myfile,i,loadnumber;

char filename[30]="D:\\temp\\loans.txt";

myfile=(int)fopen(filename,"r");

for(i=1;i<=5;i++)

{

fscanf(myfile,"%d",&loadnumber);

lr_output_message("load number %d:%d",i,loadnumber);

}

fclose(myfile);

return0;

}

备注:

loans.txt文件的内容如下:

11111

22222

33333

44444

55555

运行结果如下:

Virtual User Script started

Starting action vuser_init.

Ending action vuser_init.

Running Vuser...

Starting iteration 1.

Starting action Action.

Action.c(10): load number 0:11111

Action.c(10): load number 1:22222

Action.c(10): load number 2:33333

Action.c(10): load number 3:44444

Action.c(10): load number 4:55555

Ending action Action.

Ending iteration 1.

Ending Vuser...

Starting action vuser_end.

Ending action vuser_end.

Vuser Terminated.

-----------------------------------------------------------------------------

Action()

{

int myfile;

char name[]="lihuichang";

myfile=fopen("D:\\temp\\names.txt","w");

fprintf(myfile,"%s",name);

fclose(myfile);

return 0;

}

----------------------------------------------------------------------------

float average_function(int a,int b,intc)
//注意这块后便不加分号(;)

{

// This function calculates the average of three numbers andreturns the value

float averagevalue=0.0;

// calculate the average of the incomingvalues

averagevalue=(a+b+c)/3;

// return the average to the callingfunction

return(averagevalue); //
注意return在花括号(})内;而且resturn中返回的是averagevalue

}

Action()

{

float x;

x=average_function(4,6,2);

lr_output_message("x=%.2f",x);

x=average_function(10,5,6);

lr_output_message("x=%.2f",x);

return 0;

}

---------------------------------------------------------------------------

void split_function(char myname[]);

{

// your code goes here

return;

}

Action()

{

split_function("lihuichang");

split_function("goodbye");

split_function("hello word");

return 0;

}

----------------------------------------------------------------------------

Action()

{

system("del D:\\temp\\temp.txt");

return 0;

}

------------------------------------------------------------------

Action()

{

int i;

// srand(time(NULL));

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

lr_output_message("Random nummber: %d",rand());

return 0;

}

-------------------------------------------------------------------

int random_function(int lowerbound,intupperbound)

{

return((int)((float)(upperbound-lowerbound+1)*rand()/32770.0)+lowerbound);

}

Action()

{

int i;

srand(time(NULL));

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

lr_output_message("Randomnummber,%d,",random_function(1,10));

return 0;

}

--------------------------------------------------------------------

Action()

{

int Year=1;

float Interest=0.00;

float InterestRate = 8.0;

float InitialAmount = 10000.00;

float BeginningBalance;

float EndingBalance;

BeginningBalance = InitialAmount;

do

{

Interest = BeginningBalance * InterestRate/100;

EndingBalance = BeginningBalance + Interest;

lr_output_message("Year = %d, Beginning Balance = %.2f,"

" Interest = %.2f, Ending Balance = %.2f",

Year++,BeginningBalance,Interest,EndingBalance);

BeginningBalance = EndingBalance;

}

while(EndingBalance <= 20000.00);

return 0;

}

----------------------------------------------------------------------------

Action()

{

int Year=1;

float Interest=0.00;

float InterestRate = 8.0;

float InitialAmount = 10000.00;

float BeginningBalance;

float EndingBalance;

BeginningBalance = InitialAmount;

for(Year=1; Year<=5; Year++)

{

Interest = BeginningBalance * InterestRate/100;

EndingBalance = BeginningBalance + Interest;

lr_output_message("Year = %d, Beginning Balance = %.2f,"

" Interest = %.2f, Ending Balance = %.2f",

Year,BeginningBalance,Interest,EndingBalance);

BeginningBalance = EndingBalance;

}

return 0;

}

---------------------------------------------------------------------------

Action()

{

char MyName[] = "Jim Morrison";

char TempArray[20], TempChar;

int i=0,j=0;

do

{

//

// Assigning the current character to a temporarychar

// Increment i for the next character

//

TempChar = MyName[i++];

//

// If the current char is not a blank orend-of-string

// then put it in a temporary string calledTempArray

//

if(TempChar != ' '&& TempChar != \0')

{

TempArray[j++] = TempChar;

}

//

// If it is a blank, it means the end of firstname

// so put a end-of-string character on theTempArray

// and print it out.

// Since we will use the TempArray to hold thelast name

// reset j to 0.

//

else if(TempChar == ' ')

{

TempArray[j++] = '\0';

lr_output_message("First Name:%s",TempArray);

j=0;

}

//

// If the current character is '\0', itmeans

// the end of the last name. So put a '\0' inthe

// TempArray and print it out

//

else if(TempChar == '\0')

{

TempArray[j++] = '\0';

lr_output_message("Last Name:%s",TempArray);

}

// Exit the while loop once '\0' character isencountered.

}

while(TempChar != '\0');

return 0;

}

-------------------------------------------------------------------------

Action()

{

int MyFile;

int LoanNumber,i;

// Assigning the file path to a string

char FileName[80] = "C:\\temp\\loans.txt";

// Opening the file

// Note the use of variable to replace the filepath

//

MyFile = (int)fopen(FileName,"r");

// Reading and printing one loan number at atime

while(feof(MyFile == 0)

{

fscanf(MyFile,"%d",&LoanNumber);

lr_output_message("Loan Number%d: %d",i,LoanNumber);

}

fclose(MyFile);

return 0;

}

----------------------------------------------------------------------

int MyFile1;

int MyFile2;

// Assigning the file path to a string

char FileName1[20] ="D:\\temp\\data.txt";

char FileName2[20] ="D:\\temp\\data2.txt";

vuser_init()

{

// Opening the file data.txt to read

// Note the use of variable to replace the filepath

//

MyFile1 = (int)fopen(FileName1,"r");

// Opening the file data2.txt to write

// Note that the mode changed to "w"

//

MyFile2 =(int)fopen(FileName2,"w");

return 0;

}

Action()

{

char Name[80]="";

fscanf(MyFile1,"%s",Name);

fprintf(MyFile2,"%s\n",Name);

fprintf(MyFile2,"%s\n",Name);

if(feof(MyFile1) != 0)

exit(-1);

return 0;

}

vuser_end()

{

fclose(MyFile1);

fclose(MyFile2);

return 0;

}

----------------------------------------------------------------------------------

void split_function(char MyName[])

{

char TempArray[20], TempChar;

int i=0,j=0;

do

{

//

// Assigning the current character to a temporarychar

// Increment i for the next character

//

TempChar = MyName[i++];

//

// If the current char is not a blank orend-of-string

// then put it in a temporary string calledTempArray

//

if(TempChar != ' '&& TempChar != '\0')

TempArray[j++] =TempChar;

//

// If it is a blank, it means the end of firstname

// so put a end-of-string character on theTempArray

// and print it out.

// Since we will use the TempArray to hold thelast name

// reset j to 0.

//

else if(TempChar == ' ')

{

TempArray[j++] = '\0';

lr_output_message("First Name:%s",TempArray);

j=0;

}

//

// If the current character is '\0', itmeans

// the end of the last name. So put a '\0' inthe

// TempArray and print it out

//

else if(TempChar == '\0')

{

TempArray[j++] = '\0';

lr_output_message("Last Name:%s",TempArray);

}

// Exit the while loop once '\0' character isencountered.

}

while(TempChar != '\0');

return;

}

Action()

{

split_function("JimMorrison");

split_function("JohnDoe");

split_function("Joe Shmo");

return 0;

}

----------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: