目录
一:数据类型和表达式
C头文件
#include<stdio.h>
void main()
{
}
C++头文件
#include<iostream.h>
void main()
{
}
C++与C兼容性头文件
#include < iostream >
using namespace std;
void main()
{
}
//文本原样输出程序
#include <iostream.h> //头文件
void main(void) //函数
{
cout<<"Welcome to C++!\n";
}
运行结果:
Welcome to C++!
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello!"<<endl;
cout<<"Welcome to C++!"<<endl;
return 0;
}
运行结果:
Hello!
Welcome to C++!
/*求两个整数的和程序c++*/
#include <iostream.h>
void main(void)
{
int a,b,sum; //说明变量a,b,sum为整数
cout<<"Input a,b:"; //显示提示信息,""里面是什么原本输出即可
cin>>a>>b; //从键盘上输入变量a/b的值
sum=a+b; //求和
cout<<"Sum="<<sum<<endl; //输出结果,sum没有""则表示输出值
}
/*求两个整数的和程序c*/
#include <iostream.h>
void main()
{
int a,b,s;
printf("input a,b\n");
scanf("%d %d",&a,&b);
s=a+b;
printf("s=%d\n",s=a+b);
}
/*求两个整数的和程序c++&c*/
#include <iostream>
using namespace std;
void main()
{
int a,b,s;
cout<<"input a,b\n";
cin>>a>>b;
s=a+b;
cout<<"s="<<s<<endl;
}
运行结果:
Input a,b: 3 5
Sum=8
#include<iostream.h>
int add(int x,int y)
{
int z;
z=x+y;
return z;
}
void main(void)
{
int a,b,sum;
cout<<"Input a,b:";
cin>>a>>b;
sum=add(a,b);
cout<<"Sum ="<<sum<<endl;
}
运行结果:
Input a,b: 3 5
Sum =8
#include<iostream.h>
void main()
{
int i,j;
i=3;
j=i++;
cout<<"i="<<i<<","<<"j="<<j<<endl;
}
运行结果:
i=4,j=3
#include<iostream.h>
void main()
{
int i,j;
i=3;
j=++i;
cout<<"i="<<i<<","<<"j="<<j<<endl;
}
运行结果:
i=4,j=4
{
#include<iostream.h>
void main()
{
float a=1.234,b=10,c;
c=a+b;
cout<<"c="<<c<<endl;
}
运行结果:
c=11.234
#include<iostream.h>
#include<iomanip.h>
void main()
{
float a=1.234,b=10,c;
c=a+b;
cout<<"c="<<setw(8)<<c<<endl;
}
运行结果:
c= 11.234
#include<iostream.h>
#include<iomanip.h>
void main()
{
float a=1.234,b=10,c;
c=a+b;
cout<<"c="<<setw(3)<<c<<endl;
}
运行结果:
c=11.234
#include<iostream.h>
#include<iomanip.h>
void main()
{
float a=1.234,b=10,c;
c=a+b;
cout<<"c="<<setprecision(3)<<c<<endl;
}
运行结果:
c=11.2
二:程序结构与流程控制
1.分支语句:if,if…else,if…else…if
#include<iostream>
using namespace std;
void main()
{
int a,b,max;
cout<<"input a,b:"<<endl;
cin>>a>>b;
max=a;
if(b>max)
max=b;
cout<<"max="<<max<<endl;
}
运行结果:
input a,b:
3 5
max=5
input a,b:
5 3
max=5
#include<iostream>
using namespace std;
void main()
{
int a,b;
cout<<"input a,b:"<<endl;
cin>>a>>b;
if(a>b)
cout<<a<<endl;
else
cout<<b<<endl;
}
运行结果:
input a,b:
3 6
6
input a,b;
6 3
6
#include<iostream>
using namespace std;
void main()
{
int x,y;
cout<<"input x:"<<endl;
cin>>x;
if(x<0)
y=x+1;
else if(x<10)
y=x*x-5;
else
y=x*x*x;
cout<<"y="<<y<<endl;
}
运行结果:
input x:
-1
y=0
input x:
0
y=-5
input x:
10
y=1000
#include<iostream>
using namespace std;
void main()
{
int a,b,c,max;
cout<<"input a,b,c:"<<endl;
cin>>a>>b>>c;
if(a>b)
if(a>c)
max=a;
else
max=c;
else
if(b>c)
max=b;
else
max=c;
cout<<"max="<<max<<endl;
}
运行结果:
input a,b,c:
1 2 3
max=3
#include<iostream.h>
void main()
{
int a,b,max;
cout<<"input a,b:"<<endl;
cin>>a>>b;
if(a>b)
max=a;
else
max=b;
cout<<"max="<<max<<endl;
}
运行结果:
input a,b:
3 5
max=5
#include<iostream.h>
void main()
{
int a,b,max;
cout<<"input a,b:"<<endl;
cin>>a>>b;
/* if(a>b)
max=a;
else
max=b;
*/
max=a>b?a:b; //a>b?max=a:max=b;
cout<<"max="<<max<<endl;
}
运行结果:
input a,b:
3 5
max=5
习题1.
输入学生成绩,要求输出成绩等级,90分及以上为’A’,80-90分为‘B’,70-79分为‘C’,60-69分为‘D’,60分以下为‘E’
#include<iostream>
using namespace std;
void main()
{
int s;
cout<<"input s:"<<endl;
cin>>s;
if(s<0||s>100)
cout<<"error!"<<endl;
else
if(s>=90)
cout<<"A"<<endl;
else if(s>=80)
cout<<"B"<<endl;
else if(s>=70)
cout<<"C"<<endl;
else if(s>=60)
cout<<"D"<<endl;
else
cout<<"E"<<endl;
}
运行结果:
input s:
110
error!
input s:
-10
error!
input s:
95
A
#include<iostream>
using namespace std;
void main()
{
int s;
cout<<"input s:"<<endl;
cin>>s;
if(s<0||s>100)
cout<<"error!"<<endl;
else
if(s<90)
if(s<80)
if(s<70)
if(s<60)
cout<<"E"<<endl;
else
cout<<"D"<<endl;
else
cout<<"C"<<endl;
else
cout<<"B"<<endl;
else
cout<<"A"<<endl;
}
习题2.
判断某一年是闰年
#include<iostream>
using namespace std;
void main()
{
int y,flag;
cout<<"input y."<<endl;
cin>>y;
if(y%4==0)
if(y%100==0)
if(y%400==0)
flag=1;
else
flag=0;
else
flag=1;
else
flag=0;
if(flag==1)
cout<<y<<"年是闰年。"<<endl;
else
cout<<y<<"年是平年。"<<endl;
}
运行结果:
input y:
2019
2019是平年。
input y:
2100
2100是平年。
input y:
2000
2000是闰年。
#include<iostream>
using namespace std;
void main()
{
int y,flag;
cout<<"input y:"<<endl;
cin>>y;
if(y%4!=0)
flag=0;
else if(y%100!=0)
flag=1;
else if(y%400!=0)
flag=0;
else
flag=1;
if(flag==1)
cout<<y<<"是闰年。"<<endl;
else
cout<<y<<"是平年。"<<endl;
}
习题3.
求方程ax^2+bx=c=0的根
#include<iostream>
#include<math.h>
using namespace std;
void main()
{
float a,b,c,dt,x1,x2,p,q;
cout<<"input a,b,c"<<endl;
cin>>a>>b>>c;
if(a==0)
{
x1=-c/b;
cout<<"x="<<x1<<endl;
}
else
{
dt=b*b-4*a*c;
p=-b/(2*a);
q=sqrt(fabs(dt))/(2*a);
if(dt>0)
{
x1