温习c++基础题型(更新中)
求圆的面积👇
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 double PI=3.14; 5 int radius,area; 6 7 cout<<"输入圆的半径:"; 8 cin>>radius; 9 10 cout<<"圆的面积area为:"<<PI*radius*radius<<endl; 11 12 13 return 0; 14 }
输入字符,输出该字符的ascii码👇
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char arr{}; 6 std::cin >> arr; 7 std::cout << int(arr) << '\n'; 8 return 0; 9 }
输入大写字母输出小写字母 👇
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 char a,b; 5 cin>>a; 6 { 7 if(64<a<91){ 8 b=a+32; 9 cout<<b<<endl; 10 } 11 else 12 cout<<"请输入大写字母"<<endl; 13 14 } 15 }
求三个整型数中最小的数👇
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 int a,b,c,min; 5 cout<<"请输入三个整型数:"; 6 cin>>a>>b>>c; 7 if(a<b) 8 min=a; 9 else 10 min=b; 11 if(c<min) 12 min=c; 13 cout<<a<<","<<b<<","<<c<<"中最小数为:"<<min<<endl; 14 return 0; 15 }
如果a>b交换a b的值(考察if语句)👇
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 int a,b,x; 5 cout<<"请输入两个整型数:"<<endl; 6 cout<<"a="; 7 cin>>a; 8 cout<<"b="; 9 cin>>b; 10 if(a>b){ 11 x=a; 12 a=b; 13 b=x; 14 15 } 16 cout<<"a="<<a<<"b="<<b<<endl; 17 return 0; 18 }
用C++编写商品打折代码👇
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 int count,identify; 5 float price,total; 6 cout<<"请输入购买物品的数量:"; 7 cin>>count; 8 cout<<"请输入购买物品的价格:" ; 9 cin>>price; 10 cout<<"q请输入购买物品的身份号码,其身份号码如下:"<<endl; 11 cout<<"*****1*****vip*****" <<endl; 12 cout<<"*****2*****高级*****" <<endl; 13 cout<<"*****3*****中级*****" <<endl; 14 cout<<"*****4*****初级*****" <<endl; 15 cout<<"*****0*****普通*****" <<endl; 16 cin>>identify; 17 if(identify==1) 18 { 19 cout<<"高级会员的购物总额为:"; 20 total=count*price*0.7; 21 22 } 23 else if(identify==2) 24 { 25 cout<<"高级会员的购物总额为:"; 26 total=count*price*0.75; 27 28 } 29 else if(identify==3) 30 { 31 cout<<"中级会员的购物总额为:"; 32 total=count*price*0.8; 33 34 } 35 else if(identify==4) 36 { 37 cout<<"初级会员的购物总额为:"; 38 total=count*price*0.9; 39 } 40 else{ 41 cout<<"普通顾客的购物总额为:"; 42 total=count*price; 43 44 } 45 cout<<total<<endl; 46 return 0; 47 }
用C++编写二元一次方程,根据系数求解的代码👇
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 int main(){ 5 double a,b,c,judge; 6 cout<<"请输入二次方程的3个系数的值:"<<endl; 7 cout<<"a="; 8 cin>>a; 9 cout<<"b="; 10 cin>>b; 11 cout<<"c="; 12 cin>>c; 13 if(a==0) 14 { 15 cout<<"方程不是二次方程:"<<endl; 16 if(b!=0) 17 cout<<"方程的根是:"<<(-c/b)<<endl; 18 else 19 if(c==0) 20 cout<<"方程无定根"<<endl; 21 else 22 cout<<"方程无解"<<endl; 23 } 24 else 25 { 26 cout<<"方程是二次方程:"<<endl; 27 judge=b*b-4*a*c; 28 if(judge==0) 29 { 30 cout<<"方程有两个相等的实根:"<<endl; 31 cout<<"x1=x2="<<(-b/(2*a))<<endl; 32 33 } 34 else if(judge>0) 35 { 36 cout<<"方程有不等的实根:"<<endl; 37 cout<<"x1="<<(-b+sqrt(judge)/(2*a))<<endl; 38 cout<<"x2="<<(-b-sqrt(judge)/(2*a))<<endl; 39 } 40 else 41 { 42 cout<<"方程有两个虚根:"<<endl; 43 cout<<"x1="<<(-b/(2*a))<<"+"<<sqrt(-judge)/(2*a)<<"i"<<endl; 44 cout<<"x2="<<(-b/(2*a))<<"-"<<sqrt(-judge)/(2*a)<<"i"<<endl; 45 } 46 } 47 return 0; 48 49 }