C++程序设计第二版(谭浩强)学习笔记
文章平均质量分 57
yanglize
这个作者很懒,什么都没留下…
展开
-
输出所有“水仙花数”
输出所有“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如,153是一个“水仙花数”,因为153=1^3+5^3+3^3。#include "stdafx.h"#include#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c,原创 2015-08-09 10:17:01 · 2118 阅读 · 0 评论 -
键盘输入三个数,输出其中最大的数
有三个整数a,b,c,由键盘输入,输出其中最大的数。#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c,max;cin>>a;cin>>b;cin>>c;if((a>b)&&(a>c)){max = a;原创 2015-08-06 16:46:14 · 11322 阅读 · 0 评论 -
用C++求两个数的最大公约数和最小公倍数
输入两个正整数m和n,求其最大公约数和最小公倍数#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int m,n,i,j,gys,gbs;cin>>m;cin>>n;//求最大公约数for(i=1;i{if((m%原创 2015-08-06 23:38:40 · 2313 阅读 · 0 评论 -
用getchar和putchar输入输出字符
用getchar函数读入两个字符给c1,c2,然后用putchar函数输出这两个字符。#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){ char c1,c1;c1 = getchar();fflush(stdin);c2原创 2015-08-06 16:06:51 · 5935 阅读 · 0 评论 -
求解函数
有一个函数: y = x (x 2x-1 (1 3x-11 (x>=10)编写一个程序,输入x,输出y的值。#include "stdafx.h"#includeusing namespace std;int _tma原创 2015-08-06 16:55:40 · 696 阅读 · 0 评论 -
不同存储类型的变量的声明、内部函数、外部函数
存储类型:1、用auto声明自动变量 例: int f(int a) { auto int b,c=3; …… } 函数调用时自动分配内存空间,函数调用结束时自动释放这些空原创 2015-08-19 21:58:22 · 1146 阅读 · 0 评论 -
统计字符串中不同类型字符的个数
输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int l=0,b=0,n=0,a=0;char c; //用来获取字符while((c = getch原创 2015-08-07 10:13:31 · 5452 阅读 · 0 评论 -
简单的冒泡排序
输入4个整数,要求按由小到大的顺序输出。#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c,d,t;coutcin>>a ;cin>>b ;cin>>c ;cin>>d ;if(a>b){t原创 2015-08-06 22:48:34 · 412 阅读 · 0 评论 -
用筛选法求100以内的素数(数组)
用筛选法求100以内的素数,要求使用数组。#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int a[100],i,b[100],j,n=0; //数组a[100]来放用于判断的数,数组b[100]来放素数for(i=0;i原创 2015-08-21 10:58:19 · 9687 阅读 · 1 评论 -
用选择法对10个整数按从小到大排序(数组)
用选择法对10个整数按从小到大排序,要求使用数组。算法思想:第一次选择:从10个整数中挑出其中最小的整数,拿出来;第二次选择:从余下的9个整数中挑出其中最小的整数,拿出来,放到上一次挑出的数的后面;第三次选择:从余下的8个整数中挑出其中最小的整数,拿出来,放到上一次挑出的数的后面;…………以此类推,直到全部挑完。#include "stdafx.h"原创 2015-08-21 11:57:58 · 36624 阅读 · 0 评论 -
输入10个学生的姓名、学号和成绩,将其中不及格者输出
输入10个学生的姓名、学号和成绩,将其中不及格者的姓名、学号和成绩输出#include "stdafx.h"#include#include using namespace std;string name[50]; //name定义为字符串数组,用来存放学生姓名int num[20],score[10];vo原创 2015-08-22 17:10:17 · 13417 阅读 · 0 评论 -
C++打印杨辉三角形
打印出以下的杨辉三角形(要求打印出10行)11 11 2 11 3 3 11 4 6 4 11 5 10 10 1…………要求打印出10行杨辉三角形:端点数为1,每个数等于它肩上两个数之和。#include "stdafx.h"#includeusing namespace原创 2015-08-22 15:05:38 · 19374 阅读 · 3 评论 -
编写程序,要求输入年月日,输出该日是本年中的第几天
定义一个结构体变量(包括年、月、日),编写程序,要求输入年、月、日,程序能计算并输出该日在本年中是第几天。注意闰年问题。#include "stdafx.h"#includeusing namespace std;struct NYR //定义结构体变量NYR{int nian;int yue;int ri;原创 2015-10-02 21:09:38 · 16775 阅读 · 0 评论 -
编写一个函数print,打印一个学生的成绩数组
编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括num(学号)、name(姓名)、score[3](3门课的成绩)。用主函数输入这些数据,用print函数输出这些数据。#include "stdafx.h"#include#includeusing namespace std;struct Student{in原创 2015-10-03 08:58:34 · 11771 阅读 · 0 评论 -
需要求三个长方体的体积,请编写一个基于对象的程序
需要求三个长方体的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。(1)由键盘分别输入3个长方体的长、宽、高。(2)计算长方体的体积。(2)输出三个长方体的体积。changfangxing.h 头文件:class Cfx{public:void set_V();int get_V();privat原创 2015-10-12 21:36:28 · 9613 阅读 · 0 评论 -
用递归算法求n阶勒让得多项式的值
用递归算法求n阶勒让得多项式的值,递归公式为:p(n)(x) = 1 (n=0) x原创 2015-08-18 15:45:54 · 5003 阅读 · 0 评论 -
用递归法将一个整数n转换成字符串。
用递归法将一个整数n转换成字符串。例如,输入483,应输出字符串“483”。n的位数不确定,可以是任意位数的整数。#include "stdafx.h"#includeusing namespace std;void s(int n){int i;if((i=n/10)!=0) s(i); //如果个位以上还有高位,则再次调用函数sput原创 2015-08-18 17:19:40 · 19845 阅读 · 1 评论 -
输入半径与高求圆周长、面积,圆球表面积、体积,圆柱表面积、体积
设圆的半径为r=1.5,圆柱高为h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用cin输入数据,输出计算结果,输出时要求有文字说明,取小数点后两位数字。请编写程序。#include "stdafx.h"#include#include#define PI 3.14using namespace std;int _tmain(int原创 2015-08-05 14:25:35 · 11735 阅读 · 0 评论 -
求1!+2!+3!+……+n!,n由键盘输入
求sum=1!+2!+3!+……n!,n由键盘输入#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int n,a=1;int sum=0;cin>>n;for(int i=1;i{a*=i;sum+=a;}cout原创 2015-08-07 22:39:52 · 5939 阅读 · 0 评论 -
求一个3*3矩阵对角线元素之和
求一个3*3矩阵对角线之和#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int array[3][3]; //定义一个3*3矩阵数组(这是一个二维数组)int i,j,sum=0;for(i=0;i{for(j原创 2015-08-22 13:41:32 · 5435 阅读 · 0 评论 -
将一个数组中的值按逆序重新存放
将一个数组中的值按逆序重新存放。例如,原来顺序为1,2,3,4,5,要求改为5,4,3,2,1。#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int a[5];int i;for(i=0;i{cin>>a[i];}for(原创 2015-08-22 15:27:17 · 6994 阅读 · 0 评论 -
C++猴子吃桃问题
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个;以此往后,到第十天早上想再吃时,就只剩一个桃子了。求第一天共摘了多少个桃子。#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[])原创 2015-08-09 10:49:28 · 16334 阅读 · 2 评论 -
const指针、void指针
const可以指定指针是一个常量,或者指定指针变量指向的对象是一个常量。有以下几种情况:1、指向常量的指针变量:const 类型名 *指针变量名;不允许通过指针变量改变它指向的对象的值。2、常指针类型名 *const 指针变量名;指针值不能改变;必须在定义时初始化,指定其指向;指针变量的指向不能改变,但其指向变量的值可以改变。3、指向常量原创 2015-08-24 15:47:20 · 479 阅读 · 0 评论 -
输入3个整数,按由小到大的顺序输出(使用指针)
输入三个数,要求使用指针#include "stdafx.h"#includeusing namespace std;int _tmain(int argc, _TCHAR* argv[]){int a,b,c,t;cin>>a>>b>>c;int *p1,*p2,*p3;p1=&a; //将变量a的地址存放到指针变量p1中原创 2015-08-24 16:24:01 · 17218 阅读 · 0 评论 -
编写一个判断素数的函数
写一个判断素数的函数,在主函数中输入一个整数,输出是否为素数的信息。#include "stdafx.h"#includeusing namespace std; bool sushu(int a) //sushu是bool型的函数,返回值只能是true或false{if(a{return false;}for(int i=2;i{if原创 2015-08-11 15:28:44 · 11500 阅读 · 0 评论 -
写一函数求sinh(x)的值
写一函数求sinh(x)的值,求sinh(x)的近似值,公式为:sinh=(e^x-e^-x)/2,其中用一个函数求e^x。#include "stdafx.h"#include#includeusing namespace std;double sinh(double a){double c;c=(a-(1/a))/2;return c;}原创 2015-08-11 16:22:19 · 3918 阅读 · 0 评论 -
C++函数的定义与调用
定义无参函数的一般形式:类型名 函数名(void){声明部分语句}定义有参函数的一般形式:类型名 函数名(形式参数列表){声明部分语句}写两个函数,分别要求两个整数的最大公约数和最小公倍数,用主函数调用两个函数,并输出结果,两个整数由键盘输入。#include "stdafx.h"#includeusing names原创 2015-08-11 13:55:09 · 1855 阅读 · 0 评论 -
写一个函数验证哥德巴赫猜想
写一个函数验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数的和。在主函数中输入一个不小于6的偶数n 然后调用函数gotbaha,在函数gotbaha中再调用函数prime,prime函数的作用是判别一个函数是否为素数。在函数中输出以下形式gotbaha的结果:34=3+31#include "stdafx.h"#includeusing namespace std原创 2015-08-11 22:24:57 · 10470 阅读 · 1 评论 -
输入10个数,将其中最小的数与第一个对换,最大的数与最后一个对换
输入10个整数,将其中最小的数与第一个数对换,将最大的数与最后一个数对换。写三个函数:1、输入10个整数 2、进行处理 3、输出10个数#include "stdafx.h"#includeusing namespace std;int array[10],*p;原创 2015-08-26 23:21:05 · 11968 阅读 · 2 评论 -
输入3个字符串,按字符串由短到长的顺序输出
输入3个字符串,按字符串由短到长的顺序输出,要求使用指针。#include "stdafx.h"#include#include#includeusing namespace std;char a[80],b[80],c[80];char *p1,*p2,*p3;//输入三个字符串,三个字符型指针分别三个字符串int _tmain(int a原创 2015-08-27 00:35:14 · 3502 阅读 · 0 评论 -
有n个整数,使前面各数向后移m个位置,最后m个数变成最前面m个数
有n个整数,使前面各数向后移m个位置,最后m个数变成最前面m个数。写一函数实现以上功能,在主函数中输入n个整数,并输出调整后的n个数(要求使用指针)。#include//#includeusing namespace std;int n,m;int array[1000];void input(){coutcin>>n;coutc原创 2015-08-27 23:05:10 · 2542 阅读 · 0 评论 -
用C++将China译成密码
要将“China"译成密码,密码的规律是:用原来的字母后面第四个字母代替原来的字母。请编写一程序,用赋初值的方法使c1,c2,c3,c4,c5这5个变量的值分别为'C',‘h','i','n','a',经过运算,使c1,c2,c3,c4,c5分别变为’G‘,’l‘,'m','r','e',并输出。China.cpp: #include "stdafx.h"#include ;u原创 2015-08-04 10:10:17 · 13935 阅读 · 0 评论 -
类声明和成员函数定义的分离
为了实现类的隐蔽性,对类成员函数的定义一般不放在头文件中,而放在另一个文件中,即类声明和函数定义是分别放在两个文件中的。实际上一个C++程序是由3个部分组成的:类声明头文件(后缀为.h);类实现文件(后缀为.cpp),它包括类成员函数的定义;类的使用文件(后缀为.cpp),即主函数文件。例如:找出一个整型数组中的元素的最大值,用多文件的程序来实现。那么,我们首先新建一个空项原创 2015-10-12 21:06:59 · 3550 阅读 · 0 评论