从C语言转到c++学习第一天的代码练习

#include <iostream>
#include <math.h>
#include <stdlib.h>
//#include <stdio.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void print_task(void);
void scan_task(void);
void text_1();
void bit_task();
void text_2();
void text_3();
void text_4();
void text_5();
int digui(int n,int num);

int main(int argc, char** argv) {
	//print_task();//cout
	//scan_task();//cin
	//text_1();//c++的输出测试
	//bit_task();//位运算
	//text_2();//运算符
	//text_3();//判断语句
	//因为学了C语言的原因这里的循环和判断就不多做简单测试直接做题
	//text_4();//循环
	text_5();//子函数相关(因为本程序都是使用子程序编写所以我就简单的写一个递归函数) 
	return 0;
}
//输出字符学习 (cout的使用方法)
void print_task(){
	cout<<2019
	<<endl;
	cout<<'a'
	<<endl;
	cout<<3.14
	<<endl;
	printf("woaini\n");
	cout<<"Hello World"<<"aini"//这里不会换行
	<<endl;
}
//输入字符相关学习(cin的基本用法)
void scan_task(){
	int i = 0;
	cout<<"Please input a number:"<<endl;
	cin>>i;
	cout<<"这个数字是"<<i<<endl;
}
void text_1(){
	static int a = 0,b = 0;
	printf("printf输出:Nice Work!\n");
	cout<<"cout输出;Nice Work!"<<endl;
	scanf("%d",&a);
	a = pow(a,3);//a的b次方函数具体请跳转定义到math.h
	printf("printf_a的立方 = %d\n",a);
	cin>>b;
	b = pow(b,3);
	cout<<"cout_b的立方"<<b<<endl;
}
void bit_task(){
	int a = 7,b = 4,c = 0,d = 0,e = 0,f = 0,g = 1;
	char str[30];
	printf("%",a);
	//注意这里有个函数需要使用打stdlib.h的库交itod但是只能储存到char字符里面
    itoa(a,str,2);//2即是代表转换为2进制
    itoa(b,str,2);
    printf("a的二进制数是: %s\n",str);
    printf("b的二进制数是: %s\n",str);
    c = (a & b); //单位的与运算111与上100
    itoa(c,str,2);
    printf("c的二进制数是: %s\n",str);
	d = (a | b);
	itoa(d,str,2);
	printf("d的二进制数是: %s\n",str);
	e = (a ^ b);
	itoa(e,str,2);
	printf("e的二进制数是: %s\n",str);
	f = (~a);
	itoa(f,str,2);//这里的取反操作111变成000
	//但是这里的itoa的输出是不一样的所以最后输出会看到输出11111111111111111111111111111000
	printf("f的二进制数是: %s\n",str);
	g = g<<1;//位移操作g = 0001,然后向左位移为 g = 0010 = 2;
	itoa(g,str,2);
	cout<<"g的二进制数是: "<<str<<endl;
	cout<<g<<endl;
	printf("占用字节:char = %d, int  = %d, void*指针 = %d, double = %d,long = %d, float = %d\n",sizeof(char),sizeof(int),sizeof(void*),sizeof(double),sizeof(long),sizeof(float));
	//三目运算也叫条件运算符相当于一个if判断后面会学到
	a>b?printf("max_ab = %d",a):printf("max_ab = %d\n",b);
}
void text_2(){
	//ex1:不使用第三个变量交换两个变量的值
	//ex2:使用复合运算计算a+=a*=a/=a-6;
	int a = 4,b = 5,c = a;//这个地方的c是用来做后面的计算
	//ex1
	a += b;
	b = a - b;
	a -= b;
	printf("a = %d,b = %d\n",a,b);
	//ex2
	c+=c*=c/=c-6;
	cout<<c<<endl;
	//"-"的优先级高于前面三个运算符的优先级所以第一步得到
	//c = c+{c*[c/[4-6]}
	//c = 4+{4*[4/[-2]}
	//c = (-2)+{-2*[-2]} 	//c = c+{4}
	//c = 4 + 4 = 8
}
void text_3(){
	//ex1:计算工资大于等于5000增加%10,大于等于2500小于5000增加%15,小于2500增加%20
	//ex2:任意输入三个数,输出从小到大;
	//ex1
	int gongzi,output_gongzi;
	cout<<"请输入你的工资:"<<endl;
	cin>>gongzi;
	if(gongzi>=5000)
		output_gongzi = gongzi + (gongzi/100*10);
	else if(gongzi>=2500&&gongzi<5000)
		output_gongzi = gongzi + (gongzi/100*15);
	else
		output_gongzi = gongzi + (gongzi/100*20);
	cout<<"你最终的工资为:"<<output_gongzi<<endl;
	//ex2
	int a,b,c,max;
	cout<<"请输入三个整数:"<<endl;
	cin>>a;
	cin>>b;
	cin>>c;
//	if(c>a&&c>b){
//		if(b>a){
//			max = a;
//			a = b;
//			b = max;
//		}
//	}
	if(b>a&&b>c){
		max = c;
		c = b;
		b = max;

	}
	if(a>b&&a>c){
		max = a;
		a = c;
		c = max;
	}
	if(a>b){
		max = a;
		a = b;
		b = max;
	}
	cout<<"输出:"<<a<<" "<<b<<" "<<c<<endl;
}
void text_4(){
	//ex1:输出一个“*”组成的三角形(while或for循环)
	//ex2:输出99乘法表
	//ex3:水仙花数(一个三位数、百、十、个位的立方等于他原来的数)
	//ex4:输出0-100不能被3整除的数字

	//ex1
	int i,j,z,t = 0,n = 1;
	for(j = 0;j<5;j++){
		for(z = 4;z>t;z--){
		//控制输出空格个数 
			cout<<" ";
		} 
		for(i = 0;i<n;i++){
			//输出*个数 
			printf("*");
		}
		i = 0;n+=2;z = 0;t++;  
		printf("\n");
	}
	//ex2
	i = 1;j = 1;n = 0;
	int hang = 2,lie = 2;
	for(i = 1;i<lie;i++){
		for(j=1;j<hang;j++){
			printf("%d * %d = %d  ",i,j,i*j);
		}
		cout<<endl;
		lie++;hang++; 
		if(hang==11)
			break;
	}
	//ex3
	int num,num_all,num_b,num_s,num_g;
	cout<<"请输入一个三位数:"<<endl;
	cin>>num;
	cout<<endl;
	num_b=num/100;
	num_s=num/10%10;
	num_g=num%100%10;
	num_all = pow(num_b,3)+pow(num_s,3)+pow(num_g,3);
	if(num_all==num){
		cout<<num<<"属于水仙花数"<<endl; 
	} 
	else{
		cout<<num<<"不是水仙花数"<<endl<<endl; 
	}
	cout<<"1000以下的水仙花数有:"<<endl;
	for(num = 100;num<1000;num++){
		num_b=num/100;
		num_s=num/10%10;
		num_g=num%100%10;
		num_all = pow(num_b,3)+pow(num_s,3)+pow(num_g,3);
		if(num_all==num){
			cout<<num<<endl;
		}
	}
	//ex4
	cout<<"100以下能被3整除的数有:"<<endl; 
	for(num = 0,hang = 0;num<=100;num++){
		if(hang%8==0)
			cout<<endl;
		if(num%3==0){
			cout<<num<<" ";
			hang++;
		}
	} 
}

void text_5(){
	//利用递归的方法计算n的阶乘
	//这里方便使用直接就计算5的阶乘 
	int n = 5,num = 1;
	//cin>>n;//关闭注释可以直接输入 
	cout<<endl;
	num = digui(n,num);
	cout<<endl;
	cout<<n<<"的阶乘 = "<<num<<endl; 
} 
int digui(int n,int num){
	if(n>0){
		cout<<"最后"<<n<<"次"<<endl;
		cout<<"num = "<<num<<"n = "<<n<<endl; 
		num *= (n);
		digui(--n,num);
	}
	if(n == 1){
		return num;
	}
 } 

猜你喜欢

转载自blog.csdn.net/qq_43581670/article/details/107605026