本文目錄一覽:
請給個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-tw/n/198191.html