CPP第三版课本课后题

2.8 将“China”译成密码

密码规律是:用原来的字母后面第4个字母代替原来的字母。

#include<iostream>
using namespace std;
int main()
{
	char c1='C',c2='h',c3='i',c4='n',c5='a';
	cout<<(c1+=4)<<(c2+=4)<<(c3+=4)<<(c4+=4)<<(c5+=4)<<endl;
	return 0;
	
}

3.2求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积

设圆半径r=1.5, 圆柱高h=3, 求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用cin输入数据,输出计算结果,输出时要求有文字说明,取小数点后两位数字。

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double r,h,cir_l,cir_s,cir_ss,cir_v,cir_hv;
	double pi=3.1415;
	
	cout<<"请输入r,h";
	
	cin>>r>>h;
	cir_l=2*pi*r;
	cir_s=pi*r*r;
	cir_ss=4*pi*r*r;
	cir_v=(4/3)*pi*r*r*r;
	cir_hv=pi*r*r*h;
	
	cout<<setiosflags(ios::fixed)<<setprecision(2);
	//如果setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。 
	
	cout<<"圆周长为"<<cir_l<<endl;
	cout<<"圆面积为"<<cir_s<<endl;
	cout<<"圆球表面积为"<<cir_ss<<endl;
	cout<<"圆球体积为"<<cir_v<<endl;
	cout<<"圆柱体积为"<<cir_hv<<endl;
	
	system("pause");
	return 0;
	
}

3.3 输出摄氏温度

输入一个华氏温度,要求输出摄氏温度。公式为C=5/9(F-32),输出要有文字说明,取两位小数。

#include<iostream>
#include<iomanip>

using namespace std;
int main()
{
	double f,c;
	cout<<"	请输入华氏温度";
	cin>>f;
	c=(5*(f-32))/9;
	cout<<"摄氏度为"<<setiosflags(ios::fixed)<<setprecision(2)<<c<<endl;
	system("pause");
	return 0;
	
}

3.9 输出最大数

有3个整数a,b,c,由键盘输入,输出其中最大的数。

#include<iostream>
using namespace std;
int main()
{
	int a ,b ,c ,max;
	cout<<"请输入a,b,c";
	cin>>a>>b>>c;
	max=a>b?a:b;
	max=max>c?max:c;
	cout<<"最大值为"<<max<<endl;
	system("pause");
	return 0;
	
}

3.10输出函数值

有一函数:

 写一程序,输入x,输出y值。

#include<iostream>
using namespace std;
int main()
{
	int x,y;
	cout<<"请输入x";
	cin>>x;
	if(x<1)y=x;
	else if(x>=1&&x<10)y=2*x-1;
	else if(x>=10)y=3*x-11;
	cout<<"y="<<y<<endl;
	system("pause");
	return 0;
	
	}

3.11 评分

给出一个百分制的成绩,要求输出成绩等级'A','B','C','D','E'。90分以上为'A',80~89分为'B',70~79分为'C',60~69分为'D',60分以下为'E'。

#include<iostream>
using namespace std;
int main()
{
	float score;
	int d;
	cout<<"请输入分数(不大于100)";
	cin>>score;
	
	d=score/10;
	
	switch(d)
	{
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:cout<<"分数为E";break; 
		case 6:cout<<"分数为D";break; 
		case 7:cout<<"分数为C";break; 
		case 8:cout<<"分数为B";break; 
		case 9:
		case 10:cout<<"分数为A";break; 
		default:cout<<"错误";break;
		//default顾名思义是缺省情况,只有任何条件都不匹配的情况下才会执行,所以应该将default语句放在所有case结束之后
		
	 } 
	 cout<<endl;
	 system("pause");
	 return 0;
}

3.12求几位数;打印数字;逆序打印数字

给出一个不多于5位的正整数,要求:1. 求出它是几位数;2. 分别打印出每一位数字;3. 按逆序打印出各位数字,例如原数位321,应输出123。

#include<iostream>
using namespace std;
int main()
{
	int num,i,j,n;
	cout<<"请输入数字:";
	cin>>num;
	while(num>99999)
	{
		cout<<"错误!请重新输入数字"<<"\n";
		cin>>num;
		
	}
	for(i=1,j=0;(num/i)!=0;i*=10,j++);
	cout<<num<<"是"<<j<<"位数\n";
	
	for(i=i/10,n=i,cout<<"数字为";i!=0;i/=10)
	cout<<(num/i)%10<<" ";
	cout<<endl;
	
	for(i=1,cout<<"逆序为";i<=n;i*=10)//!!!!
	cout<<(num%(i*10)/i);
	cout<<endl;
	
	system("pause");
	return 0;
	 
}

3.13根据利润提成计算奖金

企业发放的奖金根据利润提成。

利润I低于或等于100000元的,奖金可提0.1;

利润高于100000元,低于200000(100000<I<=200000)时,低于100000元的部分按10%提成,高于100000元的部分,可提成7.5%;

200000<I<=400000时,低于200000元的部分仍按上述办法提成(下同)。高于200000元的部分按5%提成;

400000<I<=600000元时,高于400000元的部分按3%提成;600000<I<=1000000时,高于600000元的部分按1.5%提成;

I>1000000时,超过1000000元的部分按1%提成。

从键盘输入当月利润I,求应发奖金总数。
 

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double i,bonus;
	cout<<"输入利润";
	cin>>i;
	
	while(i<0)
	{
		cout<<"错误!\n"<<"重新输入";
		cin>>i;
		 
	 } 
	
	if(i<=100000)
	bonus=i*0.1;
	
	else if (i>100000&&i<=200000)
	bonus=1000+(i-100000)*0.075;
	
	else if(i>200000&&i<=400000)
    bonus = 17500 + (i - 200000) * 0.05;
    
	else if (i > 400000 && i <= 600000)
    bonus = 27500 + (i - 400000) * 0.03;

    else if (i > 600000 && i <= 1000000)
    bonus = 39500 + (i - 600000) * 0.015;

    else
    bonus = 48500 + (i - 1000000) * 0.01;
    
    cout<<"奖金为"<<setiosflags(ios::fixed)<<setprecision(2)<<bonus<<endl;
    system("pause");
    return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int i, s;
    double bonus;
   
    cout<<"输入利润: ";
    cin>>i;
    
	while (i < 0){
        cout<<"错误\n"<<"重新输入利润: ";
        cin>>i;
    }
    
    if (i <= 100000)
        s = 1;      
    else if (i > 100000 && i <= 200000)
        s = 2;
    else if (i > 200000 && i <= 400000)
        s = 3;
    else if (i > 400000 && i <= 600000)
        s = 4;
    else if (i > 600000 && i <= 1000000)
        s = 5;
    else
        s = 6;
   
    switch(s)
	{
    case 1: bonus = i * 0.1; break;
    case 2: bonus = 10000 + (i - 100000) * 0.075; break;
    case 3: bonus = 17500 + (i - 200000) * 0.05; break;
    case 4: bonus = 27500 + (i - 400000) * 0.03; break;
    case 5: bonus = 39500 + (i - 600000) * 0.015; break;
    case 6: bonus = 48500 + (i - 1000000) * 0.01; break;
    }
    cout<<"奖金为"<<setiosflags(ios::fixed)<<setprecision(2)<<bonus<<endl;
    system("pause");
    return 0;
}

 3.14 排序输出

