C程序设计经典300例1of3.doc_第1页
C程序设计经典300例1of3.doc_第2页
C程序设计经典300例1of3.doc_第3页
C程序设计经典300例1of3.doc_第4页
C程序设计经典300例1of3.doc_第5页
已阅读5页,还剩62页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1*#include using namespace std;int main()int i = 1;/整型变量cout欢迎endl; /输出一个常量字符串coutaendl;/输出一个常量字符couti=iendl;/输出一个整型变量coutn;/输出一个表示换行的转义字符return 0;2*#include #include #include using namespace std;int main()int i;/基本整型short j;/短整型long k;/长整型float ii;/单精度型double jj;/双精度型long double kk;/扩展双精度型couti:(INT_MIN,INT_MAX)endl;coutj:(SHRT_MIN,SHRT_MAX)endl;coutk:(LONG_MIN,LONG_MAX)endl;coutii:(FLT_MIN,FLT_MAX)endl;coutjj:(DBL_MIN,DBL_MAX)endl;coutkk:(LDBL_MIN,LDBL_MAX)endl;return 0;3*#include using namespace std;int main()int i = 1;/基本整型short j = 2;/短整型long k = 3;/长整型float ii = 1.1;/单精度型double jj = 1.234;/双精度型long double kk = 1.2342546;/扩展双精度型couti=iendl;coutj=jendl;coutk=kendl;coutii=iiendl;coutjj=jjendl;coutkk=kkendl;return 0;4*#include using namespace std;int main()int i;/整型变量iint j;/整型变量jint temp;/临时变量i=5;j=10;couti=iendlj=jendl;/交换前/交换temp = i;i = j;j = temp;couti=iendlj=jendl;/交换后return 0;5*#include using namespace std;int main()float i;/浮点型数值-重量1int j;/整型数值-重量2int sum;/总重量i = 0.002;j = 2;sum = i+j;/计算总重量coutsum=sumendl;/输出return 0;6*#include using namespace std;int main()int Pool_volume;/游泳池容量int count = 10;/循环次数while(count!=0)cinPool_volume;/输入数据if(Pool_volume 100)/如果小于100升,池中水没溢出cout游泳池中水没溢出,可以继续加水endl;elsecout游泳池中水已满,必须停止加水或者加入另一个游泳池endl;count -;return 0;7*#include using namespace std;int main()int i = 1;/整型float j = 2.234;/浮点型i = (int)j;/显式类型转换cout赋值后的i值:iendl;8*#include using namespace std;int main()int a = 10;int b = 2;int c;c = a & b;/按位与cout对a和b进行按位与后的结果为:cendl;c = a | b;/按位或cout对a和b进行按位或后的结果为:cendl;c = a b;/按位异或cout对a和b进行按位异或后的结果为:cendl;c = a;/按位求反cout对a进行按位求反后的结果为:cendl;c = a1;/左移1位cout对a向左移动1位后的结果为:c1;/右移1位cout对a向右移动1位后的结果为:cendl;return 0;9*#include using namespace std;int main()float width;/房间宽度float length;/房间长度float area;/房间面积cinwidthlength;/依次输入宽度和长度area = width * length;/计算面积cout房间面积为:areaendl;return 0;10*#include using namespace std;int main()int apple_number = 60;/60个苹果int people = 12;/12个人int apple_per_people;/每个人分多少个苹果if(people != 0)/判断除数是否为0apple_per_people = apple_number/people;cout平均分配,每个人得apple_per_people个苹果endl;elsecout除数为0,出现异常endl;return 0;11*#include using namespace std;int main()int year;/年份cinyear;/输入年份if(year % 400 = 0)/如果能被400整除,则为闰年coutyear年是闰年endl;else/不能被400整除if(year % 4 = 0) & (year % 100 != 0)/判断闰年的另一个条件coutyear年是闰年endl;elsecoutyear年不是闰年endl;return 0;12*#include using namespace std;int main() int i =0; for(int rows = 0; rows 16; rows+) i = rows; while(i = 127) switch(i)case 7:/震铃couti=a | ;break;case 8:/退格couti=b | ;break;case 9:/水平制表符couti=t | ;break;case 10:/换行couti=n | ;break;case 11:/竖直制表符couti=v | ;break;case 12:/换页couti=f | ;break;case 13:/回车couti=r | ;break;default:couti=char(i) | ;break; i+=16;/每隔16个另起一列 coutendl; return 0;13*#include#includeusing namespace std;struct huffTreeint parent;/父亲int lchild;/左孩子int rchild;/右孩子int weight;/权重string flag;/标志;struct Lowest_Node/第0级节点的字符与频度char ch;int ch_num;/确定每个字符的huffman编码,输出参数为a、bvoid coding(int length, huffTree tree,int n,int &a,int &b)int i;int r,s;r=s=length;/节点个数最大不会超过字符串的长度for(i=0;in;i+)if(treei.weightr)&(treei.parent=-1)r=treei.weight;a=i;for(i=0;in;i+)if(treei.weights)&(i!=a)&(treei.parent=-1)s=treei.weight;b=i;/计算每个字符出现的频度并排序void frequency(string str) int length=str.length();/长度Lowest_Node *node=new Lowest_Nodelength;/声明最0级节点int i,j;/循环因子for(i=0;ilength;i+)nodei.ch_num=0;/初始化频度int char_type_num=0;/初始为0种字符for(i=0;ilength;i+)/循环整个字符串 for(j=0;j=a&nodej.ch=z&stri+32=nodej.ch)break;/该字符没有出现过,跳出循环if(j=A&stri=Z) nodej.ch=stri+32; else nodej.ch=stri; nodej.ch_num+; char_type_num+;/字符的种类数加1 /按频度从大到小排序for(i=0;ichar_type_num;i+)for(j=i;jchar_type_num;j+)if(nodej.ch_numnodej+1.ch_num)/如果前一个小于后一个,交换int temp;/临时频度char ch_temp;/临时字符temp=nodej.ch_num;ch_temp=nodej.ch;nodej.ch_num=nodej+1.ch_num;nodej.ch=nodej+1.ch;nodej+1.ch_num=temp;nodej+1.ch=ch_temp;for(i=0;ichar_type_num;i+)/打印字符频度cout字符nodei.ch出现了nodei.ch_num次endl;huffTree *huff=new huffTree2*char_type_num-1;/此变量的声明需位于确定char_type_num值后huffTree temp;string *code=new string2*char_type_num-1;/存放各个字符的编码for(i=0;i2*char_type_num-1;i+)/节点初始化huffi.lchild=-1;huffi.parent=-1;huffi.rchild=-1;huffi.flag=-1;for(j=0;jchar_type_num;j+)/将排序后的第0级节点权重赋给树节点huffj.weight=nodej.ch_num;int min1,min2;for(int k=char_type_num;k2*char_type_num-1;k+)/赋值0级之上的节点coding(length,huff,k,min1,min2);huffmin1.parent=k;huffmin2.parent=k;huffmin1.flag=0;huffmin2.flag=1;huffk.lchild=min1;huffk.rchild=min2;huffk.weight=huffmin1.weight+huffmin2.weight; for(i=0;ichar_type_num;i+)temp=huffi;while(1)codei=temp.flag+codei;temp=hufftemp.parent;if(temp.parent=-1)break;cout字符串的每个字符huffman编码为:endl;for(i=0;ichar_type_num;i+)coutnodei.ch codeiendl;cout整个字符串的huffman编码为:endl;for(i=0;ilength;i+)for(j=0;jchar_type_num;j+)if(stri=nodej.ch)coutcodej;/释放内存delete node;node=NULL;delete huff;huff=NULL;delete code;code=NULL; int main()int length=0;/字符串长度 string str; /目标字符串coutstr;frequency(str);/求各个元素的频度return 0;14*#include using namespace std;int level(char ch)switch(ch)case !:return 2;break;case *:return 3;break;case /:return 3;break;case %:return 3;break;case +:return 4;break;case -:return 4;break;case =:return 5;break;default:return -1;break;int main()char symbol3;int value3;cout从!、*、/、%、+、-、=运算符中选择3个参与算术运算symbol0symbol1symbol2;value0=level(symbol0);value1=level(symbol1);value2=level(symbol2);if(value0=-1) | (value1=-1) | (value2=-1)/只要有一个返回负数cout库中没有输入的运算符endl;else/从大到小排序for (int i = 0;i 3; i+)for (int j = i; jvaluej)/如果前一个大于后一个,开始交换/交换char temp;temp=symboli;symboli=symbolj;symbolj=temp;cout依次进入运算的顺序为:endl;for(int k=0;k3;k+)coutsymbolkendl;return 0;15*#include using namespace std;/称水果,四舍五入int main()float fruit_weight;/水果的重量int fruit_weight_cal;/被计算的水果重量cout输入所称水果的重量:fruit_weight;if(fruit_weight - (int)fruit_weight) 0.5)/舍去fruit_weight_cal = (int)fruit_weight;else/五入fruit_weight_cal = (int)fruit_weight + 1;cout一共有fruit_weight_cal斤水果需要被付钱endl;return 0;16*#include using namespace std;int main()float sum = 1000.0;/总额为1000元int male = 4;/4名男性员工int female = 4;/4名女性员工float ticket_value_male = 35.0;float ticket_value_female;ticket_value_female = ticket_value_male/2;float remain;/剩余多少钱int male_remain;remain = sum - (male * ticket_value_male + female * ticket_value_female);cout还剩remain元endl;male_remain = (int)(remain / ticket_value_male);cout剩余remain元还能让male_remain位男性看电影endl;return 0;17*#include using namespace std;int main()int apple_number = 60;/一共60个苹果int people = 7;/有7个小朋友int apple_remain;/平均分后还剩几个苹果if(people = 0)cout错误endl;elseapple_remain = apple_number % people;cout平均分配后还剩apple_remain个苹果endl;return 0;18*#include using namespace std;/*学校的图书管理系统一般有以下多个功能:借书、还书、图书分类管理、图书基本信息管理、当前图书状态查询等等*/int main()cout图书管理系统功能:endl;cout1-借书功能管理endl;cout2-还书功能管理endl;cout3-图书分类管理endl;cout4-图书基本信息管理endl;cout5-当前图书状态查询功能endl;cout0-退出endl;int status;int count = 10;while(count!=0)coutstatus;if(status = 0)/退出cout退出成功!endl;break;else if(status = 1)/借书cout进入借书功能管理模块!endl;else if(status = 2)/还书cout进入还书功能管理模块!endl;else if(status = 3)/图书分类管理cout进入图书分类管理模块!endl;else if(status = 4)/图书基本信息cout进入图书基本信息管理模块!endl;else/查询当前图书状态cout进入当前图书状态查询功能!endl;count-;return 0;19*#include #include using namespace std;#define Code 123456int main()coutinput_code;if(input_code = Code)/如果相同,则密码为真cout密码验证成功!endl;else/为假cout密码验证失败!endl;return 0;20*#include using namespace std;int main()int shu;/变量数字cout请输入一个任意的自然数:shu;/输入数字/求输入数字的所有真因子int *zhen_yinzi = new intshu;/不知道真因子有多少个,所以用指针指向int index = 0;/个数索引变量,初始化为0int sum = 0;/所有真因子的总和zhen_yinzi0 = 1;/所有数字都有一个真因子为1for (int i = 2; i shu; i+)/循环找所有因子if(shu % i = 0)/表示该因子为真因子index +;zhen_yinziindex = i;for(int j = 0; j = index; j+)/将所有真因子加和sum += zhen_yinzij;delete zhen_yinzi;/释放内存zhen_yinzi = NULL;if(sum = shu)/如果真因子之和等于数字本身,即为完数cout数字shu是完数endl;elsecout数字shu不是完数endl;return 0;21*#include using namespace std;int main()int a = 9;int result = 1;/结果for(int i = 1; i = 9; i+)/循环result *= a;cout9的9次方等于resultendl;return 0;22*#include using namespace std;#define Pi 3.1415#define code 1234int main()cout常量例子1:1endl;cout常量例子2:trueendl;cout常量例子3:3456endl;cout常量例子4:Piendl;cout常量例子5:codeendl;const double Pi1 = 3.1415;cout常量例子6:Pi1endl;return 0;23*#include using namespace std;double capital=0.0;/金额double cal(int num, double quantity)switch(num)case 0:return quantity*1.50;case 1:return quantity*1.70;case 2:return quantity*4.60;case 3:return quantity*0.90;case 4:return quantity*2.50;case 5:return quantity*3.70;case 6:return quantity*7.60;case 7:return quantity*3.90;int main()cout提示:本店提供8种水果,如下(左边为水果编号及名称,右边为其单价)endl;cout0西瓜 1.50, 1木瓜 1.70, 2哈密瓜 4.60, 3苹果0.9n 4梨 2.50, 5柚子 3.70, 6弥猴桃 7.60, 7山楂 3.9endl;int num_choice=0;/几种选择while(1)int Y_N;coutnum_choice;if(num_choice=0)/不买东西cout您确定只是看看吗?要不买点吧?继续请按1,否则请按0,直接退出Y_N;if(Y_N=0)cout退出成功!endl;break;else if(Y_N=1)elsecout不要捣乱,按规定输,亲endl;elsecout种类编号分别为:;int *choice=new intnum_choice;double *quantity_choice=new doublenum_choice;for(int i=0;ichoicei;/种类编号cout请输入对应购买种类的重量:;for(int i=0;iquantity_choicei;for(int j=0;jnum_choice;j+)/计算金额capital += cal(choicej, quantity_choicej);cout继续购物吗?继续请按1,否则请按0,开始结算Y_N;if(Y_N=0)cout开始结算,请付capital元endl;break;else if(Y_N=1)elsecout不要捣乱,按规定输,亲endl;delete choice;choice=NULL;delete quantity_choice;quantity_choice=NULL;return 0;24*#include using namespace std;int main()bool switcher=false;/开关状态bool switcher_ex=false;/前一时刻的开关状态char ch;while(1)cout是否要响应命令?是:输入Y,不是:输入N,退出:Qch;if(ch=Y)switcher_ex=switcher;switcher=true;if(switcher_ex)cout继续响应命令!endl;elsecout开始响应命令!endl;else if(ch=N)switcher_ex=switcher;switcher=false;if(switcher_ex)cout暂停命令响应!endl;elsecout继续等待命令响应!endl;else if(ch=Q)cout退出成功!endl;break;elsecout输入错误!endl;return 0;25*#include using namespace std;int main()int number;while(1)cout请输入一个整数,必须在1-50之间number;if(number=0)cout符合条件endl;break;return 0;26*#include using namespace std;int main()int quantity_goods=10000;/总共的物品数int number_staff=189;/员工数目int goods_remain;/剩余物品goods_remain = quantity_goods % number_staff;cout余下的物品数:goods_remainendl;return 0;27*#include using namespace std;int main()int a = 6;/一个整型变量int int_size;/整型变量的字节大小int_size = sizeof(a);cout变量a所占的字节大小为:int_sizeendl;return 0;28*#include using namespace std;/信号灯的状态时亮时灭,现计数,如果次数为偶则亮,如果次数为奇则不亮/需要注意+的位置int main()int count=1;cout灭endl;for(;count10;)int a;a = +count;if(a % 2 = 0)/偶数cout亮endl;elsecout灭endl;return 0;29*#include #include using namespace std;int main()cout请输入一串数字,不能有空格等非数字字符str;/输入cout数字0出现的次数为:;for(int i=0;istr.length();i+)/判断if(stri=0)/如果等于0count_0+;elsecoutcount_0endl;return 0;30*#include using namespace std;int main()int migong55=0,0,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,0,2;/迷宫int row,column;/行列int path_row25;/通行路径的行int path_column25;/通行路径的列for(int i=0;i25;i+)/初始化path_rowi=path_columni=0;row=0;column=0;int count=0;/次数do/按行循环,先处理,后判断column = 0;do/按列循环,先处理,后判断switch(migongrowcolumn)case 0:/可以通行path_rowcount=row;path_columncount=column;cout加油,快要找到出口了endl;count+;break;case 1:cout不可通行endl;break;cas

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论