Linux C Function Practice __oracle

Source: Internet
Author: User

Learning function is mainly to learn the function of the declaration, definition and invocation, see the following two examples to help us learn functions:

Topic One:

Write a function Iswithin () that accepts two arguments, one is a character, and the other is a string pointer. Its function is if the character is in a string. Returns 1 (true), or 0 (false) if the character is not in the string. Test in a complete program that uses circular statements to provide comfort for this function.

The code is as follows:

#include <stdio.h>

int iswithin (char p,char *q)
{while
	(*q)
	{
		if (p = = *q) return
			1;
		else
			q++;
	}
			return 0;
}
int main (int argc, char *argv[])
{
	int m;
	char p,*q;
	p = *argv[1];
	Q = argv[2];

	m = Iswithin (p,q);

	if (m = = 1)
		printf ("\ '%c\ ' is in the string!\n", p);
	else
		printf ("\ '%c\ ' isn't in" string!\n ", p);

	return 0;
}

The results of the implementation are as follows:

fs@ubuntu:~/qiang/hanshu$./hanshu2 h Hello
' h ' is in the string!
fs@ubuntu:~/qiang/hanshu$/hanshu2 H World
' H ' isn't in the string!
fs@ubuntu:~/qiang/hanshu$ 

Notice how the function passes the argument.

Topic Two,

the function of the following function is to compute the value of the N-Order de-phasor of X by a recursive method. An existing Call statement P (n,x): Write function implementation functionality.

The code is as follows:

#include <stdio.h>

int p (int n,int x)
{
	int m;

	if (n = = 0) return
		0;
	else
		if (n = = 1) return
			x;
		else
		{
			m = ((2*n-1) *x*p (n-1,x)-(n-1) *p (n-2,x))/n;
			return m;
		}
}

int main (int argc, const char *argv[])
{
	int x, n;
	int q;
	printf ("Please input x and n:\n");
	scanf ("%d%d", &x,&n);
	Q = P (n,x);

	printf ("p =%d\n", q);

	return 0;
}

The results of the implementation are as follows:

fs@ubuntu:~/qiang/hanshu$./HANSHU1 please
input x and N:
2
1
p = 2
Fs@ubuntu:~/qiang/hanshu $./HANSHU1 please
input x and N:
2 
5
p = 194
fs@ubuntu:~/qiang/hanshu$ 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.