注释的时候,才细心地看过代码|求本班最大姓C++源码

刚刚给别人写的注释,不晓得看不看得懂,汗啊!写程序自己懂是一回事 给别人解释比程序本身还费事!可是边写边注释又太不效率了。怎么办啊!

 

#include <iostream.h>
#include<string.h> // strncpy()这个函数需要的头文件

const int number = 10; //班级人数,测试的时候建议10个左右

 struct student { //结构体
  student(){ 
   count = 0;
  } 
  char name[3];//储存姓名
  int count; //计数出现次数
 };
void main(){
 char Name[number][7]; //储存姓名的数组
 int style=0; //非重名计数器
 cout<<"请输入"<<number<<"个学生的姓名[三个字一以内]"<<endl;
 for(int i=0;i<number;i++) {
  cin>>Name[i];  //一次FOR循环 完成所有 姓名的输入
  char p[3]; //p[0] p[1] p[2];
  strncpy(p,Name[i],2);//这个系统函数,自己看用法,就是简单的复制 
  //就是把前两位复制给p,得到名字中的姓!
  p[2]=0; //清空最后一个 因为前面2个存姓够了
  int j=i-1; 
  while(j>=0){
   char q[3];
   strncpy(q,Name[j],2);
   q[2]=0;  
   if(strcmp(p,q)==0) //比较相等时返回=0
    break; 
   j–;//否则J减一
  }
  if(j==-1)
  { 
   style++; //非重名就+1  最后得到不重名的名字总数!
  }
 }
 cout<<"不重名的名字总个数是:"<<style<<endl;
 student *p = new student[style];  //这里开始,统计
 for(int i=0;i<style;i++)
 { 
  for(int j=0;j<number;j++)  //冒泡法
  {  
   char temp[3];
   strncpy(temp,Name[j],2); 
   temp[2]=0;  //用临时变量 得到另一个数组,其中储存姓!
   int k=i-1;  //比较次数
   int flag=1;  //标志位
   while(k>=0) 
   {  
    if(strcmp(p[k].name,temp)==0)  //比较姓
     flag = 0;  //相同 标记为0;
    k–; 
   } 
   if(flag==0)  //标记为0就跳出 IF语句
    continue; 
   else   //标记不为零执行
   {  
    strcpy(p[i].name,temp); //把不重复的姓存起来!
   }
  }
  for(int k=0;k<number;k++)
  {  
   char q[3];
   strncpy(q,Name[k],2);
   q[2]=0; 
   if(strcmp(p[i].name,q)==0)
    p[i].count++;  //计算姓测重复次数
  }
 }
 for(int i=0;i<style;i++)
 { 
  cout<<"姓  "<<p[i].name<<"  的人有: "<<ends<<p[i].count<<" 人!"<<endl;
 }
 for(;;) int i=1;   //这句话最好去掉。为了留下输出结果做的 无限循环!
 delete []p;    //用玩删除
}
版权所有© HzlzH | 本文采用 BY-NC-SA 进行授权
转载需注明 转自: 《注释的时候,才细心地看过代码|求本班最大姓C++源码

相关文章

{ Leave a Reply ? }

  1. houkai China Internet Explorer Windows

    这是C编程

  2. 深度逻辑 China Internet Explorer Windows

    你写给谁看,看不懂我看他功力还不够。。。

  3. itql China Internet Explorer Windows

    用C开发个m8软件嘛! :mrgreen:

  4. Anonymous China 360 Browser Windows

    调不出来啊

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)