📄 fundament.cpp
字号:
#include<iostream.h>
//【例1.1】已知半径为2,求圆面积和周长。
#define PI 3.1415926
const double r=2;
void main()
{
double area,circumference;
area=PI*r*r;
circumference=2*PI*r;
cout<<"area="<<area<<endl; //输出
cout<<" circumference="<< circumference<<endl;
//int unsigned a=2,b;
// b=a<<2;
// cout<<b<<" "<<hex<<~a<<endl;
}
//【例1.2】用cout输出字符串
void main1_2(void)
{
cout<<"Hello, World!"<<endl;
}
//【例1.3】用cout输出整数和实数
void main1_3()
{
int a=2, b=3, c=5, d=6;
float x=3.2, y=4.6;
cout<<4<<'\t'<<5.34<<endl;
cout<<a<<'\t'<<b<<'\t'<<c+d<<endl;
cout<<x<<'\t'<<y+2<<endl;
}
//【例1.4】存储和刷新缓冲区示例
void main1_4()
{
int a=3, b=5, c=7;
cout<<a<<'\t'<<a++<<endl; //A
cout<<b<<'\t'<<++b<<endl; //B
cout<<++c<<'\t'<<c<<endl; //C
}
//【例1.5】以八进制和十六进制的形式输出整数
void main1_5()
{
int i=16, j=34, k=40;
float x=11.34;
cout<<oct<<i<<'\t'<<hex<<j<<endl;
cout<<k<<'\t'<<x<<endl;
}
//【例1.6】实数的输出形式
#include<iomanip.h>
void main1_6(void)
{
float a=123.4, b=1000;
cout.setf(ios::scientific,ios::floatfield); //设置为科学表示格式,只适用于实型
cout<<setprecision(6)<<a*b<<endl; //设置精度为6
cout.setf(ios::fixed,ios::floatfield); //设置为定点数格式,只适用于实型
cout<<setprecision(2)<<a*b<<endl; //设置精度为2
}
//【例1.7】库函数setw(int)的使用
void main1_7()
{
int a=2, b=3, c=4;
cout<<a<<setw(3)<<b<<setw(5)<<c<<endl;
cout<<a+1<<setw(2)<<b+2<<setw(7)<<c+3<<endl;
}
//【例1.8】用cin输入整数和实数
void main1_8()
{
int a;
float b;
cin>>a>>b; //A
}
//【例1.9】以八进制和十六进制输入整数
void main1_9()
{
int a, b, c;
cin>>hex>>a>>b;
cin>>oct>>c;
cout<<a<<'\t'<<b<<'\t'<<c<<'\n';
}
//【例1.10】用cin输入字符数据
void main1_10()
{
char ch1, ch2, ch3;
cin>>ch1>>ch2>>ch3;
cout<<ch1<<'\t'<<ch2<<'\t'<<ch3<<'\n';
}
//【例1.11】用函数cin.get(char)获取字符
void main1_11()
{
char ch1, ch2, ch3;
cin.get(ch1);
cin.get(ch2);
cin.get(ch3);
cout<<ch1<<'\t'<<ch2<<'\t'<<ch3<<'\n';
}
//【例1.12】printf和scanf应用举例
#include <stdio.h>
void main1_12()
{
int i,j;
printf("i, j=?\n");
scanf("%d, %d",&i,&j);
printf("i=%d, j=%d",i,j);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -