目录
1、分解一已知数的各个位数
#include <iostream>
using namespace std ;
int main()
{
int a , b , c , d , e ;
cout << "需要分解的五位整数是 :" <<endl;
cin >> a ;
b = a % 10000 ;
c = a % 1000 ;
d = a % 100 ;
e = a % 10 ;
cout << "该数的万位是 :" << a/10000 <<endl;
cout << "该数的千位是 :" << b/1000 <<endl;
cout << "该数的百位是 :" << c/100 <<endl;
cout << "该数的十位是 :" << d/10 <<endl;
cout << "该数的个位是 :" << e <<endl;
return 0;
}
2、多类型输出
#include <iostream>
using namespace std;
int main()
{
cout << 'A' << endl;
cout << 4 << endl;
cout << "I am ok." << endl;
cout << 123.45678 << endl;
cout << '\x61' << endl;
cout << '\n';
cout << 010 << endl;
cout << 0x10 << endl;
}
3、字符与数值型的转换
#include <iostream>
using namespace std ;
int main()
{
char a ,b ;
a = 97 ;
b = 65 ;
cout << '\t' << a << '\t' << b << endl;
cout << '\t' << (int)a << '\t' << (int)b << endl;
return 0;
}
4、字符串间的替换(复制)
#include <iostream>
using namespace std ;
int main()
{
char a[] = "You look like a boy" ,b[30] ;
int i;
for (i=0 ; a[i] != '\0' ; i++)
b[i] = a[i] ;
b[i] = '\0' ;
cout << "数组a = " << a << endl;
cout << "数组b = " ;
for (i=0 ; b[i] != '\0' ; i++)
cout << b[i] ;
cout <<endl;
return 0;
}
5、i++与++i
#include <iostream>
using namespace std;
int main()
{ int i=8;
cout << ++i << endl;
cout << --i << endl;
cout << i++ << endl;
cout << i-- << endl;
return 0;
}
6、转义字符
#include <iostream>
using namespace std ;
int main()
{
cout << " a b\r cde\f fg hi\n " ;
cout << " jkl\t mno\b\b xyz\n" ;
return 0;
}
7、数值类型转换
#include <iostream>
using namespace std ;
int main()
{
float a , PI ;
int s , r ;
r = 1 ;
PI = 3.14 ;
a = 6.66 ;
s = PI * r * r ;
cout << "(int)(a) = " << (int)(a) <<endl;
cout << "a = " << a << endl ;
cout << "s = " << s <<endl; //数值的自动转换
return 0;
}
8、结构体的简单使用
#include <iostream>
using namespace std;
struct student
{
char name[10] ;
int grade[3] ;
};
int sumgrade(struct student a)
{
int i ,sum = 0;
for(i=0 ; i<3 ; i++)
sum += a.grade[i];
return sum;
}
int main()
{
struct student a = {"mumu",{66,88,99}};
cout << a.name << "的三门成绩和为:"<< sumgrade(a) <<endl;
}
/*
struct student{
long int num ;
char name[10] ;
char sex ;
char adr[20] ;
}a={666, "xumu" , 's' , "696 henan oh"} ;
cout << "NO: " << a.num <<endl<<"name : " << a.name << endl
<<"sex = " << a.sex << endl << "adr = " << a.adr <<endl;
*/
9、结构体与指针
#include <iostream>
#include <string.h>
using namespace std ;
void main()
{
struct student
{
int num ;
char name ;
char x ;
float score ;
};
struct student stu ;
struct student *p = &stu ;
stu.num = 666;
//stu.name = 'x' ;
strcpy(stu.name,"xxs") ;
stu.x = 's' ;
stu.score = 120 ;
cout << " NO : " << stu.num <<endl
<< " name : " << stu.name <<endl
<< " x : " << stu.x <<endl
<< " score : " << stu.score <<endl;
cout << " NO : " << (*p).num <<endl
<< " name : " << (*p).name <<endl
<< " x : " << (*p).x <<endl
<< " score : " << (*p).score <<endl;
}
10、结构体与数组
#include <iostream>
#include <string.h>
using namespace std;
struct student
{
char name[10] ;
int grade;
};
struct student mingrade(struct student s[] , int n)
{
int i , j=0;
for(i=1 ; i<3 ;i++)
if(s[i].grade < s[j].grade)
j = i;
return s[j];
}
int main()
{
struct student min , a[3] = {{"张三",66},{"李四",88},{"王五",99}};
min = mingrade(a , 3) ;
cout << "成绩最低的是:" << min.name <<endl;
}
11、温度类型转换
#include <iostream>
using namespace std ;
int main()
{
double ftemp , ctemp;
cin >> ftemp ;
cout << "输入的华氏温度是 :" << ftemp << endl ;
ctemp = (ftemp - 32) / 1.8 ;
cout << "转换成摄氏温度为 :" << ctemp << endl ;
return 0 ;
}
12、三角形面积
#include <iostream>
#include <math.h>
using namespace std ;
int main()
{
float s , a , b , c;
double area ;
cout << "请输入需要求的三角形的三条边长 :" <<endl;
cin >>a>>b>>c ;
if( a+b > c && a-b < c)
{
s = (a + b + c) / 2 ;
area = sqrt( s * (s-a) * (s-b) * (s-c)) ;
cout << "a=" <<a << '\t' << "b=" << b << '\t' << "c=" <<c <<endl;
cout << "所求的三角形的面积是 :" << area <<endl;
}
else
cout << "这三条边构不成三角形" <<endl;
return 0;
}