c++程序设计实验报告六/c++实验报告

一、实验内容

1. 义一个一维数组a[10],从键盘输入元素的值,然后找出所有元素中的最大值并输出。

2. 从键盘输入一行字符,统计其中大写英文字符的个数。要求使用字符数组。

3. 用“Virtue and happiness are mother and daugher.初始化一个字符串,用指针偏移符号遍历字符串的内容,并打印各个字符。用字符“+”来分隔相邻的字符。

二、实验目的

1. 掌握声明数组、初始化数组,以及引用各个数组元素的方法。

2. 学会运用指针。

3. 理解指针、数组和字符串之间的紧密关系。

4. 掌握声明并使用字符串数组。

三、主要仪器设备及耗材

硬件:计算机一台

软件:VC++ 6.0,MSDN2003或者以上版本

四、实验步骤

1. 创建空白新工程,向新工程中添加空文件

2. 编写代码

3. 编译、调试并运行

五、实验数据及处理结果

试验内容(1)的相关代码:

#include<iostream>

using namespace std;

void main()

{

int maximum(int a[],int n);

int a[10];

int j;

for(j=0;j<10;j++)

cin>>a[j];

cout<<”The maximum is ”<<maximum(a,10)<<endl;

}

int maximum(int a[],int n)

{

int max,i;

max=*a;

for(i=1;i<n;i++)

if(max<*(a+i)) max=*(a+i);

return max;

}

试验内容(2)的相关代码:
#include<iostream>

#include<cstring>

using namespace std;

void main()

{

int statistics(char a[],int m);

int n;

char str1[34];

cin>>str1;

n=strlen(str1);

cout<<statistics(str1,n)<<endl;

}

int statistics(char a[],int m)

{

int i ,countp=0;

for (i=0;i<m;i++)

if(*(a+i)>=’A'&&*(a+i)<=’Z')countp++;

return countp;

}

试验内容(3)的相关代码:

#include<iostream>

using namespace std;

void main()

{

char str[]=”Virtue and happiness are mother and daugher”;

int i;

for(i=0;str[i]!=’\0′;i++)

cout<<*(str+i)<<+”;

}

六、思考讨论题或体会或对改进实验的建议

Leave a Reply