现在的位置: 首页 > 综合 > 正文

15,2

2013年03月11日 ⁄ 综合 ⁄ 共 1646字 ⁄ 字号 评论关闭

 

第15周报告2 
实验目的:学会操作字符数组 
实验内容:完成对字符数组的操作  
   
* 程序头部注释开始 
* 程序的版权和版本声明部分 
* Copyright (c) 2011, 烟台大学计算机学院学生  
* All rights reserved. 
* 文件名称:                              
* 作    者:                            
* 完成日期:         2011年   12    月       
* 版 本 号:          
  
* 对任务及求解方法的描述部分 
* 输入描述:要处理的字符串在程序中通过赋初值完成  
* 问题描述:求出字符数组中字符的个数及计算句子中各字符出现的频数 
* 程序输出:字符数组中字符的个数和句子中各字符出现的频数 
* 程序头部的注释结束 
#include<iostream> 
using namespace std; 
//函数声明 
  
int main(void)  //不要对main函数有任何改动 
{ 
        char str[]="he threw three free throws"; 
        cout<<"要处理的字符串为:"<<str<<endl; 
        cout<<"字符串长度为:"<<length(str)<<endl;    
        cout<<"字符串中各字符出现的频数为:"; 
        output_frequency(str); 
        cout<<endl;  
        return 0; 
}运行结果:(贴图)15周任务2
 
 
经验积累: 注意变量的控制 
1.同一表达式在不同位置有不同的意义 
2.尽量用for循环 
3. 
 上机感言: 
#include<iostream>
using namespace std;
int length(char array[]);
void output_frequency(char array[]);
//函数声明

int main(void)  //不要对main函数有任何改动
{
 char str[]="he threw three free throws";
 cout<<"要处理的字符串为:"<<str<<endl;
 cout<<"字符串长度为:"<<length(str)<<endl;                                  //看看别人定义来吗
 cout<<"字符串中各字符出现的频数为:";
 output_frequency(str);
 cout<<endl;                                         
//自定义函数中无cout<<endl,只有主函数中有
 return 0;
                                                     
//while/do-while/for.
int length(char array[])
{
 int i=0,n=0;
 while(array[i]!='\0')
 {
  n=n+1;
  i++;
 }
 return n;
}

void output_frequency(char array[])
{
 int i=0,k=0,j=1,f;
 while(array[i]!=0)               //控制整个函数循环
 {
  while(k<i)
  {
   k=0;
   while (k<i)
   {
    if(array[k]==array[i])
    {
     i++;
     break;
    }
    k++;
   }
  }
     j=i+1;
        f=1;              //f 不能放前边,否则为全局变量,影响结果。
  while (array[j]!=0)
  {
   if(array[i]==array[j])
   {
    f=f+1;
   }
   
   j++;
  }
  
       cout<<array[i]<< "-"<<f<<" ";
       i++;

 

 

抱歉!评论已关闭.