输入4个整数,要求按由小到大的顺序输出。

#include<iostream>
using namespace std;
int main()
{
	int a,b,c,d,temp;
	
	cout<<"请输入四个数字:";
	cin>>a>>b>>c>>d;
	
	if(b<a)
	temp=a,a=b,b=temp;
	
	if(c<a)
	temp=a,a=c,c=temp;
	
	if(d<a)
	temp=a,a=d,d=temp;
	
	if(c<b)
	temp=b,b=c,c=temp;
	
	if(d<b)
	temp=b,b=d,d=temp;
	
	if(d<c)
	temp=c,c=d,d=temp;
	
	cout<<"从小到大排序"<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
	system("pause");
	return 0;
	
	
}

 3.15 求最大公约数和最小公倍数

输入两个正整数m和n,求其最大公约数和最小公倍数。

#include<iostream>
using namespace std;
int main()
{
	int m,n,a,b,r,g;
	cout<<"请输入两个正整数";
	cin>>m>>n;
	
	for(a=m,b=n,r=m%n;r!=0;m=n,n=r,r=m%n);
	
	g=a*b/n;
	
	cout<<"最大公约数"<<n<<endl;
	cout<<"最小公倍数"<<g<<endl;
	
	system("pause");
	return 0;
	
}

3.16统计英文字母、空格、数字和其他字符的个数

输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。

#include<iostream>
using namespace std;
int main()
{
	char c;
	int letter,space,num,other;
	for(letter=0,space=0,num=0,other=0;(c=getchar())!='\n';)
	
	//while ((c=getchar ())!='n')的意思是:一直循环,等到用户输入回车为止,结束循环。 
	
	{
		if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))letter++;
		else if(c==' '||c=='\t')space++;
		else if(c>='0'&&c<='9')num++;
		else other++;
	}
	
	cout<<"英文字母个数为"<<letter<<"\n空格个数为"<<space<<"\n数字个数为"<<num<<"\n其他字符个数为"<<other<<"\n";
	system("pause");
	return 0;
}

 3.17 求Sn=a+aa+aaa+···+aa···a之值

其中a是一个数字。例如:2+22+222+2222+22222(此时n=5),n由键盘输入。

#include<iostream>
using namespace std;
int main()
{
	int a,n,sum,i,j,t;
	cout<<"输入a和n";
	cin>>a>>n;
	
	for(i=1,sum=0,t=a*i,j=0;j<n;sum+=t,i*=10,t=a*i+t,j++);
	
	cout<<"和为"<<sum<<endl;
	system("pause");
	return 0; 
}

3.18求1!+2!+3!+4!+···+20!)。

#include<iostream>
using namespace std;
int main()
{
	int i,j,k,sum;
	for(i=1,sum=0;i<=20;i++)
	{
		for(j=i,k=1;j>=1;k*=j--);
		sum+=k;
	}
	cout<<"和为"<<sum<<endl;
	system("pause");
	return 0; 
}

3.19 输出所有的“水仙花数”

所谓“水仙花数”是指一个3位数,其各位数字的立方之和等于该数本身。例如,153是一水仙花数,因为153=1^3+5^3+3^3。

#include<iostream>
using namespace std;
int main()
{
	int i,j,k,pro,sum;
	for(i=100;i<=999;i++)
	{
		for(j=100,sum=0;j>=1;j/=10)
		{
			for(k=0,pro=1;k<3;pro*=(i/j%10),k++);
			sum+=pro;
		}
		if(sum==i) cout<<sum<<' ';
		
		else continue;
	}
	cout<<endl;
	system("pause");
	return 0; 
}

3.20找出1000之内的所有完数,并输出其因子

一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如,6的因子为1,2,3,而6=1+2+3,因此6是“完数”。编程序找出1000之内的所有完数,并按下面格式输出其因子:6,its factors are 1, 2, 3

#include<iostream>
using namespace std;
int main()
{
	int i,j,sum;
	for(i=1;i<=1000;i++)
	{
		for(j=1,sum=0;j<i;i%j==0?sum+=j++:j++);
		//?sum+=j++等价于sum+=j,j++
		//Condition ? X : Y	条件运算符。如果 Condition 为真 ? 则值为 X : 否则值为 Y。 
		if(sum==i)
		{
			cout<<i<<','<<"its factors are ";
			for(j=1;j<i;i%j==0?cout<<j<<",",j++:j++);
			cout<<"\b"<<' '<<endl;
			// \b是退格的意思 
			//可以换成\n对比两者的区别 
		}
	}
	system("pause");
	return 0;
}

6,its factors are 1,2,3
28,its factors are 1,2,4,7,14
496,its factors are 1,2,4,8,16,31,62,124,248

3.21求数列和

 求出这个数列的前20项之和。

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double i,j,k,n,sum;
	
	for(i=1.0,j=2.0,sum=0.0,n=0;n<20;sum+=j/i,k=i,i=j,j=i+k,n++);
	//j是分子,i是分母
	//之后的分子是之前的分子+分母
	//之后的分母是之前的分子 
	cout<<"和为"<<sum<<endl;
	system("pause");
	return 0; 
}

3.22猴子吃桃问题

猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半另加一个。到第10天早上想再吃时,就只剩一个桃子了。求第一天共摘了多少桃子。

