Linux system function call experiment
The purpose of the experiment: to
understand the use of system functions of the Linux operating system
Experimental content:
Create a test user, the password is also test.
After logging in with test, create the src directory, and all source code, intermediate code, and target code are placed in the / home / test / src directory.
- Write a simple c language program: the function int input (int a [], int n) is used to input an integer array with n elements, and void output (int b [], int n) is used to output n elements Integer array, the function int sum (int a [], int n) is used for array summation, and the function input, output, sum are called in turn in the main function.
#include<stdio.h>
#define N 100
int input(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
return 0;
}
void output(int b[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%2d",b[i]);
}
}
int sum(int a[],int n)
{
int sum=0;
int i;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("%d",sum);
return sum;
}
int main()
{
int n,a[N];
printf("input n:");
scanf("%d",&n);
printf("input number:");
input(a,n);
printf("output:");
output(a,n);
printf("\n");
printf("sum:");
sum(a,n);
printf("\n");
return 0;
}
- Write a simple c language program: use the random number function to generate two integer numbers, and perform arithmetic operations based on the input characters '+', '-', '*', and '/' (Addition, subtraction, multiplication and division of symbols and numerical expressions, how should be parsed and calculated).
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int i,a,b;
int m,n,p;
float q;
char c;
srand((int)time(0));
for(i=0;i<2;i++)
{
a=1+(int)(4.0*rand()/(RAND_MAX+1.0));
b=1+(int)(4.0*rand()/(RAND_MAX+1.0));
}
printf("%d %d\n",a,b);
printf("Please input:+、-、*、/\n");
c=getchar();
m=a+b;
n=a-b;
p=a*b;
q=(float)(a/b);
switch(c)
{
case'+':printf("sum:%d\n",m);
break;
case'-':printf("difference:%d\n",n);
break;
case'*':printf("product:%d\n",p);
break;
case'/':printf("shang:%d\n",q);
break;
default:
printf("error!");
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define N 10000
static int compare (const void *a, const void *b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
int a[N];
int n;//array length
int i;
printf("please input n:\n");
scanf("%d",&n);
printf("please input a[]:");
for( i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
qsort(a,n,sizeof(a[0]),compare);
for( i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\n");
int key;
printf("please input the key:");
scanf("%d",&key);
int *item=NULL;
item=bsearch(&key,a,n,sizeof(a[0]),compare);
if(item == NULL)
printf("%d is not in the array\n",key);
else
printf("%d is in the array\n",key,*item);
return 0;
}