这里主要还是强制类型转换的使用

//打印字符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;
 } 

更多推荐