![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++经典代码
文章平均质量分 55
THISISPAN
这个作者很懒,什么都没留下…
展开
-
用switch-case语句实现两个数的简单四则运算
#include using namespace std;void main(){ double a ,b; char c; cout cin>>a>>c>>b; switch(c) { case '+': cout break; case '-': c原创 2012-04-11 16:07:19 · 13474 阅读 · 0 评论 -
C++统计正数数目和负数的数目,并计算平均值
编写一个程序,读入整数,数目未定,统计读入的正数数目和负数的数目,并计算平均值、如果读入的是0,程序终止。平均值以浮点数形式显示#include using namespace std;void main(){ int positiveNum = 0; int negativeNum = 0; int count = 0; d原创 2012-04-16 16:54:59 · 7985 阅读 · 1 评论 -
C++输出一个表格
编写一个程序,输出上述表格#include #include using namespace std;int main(){ cout cout cout int miles = 1; int kilometers = 20; for (int i = 1; i { cout c原创 2012-04-16 17:02:11 · 22903 阅读 · 0 评论 -
C++:使用循环输出各种图形
1) for (int x = 1;x { for (int y = 1;y { cout } cout }2) for(int i = 6;i>=1;i--) { for (int k = 1;k原创 2012-04-16 22:43:53 · 12298 阅读 · 0 评论 -
C++:编写函数,显示当前时间
#include #include #include using namespace std;/** Determine if it is a leap year */bool isLeapYear(int year){ if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) re原创 2012-04-16 22:56:51 · 2014 阅读 · 1 评论 -
C++:显示每年的各月份第一天是星期几
编写一个程序,提示用户输入一个年份以及这一年中第一天是星期几,然后,输出每个月的第一天是星期几。#include using namespace std;int main(){ cout int year; cin >> year; cout int firstDay; cin >> firstDay; int numb原创 2012-04-16 23:01:14 · 4930 阅读 · 0 评论 -
c++中qsort()排序函数的使用qsort函数应用大全
在c++中qsort()排序函数的使用qsort函数应用大全七种qsort排序方法 一、对int类型数组排序 int num[100]; Sample: int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; } qsort(num,100,sizeof(n转载 2012-04-16 23:13:30 · 1830 阅读 · 0 评论 -
C++打印数字金字塔
#include#include using namespace std;void main(){ cout int row; cin>>row; int i,j,k,h; for (i = 1;i { for (j = 1;j { cout原创 2012-04-16 15:01:54 · 16298 阅读 · 1 评论 -
C++:求一个整数的所有因子
#includevoid shunum(int a){ int i; for(i=2;i { if(a%i==0) { printf("%d ",i); shunum(a/i); break; } }}void main(){ int i,j,n; printf("input a number:"); sca原创 2012-04-16 17:42:05 · 21640 阅读 · 2 评论 -
C++:输出日历的算法
功能:只要输入四位数的年份和该年的元旦是星期几,就可以输出全年的日历。思路提示:(1)一年有12个月,所以,输出年历的算法是:int month=1;while (month{计算第month月的天数MAX_DAY;输出天数是MAX_DAY的第month月的年历;month++;}(2)根据大月、小月、闰年、平年,计算year第month月的天数MAX_DA原创 2012-04-16 23:06:10 · 7193 阅读 · 0 评论 -
C++:计算一个整数的数字之和
算法:可以使用%提取整数中的数字,用/将提取出的数字从整数中去掉。、比如,234%10 = 4,即可提取4,而用234/10则可以剔除4.用一个循环,反复提取并剔除数字,直至整数中所有数字都处理完毕#include using namespace std;int sum(int );void main(){ cout int num;原创 2012-04-17 23:04:00 · 6915 阅读 · 0 评论 -
c++:按逆序显示一个数
算法:可以使用%提取整数中的数字,用/将提取出的数字从整数中去掉。、比如,234%10 = 4,即可提取4,而用234/10则可以剔除4.用一个循环,反复提取并剔除数字,直至整数中所有数字都处理完毕#include using namespace std;void sum(int );void main(){ cout int num;原创 2012-04-17 23:10:17 · 1930 阅读 · 0 评论 -
c++:输出小于1000的所有孪生素数
如果两个素数之差为2,则称他们为孪生素数。如3和5是孪生素数,5和7是孪生素数#include #include #include using namespace std;bool isPrime(int num){ if ((num == 1) || (num == 2)) { return true; } for (int i原创 2012-04-17 23:35:02 · 10142 阅读 · 0 评论 -
c++排序函数详解
c++排序函数详解原文地址:http://www.cnblogs.com/A-Song/archive/2012/02/02/2336337.html首先看sort函数见下表:函数名功能描述sort对给定区间所有元素进行排序stable_sort对给定区间所有元素进行稳定排序 partial_sort对转载 2012-04-16 23:10:48 · 885 阅读 · 0 评论 -
随机生成指定的字符(定义一个简单但很实用的头文件)
定义一个头文件“getRandomCharacter”这是一个非常简单但很实用的头文件,是自己定义的,在今后的编程中,你可以利用这些函数#include using namespace std;char getRandomCharacter(char ch1,char ch2){ return static_cast(ch1+rand()%(ch2-ch1+1))原创 2012-04-17 15:49:51 · 1055 阅读 · 0 评论 -
C++:求10个数中最大的两个数
[C++] 求10个数中最大的两个数代码用了2次的if判断,第一次先求出最大的数。第二次则利用if语句剔除第一个最大的数,把剩余的数再次排序,于是最大的2个数就出来了。不过C++或者C#中有sort和arrays函数。也可以排序,而且代码也简介... 不过这样就失去了思考的意义了....?#include usingnamespacestd; int main()转载 2012-04-16 23:16:46 · 4393 阅读 · 0 评论 -
显示日历
#include #include using namespace std;int main(){ cout int year; cin >> year; cout int firstDay; cin >> firstDay; int numberOfDaysInMonth = 0; // Disp原创 2012-06-27 00:38:45 · 1294 阅读 · 0 评论 -
类
实验1:实验内容定义员工类Worker,其属性有工号id、姓名name、年龄age、性别gender、工资salary等;(1) 提供构造函数以不同方式构造对象,并调用成员函数输出各对象的所有信息;(2) 计算工资最高者、最低者以及工资平均值;(3) 比较男性员工和女性员工的平均工资。实验2实验内容编写程序,实现下列要求:(1) 定义人员类Person,其属性有(保护类原创 2012-06-27 01:21:03 · 1594 阅读 · 0 评论 -
C++输出1000之内的素数
素数:如果一个大于一的整数,只能被1及其自身整除,那么这个数为素数1.输出1000之内的素数#include#include bool isPrime(int);using namespace std;void main(){ int count = 0; for (int i = 2;i { if (isPrim原创 2012-04-16 15:37:35 · 12966 阅读 · 3 评论 -
2012.4.13笔记
1.计算程序的运行时间long startTime = time(0);//注意time(0)赋予的变量类型是long........long endTime = time(0);cout2.在进行两个数的交换函数中,引入的temp是个临时变量,函数输出不需要它,所以只需在函数内部声明即可,(不需要在外部声明)如: if (a {原创 2012-04-13 14:09:36 · 2004 阅读 · 0 评论 -
给定ASCII码,输出对应字符
ASCII码的范围是0-128一个强制转换,搞定问题#includeusing namespace std;int main(){ coutint i; cin>>i; if (i182) { cout return 0; } cout(i) return 0;原创 2012-04-10 20:17:53 · 7836 阅读 · 0 评论 -
一个数学学习工具(10之内的简单减法)
小学生减法联系工具,随机生成两个10之内的数字,进行相减,输出答案,如果答案正确,给出提示。#include#include #include using namespace std;void main(){ int a ,b; srand(time(0)); a = rand()%10; b = rand()%10; if原创 2012-04-11 15:48:53 · 864 阅读 · 0 评论 -
输入一个100--1000之内是数字,求出其各位数字相加之和
#includeusing namespace std;int sumNum(int);int main(){ cout int k; cin>>k; if (k1000) { cout return 0; } cout return 0;}int sumNum原创 2012-04-10 20:04:18 · 1813 阅读 · 0 评论 -
C++最简单的四舍五入!!(setprecision(n)方法)
setprecision(n)是流格式控制符之一,在iomanip头文件中。setprecision(n)指定一个浮点数的精度。1.)可以指定一个浮点数打印几位数字,其中n是总位数!!包括小数之前和之后的数字。超出的位数会被四舍五入进去!!!(setprecision(n)不是舍弃,在高精度向低精度转换时,超出部分会被舍弃,如一个double k =1.5,int i = k原创 2012-04-11 16:59:47 · 16320 阅读 · 1 评论 -
将三个数进行排序
int a,b,c;coutcin>>a>>b>>c;#include #include #include void sort(int,int,int);using namespace std;//注意,函数原型后面必须加双引号void main(){sort(a,b,c);}void sort(int a,int b,int c){原创 2012-04-12 23:13:10 · 663 阅读 · 0 评论 -
将小写字母转换为大写字母
大写字母在小写字母之前,所以小写字母的ASCII码要比大写字母的大,都大32.比如a-A=b-B=c-C=32;对于一个小写字母,要转换为大写字母,则公式为static_cast('A'+(ch-'a'))对于一个大写字母,要转换为小写字母,则公式为static_cast('a'+(ch-'A'))65---90为A--Z97---122为a--z#includ原创 2012-04-10 20:13:10 · 4240 阅读 · 0 评论 -
C++计算一元二次方程的跟
#includeiostream.h> //例2-4#includemath.h>voidmain(){double a, b, c, d, x1, x2, rp, ip ; cout cin >> a >> b >> c ; if ( fabs( a ))//<= 1e-8表示==0--浮点数的误差判断!!1 cout endl原创 2012-04-13 14:52:24 · 1778 阅读 · 0 评论 -
求一个月的天数
#include bool isLeapYear(int);int monthDays(bool,int);using namespace std;void main(){ cout int year,month; cin>>year>>month; bool leapYear = isLeapYear(year); in原创 2012-04-12 23:33:29 · 635 阅读 · 0 评论 -
随机数生成---加法学习工具
#include #include #include using namespace std;void main(){ srand(time(0)); int a = rand() % 100; int b = rand() % 100; int c = rand() % 100; cout int原创 2012-04-12 23:40:43 · 1096 阅读 · 0 评论 -
用switch语句,根据成绩登记打印成绩分数范围,允许使用大小写
原创 2012-04-13 15:00:25 · 2507 阅读 · 0 评论 -
枚举类型+switch语句的使用
原创 2012-04-13 15:07:31 · 1143 阅读 · 0 评论 -
C++计算最大公约数(辗转相除法)
典型例题:一.辗转相除法例1 。求两个正数8251和6105的最大公因数。(分析:辗转相除→余数为零→得到结果)解:8251=6105×1+2146显然8251与6105的最大公因数也必是2146的因数,同样6105与2146的公因数也必是8251的因数,所以8251与6105的最大公因数也是6105与2146的最大公因数。6105=2146×2+18132146=18原创 2012-04-13 15:54:41 · 31716 阅读 · 0 评论 -
C++利用技术求圆周率
原创 2012-04-13 16:11:26 · 2304 阅读 · 0 评论 -
改进的数学学习工具
生成10个问题,进行10之内的加减法,并记录正确的回答数目,和程序的运行时间#include#include #include using namespace std;void main(){ int i = 0; int rightNum = 0; long startTime = time(0); while (i {原创 2012-04-13 14:11:16 · 602 阅读 · 0 评论 -
大小写字母转换 (简化算法)
把输入字符转换为小写字母。对输入字符进行判断,如果是大写 字母,则转换为小写字母;否则,不转换。 #includeiostream.h>voidmain(){ char ch; cout ch= "; cin >>ch ; if ( ch >= 'A' &&ch<= 'Z' ) ch+= 32 ; cout原创 2012-04-13 14:37:22 · 1804 阅读 · 0 评论 -
C++输入若干个数(未知数量),计算平均值
原创 2012-04-13 15:34:07 · 11642 阅读 · 0 评论 -
C++进制转换程序(二进制转换为十进制,十进制转换为二进制)
1.十进制转换为二进制#includeusing namespace std;int main(){ int N; cout int aa[20]; cin>>N; int temp; temp = N; int num; int i=0; while(temp !=0) {原创 2012-04-13 17:38:41 · 40420 阅读 · 3 评论 -
打印不同的数(编写一个程序,使用指针访问数组方式,读入十个数,输出其中不同的数,即一个数如果出现多次,只打印一次)
打印不同的数(编写一个程序,使用指针访问数组方式,读入十个数,输出其中不同的数,即一个数如果出现多次,只打印一次)提示:读入一个数,如果未出现过,则将其存入数组,如果已在数组中,则丢弃。当输入完毕后,数组保存的就是不同的数。:#include using namespace std;int main(){ int numbers[10]; int原创 2012-06-27 21:35:32 · 7242 阅读 · 0 评论