C++、java、python的一些区别

1.变量类型

变量赋值命名不同

Python 中的变量赋值不需要类型声明

counter = 100 # 赋值整型变量

miles = 1000.0 # 浮点型

name = "John" # 字符串

       C++中变量必须有效的声明,变量可以在声明的时候被初始化(指定一个初始值)

int d = 3, f = 5; // 定义并初始化 d 和 f

char x = 'x'; // 变量 x 的值为 'x'

在Java语言中,所有的变量在使用前必须声明

int a, b, c;         // 声明三个int型整数:a、 b、c

int d = 3, e = 4, f = 5; // 声明三个整数并赋予初值

String s = "runoob"; // 声明并初始化字符串 s

double pi = 3.14159; // 声明了双精度浮点型变量 pi

char x = 'x';        // 声明变量 x 的值是字符 'x'。

2.运算符

算数运算符:

Python算术运算符

**  : 幂 - 返回x的y次幂

a**b   //为10的20次方

 输出结果 :

100000000000000000000

//   : 取整除 - 返回商的整数部分(向下取整

>>> 9//2

4

>>> -9//2

-5

C++算数运算符:

++ : 自增运算符,整数值增加 1

1++ 得 2

       --:自减运算符,整数值减少1

       1-- 得 0

       Java算数运算符:

       ++ : 自增运算符,整数值增加 1

1++ 得 2

++1 得2

       --:自减运算符,整数值减少1

       --1 得 0

       1—得0

前缀自增自减法(++a,--a): 先进行自增或者自减运算,再进行表达式运算。

后缀自增自减法(a++,a--): 先进行表达式运算,再进行自增或者自减运算 实例:

public class selfAddMinus

{

public static void main(String[] args){

int a = 5;//定义一个变量;

int b = 5;

int x = 2*++a;

int y = 2*b++;

System.out.println("自增运算符前缀运算后a="+a+",x="+x); System.out.println("自增运算符后缀运算后b="+b+",y="+y);

}

}

自增运算符前缀运算后a=6,x=12

自增运算符后缀运算后b=6,y=10

3.循环

while循环

Python:

while 判断条件:

       执行语句……

count = 0

while (count < 3):

print 'The count is:', count

count = count + 1

print "end"

 

The count is: 0

The count is: 1

The count is: 2

End

       C++:

C++ 中 while 循环的语法:

while(condition)

{

   statement(s);

}

statement(s) 可以是一个单独的语句,也可以是几个语句组成的代码块。condition 可以是任意的表达式,当为任意非零值时都为真。当条件为真时执行循环

#include <iostream>

using namespace std;

 

int main ()

{

   // 局部变量声明

   int a = 1;

 

   // while 循环执行

   while( a < 4 )

   {

       cout << "a 的值:" << a << endl;

       a++;

   }

 

   return 0;

}

a 的值: 1

a 的值: 2

a 的值: 3

java:

while( 布尔表达式 )

{

//循环内容

}

只要布尔表达式为 true,循环就会一直执行下去

public class Test {

 public static void main(String args[]) {

int x = 1;

while( x < 3 )

{

System.out.print("value of x : " + x ); x++;

System.out.print("\n");

}

}

}

value of x : 1

value of x : 2

 

For循环

 

Python语法:

for循环的语法格式如下:

for iterating_var in sequence:

           statements(s)

for letter in 'Python':

print '当前字母 :', letter

当前字母 : P

当前字母 : y

当前字母 : t

当前字母 : h

当前字母 : o

当前字母 : n

C++ 中 for 循环的语法:

for ( init; condition; increment )

{

   statement(s);

}

#include <iostream>

using namespace std;

int main () { // for 循环执行

 for( int a = 1; a < 3; a = a + 1 )

{

cout << "a 的值:" << a << endl;

}

return 0;

}

a 的值: 1

a 的值: 2

Java:

for(初始化; 布尔表达式; 更新)

{

//代码语句

}

public class Test

{

public static void main(String args[])

{

for(int x = 1; x < 3; x = x+1)

{

System.out.print("X的值 : " + x );

System.out.print("\n");

}

}

}

X的值 :1

X的值 :2

4.判断语句

Python 编程中 if 语句用于控制程序的执行,基本形式为:

if 判断条件:

          执行语句……

else:

            执行语句……

flag = False name = 'luren'

if name == 'python': # 判断变量否为'python'

flag = True # 条件成立时设置标志为真

print 'welcome boss' # 并输出欢迎信息

else:

print name # 条件不成立时输出变量名称

luren            # 输出结果

C++:

一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。

if(boolean_expression)

{

   // 如果布尔表达式为真将执行的语句

}

else

{

   // 如果布尔表达式为假将执行的语句

}

#include <iostream>

using namespace std;

 

int main ()

{

   // 局部变量声明

   int a = 100;

 

   // 检查布尔条件

   if( a < 20 )

   {

       // 如果条件为真,则输出下面的语句

       cout << "a 小于 20" << endl;

   }

   else

   {

       // 如果条件为假,则输出下面的语句

       cout << "a 大于 20" << endl;

   }

   cout << "a 的值是 " << a << endl;

 

   return 0;

}

结果:

