using std::cout;
using std::endl;
using std::cin;
namespace的个别引用
using std::cout;
using std::endl;
using std::cin;
namespace的个别引用
|| 或运算
&& 与运算
表达式1?表达式2:表达式3
//if(表达式)
{
表达式2
}
else
{
表达式3
}
#include <iostream>
#include <stdio.h>
int main()
{
// 命名空间std
/*
cout or endl就包含在std这个命名空间中
*/
using namespace std;
// cout << "..." << endl;等于
// c语言中的printf("...\n");
cout << "Hello world!" << endl;
printf("hello world\n");
return 0;
}
1.用switvh改写下面的代码段
if(ch=='A')
a_grade++;
else if(ch=='B')
b_grade++;
else if(ch=='C')
c_grade++;
else if(ch=='D')
d_grade++;
else
f_grade++;
答:
switch(ch)
{
case ‘A’: a_grade++;
break;
case ‘B’: b_grade++;
break;
case ‘C’: c_grade++;
break;
case ‘D’: d_grade++;
break;
default: f_grade++;
break;
}
2.求得20!
#include "pch.h"
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
long long s=1;
for (int i = 1; i <= 20; i++)
s *= i;
cout << "20!=" << s << endl;
return 0;
}
3.求得1!+2!+3!+...+20!
#include "pch.h"
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
long long s=1,a=0;
for (int i = 1; i <= 20; i++) {
s *= i;
a += s;
}
cout << "1!+2!+3!+...+20!=" << a << endl;
return 0;
}
4.实现函数判断一字符串是否是回文。若是回文,函数返回值为1,否则返回值0。
5.输入三个整数分别放在变量a,b,c中,然后把输入的数据从小到大顺序放到变量a,b,c中,最后输出a,b,c的值。
#include "pch.h"
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
int a, b, c, m;
cout << "请输入第一个值" << endl;
cin >> a;
cout << "请输入第二个值" << endl;
cin >> b;
cout << "请输入第三个值" << endl;
cin >> c;
if (a > b)
{
m = a; a = b; b = m;
}
if (a > c)
{
m = a; a = c; c = m;
}
if (b > c)
{
m = b; b = c; c = m;
}
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
return 0;
}
格式化:ctrl+k >>ctrl+f
注释:快捷键快速注释 Ctrl+k+c 快速取消注释 Ctrl+k+u
ctrl+k ctrl+c 注释
ctrl+k ctrl+u 取消注释
判断是否回文字符串练习题
#include "pch.h"
#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
int main()
{
string str;
cin >> str;
for (int i = 0; i < str.size() / 2; i++)
{
if (str[i] != str[str.size() - 1 - i])
{
cout << "不是回文字符串" << endl;
return false;
}
}
cout << "是回文字符串" << endl;
}
#include <iostream>
#include <stdio.h>
int main()
{
// 命名空间std
/*
cout or endl就包含在std这个命名空间中
而std就包含在iostream这个头文件中
printf包含在stdio.h这个头文件中中
*/
using namespace std;
//using std::cout;
//using std::endl;
//using std::cin;
//
// cout << "..." << endl;等于
// c语言中的printf("...\n");
cout << "Hello world!" << endl;
printf("hello world\n");
return 0;
}
main函数在一个程序中有且只有一个,它是操作系统调用程序的入口
as
int main() {} 函数是程序入口
#include <iostream>
using namespace std;
int Height();
bool TimeRoutine(struct timeStruct* timeRoutine);
float ClassAndGrade(int boyNumber, int girlNumber);
// 第二题使用的结构体
struct timeStruct {
int day;
int hour;
int minute;
int second;
};
int main()
{
//// 第一题
//int height = 0;
//height = Height();
//cout << "身高是:" << height << "cm." << endl;
/*********************************************************************************************************************************************************/
// 第二题
// 编写一个程序,让用户输入秒,然后把它转换成多少天,多少小时,多少分钟和多少秒显示出来
//bool timeBool = false;
//timeStruct timeStr;
//timeStr.day = 0;
//timeStr.hour = 0;
//timeStr.minute = 0;
//timeStr.second = 0;
//timeBool = TimeRoutine(&timeStr);
//if (true == timeBool)
//{
// cout << "时间是:" << timeStr.day << "天," << timeStr.hour << "小时," << timeStr.minute << "分钟," << timeStr.second << "秒." << endl;
//}
//else
//{
// cout << "ERROR!" << endl;
//}
/*********************************************************************************************************************************************************/
// 第三题
// 要求用户输入一个班级的男生女生的数量,并输出女生的比例(百分比)
//int boy = 0;
//int girl = 0;
//if (0 == boy || 0 == girl)
//{
// cout << "输入男生的人数:";
// cin >> boy;
// cout << endl;
// cout << "输入女生的人数:";
// cin >> girl;
// cout << endl;
//}
//float number = ClassAndGrade(boy, girl);
//cout << "比例是:" << number << "%" << endl;
return 0;
}
// 第三题
float ClassAndGrade(int boyNumber, int girlNumber)
{
int sum = 0;
float girl = 0;
sum = boyNumber + girlNumber;
girl = ((float)girlNumber / (float)sum) * 100;
if (0 == girl)
{
return 0;
}
return girl;
}
// 第二题
bool TimeRoutine(struct timeStruct *timeRoutine)
{
long int secondNumber = 0;
if (0 == secondNumber)
{
cout << "输入一个时间秒:";
cin >> secondNumber;
}
timeRoutine->day = (int)(secondNumber / 60 / 60 / 24);
timeRoutine->hour = (int)(secondNumber / 60 / 60 % 24);
timeRoutine->minute = (int)(secondNumber / 60 % 60);
timeRoutine->second = (int)(secondNumber % 60);
if (0 == timeRoutine->day && 0 == timeRoutine->hour && 0 == timeRoutine->minute && 0 == timeRoutine->second)
{
return false;
}
else
{
return true;
}
}
// 第一题
int Height()
{
float heightM = 0;
int heightCM = 0;
cout << "输入身高:" << endl;
cin >> heightM;
cout << endl;
heightCM = (int)(heightM * 100);
//cout << "身高是:" << heightCM << "cm" << endl;
return heightCM;
}
#include<iostream>
int main(){
//using name std;
using std::end;
using std::count;
using std::cin;
cout<<"你好";
count<<endl;
count<<"我开始学编程了"
cin.get();
return 0
}
C++11中for的进阶使用
for(int temp : 数组名)
{
cout << temp << " ";
}
可让数组全部输出
for(int& temp : 数组名)
{
cout << temp << " ";
temp = 10;
}
&引用,可将数组名对应的地址存储里的值通过temp进行修改