//迭代法求根号 
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int a;
	double x1,x2;
	cout<<"请输入a";
	cin>>a;
	
	for(x1=1,x2=(x1+a/x1)/2;fabs(x2-x1)>1e-5;x1=x2,x2=(x1+a/x1)/2);
	//fabs求绝对值
	//10^(-5)表示为1e-5 
	cout<<"根号a为"<<x2<<endl;
	system("pause");
	return 0;
	//C++中使用cout输出时,double类型是默认显示6位有效数字。比如这个代码输入a=5,输出为2.23607
    //在C语言中,使用printf输出时,取的是double的6位小数。
}

3.23用迭代法求x=根号a

求平方根的迭代公式为

要求前后两次求出的x的差的绝对值小于10的-5次。

//迭代法求根号 
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int a;
	double x1,x2;
	cout<<"请输入a";
	cin>>a;
	
	for(x1=1,x2=(x1+a/x1)/2;fabs(x2-x1)>1e-5;x1=x2,x2=(x1+a/x1)/2);
	//fabs求绝对值
	//10^(-5)表示为1e-5 
	cout<<"根号a为"<<x2<<endl;
	system("pause");
	return 0;
	//C++中使用cout输出时,double类型是默认显示6位有效数字。比如这个代码输入a=5,输出为2.23607
    //在C语言中,使用printf输出时,取的是double的6位小数。
}

3.24 输出以下图案:

*

* * *

* * * * *

* * * * * * *

* * * * *

* * *

*

#include<iostream>
using namespace std;
int main()
{
	int i,j;
	for(i=1;i<=4;cout<<endl,i++)
	for(j=0;j<2*i-1;cout<<'*',j++);
	
	for(i=3;i>=1;cout<<endl,i--)
	for(j=0;j<2*i-1;cout<<'*',j++);
	system("pause");
	return 0;
}

3.25 找赛手

两个乒乓球队进行比赛,各出3人。甲队为A、B、C3人,乙队为X、Y、Z3人。已抽签决定比赛名单。有人向队员打听比赛的名单,A说他不和X比,C说他不和X、Z比,请编程序找出3对赛手的名单。

#include<iostream>
using namespace std;
int main()
{
	char i,j ,k;
	for(i='X';i<='Z';i++)
	for(j='X';j<='Z';j++)
	for(k='X';k<='Z';k++)
	
	if ((i!=j&&i!=k&&j!=k)&&i!='X'&&k!='X'&&k!='Z')
    cout<<"A->"<<i<<'\n'<<"B->"<<j<<'\n'<<"C->"<<k<<endl;
    system("pause");
    return 0;
}

4.2 求方程ax^2+bx+c=0的根

用3个函数分别求当b^2-4ac大于0、等于0和小于0时的根,并输出结果。从主函数输入a, b, c的值。

#include<iostream>
#include<iomanip>
//这个头文件是声明一些 “流操作符”的,当你要用到那些“流操作符”时,就应该包含此头文件。
//所谓的“流操作符”有很多,比较常用的有:
//setw(int);//设置显示宽度。
//left//right//设置左右对齐。
//setprecision(int);//设置浮点数的精确度。
#include<cmath>
using namespace std;
void Greater(double m,double n,double p,double q);
void Equal(double m,double n);
void Less();
//之所以首字母都大写是为了避免和库函数的函数名重名 
//	[Error] reference to 'greater' is ambiguous 
int main()
{
	int a ,b ,c;
	cout<<"输入a,b,c";
	cin>>a>>b>>c;
	double d;
	d=b*b-4*a*c;
	
	if(d>0) Greater(a,b,c,d);
	else if(d==0) Equal(a,b);
	else Less();
	system("pause");
	return 0;

}

void Greater(double m,double n,double p,double q)
{
	double x1,x2,delta;
	delta=sqrt(q);
	x1=((-1)*n+delta)/(2*m);
	x2=((-1)*n-delta)/(2*m);
	
	cout<<setiosflags(ios::fixed)<<setprecision(4);
	cout<<"x1="<<x1<<endl;
	cout<<"x2="<<x2<<endl;
	
}

void Equal(double m,double n)
{
	double x;
	x=((-1)*n)/(2*m);
	cout<<setiosflags(ios::fixed)<<setprecision(4);
	cout<<"x="<<x<<endl;
	
}
void Less()
{
	cout<<"没有结果"<<endl;
	 
}

4.3 判别素数的函数

#include<iostream>
using namespace std;
void prime(int n);
int main()
{
	int num;
	cout<<"输入数字";
	cin>>num;
	prime(num);
	system("pause");
	return 0;
	
}

void prime(int n)
{
	int i;
	for(i=2;i<n&&(n%i!=0);i++);
	cout<<n;
	i==n?cout<<"是素数":cout<<"不是素数";
	cout<<endl;
}

4.4 求a!+b!+c!的值

用一个函数fac(n)求n! 。a,b,c的值由主函数输入,最终得到的值在主函数中输出。

#include<iostream>
using namespace std;
int fac(int n);
int main()
{
	int a,b,c;
	cout<<"输入a,b,c";
	cin>>a>>b>>c;
	cout<<"结果为"<<fac(a)+fac(b)+fac(c)<<endl;
	system("pause");
	return 0; 
}

int fac(int n)
{
	return n==1||n==0?1:n*fac(n-1);
	//计算n!的方法 
}

4.5写一函数求sinh(x)的值

#include<iostream>
#include<iomanip>
//声明一些流操作符,如sew(int),setprecision 
#include<cmath>
//使用exp函数时,需要头文件#include<cmath> 
using namespace std;
double sinh(int x);

int main()
{
	double num;
	cout<<"请输入x";
	cin>>num;
	cout<<setiosflags(ios::scientific)<<setprecision(4);
	//setiosflags(ios::scientific)用指数方式表示实数 
	cout<<"结果是"<<sinh(num)<<endl;
	system("pause");
	return 0;
}


double sinh(int x)
{
	return(exp(x)-1/exp(x))/2;
}

//输入4,输出2.7290e+001表示2.7290×10^1 

setiosflags的用法

1.ios::fixed

以小数点形式表示浮点数,并且在允许的精度范围内尽可能把数字移向小数点右侧

2.ios::right

在指定区域内右对齐输出

3.setprecision(2)

小数点右边数字个数为2

4.ios::scientific

指数表示

5.setfill(c)

设填充字符为c

6.setw(n)

设域宽为n个字符,保证输出宽度为n


