c語言優秀源碼,c語言優秀代碼實例

本文目錄一覽:

請給個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語言源代碼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);

}

求幾個比較有趣,簡單的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語言編寫求源代碼

int data[20]

輸入就不說了我直接弄輸出

int num=0,max=0,min=100,yx=0,lh=0,hg=0,bhg=0

for (int i=0;i20;i++){

    if (data[i]0)

        break;

        num++;

    if (data[i]max)

        max=data[i];

    if (data[i]min)

        min=data[i];

    if (data[i]60)

        bhg++;

    else if (data[i]80)

        hg++;

    else if (data[i]90)

        lh++

    else yx++

}

基本上改下類型,控制下輸出就可以了

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/198191.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-04 07:30
下一篇 2024-12-04 07:30

相關推薦

  • AES加密解密算法的C語言實現

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

    編程 2025-04-29
  • Python生成隨機數的應用和實例

    本文將向您介紹如何使用Python生成50個60到100之間的隨機數,並將列舉使用隨機數的幾個實際應用場景。 一、生成隨機數的代碼示例 import random # 生成50個6…

    編程 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教學圈提供了許多優秀的教學和學習資源,為初學者和專業開發…

    編程 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

發表回復

登錄後才能評論