cin cout
//mysecond.cpp -- display a message
#include
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?"<< endl;
cin >> carrots;
cout << "How many potatoes do you have?"<< endl;
int potatoes;
cin >> potatoes;
int all;
all = carrots;
all += potatoes;
cout << "Now you have"<< all << "vegetables."<
math.h函数调用
//sqrt.cpp -- using the sqrt{} function
#include
#include
int main()
{
using namespace std;
double area;
cout << "Enter the floor area,in square feet,of your home: ";
cin >> area;
double side = sqrt(area);
cout << "That's the equivalent of a square " << side
<< "feet to the side."<< endl;
cout << "How many fascinating!"<< endl;
return 0;
}
自定义函数
//ourfunc.cpp -- defining your own function
#include
void simon(int n)
{
using namespace std;
cout << "Simon say touch your toes" << n << "times."<> count;
simon(count);
cout << "Done!" << endl;
return 0;
}
limit.h
//limit.cpp -- defining your own function
#include
#include
int main()
{
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
cout << "int is "<< sizeof(int) <<"bytes." <
array
#include
int main()
{
using namespace std;
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 6;
int yamcosts[3] = {20,30,5};
cout << "Total yams = ";
cout << yams[0] + yams[1] + yams[2] << endl;
cout << "The package with " << yams[1] << " yams costs ";
cout << yamcosts[1] << " cents per yam.\n";
int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1];
total = total + yams[2] * yamcosts[2];
cout << "The total yam array = "<< sizeof(yams);
cout << " bytes.\n";
cout << "Size of one element = "<< sizeof(yams[0]);
cout << " bytes.\n";
return 0;
}
instr1
//instr.cpp -- defining your own function
#include
int main()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin >> name;
cout << "Enter your favorite dessert:\n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
getline
//instr.cpp -- defining your own function
#include
int main()
{
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
cout << "Enter your name:\n";
cin.getline(name,ArSize);
cout << "Enter your favorite dessert:\n";
cin.getline(dessert,ArSize);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
//structre.cpp -- defining your own function
#include
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable guest1 =
{
"YiXuan Chen",
1.88,
29.99
};
inflatable guest2 =
{
"River Chandler",
3.12,
32.99
};
cout << " Expand your guest list with "<< guest1.name;
cout << " and " << guest2.name << "!\n";
cout << " You can have both for $";
cout << guest1.price + guest2.price << "!\n";
return 0;
}
union
enum
pointer
//pointer.cpp -- defining your own pointer
#include
int main()
{
using namespace std;
int updates = 6;
int * p_updates;
p_updates = &updates;
cout << "Values:updates = " << updates;
cout << ", *p_updates = " << p_updates << endl;
cout << "Address:&updates = " << &updates;
cout << ",p_updates = " << p_updates << endl;
*p_updates = *p_updates + 1;
cout << "Now updates = " << updates << endl;
return 0;
}
for 循环
//for.cpp -- defining your own pointer
#include
int main()
{
using namespace std;
int i;
for(i=0;i<5;i++)
{
cout << "C++ knows loops.\n";
}
cout << "C++ knows when to stop!";
return 0;
}
factorial
//for.cpp -- defining your own pointer
#include
const int ArSize = 16;
int main()
{
long long factorials[ArSize];
factorials[1] = factorials[0] = 1LL;
for (int i = 2;i
逗号运算符
//comma.cpp -- defining your own pointer
#include
#include
int main()
{
using namespace std;
cout << "Enter a word! ";
string word;
cin >> word;
char temp;
int i,j;
for (j=0,i=word.size()-1;j
while
//comma.cpp -- defining your own pointer
#include
const int ArSize = 20;
int main()
{
using namespace std;
char name[ArSize];
cout << "Your First Name,Please: ";
cin >> name;
cout << "Here is your name,verticalized and Ascllied:\n";
int i = 0;
while(name[i]!='\0')
{
cout << name[i] << ": " << int(name[i]) << endl;
i++;
}
return 0;
}
dowhile
//dowhile.cpp -- defining your own pointer
#include
int main()
{
using namespace std;
int n;
cout << "Enter numbers in the range 1-19 to find: ";
cout << "My favorite number\n";
do
{
cin >> n;
}while(n!=7);
cout << "Yes,7 is my favorite.\n";
return 0;
}
if
//ifelseif.cpp -- defining your own pointer
#include
const int Fave = 27;
int main()
{
using namespace std;
int n;
cout << "Enter numbers in the range 1-100 to find: ";
cout << "My favorite number:";
do
{
cin >> n;
if (n < Fave)
{cout << "Too Low -- guess again: ";}
else if (n > Fave)
{cout << "Too high -- guess again: ";}
else
{cout << "You did good job!";}
}while(n!=Fave);
cout << "Yes,"<< Fave <<" is my favorite.\n";
return 0;
}
switch
//switch.cpp -- defining your own pointer
#include
void showmenu();
void report();
void comfort();
int main()
{
using namespace std;
showmenu();
int choice;
cin >> choice;
while(choice != 5)
{
switch(choice)
{
case 1: cout << "\a\n";
case 2: report();
break;
case 3: cout << "The boss was in all day.\n";
break;
case 4: comfort();
break;
default : cout << "That's not a choice.\n";
}
showmenu();
cin >> choice;
}
cout << "Bye!\n";
return 0;
}
void showmenu()
{
std :: cout << "Please enter 1,2,3,4, or 5:\n"
"1) alarm 2) report\n"
"3) alibi 4) comfort\n"
"5) quit\n";
}
void report()
{
std :: cout << "It's been an excellent week for business.\n"
"Sales are up 120%.Expenses are down 35%.\n";
}
void comfort()
{
std :: cout << "Your employees think you are the finest CEO\n";
}
enum
//enum.cpp -- defining your own pointer
#include
enum {red,orange,yellow,green,blue,violet,indigo};
int main()
{
using namespace std;
cout << "Enter color code (0-6): ";
int code;
cin >> code;
while (code >= red && code <= indigo)
{
switch(code)
{
case red: cout << "Her lips are red.\n";
break;
case orange: cout << "Her hair are orange.\n";
break;
case yellow: cout << "Her shoes are yellow.\n";
break;
case green: cout << "Her nails are green.\n";
break;
case blue: cout << "Her sweatsuit is blue.\n";
break;
case violet: cout << "Her eyes were violet.\n";
break;
case indigo: cout << "Her mood was indigo.\n";
break;
}
cout << "Enter color code (0-6): ";
cin >> code;
}
cout << "Bye!\n";
return 0;
}
class
#include
using namespace std;
const double PI = 3.14;
class circle
{
public:
int m_r;
double calculateC()
{
return 2 * PI * m_r;
}
};
int main()
{
circle c1;
c1.m_r = 10;
cout << "The circumference of a circle is " << c1.calculateC()<
authority
#include
#include
using namespace std;
class person {
// public authority
public:
string name;
// protected authority
protected:
string m_car;
// private authority
private:
int password;
public:
void func()
{
name = "River Chandler";
m_car = "Red Flag";
password = 123456;
}
};
int main()
{
person p1;
p1.name = "Eric";
return 0;
}
#include
#include
#include
#include
#include
#include
#define f(i,l,r) for(i=(l);i<=(r);i++)
#define ff(i,r,l) for(i=(r);i>=(l);i--)
#define ll long long
#define EPS 1e-6
using namespace std;
const int MAXN=105;
int n,m;
char output[MAXN];
string s;
struct frac
{
int x,y=1;
bool operator < (const frac &tmp)const{
return 1.0*x/y<1.0*tmp.x/tmp.y;
}
frac operator - (const frac &tmp)
{
frac ans;
ans.x=x*tmp.y-y*tmp.x;
ans.y=y*tmp.y;
ans.sim();
return ans;
}
frac operator * (const frac &tmp)
{
frac ans;
ans.x=x*tmp.x;
ans.y=y*tmp.y;
ans.sim();
return ans;
}
frac operator / (const frac &tmp)
{
frac ans;
ans.x=x*tmp.y;
ans.y=y*tmp.x;
ans.sim();
return ans;
}
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
void sim()
{
int d=gcd(x,y);
x/=d;
y/=d;
if(x<0&&y<0)
{
x=-x;
y=-y;
}
}
void write()
{
int i;
char tmp[MAXN];
int num=0;
int nx=x,ny=y;
s="";
if(!nx)
{
s+="0";
}
else{
if(nx<0||ny<0){
s+='-';
nx=abs(nx);
ny=abs(ny);
}
if(nx%ny==0){
nx/=ny;
while(nx){
tmp[++num]='0'+nx%10;
nx/=10;
}
ff(i,num,1){
s+=tmp[i];
}
}
else{
while(nx){
tmp[++num]='0'+nx%10;
nx/=10;
}
ff(i,num,1){
s+=tmp[i];
}
s+='/';
num=0;
while(ny){
tmp[++num]='0'+ny%10;
ny/=10;
}
ff(i,num,1){
s+=tmp[i];
}
}
}
f(i,s.length(),7){
cout<<" ";
}
cout<>n>>m;
cout<<"Input element:"<>a[i][j].x;
}
}
gause_1();
cout<<"Stepped matrix:"<