没搞懂为什么expor可以表示e^x

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double sinh(int x);
double expor(double x);
int main()
{
    double num;
    cout<<"Please enter x: ";
    cin>>num;
    cout<<setiosflags(ios::scientific)<<setprecision(4);
    cout<<"Result: "<<sinh(num)<<endl;
    system("pause");
    return 0;
}
double expor(double x)
{
    return pow(10, x);
}
double sinh(int x)
{
    return (expor(x)-1/expor(x))/2;
}

4.6 用牛顿迭代法求根。方程为ax^3+bx^2+cx+d=0,系数a,b,c,d的值依次为1,2,3,4,由主函数输入。求x在1附近的一个实根。求出根后由主函数输出。

 4.7 写一个函数验证哥德巴赫猜想

一个不小于6的偶数可以表示为两个素数之和,如6=3+3,8=3+5,10=3+7···在主函数中输入一个不小于6的偶数n,然后调用函数gobaha函数中再调用prime函数,prime函数的作用是判别一个数是否为素数。在gobaha函数中输出以下形式的结果:34=3+31。
 

4.8计算该日是该年的第几天

给出年、月、日,计算该日是该年的第几天。

#include<iostream>
using namespace std;
int date(int y,int m,int d);
int month(int y,int m);
int main()
{
	int year,month,day;
	cout<<"输入年月日";
	cin>>year>>month>>day;
	for(;year<0||(month<1||month>12)||(day<1||day>31);)
	{
		cout<<"错误!重新输入";
		cin>>year>>month>>day;
		
	 } 
	 
	 cout<<"这一天是这年的第"<<date(year,month,day)<<"天"<<endl;
	 system("pause");//不要把system写成systeam 
	 return 0;
 
}

int month(int y,int m) 
{
	int mon;
	switch(m)
	{
		case 1:mon=31;break;
		case 2:y%4==0&&y%100!=0||y%400==0?mon=29:mon=28;break;
		case 3:mon=31;break;
		case 4:mon=30;break;
		case 5:mon=31;break;
		case 6:mon=30;break;
		case 7:mon=31;break;
		case 8:mon=31;break;
		case 9:mon=30;break;
		case 10:mon=31;break;
		case 11:mon=30;break;
		case 12:mon=31;break;
		
		default: break;
	
	}
	
	return mon;
	
}

int date(int y,int m,int d)
{
	int i,sum;
	for(i=1,sum=0;i<m;sum+=month(y,i),i++);
	return sum+d;
}

4.9递归方法求n阶勒让德多项式

Pn(x)   = 1     (n=0)

           = x     (n=1)

           = ((2n-1)*x- Pn-1(x) - (n-1) Pn-2(x) /n        (n>=1)

4.11 用递归法将一个整数n转换成字符串

例如,输入483,应输出字符串“483”。n的位数不确定,可以是任意位数的整数。

4.12 用递归方法求 f(x)=∑i=1ni2f(x)=∑i=1ni2

n的值由主函数输入。

4.13用带实参的宏名来求面积area

三角形的面积为


a,b,c为三角形的三边。定义两个带参数的宏,一个用来求s,另一个用来求area。写程序,在程序中用带实参的宏名来求面积area。

#include<iostream>
#include<iomanip>
//
#include<cmath>
#define s(a,b,c) (a+b+c)/2
#define area(a,b,c,s) s*(s-a)*(s-b)*(s-c)

using namespace std;
int main()
{
	double a,b,c;
	double ss,aa;
	cout<<"请输入三角形三个边长";
	cin>>a>>b>>c;
	
	for(;(a+b)<=c||(a+c)<=b||(b+c)<=a;)
	//没有初始语句 
	//有判断条件 
	//没有末尾循环体 
	{
		cout<<"错误!重新输入";
		cin>>a>>b>>c;
		
	}
	//{}里是中间循环体 
	ss=s(a,b,c);
	aa=sqrt(area(a,b,c,ss));
	cout<<setiosflags(ios::fixed)<<setprecision(4);
	
	cout<<"S="<<aa<<endl;
	system("pause");
	return 0; 
 } 

4.15输出电报

用条件编译方法实现以下功能:输入一行电报文字,可以任选两种输出,一为原文输出;一为将字母变成其下一字母。用#define命令来控制是否要译成密码。

用条件编译方法实现以下功能:输入一行电报文字,可以任选两种输出,一为原文输出;一为将字母变成其下一字母。(如’a’变成’b’······‘z’变成’a’。其他字符不变)。用#define命令来控制是否要译成密码。例如:#define CHANGE 1则输出密码。若#define CHANGE 0 则不译成密码,按原码输出。
 

#include<iostream>
using namespace std;
#define CHANGE 1
int main()
{
	char c;
	cout<<"输入电报";
	for(;(c=getchar())!='\n';)
	{
		#ifdef CHANGE
		if(c!='z')
		{
			c=c+1;
			cout<<c;
		}
		else cout<<'a';
		
		#else
		cout<<c;
		#endif
	}
	//#ifdef,#else,#endif都是预处理命令
	 
	cout<<endl;
	system("pause");
	return 0;
}

5.1筛选法求100以内素数

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int n[100], i, j, c=0;
    for (i=0; i<100; n[i++]=i+1);
    for (i=0; i<100; i++){
        for(j=2; j<n[i]&&(n[i]%j!=0); j++);
        if (j==n[i]){
            c++;
            cout<<setw(4)<<n[i]<<' ';
   
        }
    }
    system("pause");
    return 0;
}

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 请按任意键继续. . .

要想看起来更好看

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int n[100], i, j, c=0;
    for (i=0; i<100; n[i++]=i+1);
    for (i=0; i<100; i++){
        for(j=2; j<n[i]&&(n[i]%j!=0); j++);
        if (j==n[i]){
            c++;
            cout<<setw(4)<<n[i]<<' ';
            if (c%5==0) cout<<endl;
        }
    }
    system("pause");
    return 0;
}

   2    3    5    7   11
  13   17   19   23   29
  31   37   41   43   47
  53   59   61   67   71
  73   79   83   89   97
 101 请按任意键继续. . .

5.2选择法对10个整数排序

#include <iostream>
using namespace std;
int main()
{
    int n[10], i, j, temp;
    cout<<"请输入十个数字: ";
    for (i=0; i<10; cin>>n[i++]);//输入数字 
    
	for (i=0; i<10; i++)
        for (j=i+1; j<10; n[i]>n[j] ? temp=n[i], n[i]=n[j], n[j]=temp, j++ : j++);
   
    cout<<"从小到大排序为: "<<n[i++]<<' ';
    cout<<endl;
    system("pause");
    return 0;
}

