第一、基本程序结构都一样
第二、输出与输入不同
C++的输出是:cout << "!!!Hello World!!!" << endl;
C#的输出是:console.write("!!!Hello World!!!");//输出语句,不自动换行
C++的输入是:
int score = 0;
cout << "用户输入分数:" << endl;
cin >> score;
C#的输入是:
int score = 0;
console.write("用户输入分数:");
score = console.ReadKey();//输入语句,不自动换行
第三,函数的调用不同
C++的函数调用,需要在调用之前,做个声明
如:int BaseTest1();
C#的函数调用,不需要在调用之前做声明,直接调用即可。
第四、格式习惯不同
C++的格式习惯如下
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
C#的格式习惯如下,主要不同在大括号
int main()
{
console.write("!!!Hello World!!!");
return 0;
}
#include <iostream>
using namespace std;
/*
下面是2个函数调用声明
*/
int BaseTest1();
int BaseTest2();
/*
程序入口函数
*/
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
BaseTest1();
BaseTest2();
return 0;
}
/*
下面是16个函数
*/
/*
选择结构 单行if语句
*/
int BaseTest1() {
//用户输入分数,如果分数大于60,视为考上一本大学,在屏幕上输出
//1.用户输入分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << score << endl;
//3.判断分数是否大于600,如果大于600,那么输出
if (score > 600)
{
cout << "Congratulations on entering a key university" << endl;
}
system("pause");
return 0;
}
/*
选择结构 多行if语句
*/
int BaseTest2() {
//输入考试分数,如果大于600,视为考上一本,在屏幕输出
//如果没考上一本大学,打印未考上一本大学
//1.输入考试分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << score << endl;
//3.判断
if (score > 600)
{
cout << "Congratulations on entering a key university" << endl;
}
else
{
cout << "sorry,you didn't get into a key university " << endl;
}
system("pause");
return 0;
}
/*
选择结构 多条件if语句
*/
int BaseTest3() {
//输入一个考试分数,如果大于600分,视为考上一本大学,在屏幕输出
//大于500,视为考上二本
//大二400,视为考上三本
//小于等于400,视为为考上本科,在屏幕输出
//1.输入考试分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << score << endl;
//3.判断
if (score > 600)
{
cout << "恭喜考上一本" << endl;
}
else if (score > 500)
{
cout << "恭喜考上二本" << endl;
}
else if (score > 400)
{
cout << "恭喜考上三本" << endl;
}
else
{
cout << "很可惜,你没考上本科" << endl;
}
system("pause");
return 0;
}
/*
嵌套if语句
*/
int BaseTest4() {
//1.输入考试分数
int score = 0;
cout << "input your score:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "your score is " << score << endl;
//3.判断
if (score > 600)
{
cout << "恭喜考上一本" << endl;
if (score > 700)
{
cout << "恭喜考上北大" << endl;
}
else if(score>650)
{
cout << "恭喜考上清华" << endl;
}
else
{
cout << "恭喜考上人大" << endl;
}
}
else if (score > 500)
{
cout << "恭喜考上二本" << endl;
}
else if (score > 400)
{
cout << "恭喜考上三本" << endl;
}
else
{
cout << "很可惜,你没考上本科" << endl;
}
system("pause");
return 0;
}
/*
输入三只小猪的体重,判断谁最重
*/
int BaseTest5() {
//1.分别输入三只小猪的体重
int A = 0;
cout << "请输入小猪A的体重:" << endl;
cin >> A;
int B = 0;
cout << "请输入小猪B的体重:" << endl;
cin >> B;
int C = 0;
cout << "请输入小猪C的体重:" << endl;
cin >> C;
cout << "小猪A的体重为" << A << endl;
cout << "小猪B的体重为" << B << endl;
cout << "小猪C的体重为" << C << endl;
//2.if语句判断
if (A > B)
{
if (A > C)
{
cout << "小猪A最重" << endl;
}
else
{
cout << "小猪C最重" << endl;
}
}
else
{
if (B > C)
{
cout << "小猪B最重" << endl;
}
else
{
cout << "小猪C最重" << endl;
}
}
system("pause");
return 0;
}
/*
三目运算符
*/
int BaseTest6() {
//创建三个变量 a b c
//比较a与b的值,将较大的值赋值给c
int a = 10;
int b = 20;
int c = 0;
c = (a > b ? a : b);
cout << "c = " << c << endl;
//在C++中三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
/*
switch语句
*/
int BaseTest7() {
//给一个电影打分
//10~9经典
//8~7非常好
//6~5一般
//5以下烂片
//1.提示用户进行评分
int score = 0;
cout << "请输入您的电影评分:" << endl;
cin >> score;
cout << "您的电影评分为:" << score << endl;
//2.switch
switch (score)
{
case 10:
cout << "电影很经典" << endl;
break;
case 9:
cout << "电影很经典" << endl;
break;
case 8:
cout << "电影很非常好" << endl;
break;
case 7:
cout << "电影很非常好" << endl;
break;
case 6:
cout << "电影很一般" << endl;
break;
case 5:
cout << "电影很一般" << endl;
break;
default :
cout << "电影很烂" << endl;
break;
}
system("pause");
return 0;
}
/*
while循环
*/
int BaseTest8() {
//在屏幕中打印0~9这10个数
int num = 0;
while (num < 10)
{
cout << num++ << endl;
}
system("pause");
return 0;
}
/*
猜数字
*/
int BaseTest9() {
//添加随机种子,利用当前系统时间生成随机数,防止每次随机数都一样
srand((unsigned int)time(NULL));
//系统生成随机数
int num = rand() % 100 + 1;//rand() % 100生成0~99的随机数
//玩家猜测
int val = 0;
int i = 1;
while (i)
{
cout << "请输入您猜测的数字:" << endl;
cin >> val;
//判断结果
if (num > val)
{
cout << "猜小了" << endl;
}
else if (num < val)
{
cout << "猜大了" << endl;
}
else
{
cout << "猜对了" << endl;
i = 0;
}
}
system("pause");
return 0;
}
/*
do_while语句
*/
int BaseTest10() {
//在屏幕中输出0~9是个数字
int num = 0;
do
{
cout << num++ << endl;
} while (num < 10);
system("pause");
return 0;
}
/*
水仙花数是指一个三位数,他的每个位上的数字的3次幂之和等于它本身
*/
int BaseTest11() {
//生成一个数
int num = 100;
int a = 0;//百位
int b = 0;//十位
int c = 0;//个位
//循环找数
do
{
a = num / 100;
b = num / 10 % 10;
c = num % 10;
if (a * a * a + b * b * b + c * c * c == num)
{
cout << num << endl;
}
num++;
} while (num < 1000);
system("pause");
return 0;
}
/*
for循环语句
*/
int BaseTest12() {
//在屏幕输出0~9是个数字
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
/*
敲桌子
从1开始疏导数字100,如果数字个位含有7,或者数字十位含有7
或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出
*/
int BaseTest13() {
//定义特征变量
int a = 0;//个位
int b = 0;//十位
int c = 0;//除7后的余数
//直接for循环
for (int i = 1; i < 101; i++)
{
a = i % 10;
b = i / 10;
c = i % 7;
if (a == 7 | b == 7 | c == 0)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
/*
break的使用时机,尽量少用
*/
int BaseTest14() {
//1.出现在switch中
cout << "请选择副本难度" << endl;
cout << "1.普通" << endl;
cout << "2.中等" << endl;
cout << "3.困难" << endl;
int select = 0;//创建选择结果的变量
cin >> select;//等待用户输入
switch (select)
{
case 1:
cout << "您选择的难度是普通" << endl;
break;
case 2:
cout << "您选择的难度是中等" << endl;
break;
case 3:
cout << "您选择的难度是困难" << endl;
break;
default :
break;
}
//2.出现在循环语句中
for (int i = 0; i < 10; i++)
{
//如果i=5,推出循环,不再打印
if (i == 5)
{
break;//退出循环
}
cout << i << endl;
}
//3.出现在嵌套循环语句中
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (j == 5)
{
break;
}
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
/*
continue语句
*/
int BaseTest15() {
for (int i = 0; i <= 100; i++)
{
//如果是奇数输出,偶数不输出
if (i % 2 == 0)
{
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}
/*
goto语句
*/
int BaseTest16() {
cout << "1" << endl;
cout << "2" << endl;
cout << "3" << endl;
goto FLAG;
cout << "4" << endl;
cout << "5" << endl;
cout << "6" << endl;
cout << "7" << endl;
FLAG:
cout << "8" << endl;
system("pause");
return 0;
}