a 大于 20

a 的值是 100

 

Java:

一个 if 语句包含一个布尔表达式和一条或多条语句。

if(布尔表达式)

{

//如果布尔表达式为true将执行的语句

}

public class Test {

public static void main(String args[]){

int x = 1;

if( x < 20 ){

System.out.print("结果小于20");

}

}

}

结果:结果小于20

5.数组

Python 元组:

tup1 = ('physics', 'chemistry', 1997, 2000)

tup2 = (1, 2, 3, 4, 5, 6, 7 )

print "tup1[0]: ", tup1[0]

print "tup2[1:5]: ", tup2[1:5]

以上实例输出结果:

tup1[0]:  physics

tup2[1:5]:  (2, 3, 4, 5)

C++数组:

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

int main () {

int n[ 10 ]; // n 是一个包含 10 个整数的数组 // 初始化数组元素

for ( int i = 0; i < 10; i++ )

{

n[ i ] = i + 100; // 设置元素 i 为 i + 100

}

cout << "Element" << setw( 13 ) << "Value" << endl;

// 输出数组中每个元素的值

for ( int j = 0; j < 10; j++ )

{

cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;

}

return 0;

}

Element        Value

      0          100

      1          101

      2          102

      3          103

      4          104

      5          105

      6          106

      7          107

      8          108

      9          109

Java数组:

public class TestArray

{

public static void main(String[] args) {

// 数组大小 int size = 10;

// 定义数组 double[] myList = new double[size];

myList[0] = 5.6;

myList[1] = 4.5;

 myList[2] = 3.3;

myList[3] = 13.2;

myList[4] = 4.0;

myList[5] = 34.33;

myList[6] = 34.0;

myList[7] = 45.45;

myList[8] = 99.993;

 myList[9] = 11123;

// 计算所有元素的总和

double total = 0;

for (int i = 0; i < size; i++)

{ total += myList[i];

}

System.out.println("总和为: " + total);

}

}

以上实例输出结果为:

总和为: 11367.373

6.基础语法

Python是交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码

>>> print "Hello, Python!"

Hello, Python!

C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互。

#include <iostream>

using namespace std;

// main() 是程序开始执行的地方

int main() {

cout << "Hello World"; // 输出 Hello World

return 0;

}

一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作。

public class HelloWorld

{

/* 第一个Java程序 * 它将打印字符串 Hello World */

public static void main(String []args) {

 System.out.println("Hello World"); // 打印 Hello World

}

}

7.字符串

Python 字符串

 

var1 = 'Hello World!'

var2 = "Python Runoob"

 

C++ 字符串

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

或者

char greeting[] = "Hello";

 

Java字符串:

String greeting = "helloword";

 

8.函数

Python函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。

def printme( str ):

"打印传入的字符串到标准显示设备上"

print str

return

C++:

// 函数返回两个数中较大的那个数

int max(int num1, int num2)

{

// 局部变量声明 i

nt result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

Java:

/** 返回两个整型变量数据的较大值 */

public static int max(int num1, int num2)

{ int result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

9.日期时间

Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间

import time; # 引入time模块

ticks = time.time()

print "当前时间戳为:", ticks

当前时间戳为: 1459994552.51

C++ 继承了 C 语言用于日期和时间操作的结构和函数

为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。

#include <iostream>

#include <ctime>

using namespace std;

int main( ) {

// 基于当前系统的当前日期/时间

time_t now = time(0);

// 把 now 转换为字符串形式

char* dt = ctime(&now);

cout << "本地日期和时间:" << dt << endl;

// 把 now 转换为 tm 结构

tm *gmtm = gmtime(&now); dt = asctime(gmtm);

cout << "UTC 日期和时间:"<< dt << endl;

}

本地日期和时间:Sat Jan  8 20:07:41 2011

 

UTC 日期和时间:Sun Jan  9 03:07:41 2011

 

Java 日期时间:

import java.util.Date;

public class DateDemo {

public static void main(String args[]) {

// 初始化 Date 对象

 Date date = new Date();

// 使用 toString() 函数显示日期时间

System.out.println(date.toString());

}

}

结果:Mon May 04 09:51:52 CDT 2013

 

10读写文件

Python File(文件) 方法

Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。

open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

参数说明:

file: 必需,文件路径(相对或者绝对路径)。

mode: 可选,文件打开模式

buffering: 设置缓冲

encoding: 一般使用utf8

errors: 报错级别

newline: 区分换行符

closefd: 传入的file参数类型

opener:

C++ 文件

在从文件读取信息或者向文件写入信息之前,必须先打开文件。ofstream 和 fstream 对象都可以用来打开文件进行写操作,如果要打开文件进行读操作,则使用 ifstream 对象。

open() 函数是 fstream、ifstream 和 ofstream 对象的一个成员

void open(const char *filename, ios::openmode mode);

 

Java:

FileInputStream该流用于从文件读取数据,它的对象可以用关键字 new 来创建。

InputStream f = new FileInputStream("C:/java/hello");

或者

File f = new File("C:/java/hello"); InputStream out = new FileInputStream(f);

猜你喜欢

转载自www.cnblogs.com/liulala2017/p/10694912.html