文档详情

实验6数组及其应用.doc

发布:2015-08-12约2.03千字共6页下载文档
文本预览下载声明
实验6 数组及其应用 成绩 专业班级数学122班学号201212010220姓名文鑫报告日期 2014-5-26 实验类型:●验证性实验 ○综合性实验 ○设计性实验 实验目的: 1熟悉定义数组的方法、引用数组元素的方法。 2学会对数组的初始化、对数组元素输入值、输出数组的内容。 实验内容: 第五章习题第5题、第10题、第13题 实验步骤:(记录实验过程中的步骤) 1 要求上机实验前先编写出程序代码 2 编辑录入程序 3 调试程序并记录调试过程中出现的问题及修改程序的过程 4 经反复调试后,运行程序并验证程序运行是否正确。 5 记录运行时的输入和输出。 实验任务的程序运行运行界面及运行结果: 第五章习题第5题 将一个数组中的值按逆序重新存放。 (我所选的数字为 1 2 3 4 5) 程序 #includeiostream using namespace std; int main() {const int n=5; int a[n],i,temp; coutenter array a:endl; for(i=0;in;i++) cina[i]; coutarray a:endl; for(i=0;in;i++) couta[i] ; i=0; while(i=n/2) {temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp; i++; } coutendlnow,array a:endl; for(i=0;in;i++) couta[i] ; coutendl; return 0; } 运行结果 第五章习题第10题 有一篇文章,共有三行文字,每行最多有80个字符。要求分别统计出其中英文大写字母·小写字母·数字·空格以及其他字符的个数。 程序: #includeiostream using namespace std; int main() {int i,j,upper,lower,digit,space,other; char text[3][80]; upper=lower=digit=space=other=0; for(i=0;i=2;i++) {coutplease input linei+1endl; gets(text[i]); for(j=0;j80text[i][j]!=\0;j++) {if(text[i][j]=Atext[i][j]=Z) upper++; else if(text[i][j]=atext[i][j]=z) lower++; else if(text[i][j]=0text[i][j]=9) digit++; else if(text[i][j]== ) space++; else other++; } } coutupper case:upperendl; coutlower case:lowerendl; coutdigit :digitendl; coutspace :spaceendl; coutother :otherendl; return 0; } 运行结果: 第五章习题第13题: 编写一个程序,将两个字符串连接起来,结果取代第一个字符串。 用字符组,不用strcat函数。 程序 #includeiostream using namespace std; int main() {char s1[80],s2[40]; int i=0,j=0; coutinput string1:; cins1; coutinput string2:; cins2; while(s1[i]!=\0) i++; while(s2[j]!=\0) s1[i++]=s2[j++]; s1[i]=\0; coutThe new string is:s1endl; return 0; } 用标准库中的strcat函数 用string方法定义字符串变量 #include iostream #include string using namespace std; int main() {string s1=week,s2=end; couts1=s1endl; couts2=s2endl; s1=s1+s2; coutThe new stringis:s1endl; return 0; } 运行结果 实验总结 每个实验任务程序代码
显示全部
相似文档