本文目錄一覽:
求幾個比較有趣,簡單的C語言源代碼 小白自己敲著練一下手感
最簡單的模擬計時器:
#includestdio.h
#includeconio.h
#includewindows.h
int m=0,s=0,ms=0; //m是分 s是秒 ms是毫秒
//以下是5個自編函數
void csh( ); //初始化界面
void yinc(int x,int y); //隱藏游標的函數(y值設為0就會隱藏)
void jishi( ); //計時器運行(每100毫秒變化一次)
void Color (short x, short y); //設定顏色的函數(y設為0就是黑底)
void gtxy (int x, int y); //控制游標位置的函數
int main( ) //主函數
{ csh( );
getch( );
while(1)
{ jishi( );
Sleep(100); //間隔100毫秒
if( kbhit( ) )break; //有鍵按下就退出循環
}
return 0;
}
void csh( ) //初始化界面
{Color(14,0); //設定淡黃字配黑底
printf(「\n\n\t 計時器」);
Color(10,0); //設定淡綠字配黑底
printf(“\n\t┌───────────┐”);
printf(“\n\t│ │”);
printf(“\n\t└───────────┘”);
gtxy(10,4); //游標到屏幕第10列4行處輸出
Color(7,0); //恢復白字黑底
printf(” 00:00:00 “);
yinc(1,0 ); //隱藏游標(yinc代表隱藏)
return;
}
void jishi( ) //計時器運行
{ms+=1;
if(ms==10){s+=1;ms=0;}
if(s==60){m+=1;s=0;}
gtxy(10,4);
Color(9,0); //設定淡藍字配黑底
if(m9) printf(” %d:”,m);
else printf(” 0%d:”,m);
Color(14,0); //設定淡黃字配黑底
if(s9) printf(“%d:”,s);
else printf(“0%d:”,s);
Color(12,0); //設定淡紅字配黑底
printf(“0%d”,ms);
}
void gtxy (int x, int y) //控制游標位置的函數
{ COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );
}
void Color (short ForeColor= 7, short BackGroundColor= 0) //設定顏色的函數
{ HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute ( handle, ForeColor + BackGroundColor * 0x10 );
}
void yinc(int x,int y) //隱藏游標的設置(gb代表游標)
{ CONSOLE_CURSOR_INFO gb={x,y}; //x為1-100,y為0就隱藏游標
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), gb);
}
請給個C語言的500行左右的源代碼
下面是我初學時的一個列子,是計算時間的。。
可以進行時間相加減“`
由於我是在linux下運行的,再轉到window上所以格式會有點亂“`諒解!!!
代碼也沒有進行優化處理的“`
#include stdio.h
#include stdlib.h
typedef struct time
{
int year;
int month;
int day;
int hour;
int minute;
int sec;
} ST_TIME;
ST_TIME date;
void add_sec(int secs);
void add_minute(int minutes);
void add_hour(int hours);
void add_day(int days);
int maxday(int leap, int month);
void add_month(int months);
void add_year(int years);
void sub_sec(int secs);
void sub_minute(int minutes);
void sub_hour(int hours);
void sub_day(int days);
void sub_month(int months);
void sub_year(int years);
void sum(void);
void sub(void);
void init_system(void);
int add_number;
int main(int argc, char *argv[])
{
char array[20];
char emblem;
init_system();
printf(“Press the ‘+’ or ‘-‘ to control.\n”);
scanf(“%c”,emblem);
switch (emblem)
{
case ‘+’:
{
sum();
break;
}
case ‘-‘:
{
sub();
break;
}
}
sprintf(array,”%d-%02d-%02d %02d:%02d:%02d”,date.year,date.month,
date.day,date.hour,date.minute,date.sec);
printf(“%s\n”,array);
return 0;
}
void init_system(void)
{
char str[20];
char *p;
p = str;
printf(“Enter the date:\n”);
printf(“Be sure your inputs like that:2008 12 12 12 12 12\n”);
gets(str);
date.year = atoi(p);
date.month = atoi(p+5);
date.day = atoi(p+8);
date.hour = atoi(p+11);
date.minute = atoi(p+14);
date.sec = atoi(p+17);
}
void sum(void)
{
int lab;
printf(“please choose the option:\n”);
printf(“1: add the secs;\n”);
printf(“2: add the minutes;\n”);
printf(“3: add the hours;\n”);
printf(“4: add the days;\n”);
printf(“5: add the months;\n”);
printf(“6: add the years;\n”);
while (getchar() != ‘\n’);
scanf(“%d”,lab);
switch (lab)
{
case 1 :
{
printf(“enter the increased secs:”);
scanf(“%d”,add_number);
add_sec(add_number);
break;
}
case 2:
{
printf(“enter the increased minutes:”);
scanf(“%d”,add_number);
add_minute(add_number);
break;
}
case 3:
{
printf(“enter the increased hours:”);
scanf(“%d”,add_number);
add_hour(add_number);
break;
}
case 4:
{
printf(“enter the increased days:”);
scanf(“%d”,add_number);
add_day(add_number);
break;
}
case 5:
{
printf(“enter the increased months:”);
scanf(“%d”,add_number);
add_month(add_number);
break;
}
case 6:
{
printf(“enter the increased years:”);
scanf(“%d”,add_number);
add_year(add_number);
break;
}
}
}
void sub(void)
{
int lab;
printf(“please choose the option:\n”);
printf(“1: reduce the secs;\n”);
printf(“2: reduce the minutes;\n”);
printf(“3: reduce the hours;\n”);
printf(“4: reduce the days;\n”);
printf(“5: reduce the months;\n”);
printf(“6: reduce the years;\n”);
scanf(“%d”,lab);
switch (lab)
{
case 1 :
{
printf(“enter the reduced secs:”);
scanf(“%d”,add_number);
sub_sec(add_number);
break;
}
case 2:
{
printf(“enter the reduced minutes:”);
scanf(“%d”,add_number);
sub_minute(add_number);
break;
}
case 3:
{
printf(“enter the reduced hours:”);
scanf(“%d”,add_number);
sub_hour(add_number);
break;
}
case 4:
{
printf(“enter the reduced days:”);
scanf(“%d”,add_number);
sub_day(add_number);
break;
}
case 5:
{
printf(“enter the reduced months:”);
scanf(“%d”,add_number);
sub_month(add_number);
break;
}
case 6:
{
printf(“enter the increased years:”);
scanf(“%d”,add_number);
sub_year(add_number);
break;
}
}
}
void add_sec(int secs)
{
date.sec = date.sec + secs;
while (date.sec = 60)
{
add_minute(1);
date.sec = date.sec – 60;
}
}
void add_minute(int minutes)
{
date.minute = date.minute + minutes;
while (date.minute = 60)
{
add_hour(1);
date.minute = date.minute – 60;
}
}
void add_hour(int hours)
{
date.hour = date.hour + hours;
while (date.hour = 24)
{
add_day(1);
date.hour = date.hour – 24;
}
}
void add_day(int days)
{
int leap;
date.day = days + date.day;
/****************************************
* 功能:判斷是否為閏年 *
****************************************/
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
while (date.day maxday(leap,date.month))
{
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
date.day = date.day – maxday(leap,date.month);
add_month(1);
}
}
void add_month(int months)
{
int leap;
date.month = date.month + months;
while (date.month 12)
{
date.month = date.month – 12;
add_year(1);
}
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
if (leap==1 date.month==2 date.day=29)
{
date.day = 29;
}
else if (leap==0 date.month==2 date.day=28)
{
date.day = 28;
}
}
void add_year(int years)
{
int leap;
date.year = date.year + years;
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
if (leap==1 date.month==2 date.day=29)
{
date.month = 2;
date.day = 29;
}
else if (leap==0 date.month==2 date.day=28)
{
date.month = 2;
date.day = 28;
}
}
void sub_sec(int secs)
{
date.sec = date.sec – secs;
while (0 date.sec)
{
sub_minute(1);
date.sec = date.sec + 60;
}
}
void sub_minute(int minutes)
{
date.minute = date.minute – minutes;
while (0 date.minute)
{
sub_hour(1);
date.minute = date.minute + 60;
}
}
void sub_hour(int hours)
{
date.hour = date.hour – hours;
while (0 date.hour)
{
sub_day(1);
date.hour = date.hour + 24;
}
}
void sub_day(int days)
{
int leap;
date.day = date.day – days;
/****************************************
* 功能:判斷是否為閏年 *
****************************************/
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
while (0 = date.day)
{
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
date.day = date.day + maxday(leap,date.month-1);
sub_month(1);
}
}
void sub_month(int months)
{
int leap;
date.month = date.month – months;
while (0 = date.month)
{
date.month = date.month + 12;
sub_year(1);
}
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
if (leap==1 date.month==2 date.day=29)
{
date.day = 29;
}
else if (leap==0 date.month==2 date.day=28)
{
date.day = 28;
}
}
void sub_year(int years)
{
int leap;
date.year = date.year – years;
if ((date.year%4 == 0 date.year%100 != 0) || (date.year%400 == 0))
{
leap = 1; /*是閏年則leap的值為1*/
}
else
{
leap = 0; /*不是則leap的值為0 */
}
if (leap==1 date.month==2 date.day=29)
{
date.month = 2;
date.day = 29;
}
else if (leap==0 date.month==2 date.day=28)
{
date.month = 2;
date.day = 28;
}
}
int maxday(int leap, int month)
{
int max_day; /*用於返回此月份的最大值*/
switch (month)
{
case 4:
{
max_day = 30;
break;
}
case 6:
{
max_day = 30;
break;
}
case 9:
{
max_day = 30;
break;
}
case 11:
{
max_day = 30;
break;
}
case 2:
{
if (leap == 1)
{
max_day = 29;
break;
}
else
{
max_day = 28;
break;
}
}
default: max_day = 31;
}
return max_day;
我這裡還有很多的,如果要的話,就聯繫我121779988““
只不過我也是個菜鳥級的“
求c語言源代碼
#include stdio.h
#include stdlib.h
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULL 0
typedef char ElemType;
char a[500];
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
void table (void);
int InitStack (SqStack S);
int DestroyStack (SqStack S);
int Push (SqStack S, ElemType e);
int Pop(SqStack S, ElemType e);
void LineEdit (void);
void main ()
{
table();
LineEdit();
}
int InitStack (SqStack S)
{
S.base = (ElemType *)malloc(100 *sizeof(ElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base;
S.stacksize = 100;
return OK;
}
int Push (SqStack S, ElemType e)
{
if(S.top – S.base = S.stacksize)
{
S.base = (ElemType *)realloc(S.base,(S.stacksize + 10)*sizeof(ElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += 10;
}
*S.top++ = e;
return OK;
}
int DestroyStack (SqStack S)
{
free(S.base);
return OK;
}
int Pop(SqStack S, ElemType e)
{
if(S.top == S.base)
return ERROR;
e = *–S.top;
return OK;
}
void LineEdit (void)
{
SqStack S;
char e;
char *p=a[0],*q;
char ch;
InitStack(S);
ch = getchar();
while(ch != EOF)
{
while(ch != EOF ch != ‘\n’)
{
switch(ch)
{
case ‘#’:
Pop(S,e);
break;
case :
S.base = S.top;
break;
default:
Push(S,ch);
break;
}
ch = getchar();
}
Push(S,’\n’);
for(q=S.base;qS.top;p++,q++)
*p=*q;
S.base = S.top;
if(ch != EOF)
ch = getchar();
}
puts(a);
*p=’\0′;
}
void table (void)
{
printf(“===============================================================================\n”);
printf(“\t\t\t\t「#」退格符\n”);
printf(“\t\t\t\t「@」退行符\n”);
printf(“\t\t\t\t「ctrl+z」結束全文\n”);
printf(“===============================================================================\n”);
printf(“\n”);
}
初級C語言源代碼
好的哦
#define N 200
#include graphics.h
#include stdlib.h
#include dos.h
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*得分*/
int gamespeed=50000;/*遊戲速度自己調整*/
struct Food
{
int x;/*食物的橫坐標*/
int y;/*食物的縱坐標*/
int yes;/*判斷是否要出現食物的變數*/
}food;/*食物的結構體*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的節數*/
int direction;/*蛇移動方向*/
int life;/* 蛇的生命,0活著,1死亡*/
}snake;
void Init(void);/*圖形驅動*/
void Close(void);/*圖形結束*/
void DrawK(void);/*開始畫面*/
void GameOver(void);/*結束遊戲*/
void GamePlay(void);/*玩遊戲具體過程*/
void PrScore(void);/*輸出成績*/
/*主函數*/
void main(void)
{
Init();/*圖形驅動*/
DrawK();/*開始畫面*/
GamePlay();/*玩遊戲具體過程*/
Close();/*圖形結束*/
}
/*圖形驅動*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(gd,gm,”c:\\tc”);
cleardevice();
}
/*開始畫面,左上角坐標為(50,40),右下角坐標為(610,460)的圍牆*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設置線型*/
for(i=50;i=600;i+=10)/*畫圍牆*/
{
rectangle(i,40,i+10,49); /*上邊*/
rectangle(i,451,i+10,460);/*下邊*/
}
for(i=40;i=450;i+=10)
{
rectangle(50,i,59,i+10); /*左邊*/
rectangle(601,i,610,i+10);/*右邊*/
}
}
/*玩遊戲具體過程*/
void GamePlay(void)
{
randomize();/*隨機數發生器*/
food.yes=1;/*1表示需要出現新食物,0表示已經存在食物*/
snake.life=0;/*活著*/
snake.direction=1;/*方向往右*/
snake.x[0]=100;snake.y[0]=100;/*蛇頭*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*節數*/
PrScore();/*輸出得分*/
while(1)/*可以重複玩遊戲,壓ESC鍵結束*/
{
while(!kbhit())/*在沒有按鍵的情況下,蛇自己移動身體*/
{
if(food.yes==1)/*需要出現新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*食物隨機出現後必須讓食物能夠在整格內,這樣才可以讓蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*畫面上有食物了*/
}
if(food.yes==0)/*畫面上有食物了就要顯示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i0;i–)/*蛇的每個環節往前移動,也就是貪吃蛇的關鍵演算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個方向,通過這個判斷來移動蛇頭*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;isnake.node;i++)/*從蛇的第四節開始判斷是否撞到自己了,因為蛇頭為兩節,第三節不可能拐過來*/
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
GameOver();/*顯示失敗*/
snake.life=1;
break;
}
}
if(snake.x[0]55||snake.x[0]595||snake.y[0]55||
snake.y[0]455)/*蛇是否撞到牆壁*/
{
GameOver();/*本次遊戲結束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1)/*以上兩種判斷以後,如果蛇死就跳出內循環,重新開始*/
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以後*/
{
setcolor(0);/*把畫面上的食物東西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一節先放在看不見的位置,下次循環就取前一節的位置*/
snake.node++;/*蛇的身體長一節*/
food.yes=1;/*畫面上需要出現新的食物*/
score+=10;
PrScore();/*輸出新得分*/
}
setcolor(4);/*畫出蛇*/
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最後一節*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1)/*如果蛇死就跳出循環*/
break;
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*按ESC鍵退出*/
break;
else
if(key==UPsnake.direction!=4)
/*判斷是否往相反的方向移動*/
snake.direction=3;
else
if(key==RIGHTsnake.direction!=2)
snake.direction=1;
else
if(key==LEFTsnake.direction!=1)
snake.direction=2;
else
if(key==DOWNsnake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*遊戲結束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,”GAME OVER”);
getch();
}
/*輸出成績*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,”score:%d”,score);
outtextxy(55,20,str);
}
/*圖形結束*/
void Close(void)
{
getch();
closegraph();
}
#include dos.h /*DOS介面函數*/
#include math.h /*數學函數的定義*/
#include conio.h /*屏幕操作函數*/
#include stdio.h /*I/O函數*/
#include stdlib.h /*庫函數*/
#include stdarg.h /*變數長度參數表*/
#include graphics.h /*圖形函數*/
#include string.h /*字元串函數*/
#include ctype.h /*字元操作函數*/
#define UP 0x48 /*游標上移鍵*/
#define DOWN 0x50 /*游標下移鍵*/
#define LEFT 0x4b /*游標左移鍵*/
#define RIGHT 0x4d /*游標右移鍵*/
#define ENTER 0x0d /*回車鍵*/
void *rar; /*全局變數,保存游標圖象*/
struct palettetype palette; /*使用調色板信息*/
int GraphDriver; /* 圖形設備驅動*/
int GraphMode; /* 圖形模式值*/
int ErrorCode; /* 錯誤代碼*/
int MaxColors; /* 可用顏色的最大數值*/
int MaxX, MaxY; /* 屏幕的最大解析度*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*畫邊框函數*/
void initialize(void); /*初始化函數*/
void computer(void); /*計算器計算函數*/
void changetextstyle(int font, int direction, int charsize); /*改變文本樣式函數*/
void mwindow(char *header); /*窗口函數*/
int specialkey(void) ; /*獲取特殊鍵函數*/
int arrow(); /*設置箭頭游標函數*/
/*主函數*/
int main()
{
initialize();/* 設置系統進入圖形模式 */
computer(); /*運行計算器 */
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0); /*結束程序*/
}
/* 設置系統進入圖形模式 */
void initialize(void)
{
int xasp, yasp; /* 用於讀x和y方向縱橫比*/
GraphDriver = DETECT; /* 自動檢測顯示器*/
initgraph( GraphDriver, GraphMode, “” );
/*初始化圖形系統*/
ErrorCode = graphresult(); /*讀初始化結果*/
if( ErrorCode != grOk ) /*如果初始化時出現錯誤*/
{
printf(“Graphics System Error: %s\n”,
grapherrormsg( ErrorCode ) ); /*顯示錯誤代碼*/
exit( 1 ); /*退出*/
}
getpalette( palette ); /* 讀面板信息*/
MaxColors = getmaxcolor() + 1; /* 讀取顏色的最大值*/
MaxX = getmaxx(); /* 讀屏幕尺寸 */
MaxY = getmaxy(); /* 讀屏幕尺寸 */
getaspectratio( xasp, yasp ); /* 拷貝縱橫比到變數中*/
AspectRatio = (double)xasp/(double)yasp;/* 計算縱橫比值*/
}
/*計算器函數*/
void computer(void)
{
struct viewporttype vp; /*定義視口類型變數*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作數和計算結果變數*/
char cnum[5],str2[20]={“”},c,temp[20]={“”};
char str1[]=”1230.456+-789*/Qc=^%”;/* 定義字元串在按鈕圖形上顯示的符號 */
mwindow( “Calculator” ); /* 顯示主窗口 */
color = 7; /*設置灰顏色值*/
getviewsettings( vp ); /* 讀取當前窗口的大小*/
width=(vp.right+1)/10; /* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; /*設置按鈕高度 */
x = width /2; /*設置x的坐標值*/
y = height/2; /*設置y的坐標值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*畫一個二維矩形條顯示運算數和結果*/
setcolor( color+3 ); /*設置淡綠顏色邊框線*/
rectangle( x+width*2, y, x+7*width, y+height );
/*畫一個矩形邊框線*/
setcolor(RED); /*設置顏色為紅色*/
outtextxy(x+3*width,y+height/2,”0.”); /*輸出字元串”0.”*/
x =2*width-width/2; /*設置x的坐標值*/
y =2*height+height/2; /*設置y的坐標值*/
for( j=0 ; j4 ; ++j ) /*畫按鈕*/
{
for( i=0 ; i5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*畫一個矩形條*/
rectangle( x, y, x+width, y+height );
sprintf(str2,”%c”,str1[j*5+i]);
/*將字元保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*移動列坐標*/
}
y +=(height/2)*3; /* 移動行坐標*/
x =2*width-width/2; /*複位列坐標*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*移動游標到x,y位置*/
arrow(); /*顯示游標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,””); /*設置str2為空串*/
while((v=specialkey())!=45) /*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER) /*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); /*顯示游標圖象*/
if(v==RIGHT) /*右移箭頭時新位置計算*/
if(x=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字元位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否則,右移到下一個字元位置*/
if(v==LEFT) /*左移箭頭時新位置計算*/
if(x=x0)
{
x=x0+6*width;
m=4;
} /*如果移到頭,再左移,則移動到最右邊字元位置*/
else
{
x=x-width-width/2;
m–;
} /*否則,左移到前一個字元位置*/
if(v==UP) /*上移箭頭時新位置計算*/
if(y=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到頭,再上移,則移動到最下邊字元位置*/
else
{
y=y-height-height/2;
n–;
} /*否則,移到上邊一個字元位置*/
if(v==DOWN) /*下移箭頭時新位置計算*/
if(y=7*height)
{
y=y0;
n=0;
} /*如果移到尾,再下移,則移動到最上邊字元位置*/
else
{
y=y+height+height/2;
n++;
} /*否則,移到下邊一個字元位置*/
putimage(x,y,rar,XOR_PUT); /*在新的位置顯示游標箭頭*/
}
c=str1[n*5+m]; /*將字元保存到變數c中*/
if(isdigit(c)||c==’.’) /*判斷是否是數字或小數點*/
{
if(flag==-1) /*如果標誌為-1,表明為負數*/
{
strcpy(str2,”-“); /*將負號連接到字元串中*/
flag=1;
} /*將標誌值恢復為1*/
sprintf(temp,”%c”,c); /*將字元保存到字元串變數temp中*/
strcat(str2,temp); /*將temp中的字元串連接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2); /*顯示字元串*/
}
if(c==’+’)
{
num1=atof(str2); /*將第一個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=1; /*做計算加法標誌值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,”0.”); /*顯示字元串*/
}
if(c==’-‘)
{
if(strcmp(str2,””)==0) /*如果str2為空,說明是負號,而不是減號*/
flag=-1; /*設置負數標誌*/
else
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=2; /*做計算減法標誌值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,”0.”); /*顯示字元串*/
}
}
if(c==’*’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=3; /*做計算乘法標誌值*/
setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,”0.”); /*顯示字元串*/
}
if(c==’/’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=4; /*做計算除法標誌值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,”0.”); /*顯示字元串*/
}
if(c==’^’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=5; /*做計算乘方標誌值*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,”0.”); /*顯示字元串*/
}
if(c==’%’)
{
num1=atof(str2); /*將第二個操作數轉換為浮點數*/
strcpy(str2,””); /*將str2清空*/
act=6; /*做計算模運算乘方標誌值*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*畫矩形*/
outtextxy(5*width,height,”0.”); /*顯示字元串*/
}
if(c==’=’)
{
num2=atof(str2); /*將第二個操作數轉換為浮點數*/
switch(act) /*根據運算符號計算*/
{
case 1:result=num1+num2;break; /*做加法*/
case 2:result=num1-num2;break; /*做減法*/
case 3:result=num1*num2;break; /*做乘法*/
case 4:result=num1/num2;break; /*做除法*/
case 5:result=pow(num1,num2);break; /*做x的y次方*/
case 6:result=fmod(num1,num2);break; /*做模運算*/
}
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆蓋結果區*/
sprintf(temp,”%f”,result); /*將結果保存到temp中*/
outtextxy(5*width,height,temp); /*顯示結果*/
}
if(c==’c’)
{
num1=0; /*將兩個操作數複位0,符號標誌為1*/
num2=0;
flag=1;
strcpy(str2,””); /*將str2清空*/
setfillstyle(SOLID_FILL,color+3); /*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆蓋結果區*/
outtextxy(5*width,height,”0.”); /*顯示字元串*/
}
if(c==’Q’)exit(0); /*如果選擇了q回車,結束計算程序*/
}
putimage(x,y,rar,XOR_PUT); /*在退出之前消去游標箭頭*/
return; /*返回*/
}
/*窗口函數*/
void mwindow( char *header )
{
int height;
cleardevice(); /* 清除圖形屏幕 */
setcolor( MaxColors – 1 ); /* 設置當前顏色為白色*/
setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 設置視口大小 */
height = textheight( “H” ); /* 讀取基本文本大小 */
settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*設置文本樣式*/
settextjustify( CENTER_TEXT, TOP_TEXT );/*設置字元排列方式*/
outtextxy( MaxX/4, 2, header ); /*輸出標題*/
setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*設置視口大小*/
drawboder(); /*畫邊框*/
}
void drawboder(void) /*畫邊框*/
{
struct viewporttype vp; /*定義視口類型變數*/
setcolor( MaxColors – 1 ); /*設置當前顏色為白色 */
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*設置畫線方式*/
getviewsettings( vp );/*將當前視口信息裝入vp所指的結構中*/
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*畫矩形邊框*/
}
/*設計滑鼠圖形函數*/
int arrow()
{
int size;
int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定義多邊形坐標*/
setfillstyle(SOLID_FILL,2); /*設置填充模式*/
fillpoly(8,raw); /*畫出一游標箭頭*/
size=imagesize(4,4,16,16); /*測試圖象大小*/
rar=malloc(size); /*分配內存區域*/
getimage(4,4,16,16,rar); /*存放游標箭頭圖象*/
putimage(4,4,rar,XOR_PUT); /*消去游標箭頭圖象*/
return 0;
}
/*按鍵函數*/
int specialkey(void)
{
int key;
while(bioskey(1)==0); /*等待鍵盤輸入*/
key=bioskey(0); /*鍵盤輸入*/
key=key0xff? key0xff:key8; /*只取特殊鍵的掃描值,其餘為0*/
return(key); /*返回鍵值*/
}
#include dos.h /*DOS介面函數*/
#include math.h /*數學函數的定義*/
#include conio.h /*屏幕操作函數*/
#include stdio.h /*I/O函數*/
#include stdlib.h /*庫函數*/
#include stdarg.h /*變數長度參數表*/
#include graphics.h /*圖形函數*/
#include string.h /*字元串函數*/
#include ctype.h /*字元操作函數*/
#define UP 0x48 /*游標上移鍵*/
#define DOWN 0x50 /*游標下移鍵*/
#define LEFT 0x4b /*游標左移鍵*/
#define RIGHT 0x4d /*游標右移鍵*/
#define ENTER 0x0d /*回車鍵*/
void *rar; /*全局變數,保存游標圖象*/
struct palettetype palette; /*使用調色板信息*/
int GraphDriver; /* 圖形設備驅動*/
int GraphMode; /* 圖形模式值*/
int ErrorCode; /* 錯誤代碼*/
int MaxColors; /* 可用顏色的最大數值*/
int MaxX, MaxY; /* 屏幕的最大解析度*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*畫邊框函數*/
void initialize(void); /*初始化函數*/
void computer(void); /*計算器計算函數*/
void changetextstyle(int font, int direction, int charsize); /*改變文本樣式函數*/
void mwindow(char *header); /*窗口函數*/
int specialkey(void) ; /*獲取特殊鍵函數*/
int arrow(); /*設置箭頭游標函數*/
/*主函數*/
int main()
{
initialize();/* 設置系統進入圖形模式 */
computer(); /*運行計算器 */
closegraph();/*系統關閉圖形模式返迴文本模式*/
return(0); /*結束程序*/
}
/* 設置系統進入圖形模式 */
void initialize(void)
{
int xasp, yasp; /* 用於讀x和y方向縱橫比*/
GraphDriver = DETECT; /* 自動檢測顯示器*/
initgraph( GraphDriver, GraphMode, “” );
/*初始化圖形系統*/
ErrorCode = graphresult(); /*讀初始化結果*/
if( ErrorCode != grOk ) /*如果初始化時出現錯誤*/
{
printf(“Graphics System Error: %s\n”,
grapherrormsg( ErrorCode ) ); /*顯示錯誤代碼*/
exit( 1 ); /*退出*/
}
getpalette( palette ); /* 讀面板信息*/
MaxColors = getmaxcolor() + 1; /* 讀取顏色的最大值*/
MaxX = getmaxx(); /* 讀屏幕尺寸 */
MaxY = getmaxy(); /* 讀屏幕尺寸 */
getaspectratio( xasp, yasp ); /* 拷貝縱橫比到變數中*/
AspectRatio = (double)xasp/(double)yasp;/* 計算縱橫比值*/
}
/*計算器函數*/
void computer(void)
{
struct viewporttype vp; /*定義視口類型變數*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作數和計算結果變數*/
char cnum[5],str2[20]={“”},c,temp[20]={“”};
char str1[]=”1230.456+-789*/Qc=^%”;/* 定義字元串在按鈕圖形上顯示的符號 */
mwindow( “Calculator” ); /* 顯示主窗口 */
color = 7; /*設置灰顏色值*/
getviewsettings( vp ); /* 讀取當前窗口的大小*/
width=(vp.right+1)/10; /* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; /*設置按鈕高度 */
x = width /2; /*設置x的坐標值*/
y = height/2; /*設置y的坐標值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*畫一個二維矩形條顯示運算數和結果*/
setcolor( color+3 ); /*設置淡綠顏色邊框線*/
rectangle( x+width*2, y, x+7*width, y+height );
/*畫一個矩形邊框線*/
setcolor(RED); /*設置顏色為紅色*/
outtextxy(x+3*width,y+height/2,”0.”); /*輸出字元串”0.”*/
x =2*width-width/2; /*設置x的坐標值*/
y =2*height+height/2; /*設置y的坐標值*/
for( j=0 ; j4 ; ++j ) /*畫按鈕*/
{
for( i=0 ; i5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*畫一個矩形條*/
rectangle( x, y, x+width, y+height );
sprintf(str2,”%c”,str1[j*5+i]);
/*將字元保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*移動列坐標*/
}
y +=(height/2)*3; /* 移動行坐標*/
x =2*width-width/2; /*複位列坐標*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*移動游標到x,y位置*/
arrow(); /*顯示游標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,””); /*設置str2為空串*/
while((v=specialkey())!=45) /*當壓下Alt+x鍵結束程序,否則執行下面的循環*/
{
while((v=specialkey())!=ENTER) /*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); /*顯示游標圖象*/
if(v==RIGHT) /*右
急需一份c語言源代碼50句左右可以運行的
#includestdio.h
#includetime.h
int main()
{
while(1){
time_t tt;
struct tm *t;
time(tt);
t = localtime(tt);
char rn[256];
sprintf(rn, “/mnt/secure/staging/extra_sd_%d_%d_%d_%d_%d_%d”,
t-tm_year+1900, t-tm_mon+1, t-tm_mday, t-tm_hour, t-tm_min, t-tm_sec);
sleep(1);
printf(“%s\n”,rn);
}
原創文章,作者:ZQTI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/146662.html