经典C语言源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
经典C语言源代码
1、(1)某年某月某日是星期几
#include int main() { ﻩint year,month, day; while (scanf_s("%d%d%d",&year, &month,&day) != EOF)...文档交流仅供参考... { ﻩif (month == 1 || month == 2)//判断month是否为1或2 { ﻩﻩyear-—; ﻩﻩmonth += 12; ﻩﻩ} ﻩint c = year/100; ﻩint y = year-c* 100; ﻩintweek= (c / 4) —2 * c + (y + y / 4) + (13 *(month + 1)/5) + day— 1;...文档交流仅供参考... ﻩwhile (week<0){week += 7; } ﻩﻩweek %= 7; ﻩﻩswitch (week) ﻩﻩ{ ﻩcase 1:printf(”Monday\n");break; ﻩﻩcase2:printf(”Tuesday\n");break; ﻩﻩcase 3:printf("Wednesday\n”); break; ﻩcase 4:printf("Thursday\n"); break; ﻩcase 5:printf("Friday\n"); break; ﻩﻩcase6:printf(”Saturday\n”); break; case 0:printf("Sunday\n"); break; ﻩ} ﻩ} ﻩreturn 0; } 1、(2)某年某月某日是第几天(一维数组) #include "stdio。h” void main(){ ﻩint i, flag, year, month, day, dayth; ﻩintmonth_day[] ={ 0,31,28,31,30,31,30,31,31,30,31,30,31};...文档交流仅供参考... ﻩprintf("请输入年/月/日:\n"); ﻩscanf_s("%d/%d/%d",&year, &month, &day); dayth = day; ﻩflag = (year %400 == 0)|| (year % 4 == 0 && year % 100 != 0);...文档交流仅供参考... ﻩif (flag) month_day[2] = 29; for (i= 1; i 〈month; i++) ﻩdayth =dayth +month_day[i]; ﻩprintf("%d/%d/%d是第%d天\n", year, month, day, dayth);...文档交流仅供参考... } 2、30个数中找最小的数及其位置 #include "stdio.h" #define SIZE 30 void main() { ﻩinti; ﻩfloat data[SIZE]; ﻩint min; printf("请输入%d个浮点数:\n”,SIZE); for(i = 0; i 〈SIZE; i++) { ﻩﻩ//scanf_s("%f", &data[i]); ﻩdata[i] = rand() % 30+ 1; ﻩprintf("%f、", data[i]); ﻩ} min = 0; for(i = 1; i〈 SIZE; i++) { if (data[i]< data[min]) ﻩﻩmin = i; ﻩ} printf("最小值是%5.2f,位置是%5d\n", data[min], min); } 3、30个数从小到大排序 (1) #include "stdio.h" # define SIZE 30 void main(){ ﻩint i,j; float data[SIZE],temp; int min; ﻩprintf("请输入%d个整型数:\n",SIZE); for (i = 0; i < SIZE;i++) { ﻩﻩscanf_s("%f", &data[i]); ﻩ} ﻩfor (i = 0; i 〈 SIZE; i++){ ﻩmin = i; ﻩﻩfor(j =i + 1; j < SIZE; j++)ﻩﻩﻩ ﻩif (data[j] < data[min]) ﻩmin = j; ﻩtemp = data[min]; data[min] = data[i]; ﻩﻩdata[i] = temp; ﻩ}ﻩ printf("\n排序后的结果是:\n”); ﻩfor (i = 0; i < SIZE;i++) ﻩprintf("%5.2f",data[i]); } (2)模块化程序(数组名作为函数参数) #include "stdio.h" # defineSIZE 5 void accept_array(float a[], int size); void sort(float a[], int size); void show_array(floata[], int size);void main(){ ﻩfloatscore[SIZE]; ﻩaccept_array(score, SIZE); printf("排序前:"); show_array(score, SIZE); sort(score, SIZE); printf("排序后:”); ﻩshow_array(score,SIZE); } void accept_array(float a[],int size) {