linux下的c語言系統函數調用

目錄

4.linux下的系統函數的使用 c語言

4.1數學函數的使用

1pow函數

2.exp函數

3.log函數

4.rand()隨機數函數

4.2字符函數的使用

4.3系統時間與日期函數的使用

系統時間

時間間隔

4.4環境控制函數


 

4.linux下的系統函數的使用 c語言

4.1數學函數的使用

pow(x,y)  //x的y次方 
exp(x)      //e的x次方
log(x)      //lnx
log10(x)  //lg(x)   
以上返回值參數全double

1pow函數

#include<stdio.h>
#include<math.h>
int main(){
	double answer,x,y;
	scanf("%lf %lf",&x,&y);
	answer=pow(x,y);
	printf("%lf的%lf次方等於%lf\n",x,y,answer);
	return 0;
}

 

2.exp函數

#include<stdio.h>
#include<math.h>
int main(){
	double answer,x;
	scanf("%lf",&x);
	answer=exp(x);
	printf("e的%lf次方等於%lf\n",x,answer);
	return 0;
}

 

3.log函數

#include<stdio.h>
#include<math.h>
int main(){
	double answer1,answer2,x,y;
	scanf("%lf %lf",&x,&y);
	answer1=log(x);//2.718281828
	answer2=log10(y);//10
	printf("log(%lf)=%lf\n",x,answer1);
	printf("log10(%lf)=%lf\n",y,answer2);
	return 0;
}

 

sqrt(x)  求平方根   

函數都在math.h 裏編譯時加上-lm    c++編譯則不用

 

4.rand()隨機數函數

先設置隨機數種子 否則每次產生的隨機數一樣
srand(time(0));

頭文件 #include<stdlib.h>

int rand(void); //0~2147483674(0~RAND_MAX) 之間的隨機數

模板1:
rand()/(RAND_MAX+1.0)                 產生:[0,1)
int(10.0×rand()/(RAND_MAX+1.0))     產生[0,10)
1+int(10.0×rand()/(RAND_MAX+1.0))     產生[1,10]

模板2:
rand()%(N+1)    產生[0,N]
M+rand()%(N+1)  產生[M,M+N]

 

4.1.產生10個1到10的隨機數

#include<stdio.h>
#include "stdlib.h"
int main(){
	int i,j;
	srand((int)time(0));
	for(i=0;i<10;i++){
		j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
		printf(" %d ",j);
	}
	printf("\n");
	return 0;
}

 

4.2產生20個[100,1000]的隨機數

#include <stdlib.h>
#include "stdio.h" 
int main(){
	int i,j; 
	srand((int)time(0)); 
	for(i=0;i<20;i++){
		j=100+rand()%901;
		printf(" %d ",j);
		if(i==9) printf("\n");
	}
	printf("\n");
}

 

4.2字符函數的使用

c語言字符函數
函數名 功能
isalnum 是否爲英文字母或數字
isalpha 是否爲英文字母
isascii 是否爲ascii碼字符
isdigit 是否爲阿拉伯數字
islower 是否爲小寫英文字母
isprint 是否爲可打印字符
isspace 是否爲空格
ispunct 是否爲標點符號或特殊符號
isupper 是否爲大寫英文字母
isxdigit 是否爲16進制數字
頭文件 #include<ctype.h>
#include<stdio.h>
#include <ctype.h>
int main(){
	char c;
	while(scanf("%c",&c)==1){
		if(isalnum(c)) printf("%c是英文字母或數字\n",c);
		if(isalpha(c)) printf("%c是英文字母\n",c);
		if(isascii(c)) printf("%c是ascii碼字符\n",c);
		if(iscntrl(c)) printf("%c是ascii碼控制字符\n",c);
		if(isdigit(c)) printf("%c是數字\n",c);
		if(islower(c)) printf("%c是小寫字母\n",c);
		if(isupper(c)) printf("%c是大寫字母\n",c);
		if(isprint(c)) printf("%c是可打印字符\n",c);
		if(isspace(c)) printf("%c是空格\n",c);
		if(isxdigit(c)) printf("%c是16進制數字\n",c);
		getchar();
	}
	return 0;
}

 

 