以上是错的输不出正确的结果,因为最后输出的代码错误

正确的应该为:

#include <iostream>
using namespace std;
int main()
{
    int n[10], i, j, temp;
    cout<<"请输入十个数字: ";
    for (i=0; i<10; cin>>n[i++]);//输入数字 
    
	for (i=0; i<10; i++)
        for (j=i+1; j<10; n[i]>n[j] ? temp=n[i], n[i]=n[j], n[j]=temp, j++ : j++);
   
    for (i=0, cout<<"从小到大排序为: "; i<10; cout<<n[i++]<<' ');
    cout<<endl;
    system("pause");
    return 0;
}

5.3求3*3矩阵对角线元素之和

#include <iostream>
using namespace std;
int main()
{
    int n[3][3], i, j, sum;
    for (i=0; i<3; i++)
	{
        cout<<"输入第 "<<i+1<<" 排的数字(3个): ";
        for (j=0; j<3; cin>>n[i][j++]);
    }
    for (i=0, sum=0; i<3; i++)
        for (j=0; j<3; (i==j)||((i+j)==2) ? sum+=n[i][j],j++ : j++);
          //或者直接sum+=n[i][j++] 
    
	cout<<"对角线元素之和为"<<sum<<endl;
    system("pause");
    return 0;
}

5.4有一个已经排好序的数组,输入一个数,按照之前的排序规律将其插入

5.5数组中的值按逆序重新存放

将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,5,4,1。要求改为1,4,5,6,8。

#include<iostream>
using namespace std;
int main()
{
	int n[5]={8,6,5,4,1};
	int i,j,temp;
	for(i=0,cout<<"原数组为";i<5;cout<<n[i++]<<" ");
	for(i=0,j=4;i<5/2;temp=n[i],n[i]=n[j],n[j]=temp,i++,j--);
	for(i=0,cout<<"\n新数组为";i<5;cout<<n[i++]<<' ');
	cout<<endl;
	system("pause");
	return 0;
	
}

5.6杨辉三角形

要求打印出10行

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

···

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int n[10][10],i,j;
	for(i=0;i<10;i++)
	for(j=0;j<10;j++)
	{
		if(j==0||i==j)n[i][j]=1;
		else if(i<j) n[i][j]=0;
		else n[i][j]=n[i-1][j-1]+n[i-1][j];
		
	}
	for(i=0;i<10;cout<<endl,i++)
	for(j=0;j<10;n[i][j]!=0?cout<<setw(4)<<n[i][j],j++:j++);
	//setw(4)表示 输出字段为4 
	system("pause");
	return 0;
	
}

   1
   1   1
   1   2   1
   1   3   3   1
   1   4   6   4   1
   1   5  10  10   5   1
   1   6  15  20  15   6   1
   1   7  21  35  35  21   7   1
   1   8  28  56  70  56  28   8   1
   1   9  36  84 126 126  84  36   9   1

如果不输cout<<endl;

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int n[10][10],i,j;
	for(i=0;i<10;i++)
	for(j=0;j<10;j++)
	{
		if(j==0||i==j)n[i][j]=1;
		else if(i<j) n[i][j]=0;
		else n[i][j]=n[i-1][j-1]+n[i-1][j];
		
	}
	for(i=0;i<10;i++)
	for(j=0;j<10;n[i][j]!=0?cout<<setw(4)<<n[i][j],j++:j++);
	//setw(4)表示 输出字段为4 
	system("pause");
	return 0;
	

  1   1   1   1   2   1   1   3   3   1   1   4   6   4   1   1   5  10  10   5   1   1   6  15  20  15   6   1   1   7  21  35  35  21   7   1   1   8  28  56  70  56  28   8   1   1   9  36  84 126 126  84  36   9   1请按任意键继续. . . 

 5.7找出二维数组的鞍点

即该位置上元素在该行最大,该列最小

5.8 有15个数按由大到小的顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则打印出“无此数”。

5.9 有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。

5.10 打印一下图案

* * * * *

   * * * * *

      * * * * *

         * * * * *

            * * * * *

 5.11 有一行电文,已按下面规律译成密码:A-Z a-z即第一个字母变成第26个字母,第i个字母变成第(26-i+1)个字母。非字母字符不变。要求编程序将密码译回原文,并输出密码和原文。

5.12 编写一程序,将两个字符串连接起来,结果取代第一个字符串。
 

(1)自己定义一个用strcat函数功能的函数

#include <iostream>
#include <string>
using namespace std;
void connect(char str1[], char str2[]);//,有返回值的就用int,没有返回值就用void

int main()
{
    char s1[20], s2[10];//s[20]可以输入最大字符数为19个 
    cout<<"输入字符串1: ";
    cin>>s1;
    cout<<"输入字符串2: ";
    cin>>s2;
    connect(s1, s2);
    cout<<s1<<endl;
    system("pause");
    return 0;
}

void connect(char str1[], char str2[])//自己定义一个 有strcat函数功能的函数 
{
    int i, j;
    for (i=0; str1[i]!='\0'; i++); // '\0'是空字符对应的ASCII码 
    for (j=0; str2[j]!='\0'; str1[i++]=str2[j++]);
 
}

(2)用标准库中的strcat函数

了解strcat函数就看(1)

#include <iostream>
#include <string>
using namespace std;
int main()
{
    char s1[20], s2[10];
    cout<<"Please enter string 1: ";
    cin>>s1;
    cout<<"Please enter string 2: ";
    cin>>s2;
    strcat(s1, s2);//用标准库里的strcat函数 
    cout<<s1<<endl;
    system("pause");
    return 0;
}

(3)用string方法定义字符串变量

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1, s2;  //用string定义字符串变量
    cout<<"Please enter string 1: ";
    cin>>s1;
    cout<<"Please enter string 2: ";
    cin>>s2;
    s1+=s2;
    cout<<s1<<endl;
    system("pause");
    return 0;
}

 5.13 输入n个字符串,将它们按字母由小到大的顺序排列并输出。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str[80], temp;
    
	int i, j, n;
    cout<<"输入字符串的总数: ";
    cin>>n;
    
	for (i=0; i<n; i++)
	{
        cout<<"输入第"<<i+1<<" 个字符串: ";
        cin>>str[i];
    }
   
    for (i=0; i<n; i++)
        for (j=i+1; j<n; j++)
            if (str[i]>str[j])
			{
                temp=str[i], str[i]=str[j], str[j]=temp;
            }
   
    for (i=0, cout<<"按字母由小到大排序为\n"; i<n; cout<<str[i++]<<endl);//endl插入换行符并刷新输出流
    system("pause");
    return 0;
}

