您的位置:

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++

}

基本上改下类型,控制下输出就可以了