4.3系統時間與日期函數的使用

系統時間

系統時間與日期函數
函數名 功能
asctime 將時間和日期以字符串格式表示
ctime 將時間和日期以字符串格式表示
gettimeofday 取得當前時間
gmtime 把時間和日期轉換爲格林尼治(GMT)時間(世界時)
localtime 取得目前當地的時間和日期,並轉換爲現在的時間日期表示方法
mktime 將時間結構數據轉換成經過的秒數
settimeofday 設置當前時間
time 取得系統當前的時間
//系統結構體
struct tm{
	int tm_sec;  //秒
	int tm_min;  //分
	int tm_hour; //時
	int tm_mday;  //當前月日數
	int tm_mon;  //月 0~11
	int tm_year;  //1900至今的年數  1900+p->tm_year得到今年年份
	int tm_wday;  //一星期的日數 0~6
	int tm_yday;  //一年內的天數 0~365
	int tm_isdst;  //夏時制時間	
};

頭文件#include<time.h>
time_t time(time_t *t);  
取得當前時間 其實是返回1970年1月1日0:0:0至今的秒數 
返回值會直接存到t指向的內存   

頭文件#include<time.h>
struct tm* gmtime(const time_t* timep);
將time_t裏的秒數轉換成現在的時間格式(格林尼治)  返回結構體struct_t

頭文件#include<time.h>
char* asctime(const struct tm * timeptr);
將結構體struct_tm裏的時間轉換成現在使用的時間日期表示法,格式爲”Wed Jun 30 16:13:00 2018\n”

頭文件#include<time.h>
struct tm *localtime(const time_t *timep);
將time_t裏的時間信息轉換成現在使用的時間日期表示法,返回強悍的struct tm結構體

eg1:time取得當前時間,gmtime轉換成格林尼治時間,字符串形式輸出(直接asctime函數轉)。
         自己轉換,成“xxxx年 x月 xx日 Tue 15:52:00"形式顯示系統時間
 