输入字符串的总数:  3
输入第1 个字符串: fghry
输入第2 个字符串: rgfd
输入第3 个字符串: fds
按字母由小到大排序为
fds
fghry
rgfd

5.14 输入n个字符串,把其中以字母A打头的字符串输出。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str[80];
    int i, n;
    cout<<"输入字符串的总数为: ";
    cin>>n;
    
	for (i=0; i<n; i++)
	{
        cout<<"第"<<i+1<<" 个字符串为: ";
        cin>>str[i];
    }
    
	for (i=0; i<n; i++)
        if (str[i][0]=='A')//str[i][0]表示 字符串打头的那个字符 
            cout<<str[i]<<endl;
    system("pause");
    return 0;
}

以字母A打头的字符串:    if (str[i][0]=='A')

str[i][0]   表示 字符串打头的那个字符  

5.15 输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,则输出THGIL。

5.16 输入3个学生的姓名、学号和成绩,将其中不及格者的姓名、学号和成绩输出。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
    string students[3];
    int num[3], i;
    float score[3];
    
    for (i=0; i<3; i++)
	{
        cout<<"输入第"<<i+1<<" 个学生的信息(姓名,学号,成绩) ";
        cin>>students[i]>>num[i]>>score[i];
    }
    
    for (i=0; i<3; i++)
    
        if (score[i]<60)
            cout<<setw(8)<<students[i]<<' '<<num[i]<<' '<<score[i]<<endl;
            //因为用到了setw()所以要用#inlcude<iomanip> 
    system("pause");
    return 0;
}

6.1 输入3个整数,按由小到大的顺序输出。

(1)用指针法处理

#include <iostream>
using namespace std;
void sort(int *x, int *y, int *z);
//sort函数是排序作用 
//void的字面意思是“无类型”,void *则为“无类型指针”,void *可以指向任何类型的数据

int main()
{
    int a, b, c;
    cout<<"请输入 a, b, c: ";
    cin>>a>>b>>c;
    sort(&a, &b, &c);   //&是引用变量声明 
    cout<<"排序为: "<<a<<' '<<b<<' '<<c<<endl;
    system("pause");
    return 0;
}
void sort(int *x, int *y, int *z)
{
    int temp;
    if (*x>*y) temp=*x, *x=*y, *y=temp;//小的数往x移 
    if (*x>*z) temp=*x, *x=*z, *z=temp;//小的数往x移 
    if (*y>*z) temp=*y, *y=*z, *z=temp;//小的数往y移 
}

(2) 用引用法处理

#include <iostream>
using namespace std;
void sort(int &x, int &y, int &z);
int main()
{
    int a, b, c;
    cout<<"请输入 a, b, c: ";
    cin>>a>>b>>c;
    sort(a, b, c);      //区别就在这里,指针法是&a,&b,&c 
    cout<<"排序为: "<<a<<' '<<b<<' '<<c<<endl;
    system("pause");
    return 0;
}
void sort(int &x, int &y, int &z)
{
    int temp;
    if (x>y) temp=x, x=y, y=temp;
    if (x>z) temp=x, x=z, z=temp;
    if (y>z) temp=y, y=z, z=temp;
}

6.2 输入3个字符串,按由小到大的顺序输出。

(1)用字符指针数组

#include <iostream>
#include <cstring>//strcmp的头文件 
#include <cstdlib>//malloc的头文件 
using namespace std;
void sort(char *s[3], int n);//char是对一个字符的定义 
int main()
{
    char *string[3];
    // char *s,申明你的s变量为char类型指针
	//一般如果需要传入函数中的是字符串时,就用char *指针 
	//而char s,则只是表示一个字符变量,它只保存了一个字符。
    int i;
    for (i=0; i<3; string[i++]=(char *)malloc(20*sizeof(char)));
    //sizeof(char)计算char类型占用的字节数
    //malloc函数用于向操作系统申请内存
    //申请分配20个单位char内存空间,并把指针赋予string[i++]
    for (i=0; i<3; cout<<"请输入第"<<i+1<<" 个字符串 ", cin>>string[i++]);
    
	sort(string, 3);
    for (i=0, cout<<"排序为"; i<3; cout<<string[i++]<<' ');
    
	cout<<endl;
    system("pause");
    return 0;
}
void sort(char *s[], int n)//定义排序函数 
{
    int i, j;
    char *temp;
    for (i=0; i<n; i++)
        for (j=i+1; j<n; j++)
            if (strcmp(s[i], s[j])>0)
            //strcmp(s1,s2)为字符串比较函数
			//比较的是从字符串的第一个字符开始比较他的ASCLL码值
			//跟字符串的长度无关。 
			//两个字符串相同时返回0
			//第一个字符串大于第二个字符串时返回一个正值,否则返回负值. 
			{
                temp=s[i], s[i]=s[j], s[j]=temp;//把小的字符串往前移 
            }
}

①char与char*的区别

char定义一个字符,char*定义一个字符串

②sizeof( )是计算占用字节的函数

③malloc用于申请内存

需要头文件#include <cstdlib>

④sort函数为排序函数,学会如何自己定义

⑤strcmp(s1,s2)是字符串比较函数

需要头文件#include <cstring>

如果s1=s2,输出0   ;  如果s1>s2,输出一个正值

(2)用string方法 

#include <iostream>
#include <string>
using namespace std;
void sort(string s[], int n);
int main()
{
    string str[3];
    int i;
    for (i=0; i<3; cout<<"请输入第"<<i+1<<" 个字符串 ", cin>>str[i++]);
    sort(str, 3);//sort函数是排序的 
    for (i=0, cout<<"排序为: "; i<3; cout<<str[i++]<<' ');
    cout<<endl;
    system("pause");
    return 0;
}
void sort(string s[], int n)//定义sort函数
//以下为sort函数的定义,应熟记 
{
    int i, j;
    string temp;
    for (i=0; i<n; i++)
        for (j=i+1; j<n; j++)
            if (s[i]>s[j])
			{
                temp=s[i], s[i]=s[j], s[j]=temp;//把小的往前移 
            }
}

