C++练习题(3)
//C++创建各类三角形图案——正三角形
#include <iostream>
using namespace std;
int main() {
char ch;
cout<<"输入符号:";
cin>>ch;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
cout << ch;
}
cout << endl;
}
return 0;
}
//C++倒三角形
#include <iostream>
using namespace std;
int main() {
int num;
cout<<"输入符号:";
cin>>num;
for (int i = 5; i >=0; i--) {
for (int j = 1; j <= i; j++){
cout<<num;
}
cout << endl;
}
return 0;
}
//C++表格形式输出数据——将 1~100 的数据以 10x10 矩阵格式输出
#include <iostream>
using namespace std;
int main(){
int i,j;
for(i=1;i<=10;i++){
{for(j=i;j<=100;j+=10)
{
cout<<j<<" ";
}
cout<<endl;
}
}
return 0;
}
//C++实现简单的计算器
#include <iostream>
using namespace std;
int main() {
int a, b;
char operate;
cout << "输入两个数字和一个运算符: ";
cin >> a >> operate >> b;
switch (operate) {
case '+':
cout << a << " + " << b << " = " << a + b << endl;
break;
case '-':
cout << a << " - " << b << " = " << a - b << endl;
break;
case '*':
cout << a << " * " << b << " = " << a * b << endl;
break;
case '/':
if (b != 0) {
cout << a << " / " << b << " = " << a / b << endl;
} else {
cout << "除数不能为0" << endl;
}
break;
case '%':
if (b != 0) {
cout << a << " % " << b << " = " << a % b << endl;
} else {
cout << "除数不能为0" << endl;
}
break;
default:
cout << "无效的运算符" << endl;
}
return 0;
}