c語言推薦源碼,源代碼和c語言

本文目錄一覽:

求幾個比較有趣,簡單的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-hant/n/146662.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
ZQTI的頭像ZQTI
上一篇 2024-10-31 15:31
下一篇 2024-10-31 15:31

相關推薦

  • g3log源代碼學習

    g3log是一個高性能C++日誌庫,其代碼十分精簡和可讀性強,本文將從3個方面詳細介紹g3log源代碼學習。 一、g3log源代碼整體架構 g3log的整體架構十分清晰,其中有3個…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • 雲智直聘 源碼分析

    本文將會對雲智直聘的源碼進行分析,包括前端頁面和後端代碼,幫助讀者了解其架構、技術實現以及對一些常見的問題進行解決。通過本文的閱讀,讀者將會了解到雲智直聘的特點、優勢以及不足之處,…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28

發表回復

登錄後才能評論