//啊啊啊,别给我报错了啊!!!!!!!!!!!!!!!
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<stdio.h>
using namespace std;
//加法
void jiafa() {
int x, y;
int result = 0;
cout << "请输入两个整数:" << endl;
cin >> x >> y;
result = x + y;
cout << "加法结果的十进制形式为:" << result << endl;
cout << "加法结果的八进制形式为:" << oct << result << endl;
cout << "加法结果的十六进制形式为:" << hex << result << endl;
}
//减法
void jianfa() {
int x, y;
int result = 0;
cout << "请输入两个整数x和y,计算x-y的值:" << endl;
cin >> x >> y;
result = x - y;
cout << "减法结果十进制为:" << result << endl;
cout << "减法结果的八进制形式为:" << oct << result << endl;
cout << "减法结果的十六进制形式为:" << hex << result << endl;
}
//乘法
void chengfa() {
double x, y;
double result;
cout << "请输入两个数:" << endl;
cin >> x >> y;
result = x * y;
printf("乘法结果的小数形式是%.6f\n", result);
printf("乘法结果的指数形式是%e\n", result);
}
//除法
void chufa() {
float x, y;
double result;
cout << "请输入被除数x:" << endl;
cin >> x;
cout << "请您输入非0除数y:";
cin >> y;
while (y == 0) {
cout << "除数不能为0,请重新输入除数y:" << endl;
cin >> y;
}
result = x / y;
printf("除法结果的小数形式是%.6f\n", result);
printf("除法结果的指数形式是%e\n", result);
}
//取模
void qumo() {
int x, y;
cout << "请输入一个整数和要模的值:" << endl;
int result;
cin >> x >> y;
result = x % y;
cout << "取模结果十进制形式为:" << result << endl;
cout << "取模结果的八进制形式为:" << oct << result << endl;
cout << "取模结果的十六进制形式为:" << hex << result << endl;
}
//计算圆面积
void yuanmianji() {
double r, pi = 3.14;
double result;
cout << "请输入圆的半径:";
cin >> r;
result = pi * r * r;
cout << "此圆的面积为:" << result<<endl;
cout << "此圆的保留两位小数的面积为:" << fixed << setprecision(2) << result << endl;
}
//判断数字的位数
void weishu() {
cout << "请输入一个三位数以内的正整数:";
int a;
cin >> a;
while (a < 0 || a >= 1000) {
cout << "输入的数不是三位以内的正整数,请重新输入:" << endl;
cin >> a;
}
int ge, shi, bai;
ge = a % 10;
shi = (a % 100) / 10;
bai = a / 100;
cout << "百位是:" << bai << ",十位是:" << shi << ",个位是:" << ge << endl;
}
//判断闰年
void runnian() {
int year;
cout << "请您输入需要判断的年份:";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
cout << year << "年是闰年" << endl;
}
else { cout << year << "年是平年" << endl; }
}
//成绩转换
void chengji() {
int grade;
cout << "请输入一个百分制的成绩:";
cin >> grade;
while (grade < 0 || grade>100) {
cout << "您输入的成绩不符合要求,请重新输入:";
cin >> grade;
}
if (grade >= 0 && grade <= 59) {
cout << "不及格" << endl;
}
else if (grade <= 69) {
cout << "及格" << endl;
}
else if (grade <= 79) {
cout << "中" << endl;
}
else if (grade <= 89) {
cout << "良" << endl;
}
else if (grade <= 100) {
cout << "优秀" << endl;
}
}
//计算从1到输入数之间的奇数和 (for)
void jishuhe() {
cout << "请您输入一个正整数:";
int a, result = 0, i;
cin >> a;
while (a < 1) {
cout << "您输入的数不符合要求,请重新输入:";
cin >> a;
}
for (i = 0; i <= a; i++) {
if ((i & 2) != 0) {
result += i;
}
}
cout << "从1到" << a << "之间的奇数和为:" << result << endl;
}
//计算从1到输入数之间的偶数和 (while)
void oushuhe() {
cout << "请您输入一个正整数:";
int a, result = 0, i;
cin >> a;
while (a < 1) {
cout << "您输入的数不符合要求,请重新输入:";
cin >> a;
}
i = 0;
while (i <= a) {
if (i % 2 == 0) {
result += i;
}
i++;
}
cout << "从1到" << a << "之间的偶数和为:" << result << endl;
}
//输出从1到输入数之间所有3的倍数 (do-while)
void sanbeishu() {
cout << "请您输入一个正整数:";
int a, result = 0, i = 0;
cin >> a;
cout << "由1到" << a << "之间3的倍数为:";
do {
i++;
if (i % 3 == 0) {
result = i;
cout << result << ",";
}
} while (i <= a);
}
//求即被3整除又被5整除的数
void zhengchushu() {
cout << "请您输入两个数:" << endl;
float a, b;
int i, A, B;
cin >> a >> b;
if (a > b) {
A = (int)b;
B = (int)a;
}
else {
A = a;
B = b;
}
for (i = A; i <= B; i++) {
if (i % 3 == 0 && i % 5 == 0) {
cout << "从" << A << "到" << B << "间第一个既能被3整除又能被5整除的数为: " << i << endl;
break;
}
}
}
//求水仙花数
void shuixianjhua() {
int i, ge, shi, bai;
cout << "水仙花数为:";
for (i = 100; i < 1000; i++) {
ge = i % 10;
shi = (i % 100) / 10;
bai = i / 100;
if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
cout << i << " , ";
}
}
}
//求最大值
int zuidazhi()
{
int x, y;
cout << "请输入两个整数,并以空格隔开:" << endl;
cin >> x >> y;
int result;
result = max(x, y);
cout << "最大值为:" << result << endl;
return 0;
}
//求最大最小值
int maxandmin() {
int x,y,m,n;
cout << "请输入两个整数:" << endl;
cin >> x >> y;
m = max(x, y);
n = min(x, y);
cout << "最大值为:" << m << ",最小值为:" << n << endl;
return 0;
}
//冒泡排序
void maopao() {
int a[10], i, j;
int temp;
cout << "请您输入十个数:" << endl;
for (i = 0; i < 10; i++) {
cin >> a[i];
}
for (i = 0; i < 10 - 1; i++) {
for (j = 0; j < 10 - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (i = 0; i < 10; i++) {
cout << a[i] << " , ";
}
}
//矩阵转置
void juzenzhuanzhi() {
int a[200][200], i, j;
int x, y;
cout << "请输入您要定义数组的行数x=";
cin >> x;
cout << "请输入您要定义数组的列数y=";
cin >> y;
cout << "请您输入" << x * y << "个数字:" << endl;
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
cin >> a[i][j];
}
}
cout << "您输入的矩阵为:" << endl;
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
cout << a[i][j] << " ,";
}
cout << endl;
}
int b[100][100];
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
b[j][i] = a[i][j];
}
cout << endl;
}
cout << "转置后的矩阵为:" << endl;
for (i = 0; i < y; i++) {
for (j = 0; j < x; j++) {
cout << b[i][j] << " ,";
}
cout << endl;
}
}
//求对角线和
void duijiaoxianhe() {
int a[200][200], i, j;
int x, y, z=0;
cout << "请输入您要定义数组的行数x=";
cin >> x;
cout << "请输入您要定义数组的列数y=";
cin >> y;
cout << "请您输入" << x * y << "个数字:" << endl;;
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
cin >> a[i][j];
}
}
cout << "您输入的矩阵为:" << endl;
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
cout << a[i][j] << " ,";
}
cout << endl;
}
for (i = 0; i < x; i++) {
z += a[i][i];
}
cout << "您所输入的主对角元素之和为:" << z << endl;
}
//统计单词个数
void dancigeshu() {
int sum = 0, i, word=0;
cout << "从键盘输入一行字符,单词之间用空格分隔开。" << endl;
cout << "请在此输入:";
char a[200];
gets_s(a);//会自动以为敲进去的回车结束输入字符串。
gets_s(a);//不规范的小技巧 ^_^!
for (i = 0; a[i] != '\0'; i++) {
//for(i=0; i<strlen(str); i++){
if (!((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z'))) {//if(str[i]不是字母)
word = 0;
}
else if (word == 0) {
word = 1;
sum++;
}
}
cout << "\n您输入了 " << sum << " 个单词。" << endl;
}
//统计单词个数,并将单词输出
void dancigeshu2() {
char ch[80];
char word[40][80];//储存分离出来的单词
int i;
int n, m;//n:word[][]中存的单词个数
n = 0;
cout << "请输入字符串,单词之间空格隔开:";
gets_s(ch);
gets_s(ch);
//判断第一个字符,不在循环里判断是因为i=0时ch[i-1]下标越界
if (ch[0] >= 'a' && ch[0] <= 'z') {
m = 0;
word[n][m] = ch[0];
m++;
}
for (i = 1; ch[i] != '\0'; i++) {
//if(ch[i]是单词的开始){
//if(chi[i]是字母且ch[i-1]不是字母){
if ((ch[i] >= 'a' && ch[i] <= 'z') && !(ch[i - 1] >= 'a' && ch[i - 1] <= 'z')) {
m = 0;
word[n][m] = ch[i];
m++;
}
//if(chi[i]在单词的内部){
//if(chi[i]是字母且ch[i-1]是字母){
if ((ch[i] >= 'a' && ch[i] <= 'z') && (ch[i - 1] >= 'a' && ch[i - 1] <= 'z')) {
word[n][m] = ch[i];
m++;
}
//if(chi[i]是单词的最后一个字母){
//if(chi[i]是字母且ch[i+1]不是字母){
if ((ch[i] >= 'a' && ch[i] <= 'z') && !(ch[i + 1] >= 'a' && ch[i + 1] <= 'z')) {
word[n][m] = '\0';
n++;
}
}
for (i = 0; i < n; i++) {
cout << word[i] << endl;
}
}
//学生成绩排序
struct Stu
{
int num;
char name[20];
int score;
};
void chengjipaixu() {
int i, j;
struct Stu student[10] = { {317,"ax",89},{318,"bx",50},{319,"cx",84},{320,"dx",70},{321,"ex",76},{322,"fx",99},{323,"gx",88},{324,"hx",121},{325,"ix",150},{326,"jx",77} };
struct Stu temp;
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10; j++)
{
if (student[i].score > student[j].score)
{
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
for (i = 0; i < 10; i++)
{
printf("%d %6s %d\n", student[i].num, student[i].name, student[i].score);
}
int a=0;
float avg;
for (i = 0; i < 10; i++) {
a += student[i].score;
}
cout << "总成绩为:" << (float)a << endl;
cout << endl;
avg = (float)a / 10;
cout << "平均分为:" << avg << endl;
}
void function_23() {
int i, a[10];
FILE* fp;
errno_t err;
err= fopen_s(&fp,"D:\\my.txt", "r+");
if (fp)
fclose(fp);
for (i = 0; i < 10; i++) {
fscanf_s(fp, "%d", &a[i]);
}
rewind(fp);
for (i = 0; i < 10; i++)
fprintf(fp, "%d\n", a[i] + 10);
fclose(fp);
printf("已完成操作\n");
}
int main() {
int a=0;
cout << "********************************************************" << endl
<< "* 小小计算器 *" << endl
<< "* 1.加法 *" << endl
<< "* 2.减法 *" << endl
<< "* 3.乘法 *" << endl
<< "* 4.除法 *" << endl
<< "* 5.取模运算 *" << endl
<< "* 6.计算圆面积 *" << endl
<< "* 7.判断数字位数 *" << endl
<< "* 8.判断闰年 *" << endl
<< "* 9.成绩转换 *" << endl
<< "* 10.计算从1到输入数之间的奇数和(for) *" << endl
<< "* 11.计算从1到输入数之间的偶数和(while) *" << endl
<< "* 12.输出从1到输入数之间所有3的倍数 (do-while) *" << endl
<< "* 13.求即被3整除又被5整除的数 *" << endl
<< "* 14.求水仙花数 *" << endl
<< "* 15.求最大值 *" << endl
<< "* 16.求最大最小值 *" << endl
<< "* 17.冒泡排序 *" << endl
<< "* 18.矩阵转置 *" << endl
<< "* 19.求对角线和 *" << endl
<< "* 20.统计单词个数 *" << endl
<< "* 21.统计单词个数,并输出单词 *" << endl
<< "* 22.学生成绩排序 *" << endl
<< "* 23.学生成绩普涨10分 *" << endl
<< "* 24.退出 *" << endl
<< "********************************************************" << endl;
while (a != 24) {
cout << endl;
cout << "请输入选项前对应的序号,将执行相应操作:";
cin >> a;
switch (a) {
case 1:jiafa(); break;
case 2:jianfa(); break;
case 3:chengfa(); break;
case 4:chufa(); break;
case 5:qumo(); break;
case 6:yuanmianji(); break;
case 7:weishu(); break;
case 8:runnian(); break;
case 9:chengji(); break;
case 10:jishuhe(); break;
case 11:oushuhe(); break;
case 12:sanbeishu(); break;
case 13:zhengchushu(); break;
case 14:shuixianjhua(); break;
case 15:zuidazhi(); break;
case 16:maxandmin();break;
case 17:maopao(); break;
case 18:juzenzhuanzhi(); break;
case 19:duijiaoxianhe(); break;
case 20:dancigeshu(); break;
case 21:dancigeshu2(); break;
case 22:chengjipaixu(); break;
case 23:function_23(); break;
}
}
cout << endl;
cout << "--------------------------------" << endl
<< "- 已退出 -" << endl
<< "--------------------------------" << endl;
}
您必须 登录 才能发表评论