×

c++代码大全及注解

c++代码大全及注解(c++用堆求中位数 求代码和注释!!)

admin admin 发表于2024-02-21 09:38:23 浏览33 评论0

抢沙发发表评论

大家好,如果您还对c++代码大全及注解不太了解,没有关系,今天就由本站为大家分享c++代码大全及注解的知识,包括c++用堆求中位数 求代码和注释!!的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!

本文目录

c++用堆求中位数 求代码和注释!!

用堆和快排都是O(nlgn),堆的常数还大。这种问题有平均O(n)算法,C++的话#include《algorithm》std::nth_element(Q,n/2+Q,n+Q);然后Q就是结果。nth_element和快排思想差不多,但是最差情况也会达到O(n2),虽然不太可能出现另外还有最差O(n)的算法,但是常数大很多,需要自己实现,称为BFPRT算法

注释一段代码 稍微详细点(很简单的) (c/c++)

看了你的代码,应该是杭电OJ上的2054这道题目,其实这倒题目没什么其他特别的地方,只需要注意几组特殊数据:0011 110.000100 0.000199.00 99下面解释下上面的代码:#include 《stdio.h》#include 《string.h》void A(char *s){ int len = strlen(s); char *p = s + len - 1; if (strchr(s, ’.’)) //如果字符串中存在小数点 { while (*p == ’0’) *p-- = 0; //如果*p是0,则将p指向的字符赋为结束 符,并将p前移一位。即消去后导0if (*p == ’.’) *p = 0; 如果全是0,即只剩下小数点的话,则输入数据位0 }}int main(void){ char *pa, *pb; char a; while (scanf("%s%s", &a, &b) != EOF) { pa = a; pb = b; while (*pa == ’0’) pa++; //消去输入字符串a的前导0 while (*pb == ’0’) pb++; //消去输入字符串b的前导0 A(pa); A(pb); puts(strcmp(pa, pb) ? "NO" : "YES"); } return 0;

【高悬赏】C++注释

#include 《iostream》#include《cstring》using namespace std;//两个头文件. const int SIZE = 100;//定义常量,整数SIZE,它的值为100. int main(){int n, i, sum, x, a;//定义整数n,i,sum,x.还有a数组,可以存放100个整数. cin》》n;//输入n. memset(a,0,sizeof(a));//这里缺了分号.意思是把a数组里所有的值赋初值为0. for (i= 1; i《= n; i++)//for循环,i=1执行一次下面的语句,i=2执行一次下面的语句,一直到i=n执行完后跳出. cin》》x;//这里缺了分号.意思是输入x. a++;//a数组中第(x-1)位加一. }//注:数组第一位是a(x属于0到size,闭区间)都可以存放一个整数. i =0;//赋值,i=0. sum =0;//赋值,sum=0. while (sum《(n/2+1)) {i++;sum +=a}//while循环语句,就是如果满足sum《(n/2+1)这一条件,则执行下面{}内语句,一直执行到不满足为止. cout《《i《《endl;//输出i,并且换行.return 0;//这里缺了分号.返回值为0.在这里表示程序结束. } 楼主 如果你对c++感兴趣,可以加我,我可以解释给你。QQ 821643032。注明你的百度ID。

求大神给这段c++程序代码做一下注释:每一句都要注释行,解释一下麻烦了

// 引入C++ io流 头文件#include《iostream》// 使用命名空间stdusing namespace std;// 定义类Aclass A{ // 公有public: // A 的构造函数。功能:设置私有变量x的值 A (int i) { x = i; } // 公有方法, 作用:在屏幕上打印x的值(后加逗号)void dispa () { cout 《《 x 《《 ","; }// 私有 private : // 私有成员变量x int x ;}; // 定义类B,继承于A,以公有方式继承class B : public A{ // 公有 public: // B 的构造函数,由于A没有默认构造函数,所以必须用A(i+10)这样的显示构造函数来初始化其子类,:是一种初始化符号B(int i) : A(i+10) //这里的“:”是什么意思,有什么作用? { x = i; } // 公有函数,作用:在屏幕上展示x,并换行 void dispb() { dispa(); cout 《《 x 《《 endl; }// 私有 private :// 私有成员x int x ;}; // 主函数void main() { // 用B的构造函数创建一个类型为B名称为b的变量B b(2); //这句是什么意思?// 调用实例b的成员函数来展示b成员的私有变量xb.dispb();}OVER