请输入第1 个字符串 sgd
请输入第2 个字符串 RAW
请输入第3 个字符串 agt
排序为: RAW agt sgd

注意:大写字母比小写字母“小”

6.3 输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。

写3个函数:1.输入10个数;2.进行处理;3.输出10个数。

6.4写一函数,使最后m个数变成最前面m个数

有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数。写一函数实现以上功能,在主函数中输入n个整数,并输出调整后的n个数。

 6.5 有n个人围成一圈,顺序排号。从第1个人开始报数(从1~3报数),凡报到3的人退出圈子,问最后留下的人原来排在第几号。

6.6 写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。

#include <iostream>
using namespace std;
int stringlen(char *s);//定义字符串的长度 
int main()
{
    char str[20];//定义一个可以有20个字符的字符串 
    cout<<"输入字符串 ";
    cin>>str;
    cout<<"字符串长度为 "<<stringlen(str)<<endl;
    system("pause");
    return 0;
}

int stringlen(char *s)//定义字符串长度函数 !!!!!!!!!!!!!!!!!!!!!!!!
{
    char *p;
    int i;
    for (p=s, i=0; *p!='\0'; p++, i++);
    return i;
}

6.7 有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。

#include<iostream>
using namespace std;
void copy(char*s,int m);
int main()
{
	char str[20];
	int num;
	cout<<"请输入字符串";
	cin>>str;
	cout<<"请输入要复制的字符个数";
	cin>>num;
	
	copy(str,num);
	cout<<"复制的字符串为"<<str<<endl;
	
	system("pause");
	return 0;
	 
}

//定义复制函数
void copy(char*s,int m)
{
	char *p1,*p2;
	for(p1=s,p2=s+m-1; *p1++=*p2++;) ;//括号里面的最后一个;不能忘记 
	//计算*p2,对指针p2间接引用
	//计算p2++
	//计算 *p1:对指针p1的间接引用
	//计算p1++
	//*p1=*p2 
	
 } 

学会定义复制函数

 6.8 输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少?

#include <iostream>
#include <string>
using namespace std;
void count(char *s);
int main()
{
    char str[30];
    cout<<"输入字符串 ";
    gets(str);
    //从缓冲区中读取字符串并存储到字符指针变量 str 所指向的内存空间 
    
    count(str);
    
    system("pause");
    return 0;
}

//定义count 函数 
void count(char *s)
{
    char *p;
    int up, low, space, num, other;
    for (p=s, up=0, low=0, space=0, num=0, other=0; *p!='\0'; p++)
	{
        if (*p>='A'&&*p<='Z') up++;
        else if (*p>='a'&&*p<='z') low++;
        else if (*p==' '||*p=='\t') space++;//注意空格还有一种形式为\t 
        else if (*p>='0'&&*p<='9') num++;
        else other++;
    }
    cout<<"大写字母个数= "<<up<<"小写字母个数= "<<low<<" 空格个数= "<<space<<" 数字个数 "<<num<<" 其他字符个数"<<other<<endl;
}

输入字符串 dfaaSA\t  314
大写字母个数= 2小写字母个数= 5 空格个数= 2 数字个数 3 其他字符个数1

如果不用gets(str);   而是还是用cin>>str;

结果为:

输入字符串 dfaaSA\t  314
大写字母个数= 2小写字母个数= 5 空格个数= 0 数字个数 0 其他字符个数1

明显代码得不出正确的结果

然而还是实现不了\t为空格

6.9 写一函数,将一个3*3的整型矩阵转置

#include <iostream>
using namespace std;
void trans(int (*s)[3]);//定义转置函数 
int main()
{
    int a[3][3], i, j;
    for (i=0; i<3; i++)
	{
        cout<<"请输入第 "<<i+1<<" 行的数字 ";
        for (j=0; j<3; cin>>a[i][j++]);
    }
    for (i=0, cout<<"初始矩阵为\n"; i<3; cout<<endl, i++)
        for (j=0; j<3; cout<<a[i][j++]<<' ');
    
	trans(a);//对a行使转置函数 
    
	for (i=0, cout<<"新矩阵为\n"; i<3; cout<<endl, i++)
        for (j=0; j<3; cout<<a[i][j++]<<' ');
    system("pause");
    return 0;
}

void trans(int (*s)[3])
{
    int i, j, temp;
    for (i=0; i<3; i++)
        for (j=0; j<3; i!=j&&i<j ? temp=*(*(s+i)+j), *(*(s+i)+j)=*(*(s+j)+i), *(*(s+j)+i)=temp, j++ : j++);
        //i!=j&&i<j条件的解读 
		//就是对角线以下的三个数字 
		//  *(s+i)就是s[i]
		//  *(s+i)+j 就是 s[i]的第j个元素 
		//  *(*(s+i)+j)就是  s[i][j]的值 
		//  *(*(s+i)+j)=*(*(s+j)+i)就是 s[i][j]=s[j][i]
		//所以上面一大串就是说 temp=s[i][j];s[i][j]=s[j][i];s[j][i]=temp
		// 也就是s[i][j]与s[j][i] 互换 
}

初始矩阵为
3 5 2
2 3 1
1 3 7
新矩阵为
3 2 1
5 3 3
2 1 7

假设int a[][3]={1,2,3,4,5,6},b;
b=*(*(a+1)+1);
a是个二维数组,表示二维数组a的地址

a[0]、a[1]可看作是2个一维数组,分别是一维数组a[0]、a[1]的地址

a[0]的值为{1,2,3},a[1]的值为{4,5,6}

也就是a[0][0]的值为1,a[1][0]的值为4
a+1是个地址,是二维数组a的第二个元素的地址,也就是a[1]的地址
*(a+1)是个值,但也是个地址,其值就是二维数组a的第二个元素的值,也就是a[1]的值
因此a+1和*(a+1)其实都是a[1]的地址值,前者是二维数组的地址,后者是二维数组的值,也即一维数组的地址
*(a+1)+1也就是a[1]+1,即一维数组a[1]的第二个元素,即a[1][1]的地址
*(*(a+1)+1)就是a[1][1]的值,也就是5
 

6.10 将一个5*5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(按从左到右,从上到下顺序依次从小到大存放),写一函数实现之。用main函数调用。

6.10 将一个5*5的矩阵中最大的元素放在中心,4个角分别放4个最小的元素(按从左到右,从上到下顺序依次从小到大存放),写一函数实现之。用main函数调用。

