《程序设计基础课程设计》实验报告
- 格式:wps
- 大小:355.55 KB
- 文档页数:45
《程序设计基础课程设计》实验报告班级:姓名:学号:所选题目:1(算法类第一题),2(文件类第一题),3(文件类第三题),4(字符串类第一题),5(字符串类第二题),6(综合类第一题),7(其他类第一题)。
第1题算法类第一题算法描述:1.用数组保存一年中每月的天数。
2.用字符串保存12个月份。
3.用for 循环进行某若干年份的日历计算。
4.输入年份,如果年份是0,就跳出。
5.对年份就行瑞年、平年计算,如果是瑞年,2月就加一天。
6.对输入的年份的元旦是星期几进行计算。
7.最后对写好的日历进行设计。
源程序:#include<stdio.h>void main(){int i,j,k,h,year,week;int a=0,b=0;int day[]={31,28,31,30,31,30,31,31,30,31,30,31};charmonth[12][10]={"January","February","March","April","May","June","July","August","Septembe r","October","November","December"};for(h=0;h<=100;++h){scanf("%d",&year);if(year==0)break;if((year%4==0&&year%100!=0)||(year%400==0)) day[1]++;if(year<=2000) {a=2000-year;b=6-((a/4)*366+(a-a/4)*365)%7;week=b;}else{a=year-2000;b=6+((a/4+1)*366+(a-a/4-1)*365)%7;week=b%7;}for(i=0;i<12;i++){printf("\n%s\n",month[i]);printf("Sun Mon Tue Wed Thu Fri Sat\n");for(k=0;k<week;k++) printf(" ");for(j=1;j<=day[i];j++){printf("%3d ",j);if(++week>=7){printf("\n");week=week%7;}}printf("\n");}}测试数据(输入输出):第2题文件类第一题算法描述:先建立一个file1.txt文档,将文本内容保存在一个字符串中。
每次从文件中读入一个字符,利用其ASCII值和字符a的ASCII之差与数组的下标相对应,可以统计出各字母的数量c[i]。
分别输入要替换的字符串和替换后的字符串。
对文件中的内容字符一个一个的读取,常用while(!feof(fp1)) or(getc(fp1))来循环读取。
用函数将要替换的字符串和替换后的字符串进行比较。
如果相符,则将该字符串替换,并写入另一个文件。
源代码:#include<stdio.h>#include<string.h>#include<ctype.h>int main(){FILE *fp1;FILE *fp2;fp1=fopen("file1.txt","r");fp2=fopen("file2.txt","w");char ch;char a[1024];char b[1024];char t[1024];ch=fgetc(fp1);int c[26]={0};int i;printf("一段英文文本为:\n");while(ch!=EOF){if(isalpha(ch)){c[tolower(ch)-'a']++;}printf("%c",ch);ch=fgetc(fp1);}putchar('\n');printf("各字母在文本中出现的次数:\n");for(i=0;i<26;i++){printf("%c:%d\n",'a'+i,c[i]);}printf("要替换的字符串:\n");scanf("%s",a);printf("将该字符串替换为:\n");scanf("%s",b);fp1=fopen("file1.txt","r");while(!feof(fp1)) //对文件中的内容字符一个一个的读取,常用while(!feof(fp1)) or(getc(fp1))来循环读取{fscanf(fp1,"%s",t);if(strcmp(a,t)==0){printf("%s ",b);fprintf(fp2,"%s ",b);}else{printf("%s ",t);fprintf(fp2,"%s ",t);}}putchar('\n');fclose(fp2);fclose(fp1);return 0;}测试数据(输入输出):操作前:操作后:第3题文件类第三题算法描述:首先建立两个文件,分别记载数学和英语成绩。
再用数组name,score 等分别存放从file1.txt、file2.txt文件中读取的信息。
利用strcmp 函数分别依次比较name、dname,若相等则将该组信息合并到cname 中。
最后写入到f i l e3.t x t中。
并算出成绩的平均分。
源代码:#include <stdio.h>#include<string.h>char a[10][64], name[10][32], score[10][32];char b[10][64],dname[10][32],dscore[10][32];char c[10][64], cname[10][32], cscore[10][32];char avescore[10][64];int main(){FILE *fp1, *fp2, *fp3;int i, n, j, k, stu;int score1[10], score2[10], averscore[10];if((fp1 = fopen("file1.txt", "r")) == NULL){printf("error!\n");}if((fp2 = fopen("file2.txt", "r")) == NULL){printf("error!\n");}if((fp3 = fopen("file3.txt", "w")) == NULL){printf("error!\n");}i = n = 0;while(!feof(fp1)){fgets(a[i], 64, fp1);i++;} for(stu = 0; stu < i; stu++){j = 0;while((a[stu][j] >= 'A' && a[stu][j] <= 'Z') || (a[stu][j] >= 'a' && a[stu][j] <= 'z')) {j++;}for(k = 0; k < j; k++){name[stu][k] = a[stu][k];}while(a[stu][k] == ' '){k++;}score[stu][0] = a[stu][k];score[stu][1] = a[stu][k+1];score[stu][2] = '\0';score1[stu] = (score[stu][0]-'0') * 10 + (score[stu][1] - '0');}while(!feof(fp2)){fgets(b[n], 64, fp2);n++;}for(stu = 0; stu < n; stu++){j = 0;while((b[stu][j] >= 'A' && b[stu][j] <= 'Z') || (b[stu][j] >= 'a' && b[stu][j] <= 'z')) {j++;}for(k = 0; k < j; k++){dname[stu][k] = b[stu][k];}while(b[stu][k] == ' '){k++;}dscore[stu][0] = b[stu][k];dscore[stu][1] = b[stu][k+1];dscore[stu][2] = '\0';score2[stu] = (dscore[stu][0]-'0') * 10 + (dscore[stu][1] - '0');}for(j = 0; j < i;){for(k = 0; k < n; k++){if(strcmp(name[j],dname[k]) == 0){averscore[j] = (score1[j] + score2[k]) / 2;avescore[j][0] = averscore[j] / 10 + '0';avescore[j][1] = averscore[j] % 10 + '0';avescore[j][2] = '\n';avescore[j][3] = '\0';fputs(name[j], fp3);fputs(" ", fp3);fputs(score[j], fp3);fputs(" ", fp3);fputs(dscore[k], fp3);fputs(" ", fp3);fputs(avescore[j], fp3);j++;}}}fclose(fp1);fclose(fp2);fclose(fp3);return 0;}输入输出:第四题字符串类第1题算法描述:先将英语文本放入一个数组当中去,然后对数组进行操作。