文章目录
- NO1、选出妃子、宫女和嬷嬷
- No2、根据数字判断月份
- No3、循环计数
- No4、循环选数
- No5、玩转字符
- No6、计算字符串长度
- No7、显示字符串中的字符
- No8、字符串反转
- No9、二维数组的应用
NO1、选出妃子、宫女和嬷嬷
其他要求:
- 超女用结构体表示
- 不要嵌套if
- 输入所有数据后再判断
#include <iostream>
#include <string>
using namespace std;
struct CGirl {
int age;
int high;
string sc;
//c风格字符串 char sc[31]; 字符串比较就要用 strcmp
bool yz;
};
int main() {
CGirl g;
cout << "请依次输入年龄、身高(cm)、身材(火辣/普通)、颜值(漂亮/一般):";
string stringYz{0};
cin >> g.age >> g.high >> g.sc >> stringYz;
g.yz = (stringYz == "漂亮") ? true : false;
if ((g.age >= 18 && g.age <= 25) && (g.high >= 165 && g.high <= 178) && (g.sc == "火辣") && g.yz)
cout << "妃子" << endl;
else if ((g.age >= 18 && g.age <= 30) && (g.high >= 160 && g.high < 165) && (g.sc == "火辣" || g.sc == "普通") &&
g.yz)
cout << "宫女" << endl;
else if ((g.age >= 35 && g.age <= 40) && (g.high >= 155 && g.high < 165) && (g.sc == "飞机场" || g.sc == "普通") &&
(!g.yz))
cout << "嬷嬷" << endl;
/* else
cout<<"落选"<<endl;*/
return 0;
}
No2、根据数字判断月份
其他要求:
- ifelse或者switchcase实现
- 字符串数组实现(C/C++)
#include <iostream>
int main() {
int mouth = -1;
std::cout << "请输入月份(1-12):";
std::cin >> mouth;
/*switch (mouth) {
case 1:
std::cout<<"January"<<std::endl;
break;
case 2:
std::cout<<"February"<<std::endl;
break;
case 3:
std::cout<<"March"<<std::endl;
break;
case 4:
std::cout<<"April"<<std::endl;
break;
case 5:
std::cout<<"May"<<std::endl;
break;
case 6:
std::cout<<"June"<<std::endl;
break;
case 7:
std::cout<<"July"<<std::endl;
break;
case 8:
std::cout<<"August"<<std::endl;
break;
case 9:
std::cout<<"September"<<std::endl;
break;
case 10:
std::cout<<"October"<<std::endl;
break;
case 11:
std::cout<<"November"<<std::endl;
break;
case 12:
std::cout<<"December"<<std::endl;
break;
default:
std::cout<<"输入错误"<<std::endl;
}*/
//效率高于ifelse 和 switch case
char months[12][10] = {"January","February","March","April","May","June","July"
,"August","September","October","November","December"};
if (mouth>=1&&mouth<=12)
std::cout<<months[mouth-1]<<std::endl;
else
std::cout<<"输出错误"<<std::endl;
}
No3、循环计数
#include <iostream>
int main() {
int sum = 0;
int i = 0;
while (i <= 100) {
sum += i++;
}
std::cout << "1-100之间的和为" << sum << "\n";
int num{0};
int sumF{0};;
for (;sumF <= 5000;) {
std::cin>>num;
sumF+=num;
}
std::cout << "输入的和为" << sumF << "\n";
return 0;
}
No4、循环选数
/**
******************************************************************************
* @file : demo4.cpp
* @author : DELL
* @brief : None
* @attention : None
* @date : 2024-01-25
******************************************************************************
*/
#include <iostream>
int main() {
int num{0}, count{0}, sum{0};
while (std::cin >> num) {
if (!num)break;
if (num < 1 || num > 100)
continue;
count++;
sum += num;
}
std::cout << "符合条件个数:" << count << "--" << "它们的和:" << sum << "\n";
int n{1};
//取余数
while (n <= 100) {
if (n % 5 == 0)
std::cout << n << " ";
++n;
}
std::cout << "\n";
//另外一种写法
n = 5;
while (n <= 100) {
std::cout << n << " ";
n += 5;
}
return 0;
}
No5、玩转字符
//ASCII A->65 Z->90 a->97 z->122
void printLastChar(char &&c) {
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) {
std::cout << "输入不合规" << "\n";
return;
}
if (c == 'Z' || c == 'z') {
std::cout << c << "的后面没有字符" << "\n";
return;
}
std::cout << c << "后面的字符是:" << char(c + 1) << std::endl;
}
int ctoi(const char ch) {
if (ch < '0' || ch > '9') {
std::cout << "参数不合规" << std::endl;
return -1;
}
return int(ch - 48);
}
No6、计算字符串长度
其他要求
- 封装成一个函数
- for循环+数组表示法+临时变量
- while+指针表示+临时变量
- 不用临时变量->递归
int len(char *c) {
//1、for循环+临时变量+数组表示法
/*int len{0};
for (; c[len] != '\0'; ++len);
return len;*/
//2、while循环+临时变量+指针表示法
/*char *p = c;
int len{0};
while (*p++)
++len;
return len;*/
//3、递归思路+不采用临时变量
//递归开销大于循环)(
if (*c == '\0')return 0;//中止条件
c++;//指针后移
return len(c) + 1;//后移之后的长度+1=本来的长度
//递归中我们可以把下一层当作已知参数,比如
}
No7、显示字符串中的字符
void printStr(const char *c, bool isSequence = true) {
int len = strlen(c);
if (isSequence) {
for (int i = 0; i < len; ++i) {
std::cout << c[i] << " ";
}
} else {
for (int i = len - 1; i >= 0; --i) {
std::cout << c[i] << " ";
}
}
std::cout << std::endl;
}
No8、字符串反转
//使用另外一个字符串
char *reverse(const char *str){
char *newStr = new char[30];
int len = ::strlen(str);
::memset(newStr,0, sizeof(newStr));
for (int i = 0,j=len-1; i < len;)
newStr[i++] = str[j--];
return newStr;
}
//原地反转
void reserveS(char *str){
//abc d
int len = ::strlen(str);
int n = len/2;
for (int i = 0; i < n; ++i) {
char tmp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = tmp;
}
}
No9、二维数组的应用
任务一
double weights[3][4];
::memset(weights,0, sizeof(weights));
double total{0};
for (int i = 0; i < 3; ++i) {
double sum{0};
for (int j = 0; j < 4; ++j) {
double weight{0};
cout<<"请输入第"<<i+1<<"组第"<<j+1<<"个超女的体重:";
cin>>weight;
weights[i][j] = weight;
sum+=weight;
}
total+= sum;
cout<<"第"<<i+1<<"小组超女的平均体重:"<<sum/4<<"\n";
}
cout<<"所有小组超女的平均体重:"<<total/12<<endl;
任务二
char names[3][11];
::memset(names,0, sizeof(names));
for (int i = 0; i < 3; ++i) {
cout<<"请输入超女姓名:";
cin>>names[i];
}
for (int i = 0; i < 3; ++i) {
cout<<"表白:"<<names[i]<<"\n";
}