表格处理 学号 数学 物理 英语 总分 9901 97.2 87.7 93.6 9902 875 91.3 90.7 9903 78.6 81.9 91.9
26 表格处理 学号 数学 物理 英语 总分 9901 97.2 87.7 93.6 9902 87.5 91.3 90.7 9903 78.6 81.9 91.9
类的设计 include <iostream. h static float al={{9901,972,87.7,93.60}, 9902,87.5,91.3,90.70}, 9903,786,81.9,91.9,0}; class score table public void getScore Table cout <<"nNo. MT PH EN SUM 打印表头 cout<<“n- ∥打印隔线 cout <<n<<ajl0< ∥打印学号 for(int j=1;j<=3,j++) a[4+吨[可 ∥求总分 cout <<all ∥打印学号 打印总分
27 类的设计 # include <iostream.h> static float a[][5] = {{9901,97.2,87.7,93.6,0}, {9902,87.5,91.3,90.7,0}, {9903,78.6,81.9,91.9,0}}; class ScoreTable { public: void getScoreTable() { cout << “\nNo. MT PH EN SUM “; // 打印表头 cout << “\n------------------------------“; // 打印隔线 for(int i = 0; i <= 2; i ++) { cout << “\n” << a[i][0] <<" "; // 打印学号 for(int j = 1; j <= 3; j ++) { a[i][4] += a[i][j] <<" "; // 求总分 cout << a[i][j]; // 打印学号及单科成绩 } cout << a[i][4]; // 打印总分 } } };
日期转换 此题的算法很简单,若给定的是第月,则 应将第1,2,3,…,i1月的天数相加,再 加上该月的天数 由于每月的天数不同,且闰年与否,2月 份的天数也不相同。为此,可以设计 有两行的二维数组如下: 031813313313133 0,3129,3130,31,3031,313031,3031} 当非闰年时,选第0行;闰年时,选第1行 第0个元素取0的目的是为了使月份与列数 一致
28 日期转换 此题的算法很简单,若给定的是第i月,则 应将第1,2,3,…,i-1月的天数相加,再 加上该月的天数。 由于每月的天数不同,且闰年与否,2月 份的天数也不相同。为此,可以设计一个 有两行的二维数组如下: {{0,31,28,31,30,31,30,31,31,30,31,30,31} {0,31,29,31,30,31,30,31,31,30,31,30,31}} 当非闰年时,选第0行;闰年时,选第1行。 第0个元素取0的目的是为了使月份与列数 一致
判断闰年的算法 12Afag=year%4==0&&year%100!=0) year%400==0 次当fag为1时,year为闰年;为0时,year为 平年
29 判断闰年的算法 flag =(year % 4 = = 0 && year % 100 != 0) || year % 400 = = 0 当flag为1时,year为闰年;为0时,year为 平年
类设计 tatic int day tabl13={{0,31,28,31,30,31,30,31,31,30,31,30,31} 0,31,29,31,30,31,30,31,31,30,31,30,31} class date day Int mYY, mMM, mDD ublic Date Day(int onth, int da mMM=month, mDD==day; j int getDayofYearO ∥函数原型说明 int Date Day get DayofYear() ag=(mYY%4=0&&mYY%1001=0)mYY%400=0,∥判定闰年 for(int j=1; j<mMM; j++)mDD+= day tab[flagIlj ∥累加日数 eturn(mDD)
30 类设计 #include <iostream.h> static int day_tab[][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}}; class Date_Day { int mDay; int mYY,mMM,mDD; public: Date_Day(int year,int month,int day) { mYY = year; mMM = month; mDD = day;} int getDayOfYear(); // 函数原型说明 }; int Date_Day::getDayOfYear( ) { int flag; flag=(mYY % 4 == 0 && mYY % 100 != 0) || mYY % 400 == 0; // 判定闰年 for(int j = 1; j < mMM; j++) mDD += day_tab[flag][j]; // 累加日数 return(mDD); }