year = 2024
age = 22
print("Year:",year)
print("Age:", age)
🡮
Year: 2024
Age: 22
☑️
#include <stdio.h>
int main() {
int year = 2024;
int age = 22;
printf("Year: %d\n", year);
printf("Age: %d\n", age);
return 0;
}
🡮
Year: 2024
Age: 22
☑️
#include <iostream>
int main()
{
int year = 2024;
int age = 22;
std::cout << "Year: " << year << std::endl;
std::cout << "Age: " << age << std::endl;
}
🡮
Year: 2024
Age: 22
☑️
using System;
class Program
{
static void Main()
{
int year = 2024;
int age = 22;
Console.WriteLine("Year: " + year);
Console.WriteLine("Age: " + age);
}
}
Python 示例中的 `%.20f`,C 示例中的`%.20f`,C++ 示例中的`{:.20f}`,C# 示例中的`{0:F20}` 都是字符串格式化的方法。在 C++ 中格式化方法包含于`format`头文件中,为 C++20 的新特性,因此需要在 MSVS 中 `Project -> CPP Properties -> Configuration Properties -> General -> C++ Language Standard`,配置选项为 ISO C++20 Standard (/std:c++20)或更高版本,如图
❎
☑️
#include <stdio.h>
int main() {
float pi = 3.14159265358979323846f;
printf("Value of Pi: %.20f\n", pi);
}
🡮
Value of Pi: 3.14159274101257324219
☑️
#include <iostream>
#include <format>
int main()
{
float pi = 3.14159265358979323846f;
std::cout << std::format("Value of Pi: {:.20f}",pi) << std::endl;
}
🡮
Value of Pi: 3.14159274101257324219
☑️
using System;
class Program
{
static void Main()
{
float pi = 3.14159265358979323846f;
Console.WriteLine("Value of Pi: {0:F20}", pi);
}
}
🡮
Value of Pi: 3.14159274101257324219
double((双精度)浮点型)
☑️(float)
pi = 3.14159265358979323846
print("Value of Pi: %.20f"%pi)
🡮
Value of Pi: 3.14159265358979311600
☑️
#include <stdio.h>
int main() {
double pi = 3.14159265358979323846;
printf("Value of Pi: %.20f\n", pi);
}
🡮
Value of Pi: 3.14159265358979311600
☑️
#include <iostream>
#include <format>
int main()
{
double pi = 3.14159265358979323846;
std::cout << std::format("Value of Pi: {:.20f}",pi) << std::endl;
}
🡮
Value of Pi: 3.14159265358979311600
☑️
using System;
class Program
{
static void Main()
{
double pi = 3.14159265358979323846;
Console.WriteLine("Value of Pi: {0:F20}", pi);
}
}
🡮
Value of Pi: 3.14159265358979311600
complex(Complex number,复数)
形如 a + bi(a、b均为实数)的数为复数,其中 a 称为实部,b 称为虚部,i 为虚数单位,是 -1 的一个平方根,即$i^2=-1$。
☑️
z = 3 +4j
real_part = z.real
imaginary_part = z.imag
print(f"Complex number: {z}; Real part: {real_part}; Imaginary part: {imaginary_part}")
🡮
Complex number: (3+4j); Real part: 3.0; Imaginary part: 4.0
⭕
虽然 MSVS (Microsoft Visual Studio)支持 complex.h头文件,但复数类型的实现是在内部使用了一个结构体,与标准 C 不兼容,因此在 MSVS 中double complex z = 3.0 + 4.0 * I;的 C 语句不能通过编译,但下述的示例 C 代码可以在Code::Blocks(GNU GCC Compiler)等 IDE 中实现
is_true = True
is_false = False
if is_true:
print("The condition is true.")
if not is_false:
print("The condition is false.")
🡮
The condition is true.
The condition is false.
⭕
C 语言中,bool 类型在旧标准(C99之前)中不可用,通常用 int 表示,0 为假,而任何非0值为真。C99和更高版本,则可以使用<stdbool.h>,用 bool 关键字声明布尔类型的变量
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isTrue = true;
bool isFalse = false;
if (isTrue) {
printf("The condition is true.\n");
}
if (!isFalse) {
printf("The condition is false.\n");
}
return 0;
}
🡮
The condition is true.
The condition is false.
☑️
#include <iostream>
int main() {
bool isTrue = true;
bool isFalse = false;
if (isTrue) {
std::cout << "The condition is true." << std::endl;
}
if (!isFalse) {
std::cout << "The condition is false." << std::endl;
}
return 0;
}
🡮
The condition is true.
The condition is false.
☑️
using System;
class Program
{
static void Main()
{
bool isTrue = true;
bool isFalse = false;
if (isTrue)
{
Console.WriteLine("The condition is true.");
}
if (!isFalse)
{
Console.WriteLine("The condition is false.");
}
}
}
#include <stdio.h>
int main() {
signed int a = -10;
unsigned int b = 10;
short int c = 32767;
long int d = 2147483647;
unsigned long int e = 4294967295;
printf("%d,%u,%d,%ld,%lu", a, b, c, d, e);
return 0;
}
🡮
-10,10,32767,2147483647,4294967295
☑️
#include <iostream>
#include <format>
int main() {
signed int a = -10;
unsigned int b = 10;
short int c = 32767;
long int d = 2147483647;
unsigned long int e = 4294967295;
std::cout << std::format("{},{},{},{},{}", a,b,c,d,e) << std::endl;
return 0;
}
🡮
-10,10,32767,2147483647,4294967295
☑️
using System;
class Program
{
static void Main()
{
short a = -32768; // short, 16-bit signed integer
ushort b = 65535; // ushort, 16-bit unsigned integer
int c = -2147483648; // int, 32-bit signed integer
uint d = 4294967295; // uint, 32-bit unsigned integer
long e = -9223372036854775808; // long, 64-bit signed integer
ulong f = 18446744073709551615; // ulong, 64-bit unsigned integer
Console.WriteLine("{0},{1},{2},{3},{4},{5}", a,b,c,d,e,f);
}
}
C++ 中的 auto 和 C# 中的 var 关键字,都允许编译器从赋值操作符的右侧表达式中推断变量的数据类型
示例中为了查看变量的数据类型,在 C++ 中包含了头文件 typeinfo,用 typeid()查看;在 C# 中,可以直接用 var.GetType() 方法查看
❎
❎
☑️(auto)
#include <iostream>
#include <format>
#include <typeinfo>
int main()
{
auto x = 3.14;
auto y = 42;
auto name = "Alice";
std::cout << std::format("{}:{}\n{}:{};\n{}:{}", typeid(x).name(),x, typeid(y).name(),y, typeid(name).name(),name) << std::endl;
}
🡮
double:3.14
int:42;
char const * __ptr64:Alice
☑️(var)
using System;
class Program
{
static void Main()
{
var x = 3.14;
var y = 42;
var name = "Alice";
Console.WriteLine("{0}:{1}\n{2}:{3};\n{4}:{5}", x.GetType(),x,y.GetType(), y,name.GetType(), name);
}
}
using System;
class Program
{
static void Main()
{
int x = 10;
object obj = x; // Boxing: x is boxed into obj
int y = (int)obj; // Unboxing: obj is unboxed back into an int
object objStr = "Hello";
object objDouble = 3.14;
Console.WriteLine("{0}:{1}\n{2}:{3}\n{4}:{5}\n{6}:{7}\n{8}:{9}",
x.GetType(), x, obj.GetType(), obj, y.GetType(), y, objStr.GetType(), objStr, objDouble.GetType(), objDouble);
}
}
def square(x):
return x * x
num1 = 12.34567
num2 = 7
precision = 2
result = f"The sum of {num1:.2f} and {num2} is {(num1 + num2):.{precision}f}.\n The square of {num2} is {square(num2)}."
print(result)
🡮
The sum of 12.35 and 7 is 19.35.
The square of 7 is 49.
using System;
class Program
{
static double square(double x)
{
return x * x;
}
static void Main()
{
double num1 = 12.34567;
double num2 = 7;
string output = $"The sum of {num1:F2} and {num2} is {(num1 + num2):F2}.\n The square of {num2} is {square(num2)}.";
Console.WriteLine(output);
}
}
🡮
The sum of 12.35 and 7 is 19.35.
The square of 7 is 49.