#include<stdio.h>
void main()
{
int a,x1,x2,x3,x4;
double sum;
printf("需要計算費用嗎?(輸入1就進行計算,0就退出)\n");
scanf("%d",&a);
if(a==1)
{
printf("請輸入開始時間(小時)\n");
scanf("%d",&x1);
printf("請輸入開始時間(分鍾)\n");
scanf("%d",&x2);
printf("請輸入結束時間(小時)\n");
scanf("%d",&x3);
printf("請輸入結束時間(分鍾)\n");
scanf("%d",&x4);
sum=7.5*(((x3-x1)*60+x4-x2)/30);
printf("結果是%.1f\n",sum);
}
}
B. C語言編程實現時間片輪轉演算法,盡量寫得簡單易懂,謝謝
#include<stdlib.h>
#define MAX 5 //進程數量
#define RR 2 //時間片大小
/*時間片輪轉演算法*/
struct pro
{
int num;
int arriveTime;
int burst;
int rt; //記錄進程被運行的次數
struct pro *next;
};
int TOTALTIME; //記錄所有進程的總時間
//函數聲明
struct pro* creatList();
void insert(struct pro *head,struct pro *s);
struct pro* searchByAT(struct pro *head,int AT);
void del(struct pro* p);
int getCount(struct pro *head,int time);
struct pro* searchEnd(struct pro *head);
void move(struct pro *headF,struct pro *headT,int n);
struct pro* creatList() //創建鏈表,按照進程的到達時間排列,記錄所有進程的信息
{
struct pro* head=(struct pro*)malloc(sizeof(struct pro));
head->next=NULL;
struct pro* s;
int i;
TOTALTIME=0;
for(i=0;i<MAX;i++)
{
s=(struct pro*)malloc(sizeof(struct pro));
printf("請輸入進程名:\n");
scanf("%d",&(s->num));
printf("請輸入到達時間:\n");
scanf("%d",&(s->arriveTime));
printf("請輸入運行時間:\n");
scanf("%d",&(s->burst));
TOTALTIME+=s->burst; //計算總時間
s->rt=1; //rt的初始值為1
s->next=NULL;
insert(head,s);
}
return head; //到達隊列中的進程按照其到達時間的先後順序排列
}
void insert(struct pro *head,struct pro *s) //插入節點
{
struct pro *p=searchByAT(head,s->arriveTime);
s->next=p->next;
p->next=s;
return;
}
struct pro* searchByAT(struct pro *head,int AT) //查找第一個到達時間大於等於AT的節點,返回其前一個指針
{
struct pro *p,*q;
p=head;
q=head->next;
while(q!=NULL&&q->arriveTime<=AT)
{
p=q;
q=q->next;
}
return p;
}
void del(struct pro* p) //刪除p的下一個節點
{
struct pro *tmp;
tmp=p->next;
p->next=tmp->next;
free(tmp);
return;
}
int getCount(struct pro *head,int time) //察看在time之前到達但未移動到運行隊列的進程數量
{
int count=0;
struct pro *s,*t;
s=head;
t=s->next;
while(t!=NULL&&t->arriveTime<=time)
{
s=t;
t=t->next;
count++; //count記錄當前時刻到達的進程數
}
return count;
}
struct pro* searchEnd(struct pro *head) //查找並返回循壞隊列的尾節點的前一個節點
{
struct pro *p,*q;
p=head;
q=head->next;
while(q->next!=head)
{
p=q;
q=q->next;
}
return p;
}
void move(struct pro *headF,struct pro *headT,int n) //將headF後的n個節點移動到循環隊列headT中
{
struct pro *r,*s,*t;
s=headF;
t=s->next;
r=t; //r記錄要移動的第一個節點
while(n>1)
{
t=t->next;
n--;
}
s->next=t->next; //以上完成從原隊列中摘除相關節點,r,t分別為第一個和最後一個節點
s=searchEnd(headT);
t->next=s->next;
s->next=r;
}
void run(struct pro *head)
{
int time=0; //記錄當前時間
int newarrive;//新到達進程數
struct pro *runhead=(struct pro*)malloc(sizeof(struct pro));
runhead->next=runhead; //創建新的循環鏈表,存放當前就緒隊列中的進程
struct pro *p,*q;
p=runhead;
q=p->next; //q記錄當前應當運行的進程
while(time<=TOTALTIME)
{
newarrive=getCount(head,time);
if(newarrive>0)
move(head,runhead,newarrive); //將head後的newarrive個節點移動到runhead隊列中
if(runhead->next==runhead) //就緒隊列中沒有進程
time++;
else if(q==runhead)
{
p=q;
q=q->next;
}
else
{
printf("進程名:%d\n",q->num);
printf("到達時間:%d\n",q->arriveTime);
if(q->rt==1)
printf("響應時間:%d\n",time-q->arriveTime);
else
printf("第%d次運行開始時間:%d\n",q->rt,time);
if(q->burst<=RR)
{
time+=q->burst;
printf("第%d次運行結束時間:%d\n",q->rt,time);
printf("周轉時間:%d\n",time-q->arriveTime);
printf("************************************\n");
struct pro *tmp=q;
q=q->next;
p->next=q;
free(tmp);
}
else //q->burst>RR
{
time+=RR;
printf("第%d次運行結束時間:%d\n",q->rt,time);
printf("************************************\n");
q->burst-=RR;
q->rt++;
p=q;
q=q->next;
}
}
}
}
void main()
{
struct pro *head=creatList();
printf("當前時間片大小為:%d\n",RR);
run(head);
}
C. c語言,時間差怎麼編程
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
unsigned char time1[] = {10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime( &t1 );
_t2 = _mkgmtime( &t2 );
diff = difftime(_t2, _t1 );
printf( "相差 %.0f 分鍾
", diff / 60 );
}
C語言中有兩個相關的函數用來計算時間差,分別是:
time_t time( time_t *t) 與 clock_t clock(void)
頭文件: time.h
計算的時間單位分別為: s , ms
time_t 和 clock_t 是函數庫time.h 中定義的用來保存時間的數據結構
返回值:
1、time : 返回從公元1970年1月1號的UTC時間從0時0分0秒算起到現在所經過的秒數。如果參數 t 非空指針的話,返回的時間會保存在 t 所指向的內存。
2、clock:返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數。 1單元 = 1 ms。
所以我們可以根據具體情況需求,判斷採用哪一個函數。
具體用法如下例子:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t c_start, t_start, c_end, t_end;
c_start = clock(); //!< 單位為ms
t_start = time(NULL); //!< 單位為s
system("pause");
c_end = clock();
t_end = time(NULL);
//!<difftime(time_t, time_t)返回兩個time_t變數間的時間間隔,即時間差
printf("The pause used %f ms by clock() ",difftime(c_end,c_start));
printf("The pause used %f s by time() ",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要計算某一函數塊的佔用時間時,只需要在執行該函數塊之前和執行完該函數塊之後調用同一個時間計算函數。再調用函數difftime()計算兩者的差,即可得到耗費時間。
D. 一道C語言編程題求助:計算時間差V1.0
#include<stdio.h>
intmain()
{
inth1,s1,h2,s2,h3,s3;;
printf("Inputtimeone(hour,second):");
scanf("%d,%d",&h1,&s1);
printf("Inputtimetwo(hour,second):");
scanf("%d,%d",&h2,&s2);
h3=h1-h2;
s3=s1-s2;
if(s3<0&&h3==0)s3=-s3;
elseif(h3<0)h3=-h3,s3=-s3;
if(s3<0)h3-=1,s3+=60;
printf("%dhour%dsecond ",h3,s3);
}
E. 關於C語言中「年月日時分秒」的演算法。歡迎各位解答。
我只解時分秒。年月日自己去揣摩:
(秒:mm,分:ff,時:ss)
設置一個定時器,時間為一秒,一秒過後 : mm++,
如果mm大於60; mm=0 && ff++ ;
如果 ff 大於60 ff=0 && ss++ ;
如果 ss 大於24 ss=0 && …….
剩下的問題,就是把mm,ff,ss,分別個十位分開並且顯示出來(用數碼管或者LED)
F. c語言編程題:輸入某一時刻的時間,加一秒後輸出新時間。時間以時,分,秒錶示(利用結構類型)。。希望
struct time{
int hour;//小時
int minute;//分鍾
int second;//秒
}
int mian(){
time time1;
time1.hour = 10;
time1.minute = 10;
time1.second = 10;
if(second == 59){//判斷秒
second = 0;
if(minute == 59){//判斷分鍾
minute = 0;
if(hour == 23)//判斷小時
hour = 0;
else
hour++;
}else{
minute++;
}
}else{
second++;
}
return 0;
}