C++数据结构代码注释!

#include 《iostream.h》#include《stdlib.h》 #define MAX 100typedef int datatype; //定义datatype为int类型 typedef struct List //list结构存储一个elem数组,和一个last值 { datatype elem; //定义一个elems数组元素类型int int Last;}*SeqList; //定义指向list类型的指针类型 SeqList InitList() { SeqList L;//L为seqlist类型的指针 L=(SeqList)malloc(sizeof(List));//开辟空间,空间大小LIST类型大小 强制转换为SEQLIST类型的指针 L赋值为指向新空间的指针 L-》Last=-1;//为空间list中last赋值为-1 return L;//返回空间指针 }void CreateList(SeqList L) { int n; cout《《"请输入你要创建的顺序表元素个数n= "; cin》》n; cout《《"请输入你要创建的顺序表:"; for(int i=0;i《n;i++) { cin》》L-》elem成员赋值 L-》Last++; //last成员值加1 }}int Location(SeqList L,datatype x) { int i=0; while(L-》elem!=x&&i《=L-》Last) //逻辑与两个都必须为真才能继续L中的elem成员不等于x值,i也就是0《=L中last的成员值elem的个数 { i++; } if(i》L-》Last)//如果i》last成员值也就是总个数 函数返回-1否则返回i的值也就是在elem数组中的位置 return -1; else return i;}void Insertelem(SeqList L,datatype m) { int n; cout《《"请输入你要插入的位置n="; cin》》n;//在第n位置 if((L-》Last+1)》MAX)//L的成员Last+1》100也就是元素个数大于100 cout《《"表以满,能插入"《《endl; else { L-》Last++;//个数加1 for(int i=L-》Last;i》=n-1;i--) { L-》elem;//把n的位置的值向右移动 } L-》elem=m;//n位置的值赋予新的值 }}void Deleteelem(SeqList L,datatype m) { int i; i=Location(L,m);//m在L中elem的位置 while(i==-1)//i不为-1一直循环可以参考location,表示没找到 { datatype n; cout《《"你所查找的元素不在表中,请重新输入你要删除的元素"《《endl; cin》》n; i=Location(L,n); } for(int j=i;j《=L-》Last;j++) { L-》elem;//elem第i个的值赋值为i+1的值,也就是删掉i } L-》Last--;//元素个数减1 } void ShowList(SeqList L) { cout《《"当前顺序表元素为:"; for(int i=0;i《=L-》Last;i++) { cout《《L-》elem《《" ";//现实每个元素的值 } cout《《endl;}void main() { SeqList L;//指向list类型的指针 L=InitList();//初始化 CreateList(L);//进行赋值 int Opration; cout《《"输入操作(1)为删除某元素(2)为插入(3)为查找(4)为输出当前顺序表(5)为退出"《《endl; while(Opration!=5) { cin》》Opration;//输入的值不为5一直循环 if(Opration==1) { int n; cout《《"请输入你要删除的元素n="; cin》》n; Deleteelem(L,n); } if(Opration==2) { int n; cout《《"请输入你要插入的元素n="; cin》》n; Insertelem(L,n);//插入元素n值 } if(Opration==3) { datatype x; cout《《"请输入你要查找的元素x="; cin》》x; cout《《"此元素在顺序表中的位置为:"《《Location(L,x)+1《《endl;//返回的是elem中元素的位置,数组从0开始所以第几个要+1 } if(Opration==4) { ShowList(L);// 显示elem中的值 } if(Opration==5) { break; } }} /*1、先 InitList()初始化 2、CreateList(L)创建,要求输入elem元素个数,在为每个elem元素赋值 3,输出 "输入操作(1)为删除某元素(2)为插入(3)为查找(4)为输出当前顺序表(5)为退出"让用户选择, 不为5一直循环 选(1)"请输入你要删除的元素n=",要求输入删除的值n, Deleteelem(L,n)调用void Deleteelem(SeqList L,datatype m) m=n Deleteelem函数里先调用 Location函数 查询是否存在这个值如果存在返回他在elem中的位置 把elem当前位置的值向前进1位,last-1就是总个数减1 选(2)"请输入你要插入的元素n="要求输入一个要插入的值n Insertelem(L,n)调用void Insertelem(SeqList L,datatype m) m=n 然后要求输入插入位置 n如果插入后元素的个数小于100可以插入把插入的位置的值向右移动 选(3) "请输入你要查找的元素x="输入要查找的值,然后 Location(L,x)调用 int Location(SeqList L,datatype x) 计算值在elem中的索引,索引是从0开始的,所以第几个就要加1,因为没有第0个 选(4) ShowList(L)用循环显示elem中每个的值 选(5)break强制退出循环,结束程序

