這里主要還是強制類型轉換的使用
//打印字符ASCII碼值?
//輸入一個除空格以外的可見字符
//輸出其ASCII值--十進制整數?
#include <iostream>
using namespace std;
int main()
{
char ch;
cin >> ch;//字符
cout << ?(int)ch << endl;?
return 0;
}
//打印字符?
#include <iostream>
using namespace std;
//之前寫的是?
int main()
{
int n = 0;
cin >> n;
char ch = n;
cout << ch << endl;?
return 0;
}?
//現在學習了強制類型轉換可以寫成?
int main()
{
int n = 0;?
cin >> n;
cout << (int)ch << endl;?
?? ?
return 0;
}?
//++和 ——
//前置++ ?后置++
//無論前置后置++ --都是為了讓操作數自+1 -1
#include <iostream>
using namespace std;
int main()
{
int a = 10;
//a++;
//
//cout << a << endl;//----11
//++a;
//cout << a << endl;//-----11
//?? ?a--;
//?? ?cout << a << endl;//----9
//?? ?
//?? ?--a;
//?? ?cout << a << endl;//----9
return 0;
}
//區別是后置++是先使用再+1?
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = a++;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a = 11,,b = 10?? ?
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = ++a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a = 11,,,b = 10
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = a--;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a =9,,,b = 10
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = a--;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//a = 9,,,b = 9
return 0;
}
C/C++的輸入輸出
getchar/putchar
gechar-----必須包含頭文件---#include <cstdio>
int getchar (void);
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int ch = gechar();//輸入端口--()是傳參需要,必不可少?
cout << ch << endl;
//輸入a--輸出97?
//注意gechar有嚴格的輸入格式要求? ?
return 0;
}?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int ch = gechar();
cout << ch << endl;
cout << (char)ch;
ch = gechar();
cout << ch << endl;
cout << (char)ch;
cout << "xxxxx" << endl;
//輸出a
// ? ? 97
// ? ? a10----gechar有嚴格的格式,在輸入a時輸入的回車(/n)鍵也被記錄的--10?
?? ? ?//
// ? xxxxx?
return 0;
}
putchar---輸出字符
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
//?? ?int ch = getchar();
//?? ?putchar(ch);
putchar('x');
putchar('\n');
putchar('y');
putchar('\n');
putchar('z');
putchar('\n');
//輸出
// x
// y
// z
//?
return 0;
}?
//輸入兩個整數a,b,輸出a除以b的值,保留三位小數
//輸入兩位整數,在int范圍內
//輸出一個浮點數,保留三位小數?
#include <iostream>
#include <cstdio>
using namespace std;
int main()?
{
int a, b;
cin >> a >>b;
double r = a * 1.0 / b;
printf("%.3f\n",r);
return 0;
}
//甲流疫情死亡率
//輸入共兩行,第一行第一個整數為確診數a,第二行為整數死亡數b
//輸出僅一行,甲流死亡率,以百分數形式輸出,精確到小數點后三位?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b;
cin >> a >>b;
//計算
printf("%.3f%%\n",b*0.1 / a*100);?
return 0;
}
//溫度表達轉換?
//利用公式C = 5 8 (F- 32)/ 9?
//C是攝氏度,F是華氏溫度 輸入華氏溫度F,輸出攝氏溫度C,
//要求精確到小數點后5位?
//輸入一行,包含一個實數F(小數),表示華氏溫度,(F >=-456.67)
//輸出一行,包含一個實數,表示對應的攝氏溫度?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double f;
cin >> f;
double c = 5 * (f - 32) / 9;
printf("%.5f\n",c);
return 0;
}
//計算并聯電阻的阻值
//對于r1和r2的電阻,公式
//R=1/(1/r2 + 1/r2)?
//輸入r1,r2,輸出并聯后的阻抗大小,
//結果保留小數點后兩位?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double r1,r2;
cin >> r1 >> r2;
double r = 1 / (1 / r1 + 1 / r2);
printf("%.2f\n",r);
return 0;
}
//與圓相關的計算
//給出圓的半徑,求圓的周長,直徑和面積
//每個 數保留4位小數?
#include <iostream>
#include <cstdio>
const double PI = 3.14159;
using namespace std;
int main()
{
double r ;
cin >> r;
double z = 2 * r;
double l = 2 * PI *r;
double a = PI * r * r;
printf("%.4f %.4f %.4f\n",z,l,a);
return 0;
}
//輸入三個整數,按每一個整數占8字符的寬度,右對齊輸出他們,
//按照格式要求依次輸出他們,空格隔開
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b,c;
cin>> a >> b >> c;
printf("%8d %8d %8d\n",a,b,c);
return 0;
}
//5(1,2,3,4,5)個小朋友有(輸入)若干 糖果,
//他們都將糖果均分位三份, 自己留一份,其余的分給相鄰的兩個小朋友
//一輪后,每個小朋友手上分別有多少糖果?
//
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b,c,d,e;
cin >> a >> b >> c >> d >> e;
//1
a /= 3; b += a; e =+ a;
//2
b /= 3; c += b; a += b;
//3
c /= 3; d += c; b += c;
//4
d /= 3; e += d; c += d;
//5
e /= 3; a += e; d += e;
printf("%5d%5d%5d%5d%5d\n",a,b,c,d,e);
return 0;
}?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
return 0;
}
//數字反轉
//輸入一個不小于100,且小于1000,同時包括小數點后一位的一個浮點數
//例如 123.4,要求把這個數字翻過來,變成4.321并輸出
//#include <iostream>
//#include <cstdio>
//
//using namespace std;
//int main()
//{
//?? ?char a,b,c,d,e;
//?? ?
//?? ?cin >> a >> b >> c >> d >> e;
//?? ?// ? ? 1 ? ?2 ? ?3 ? ?. ? ?4
//?? ?cout << e << d << c << b << a;?
//?? ?
//?? ?
//?? ?return 0;
// }?
//int main()
//{
//?? ?char a,b,c,d,e;
//?? ?scanf("%c%c%c.%c",&a,&b,&c,&d,&e);
//?? ?print("%c.%c%c%c\n",e,d,c,b,a);
//?? ?
//?? ?return 0;
//}
//輸入三角形三條邊a,b,c?
// 其面積為 (p*(P-a)(p-b)(p-c))*0.5;
//p=(a +b+c)/2? ? ?
//輸出 面積?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
double a,b,c;?
int main()
{
cin >> a >> b>> c;
double p = (a + b + c) / 2;
double area = sqrt(p * (p - a) * (p - b) * (p - c));
cout << fixed << setprecision(1) << area << endl;
return 0;
}
using namespace std;
double a,b,c;
int main()
{
cin >> a >> b >> c;
double p = (a + b + c) / 2;
double area = sqrt(p * (p - a) * (p - b) * (p - c));
printf("%.1f\n",area);
return 0;
}
??
//輸入兩個整數m,n,整數輸出yes,否則輸出no?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
if (a % b == 0)
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
return 0;
}
//輸入兩個整數,比較其大小
//輸出< > =?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>?
using namespace std;
int main()
{
long long x,y;
cin >> x >> y;
if(x > y)
cout << ">" << endl;
else if(x == y)
cout << "=" << endl;
else
cout << "<" << endl;
return 0;
}
//輸入一個浮點數,輸出其絕對值
//保留小數點后兩位
//---只是浮點數--float?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>?
using namespace std;
int main()
{
float n;
cin >> n;?
if(n < 0)
printf("%.2f\n",-n);
else
printf("%.2f\n",n);
return 0;
}
但是這個還有其他的解法
//fabs--專用來求浮點數絕對值的函數?
//--頭文件cmath
int main()
{
float n;
cin >> n;?
n = fabs(n);
?? ?printf("%.2f\n",n);
return 0;
}
拓展一下~
abs----專門計算整數的絕對值的
其所需頭文件---cstdlib
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int n;
cin >> n;
int m = abs(n);
cout << m << endl;
}?
//奇偶數判斷
//n是奇數 輸出odd
//n是偶數 輸出even
//-100 <= n <= 100?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>?
#include <cstdlib>
using namespace std;
?
int main()
{
int n;
cin >> n;
if(n % 2 == 1)
cout << "odd" << endl;
else if(n % 2 == -1)
cout << "odd" << endl;
else
cout << "even" << endl; ? ?
return 0;
} ?
//還有其他寫法?
int main()
{
int n;
cin >> n;
if(n % 2)
cout << "odd" << endl;
else if(n % 2 == -1)
cout << "odd" << endl;
else
cout << "even" << endl; ? ?
return 0;
}?
//學生成績
//輸入3個0~100的數字
//輸出 該學生剛好有一門不及格輸出1,否則輸出0?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int s1,s2,s3;
int cnt = 0;
cin >> s1 >> s2 >> s3;
if(s1 , 60)
cnt++;
if(s2 < 60)
cnt++;
if(s3 < 60)
cnt++;
if(cnt == 1)
cout << 1 << endl;
else
cout << 0 << endl;
return 0;
}
//另一種做法
int main()
{
int s1,s2,s3;
cin >> s1 >> s2 >> s3;?
if((s1 <60)+(s2 < 60)+(s3 <60)==1)
cout << 1 <<endl;
else
cout << 0 << endl;
return 0;
}?
//等差數列末項計算
//輸入三個整數a1,a2,n,(-100<=a1,a2<=100,0<n<=1000;)
//輸出一個整數,即第n項的值?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a1,a2,n;
cin >> a1 >> a2 >> n;
if(n == 1)
cout << a2 << endl;
else if(n == 2)
cout << a2 << endl;
else
cout << a2 + (n - 2)*(a2 - a1) << endl;?
return 0;
}?