#include<time.h>
int main(){
	time_t timep;//時間結構體變量
	char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
	struct tm *p;
	time(&timep);//獲取當前時間
	printf("%s",asctime(gmtime(&timep)));
	p=localtime(&timep);//獲取當地時間 並按現在的時間日期來表示
	printf("%d年 %d月 %d日\n",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
	printf("%s %d:%d:%d\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
	return 0;
}

eg2:c語言寫鐘錶

#include<stdio.h>
#include<time.h>
#include <unistd.h>
main(){
	time_t timep;//時間結構體變量
	struct tm *p;
	while(1){
		time(&timep);//獲取當前時間
		p=localtime(&timep);//獲取當地時間 並按現在的時間日期來表示
		printf("%d年%d月%d日 %d:%d:%02d\n",1900+p->tm_year,p->tm_mon+1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
		sleep(1);
	}
 	printf("\n時間到!\n\a"); // \a是讓系統發出聲音的一種表達方式
 }

 

時間間隔

struct timeval{
       long tv_sec; //秒
       long tv_usec;//微秒
};

struct timezone{
       int tz_minuteswest; //和格林尼治時間差了多少分鐘
       int tz_dsttime; //日光節約時間的狀態
};

gettimeofday函數說明
頭文件:#include<sys/time.h>     #include<unistd.h>
int gettimeofday(struct timeval * tv,struct timezone * tz)
把當前時間分s與us放到tv中返回  當地時區信息放到tz中返回

做某事的時間模板
gettimeofday(&tv1,&tz);
/*do something*/
gettimeofday(&tv2,&tz);
t=tv2.tv_sec-tv1.tv_sec+(tv2.tv_usec-tv1.tv_usec)*pow(10,-6);//s+us*10^-6

eg1.

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
int main(){
	struct timeval tv1,tv2;
	struct timezone tz;
	gettimeofday(&tv1,&tz);
	printf("1970距離今天tv_sec= %d 秒\n",tv1.tv_sec);
	printf("零頭有tv_usec= %d 微秒\n",tv1.tv_usec);
	gettimeofday(&tv2,&tz);
	printf("第7行到第10行程序耗時tv2_usec-tv1_usec= %d 微秒\n",tv2.tv_usec-tv1.tv_usec);//程序一定在ms內完成
	//printf("%d\n",tv2.tv_sec-tv1.tv_sec);//絕對在ms級別 所以輸出0
	return 0;
}

 

exp2:大例

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<unistd.h>
int main()
{
	long int begin,sec,stop;
	struct timeval tv1, tv2;
	struct timezone tz;
	char tmp;
	begin=0;
	stop=0;
	sec=0;
	system("clear");
	printf("計時器程序(單位s)\n");
	printf("輸入b(begin)計時器開始計時\n");
	printf("輸入w(watch)查看已經累計時間\n");
	printf("輸入r(rest)重新開始計時\n");
	printf("輸入s(stop)暫停計時器\n");
	printf("輸入e(end)結束計時器\n");
	while(1)
	{
		scanf("%c",&tmp);
		if(tmp=='b')
		{
			if(begin==1&&stop==0)
				printf("計時器已經啓動!\n");
			if(begin==0&&stop==0)
			{
				printf("計時器啓動\n");
				gettimeofday(&tv1,&tz);
				sec=0;
				begin=1;
			}
			if(stop==1)
			{
				gettimeofday(&tv1,&tz);
				stop=0;
				printf("暫停結束!\n");
			}
		}
		if(tmp=='w'){
			if(stop==0){
				gettimeofday(&tv2,&tz);
				printf("已經計時%ld 秒\n",sec+tv2.tv_sec-tv1.tv_sec);
			}
			if(stop==1)
				printf("已經計時%ld 秒\n",sec);
		}
		if(tmp=='s'){
			if(stop==1){
				printf("計時已經暫停!\n");
			}
			if(stop==0){
				gettimeofday(&tv2,&tz);
				sec=sec+tv2.tv_sec-tv1.tv_sec;
				printf("計時暫停,已經計時%ld 秒\n",sec);
				stop=1;
			}
		}
		if(tmp=='r'){
			gettimeofday(&tv2,&tz); 
			printf("已經計時%ld 秒\n",sec+tv2.tv_sec-tv1.tv_sec);
			printf("計時器在5 秒後被重置!\n");
			sleep(5);
			begin=0;
			sec=0;
			stop=0;
			system("clear");
			printf("計時器程序(單位s)\n");
			printf("輸入b(begin)計時器開始計時\n");
			printf("輸入w(watch)查看已經累計時間\n");
			printf("輸入r(rest)重新開始計時\n");
			printf("輸入s(stop)暫停計時器\n");
			printf("輸入e(end)結束計時器\n");
		}
		if(tmp=='e') break;
	}
	return 0;
}

 

 

4.4環境控制函數

常用環境控制函數
函數名 功能
getenv 取得環境變量的內容
putenv/setenv 改變/增加環境變量
unsetenv 取消已經改變的環境變量

getenv函數:
頭文件:#include<stdlib.h>
char* getenv(const char *name);  //失敗返回null
根據環境變量名name,取得該系統環境變量的值      環境變量格式key=value    

eg1.獲取當前登錄用戶

當前登錄用戶環境變量key爲USER  (只能大寫,不能小寫)

#include<stdio.h>
#include<stdlib.h>
int main(){
	char *p;
	if((p=getenv("USER")))
		printf("USER=%s\n", p);
	return 0;
}

 

setenv函數:
頭文件:#include<stdlib.h>
int setenv(const char *key,const char *value,int overwrite);
改變或者增加環境變量     overwrite:1:改爲value新值       0:參數value被忽略(也即本次不改值)    

eg2:修改USER的值

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章