c++的代码注释!越详细越好

程序的功能是把一个手动输入的中缀表达式转化位后缀表达式并计算结果。#include 《stdlib.h》 #include《stdio.h》 #include《math.h》 #define max 100 char ex; void trans() /*手动输入中缀表达式,并转化为后缀表达式,主要用栈来处理*/ { char str; char stack; char ch; int sum,j,t,top=0; int i=0;/*计数器*/ printf("*****************************************\n"); printf("*说明:以 # 号为结束标志.\n"); printf("******************************************\n"); printf("表达示: "); /*输入中缀表达式,并放入str数组,输入字符个数不大于100*/ do { i++; scanf("%c",&str没有数据*/ if(i》=max) printf("表达式长度过长!"); }while(str!=’#’ && i!=max); sum=i; /*数组长度,即表达式长度*/ t=1; //把 i=1; /*把str数组的下标致1,回到字符头*/ ch=str;/*把输入的数组里第一个字符付给ch*/ i++; /*数组下标后移*/ while(ch!=’#’) /*字符数组的结束标志*/ { switch(ch) { case ’(’: top++; /*ch为(先处理带括号的表达式,并把左括号入栈stack=ch*/ stack=ch;break; case ’)’: while(stack的栈顶不是(,*/ { ex数组里*/ top--; /*已经放到ex下标-1*/ t++; /*数放到ex,下标+1*/ } top--;/*ch为),并且在栈stack下标-1*/ break; case ’+’: /*+-优先级相同,所以处理相同*/ case ’-’: while(top!=0&&stack非空。并且栈顶不是((也就是表达式里再没有括号),*/ { ex数组里*/ top--; /*已经放到ex下标-1*/ t++; /*数放到ex,下标+1*/ } top++; /*栈的下标后移*/ stack=ch*/ break; case ’*’: /*优先级相同,所以处理相同*/ case ’/’: while(stack是*号或者/号,*/ { ex; /*直接把*或者/入栈*/ top--; /*已经放到ex下标-1*/ t++; /*数放到ex,下标+1*/ } top++; /*栈的下标后移*/ stack=ch*/ break; case ’ ’: break; /*空格不处理*/ default: while(ch》=’0’&&ch《=’9’) { ex字*/ t++; ch=str; i++; } i--; /*字符数组后移,得到上一个数*/ ex=’&’; /*数字之间用&分开*/ t++; } ch=str; /*把得数给ch,继续判断*/ i++; } while(top!=0) /*栈里剩余的符号放入ex=*/ if(stack!=’(’) { /*括号已经先处理了,再有括号表达式就错了*/ ex; t++; top--; } else { printf("error"); top--; exit(0); } ex=’#’; /*结束标志*/ printf("\n原表达式是: "); for(j=1;j《sum;j++) printf("%c",str); printf("\n后缀表达式是: "); for(j=1;j《t;j++) printf("%c",ex); } /*用后缀表达式算法计算结果,因为后缀表达式的特性,你不用考虑运算顺序,再生成后缀表达式是已考虑*/ void compvalue() { float stack,d; char ch; int t=1,top=0; ch=ex; t++; while(ch!=’#’) { switch(ch) { /*遇到+号,从栈里弹出俩个数相加,并把相加结果入栈*/ case ’+’: stack;/*根据运算符,进行四则运算*/ top--;/*弹两个,入一个,只需-1*/ break; /*遇到-号,从栈里弹出俩个数相减,并把结果入栈*/ case ’-’: stack; top--;break; /*遇到*号,从栈里弹出俩个数相乘,并把结果入栈*/ case ’*’: stack; top--;break; /*遇到/号,从栈里弹出俩个数相除,并把结果入栈*/ case ’/’: if(stack!=0) /*被除数不可为0*/ stack; else { printf("\n\tchu0error!\n"); exit(0); } top--;break; default: d=0;/*判断是否为数字,是则将数字字符转化为对应的数值,并入栈*/ while(ch》=’0’&&ch《=’9’) { d=10*d+ch-’0’; ch=ex; t++; } top++; stack=d; } ch=ex; t++; } printf("\n计算结果为: %g\n",stack); } main() { trans(); compvalue(); }

