C++ 概述

VC++的编程基础虽然简单,但却是MFC或MS VC++面向对象的基础,非常重要

vc++6.0的代码编辑模式怎么改? 答案:按下键盘上的Insert键。

一个简单的C++程序 

#include

void main()

{
int i;
cout<<“please insert the value of i”<<‘\n’; cin>>i;
cout<<“您输入的i的数值为”<<i<<‘\n’;
}

#include #include

if判断语句

void main()

{
int a;
int s=0;
cout<<“Please insert the value of a”<<“\n”; cin>>a;
if(a>0)
s=100;
cout<<“the value a you have input is:”<<a<<‘\n’;
cout<<“the value s is:”<<s<<‘\n’;

}

判断语句

#include <math.h>
#include <iostream.h>

void main()

{
 int a,b,c;
 cout<<“Please type in the value of a,b,c”<<‘\n’;
 cin>>a>>b>>c;
 cout<<“a=”<<a<<‘\n'<<“b=”<<b<<‘\t'<<“c=”<<c<<‘\n’;
 if(a>=b&&a>=c)
 {
  cout<<“the max is a and the value is “<<a<<‘\n’;
  
 }else
  if(b>=a&&b>=c)
   
   cout<<“the max is b and the value is “<<b<<‘\n’;
  
  
  else
   cout<<“the max is c and the value is “<<c<<‘\n’;
  
}

实现计算器

#include<iostream.h>

void main(void)
{
 float num1,num2;
 char op;
 cout<<“please input the data,the format is num1 operator num2\n”;
 cin>>num1>>op>>num2;
 switch(op)
 {
 case ‘+’: cout<<num1<<op<<num2<<“=”<<num1+num2<<‘\n’;
  break;

 case ‘-‘: cout<<num1<<op<<num2<<“=”<<num1-num2<<‘\n’;
  break;

 case ‘*’: cout<<num1<<op<<num2<<“=”<<num1*num2<<‘\n’;
  break;

 case ‘/’: cout<<num1<<op<<num2<<“=”<<num1/num2<<‘\n’;
  break;
 }

}

一维数组

#include<iostream.h>

void main(void)
{
 float x[5],sum=0;
 cout<<“please input 5 number:”;
 for(int i=0;i<5;i++)
 {
  cin>>x[i];
 };

 for(int j=0;j<5;j++)
 {
 cout<<“您输入的第”<<j<<“个数值为”<<x[j]<<‘\n’; 
 sum+=x[j];
 };

 cout<<“the sum of the number you have input is:”<<sum<<‘\n’;
}

二维数组,矩阵行列互换(转置)

#include<iostream.h>

void main(void)

{
 int a[4][4]={{11,12,13,14},{15,16,17,18},{19,20,21,22},{23,24,25,26}};
 int t,i,j;
 
 cout<<“the data array you have input are: \n”;
 cout<<‘\n'<<‘\n’;
 
 for(i=0;i<4;i++){
  for(j=0;j<4;j++)
  {
   cout<<a[i][j]<<‘\t’;

  }
  cout<<‘\n'<<‘\n'<<‘\n’;
 } 

 
  
 for(i=0;i<4;i++)
  for(j=i;j<4;j++)
   {
    t=a[i][j];a[i][j]=a[j][i];a[j][i]=t;
   };
 cout<<“the data array after change are:”<<‘\n’;
 cout<<‘\n'<<‘\n’;
    
 for(i=0;i<4;i++){
  for(j=0;j<4;j++)
  {
   cout<<a[i][j]<<‘\t’;

  }
  cout<<‘\n'<<‘\n'<<‘\n’;
 }
     
     
}

Leave a comment

您的电子邮箱地址不会被公开。