How to realize Program Design with Linux
This article will explain in detail how to achieve programming in Linux. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Example:
Exponential function
/ * 4-1.c enter two real numbers x lm y to find the index x ^ y * gcc-g-o 4-1 4-1.c-lm * * / # include # include int main () {double answer,x,y; printf ("please input two double x lf y:\ n"); scanf ("% lf% lf", & x department y); answer=pow (x Magi y); printf ("% lf power of% lf is:% lf\ n" Return 0;}
Square
/ * 4-2.c enter the integer n first: then require n real numbers to be squared one by one * gcc-g-o 4-24-2.c-lm * * / # include # include int main () {int iline n; float x line y; printf ("please input an int n:\ n"); scanf ("% d", & n); for Printf ("% s% d:%d:%d\ n", Wayday [p _ tm- > tm_wday], pairtm-> tm_hour, pfanttm-> tm_min, pairtm-> tm_sec); p_tm = localtime (& timep) / * get the local time and display * / printf according to Chinese time and date ("% d year% d month% d", (pfoundtm-> tm_year+1900), (pairtm-> tm_mon+1), pairtm-> tm_mday); printf ("% s% d:%d:%d\ n", Chwayday [p _ tm- > tm_wday], pfoundtm-> tm_hour, pfoundtm-> tm_min, proomtm-> tm_sec) Printf ("current local time and date:% s", asctime (p_tm)) / * asctime (struct tm * ptr) returns a time string format of: week, month, day, hour: minute: second, year * / * ctime function prototype: char * ctime (const time_t * timer) function function: convert calendar time parameter timer into a string function representing the local current time return: return string format: week, month, day, hour: minute: second Annual parameter description: the timer parameter should be obtained by the function time It is equivalent to astime (localtime (timer)) * / time (& timep) Printf ("current time =% s", ctime (& timep)); printf ("\ n"); return 0;}
Thinking and experiment:
1. Write a simple c language program: the function int input (int a [], int n) is used to input an integer array with n elements, void output (int b [], int n), function int sum (int a [], int n) for array summation, and then call the functions input, output, sum in the main function.
/ * ex4-1.cgcc-g-o ex4-1 ex4-1.c$. / ex4-1 113 123 13 12 12 14 13 13 11 11 1 12 53 365*/#include int input (int a [], int n); void output (int b [], int n); int sum (int a [], int n); void main () {int a [10]; input (a, 10); output (a, 10) Printf ("% d\ n", sum (a, 10));} int input (int a [], int n) {for (int I = 0; I < n; iTunes +) {scanf ("% d", & a [I]);} return 1;} void output (int b [], int n) {for (int I = 0; I < n) Int +) {printf ("% d", b [I]);}} int sum (return a [], int n) {int s = 0; for (int I = 0; I < n; iBI +) {s = s + a [I];} return s;}
3. Write a C language program: use random functions to generate two integers and do arithmetic operations according to the input characters: +-* /. If not these four characters, an error will be reported.
# include # include # include int main () {int iMagne bMagazine d; char a; srand ((int) time (0)); for (iMago Tinci