C++算法题 聚会 要求代码和注释

类似与你的要求,不过输入格式跟你不太一样。

主要是我看不明白122145是什么意思

求C++ 的B树源代码 最好要有注释说明,让人能看明白的

英文注释,望能看看/*=========================== BinTreeh.h =========================*/#include 《stdio.h》/*=========================== data struct =====================*/typedef char ElementType;typedef struct Node{ ElementType data; struct Node *LChild; /* LeftChild */ struct Node *RChild; /* RightChild */ } TreeNode;/*=============================== function prounce ===============*/int CreateBinTree( TreeNode **rootp,ElementType **lp );void FreeTree( TreeNode *rootp );int DelTree( TreeNode *rootp,ElementType e );int Insert( TreeNode *rootp,ElementType Curr,ElementType e,int CurrPos );TreeNode *FindNode( TreeNode *rootp,ElementType e );TreeNode *FindParent( TreeNode *rootp,ElementType e );void Leaf( TreeNode *rootp,int *np );int Depth( TreeNode *rootp );void PreOrder( TreeNode *rootp );void MidOrder( TreeNode *rootp );void BackOrder( TreeNode *rootp );/*================================ function body ================*//*------------------------------------------------------ CreateTree -------*/int CreateBinTree( TreeNode **rootp,ElementType **lp ){ ElementType CurrElement; if(*lp==NULL) return 0; /* string isn’t exist */ if(**lp==0) return 1; /* empty string */ CurrElement=*(*lp); (*lp)++; if(CurrElement==’.’) { (*rootp)=NULL; return 1; } if(!((*rootp)=(TreeNode *) malloc(sizeof(TreeNode))) ) return 0; (*rootp)-》data=CurrElement; if(!CreateBinTree(&(*rootp)-》LChild,lp)) return 0; return CreateBinTree(&(*rootp)-》RChild,lp);}/*------------------------------------------------------- PreOrder --------*/void PreOrder( TreeNode *rootp ){ if(rootp==NULL) return; printf(" %c",rootp-》data); /* show parent */ if(rootp-》LChild!=NULL) PreOrder(rootp-》LChild); /* show LeftChild */ if(rootp-》RChild!=NULL) PreOrder(rootp-》RChild); /* show RightChild */ return;}/*-------------------------------------------------------- MidOrder --------*/void MidOrder( TreeNode *rootp ){ if(rootp==NULL) return; if(rootp-》LChild!=NULL) MidOrder(rootp-》LChild); printf(" %c",rootp-》data); if(rootp-》RChild!=NULL) MidOrder(rootp-》RChild); return;}/*------------------------------------------------------ BackOrder --------*/void BackOrder( TreeNode *rootp ){ if(rootp==NULL) return; if(rootp-》LChild!=NULL) BackOrder(rootp-》LChild); if(rootp-》RChild!=NULL) BackOrder(rootp-》RChild); printf(" %c",rootp-》data); return;}/*------------------------------------------------------- FindNode --------*/TreeNode *FindNode( TreeNode *rootp,ElementType e ){ TreeNode *temp; if(rootp==NULL) return NULL; /* if(!rootp) */ if(rootp-》data==e) return rootp; if(temp=FindNode(rootp-》LChild,e)) return temp; return FindNode(rootp-》RChild,e);}/*------------------------------------------------------- FreeTree --------*/void FreeTree( TreeNode *rootp ){ if(!rootp) return; FreeTree(rootp-》LChild); FreeTree(rootp-》RChild); free(rootp);}/*----------------------------------------------------- FindParent --------*/TreeNode *FindParent( TreeNode *rootp,ElementType e ){ TreeNode *temp; if((rootp==NULL)||(rootp-》LChild==NULL && rootp-》RChild==NULL)) return NULL; if((rootp-》LChild && rootp-》LChild-》data==e) ||(rootp-》RChild && rootp-》RChild-》data==e)) return rootp; temp=FindParent(rootp-》LChild,e); if(temp) return temp; return FindParent(rootp-》RChild,e);}/*-------------------------------------------------------- DelTree --------*/int DelTree( TreeNode *rootp,ElementType e ){ TreeNode *temp; temp=FindParent(rootp,e); if(!temp) return 0; if(temp-》LChild &&(temp-》LChild-》data==e)) { FreeTree(temp-》LChild); temp-》LChild=NULL; } else { FreeTree(temp-》RChild); temp-》RChild=NULL; } return 1;}/*-------------------------------------------------------- Insert --------*/int Insert( TreeNode *rootp,ElementType Curr,ElementType e,int CurrPos ){ TreeNode *parent,*new,*temp; if(!(parent=FindParent(rootp,Curr))) return 0; if(!(new=(TreeNode *) malloc(sizeof(TreeNode)))) return 0; new-》data=e; if(parent-》LChild-》data==Curr) { temp=parent-》LChild; parent-》LChild=new; } temp=parent-》RChild; parent-》RChild=new; if(CurrPos==0) new-》LChild=temp; new-》RChild=NULL; if(CurrPos==1) new-》RChild=temp; new-》LChild=NULL; return 1;}/*----------------------------------------------------------- Leaf --------*/void Leaf( TreeNode *rootp,int *np ){ if(rootp==NULL) return ; if(rootp-》LChild==NULL && rootp-》RChild==NULL) { (*np)++; return; } Leaf(rootp-》LChild,np); Leaf(rootp-》RChild,np);}/*---------------------------------------------------------- Depth --------*/int Depth( TreeNode *rootp ){ int ld,rd; /* ld: LeftChild’s depth */ /* rd: RightChild’s depth */ if(rootp==NULL) return 0; ld=Depth(rootp-》LChild); rd=Depth(rootp-》RChild); if(ld》rd) return ld+1; else return rd+1;}/*=========================== The end of BinTreeh.h ======================*/

