第一、基本程序结构都一样
第二、输出与输入不同
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;

本文对比分析了C++和C#在基本程序结构、输出输入、函数调用以及格式习惯上的异同。C++使用cout进行输出,cin获取输入,而C#则使用console.write和ReadKey。在函数调用上,C++需要预先声明,C#则不需要。格式习惯方面,C++使用大括号包裹代码,C#的大括号风格有所不同。此外,还通过示例展示了两者的不同应用,如条件判断和循环控制。
最低0.47元/天 解锁文章
165

被折叠的 条评论
为什么被折叠?



