C++ 教程代码

 cin cout

//mysecond.cpp  -- display a message 
#include<iostream>
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."<<endl;
	return 0;

}

math.h函数调用

//sqrt.cpp  -- using the sqrt{} function 
#include<iostream>
#include<math.h>


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<iostream>


void simon(int n)
{
	using namespace std;
	cout << "Simon say touch your toes" << n << "times."<<endl;
	
}

int main()
{
	using namespace std;
	simon(3);
	cout << "Pick an integer: ";
	int count;
	cin >> count;
	simon(count);
	cout << "Done!" << endl;
	return 0;
}



limit.h

//limit.cpp  -- defining your own function 
#include<iostream>
#include<limits.h>


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." <<endl;
	cout << "short is " << sizeof(n_short) << "bytes." << endl;
	cout << "long is " << sizeof(n_long) << "bytes." << endl;
	cout << "long long is " << sizeof(n_llong) << "bytes." << endl;
	cout << endl;
	
	cout << "Maximum values:" << endl;
	cout << "int:" << n_int << endl;
	cout << "short:" << n_short << endl;
	cout << "long:" << n_long << endl;
	cout << "long long:" << n_llong << endl << endl;
	
	cout << "Minimum int value = " << INT_MIN << endl;
	cout << "Bites per byte = "<< CHAR_BIT << endl;
	return 0;
}



char 

//morechar.cpp  -- defining your own function 
#include<iostream>

int main()
{
	using namespace std;
	char ch = 'M';
	int i = ch;
	cout << "The ASCII code for "<< ch << " is " << i << endl;
	cout << "Add one to the character code:"<< endl;
	ch += 1;
	i = ch;
	cout << "The ASCII code for " << ch << " is " << i << endl;
	cout << "Displaying char ch using cout.put(ch):";
	cout.put(ch);
	cout << endl << "Done" <<endl;
	return 0;
}




array

#include<iostream>
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<iostream>
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<iostream>
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<iostream>

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<iostream>

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<iostream>

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<iostream>

const int ArSize = 16;

int main()
{
	long long factorials[ArSize];
	factorials[1] = factorials[0] = 1LL;
	for (int i = 2;i<ArSize;i++)
		factorials[i] = i * factorials[i-1];
	for (int i = 0;i<ArSize;i++)
		std::cout << i << "! = " << factorials[i]<< std::endl;
	return 0;
}




逗号运算符

//comma.cpp  -- defining your own pointer 
#include<iostream>
#include<string>

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<i;--i,++j)
	{
		temp = word[i];
		word[i] = word[j];
		word[j] = temp;
	}
	cout << word << "\nDone\n";
	return 0;
}

while

//comma.cpp  -- defining your own pointer 
#include<iostream>
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<iostream>

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<iostream>

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<iostream>

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<iostream>

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<iostream>
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()<<endl;
	return 0;
}

authority

#include<iostream>
#include<string>
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;
}

  • C++ 行简化
#include<iostream>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<string>

#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<<s;
  }
}a[MAXN][MAXN];

void out_f()
{
  int i,j;
  f(i,1,n){
    f(j,1,m){
      cout<<1.0*a[i][j].x/a[i][j].y<<" ";
    }
    cout<<endl;
  }
}

void out()
{
  int i,j;
  f(i,1,n){
    f(j,1,m){
      a[i][j].write();
      cout<<" ";
    }
    cout<<endl;
  }
}

int find(int r,int c)
{
  int i,t=-1;
  f(i,r,n){
    if(t==-1||a[t][c]<a[i][c]) t=i;
  }
  return t;
}

void interchange(int r1,int r2)
{
  int j;
  f(j,1,m){
    swap(a[r1][j],a[r2][j]);
  }
  return;
}

void scale(int r,int c)
{
  int j;
  ff(j,m,c){
    a[r][j]=a[r][j]/a[r][c];
  }
  return;
}

void muilt(int r,int c)
{
  int i,j;
  f(i,r+1,n){
    if(!a[i][c].x) continue;
    ff(j,m,c){
      a[i][j]=a[i][j]-(a[i][c]*a[r][j]);
    }
  }
  return;
}


void gause_1()
{
  int c,r=1;
  f(c,1,n){
    int pos=find(r,c);
    if(!a[pos][c].x) continue;
    interchange(r,pos);
    scale(r,c);
    muilt(r,c);
    r++;
  }
}

void gause_2()
{
  int i,j;
  ff(i,n,1){
    f(j,i+1,n){
      a[i][n+1]=a[i][n+1]-(a[i][j]*a[j][n+1]);
      a[i][j].x=0;
    }
  }
  return;
}

int main()
{
  int i,j;
  
  cout<<"rows and columns:"<<endl;
  cin>>n>>m;
  cout<<"Input element:"<<endl;
  
  f(i,1,n){
    f(j,1,m){
      cin>>a[i][j].x;
    }
  }
  
  gause_1();
  cout<<"Stepped matrix:"<<endl;
  out();
  
  gause_2();
  cout<<"Simplest form:"<<endl;
  out();
  
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

River Chandler

谢谢,我会更努力学习工作的!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值