请C++高手帮忙注释一下这段代码每行后面简单说明即可(急)尤其是指针的地方还有程序包含哪些知识点 可追分

//这是个school类的定义及实现,当中当然用到类的析构,构造等知识//学生信息输入查询和删除查找方面都要用到字符串比较函数,另外信息的永久保存要用到文件的相关操作,贯穿始终的则是单链表的创建,查询,插入,删除等知识.其中,student类未定义,程序某些地方还不完善class school //school类{public:school()//构造函数,定义时自动调用此函数{head=new student;head-》next=NULL;key=0;}~school()//析构函数,程序退出是调用此函数,释放一些申请过的空间{delete head;//删除头结点(只删除了一个节点,空间未释放完:)}void input();//录入函数声明void mend();//修改函数声明void del();//删除函数声明int find(student **p,int num,char *pn="^");//子查找函数声明void found();//查找函数声明void show();//显示函数声明void save();//保存函数声明void begin();void clear();char mainmenu();int getkey()//读取key{return key;}void setkey(int k)//设置key{key=k;}private:student *head;int key;};void school::input() //录入函数实现{student *p,*p2=NULL;p=head;//标记表头,方便下面插入int n;//此处n 初值要设置为1while(p-》next)p=p-》next;//在链表最后一个结点(空结点之前的)后面插入内容while(n){p2=new student;//申请一个结点p2-》input();//输入结点信息p-》next=p2;//插入到链表p2-》next=NULL;//将链表末节点置空,方便以后查找,以及结点的删除释放p=p-》next;//向下移动一个结点,(跳过刚刚插入的结点),以防下次继续插入school::setkey(1);cout《《"\t\t\t 按1继续添加,按0返回首页:";cin》》n;}}int school::find(student **p1,int num,char *pn) //子查找函数实现{student *p;p=head;//标记头结点while(p-》next)//下一结点不为空时{(*p1)=p;if((p-》next)-》getnumber()==num||!strcmp((p-》next)-》getname(),pn))//学号或者姓名相等则返回1(用到字符串比较函数)return 1;p=p-》next;}return 0;}void school::found() //查找函数实现{student *p;int num=-1,n=9;char name="^";do{cout《《"\t\t\t 1:按编号查找 2:按姓名查找:";cin》》n;}while(n《1||n》2);//不满足条件继续输入if(n==1){cout《《"输入学号:";//输入查找关键字cin》》num;}if(n==2){cout《《"输入姓名:";//输入要查找的学生姓名cin》》name;}if(!find(&p,num,name))//调用查找子函数{cout《《"找不到您要查询的内容!"《《endl;return;//未找到则返回,函数结束调用}(p-》next)-》output();//输出学生信息}void school::del() //删除函数实现{student *p,*p2;int num;cout《《"输入学号:";//按学号删除cin》》num;if(!find(&p,num,"^")){cout《《"找不到您要删除的内容!"《《endl;return;}(p-》next)-》output();p2=p-》next;p-》next=p2-》next;delete p2;school::setkey(1);}void school::show() //显示函数实现{student *p;p=head;while(p-》next){(p-》next)-》output();p=p-》next;//此处为显示所有学生的信息}}void school::mend() //修改函数实现{student *p;int num=-1,n;char name="^";do{cout《《"\t\t\t 1:按学号查找 2:按姓名查找:";cin》》n;}while(n《1||n》2);//不符合要求的输入要重新输入if(n==1){cout《《"输入学号:";//输入要修改的学号cin》》num;}if(n==2){cout《《"输入姓名:";//输入要修改的姓名cin》》name;}if(!find(&p,num,name))//结果未找到{cout《《"找不到你要修改的内容!"《《endl;return;}(p-》next)-》output();//输出修改前的信息(p-》next)-》input();//输入要修改的信息school::setkey(1);}void school::save() //保存函数实现{student *p;p=head;ofstream os("student.txt",ios::out);//打开名为student.txt的文本文件if(school::getkey()==1){while(p-》next){(p-》next)-》output(os);p=p-》next;}}//将信息写入到文件当中cout《《"\t\t\t文件已保存! "《《endl;//输出提示信息school::setkey(0);}void school::begin() //初始化函数{student *p,*p2;p=head;clear();long t;ifstream is("student.txt",ios::in); //创建文件if(!is){ofstream os("student.txt",ios::out);os.close();return ;}int num=-1;while(1)//此处文件操作方面不太清楚{num=-1;t=is.tellg();is》》num;is.seekg(t);if(num《0){is.close();return;}p2=new student;p2-》input(is);p-》next=p2;p2-》next=NULL;p=p-》next;}}

关于c++代码大全及注解到此分享完毕,希望能帮助到您。