6.14 将n个数按输入时顺序的逆序排列,用函数实现。

 6.15 有一个班4个学生,5门课。1.求第一门课的平均分;2.找出有两门以上课程不及格的学生,输出他们的学号,全部课程成绩及平均成绩;3.找出平均成绩在90分以上或全部课程成绩在85分以上的学生。

6.16 输入一个字符串,内有数字和非数字字符,如。。。将其中连续的数字作为一个整数,依次存放到一数组a中。例如,123放在a[0],456放在a[1]。。。统计共有多少个整数,并输出这些数。

6.17 写一函数,实现两个字符串的比较。

即自己写一个strcmp函数,函数原型为int strcmp(char *p1, char *p2);

#include <iostream>
using namespace std;
int strcmp(char *p1, char *p2);
int main()
{
    char s1[20], s2[20];
    cout<<"输入字符串1: ";
    gets(s1);
    cout<<"输入字符串2: ";
    gets(s2);
    cout<<"输出结果为 "<<strcmp(s1, s2)<<endl;
    system("pause");
    return 0;
}


int strcmp(char *p1, char *p2)
{
    int t;
    for (; *p1!='\0'||*p2!='\0'; p1++, p2++)//只要P1,P2中有一个还有数字,都要继续下去 
        if (*p1!=*p2)
		{
            t=*p1-*p2;//满足:如果s1>s2,则输出正值,若s1< s2,则输出负值。 
            break;
        }
    if (*p1=='\0'&&*p2=='\0')//如果p1,p2都读取完了,都一直 *p1=*p2
        t=0;
    return t;//要return t所以应该int ,而不是void。 
    //return 表示从被调函数返回到主调函数继续执行,返回时可附带一个返回值
}

 6.18 编一程序,输入月份号,输出该月的英文月名。

例如,输入"3",则输出March,要求用指针数组处理。

*p和**p的区别

一、指针级别不同

*p是一级指针。

**p是二级指针。

二、表示的含义不同

*p表示p所指向的地址里面存放的是一个int类型的值 。

**p表示p所指向的地址里面存放的是一个指向int类型的指针。

三、指向的内容不同

*p=a指针p将指向a上的内容。

**p=a将变量a的地址 通过指针p 赋值到变量b上。

四、存放的内容的地址不同

*p指向的地址里面存放的是一个值 。
**p指向的地址里面存放的是一个指向p的一级指针。

*p与p区别

c语言的指针部分 *p与p区别如下:

1、作用不同

*p表示此指针指向的内存地址中存放的内容。

p是一个指针变量的名字,表示此指针变量指向的内存地址。

2、输出内容的格式不同

*p常用在一个和指针类型一致的变量或者常量。

p输出一个指针的地址,通常是输出一个16进制的数。

3、效果不同

*p是让编译器从指定的地址中读取出数据。

p是用来存放要读取数据的地址。

6.19用指向指针的指针的方法对5个字符串排序并输出。

6.20 用指向指针的指针的方法对n个整数排序并输出。要求将排序单独写成一个函数。整数和n在主函数中输入。最后在主函数中输出。

二维数组初始化的方法

(1)对所有元素赋值

int a[2][2]={1,2,3,4}

(2)对部分元素赋值

int a[2][2]={1,2}

表示2,2

       0,0

(3)对每一行部分元素赋值

int a[2][2]={(2),(6)}

表示2,0

       6,0

指针

  • double *p1,p2

p1为指向double类型的指针变量

p2为一个double类型的变量

  • arr[2]与arr+2

前者为数组arr的第3个元素,或者为数组arr第3个元素的地址

  • double类型变量内存占8B,如果数组doubleArray的基地址为1000,求doubleArray+5的值

double的指针加1,指针中的实际值增加了8,故结果为1040

结构体

  • 数组中的每个元素类型都相同
  • 结构体中的每个字段类型可同可不同
  • 使用结构体类型的必要步骤

定义结构体类型

定义结构体变量

7.2

->的作用:

#include<stdio.h>
struct role   // 定义一个结构体
{
    char name[8];  // 姓名
    int level;  // 等级
    int HP;  // 血量
    int MP;  // 蓝量
    int gold;  // 金币
};
void main()
{
    struct role *w;   // 定义一个结构体指针
    char str[]="kuangzhan";
    w->name = str;     // 对结构体中的成员变量name进行赋值
    w->level = 46;  // 对结构体中的成员变量level进行赋值
    w->HP = 3100;       // 对结构体中的成员变量HP进行赋值
    w->MP = 3100;       // 对结构体中的成员变量MP进行赋值
    w->gold = 475233;       // 对结构体中的成员变量gold进行赋值

}
 

  • 类是对象的抽象,对象是类的具体实例
  • 类是抽象的,不占内存的;对象是具体的,占用内存空间
  • 类的声明,类比结构体的声明:

class Student  //类名为Student

{

private:    //声明以下为私有的,只有对象内部可以访问

int num;

char name[20];

char sex;

//上面三个是数据成员

public:    //声明以下为公有的,则外界可以调用display函数

void display()//这是成员函数

{

cout<<"num"<<num<<endl;

cout<<"name"<<name<<endl;

cout<<"sex"<<sex<<endl;

}//函数中的操作语句

};//注意不要忘记这个;

Student stu1,stu2//定义了两个在Student类里的对象stu1和stu2

  • 定义对象也可以 class Student stu1,stu2  ,可以但是没必要
  • 如果即不指定private也不指定public,则默认是私有的
  • 私有成员只有对象内部才可以访问

8.6

#include <iostream>
using namespace std;

class Cube_volume //管的体积 
{
public:
    void set_cube();
    void volume_cube();
    void shou_cube();
private:
    int length;
    int width;
    int height;
    int volume;
};

void Cube_volume::set_cube()
{
    cout<<"输入管的长宽高 ";
    cin>>length>>width>>height;
}

void Cube_volume::volume_cube()
{
    volume=length*width*height;
}

void Cube_volume::shou_cube()
{
    cout<<"体积= "<<volume<<endl;
}

int main()
{
    Cube_volume cube[3];
    int i;
    for (i=0; i<3; i++)
	{
        cube[i].set_cube();
        cube[i].volume_cube();
        cube[i].shou_cube();
    }
    system("pause");
    return 0;
}

输入管的长宽高 1 2 3
体积= 6
输入管的长宽高 2 3 1
体积= 6
输入管的长宽高 2 2 2
体积= 8

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值