风雨同行 发表于 2024-10-27 12:57:37

C++底子:变量的正当性和三数字比较

变量的正当性

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int main()
{
    int This_little_pig;
    int latest thing;
    int the_!_no;
    int the_@_no;
    int the_#_no;
    int the_$12_method;
    int the_%12;
    int the_^_no;
    int the_&_no;
    int the_*_no;
    int the_(_no;
    int the_+_no;
    int the_<_no;
    int number;
    int string;
    double _this_is_ok;
    double correct?;   
}

https://i-blog.csdnimg.cn/direct/4fd3a664c2ab4dbb86ae690cd2e83b1c.png
特殊符号中这个美元符号$是可以作为变量的,标准库中的string也可以,但不发起利用
其中我写的一个转换函数,因为它功能上没有任何题目,但是没有返回值,结果报了一个错误,让我修改,果然C++很严谨,记录以下这个错误。

int swap(int &a, int &b){
    int temp = a;
    a = b;
    b = temp;
}
https://i-blog.csdnimg.cn/direct/5533ee0a9b5043e68e55d85d408bf673.png
标题:

编写一个程序,提示用户输入三个整数值,然后按照数值巨细次序用到逗号隔开的方式输出这些值。因此,用户输入10 4 6 时输出值为4,6,10.假如有两个值相同,那么他们按序一起输出。因此,输入4 4 5将会输出4,4,5。
#include "std_lib_facilities.h"

void swap(int &a, int &b){
    int temp = a;
    a = b;
    b = temp;
}

void compare(int first, int second, int third){
    if (first < second){
      if (first < third){
            if (second < third) {
                cout << first << ","<< second << "," << third << "\n";
            }else{
                swap(second,third);
                cout << first << ","<< second << "," << third << "\n";
            }
      }else{
            swap(first,third);
            if (second < third) {
                cout << first << ","<< second << "," << third << "\n";
            }else{
                swap(second,third);
                cout << first << ","<< second << "," << third << "\n";
            }
      }
    }else{
      swap(first,second);
      if (first < third){
            if (second < third) {
                cout << first << ","<< second << "," << third << "\n";
            }else{
                swap(second,third);
                cout << first << ","<< second << "," << third << "\n";
            }
            
      }
    }
}



int main()
{
    cout << "Please enter three integer number:\n";
    int first_num, second_num, third_num;
    cin >> first_num >> second_num >> third_num;
    compare(first_num,second_num,third_num);
}
两个值交换

//方法1:
void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

swap(a,b);//函数调用
//方法2:
void swap(int &a, int &b) {
    a = a + b;
    b = a - b;
    a = a - b;
}
swap(a,b);//函数调用

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C++底子:变量的正当性和三数字比较