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

打印 上一主题 下一主题

主题 873|帖子 873|积分 2619

变量的正当性

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7. int main()
  8. {
  9.     int This_little_pig;
  10.     int latest thing;
  11.     int the_!_no;
  12.     int the_@_no;
  13.     int the_#_no;
  14.     int the_$12_method;
  15.     int the_%12;
  16.     int the_^_no;
  17.     int the_&_no;
  18.     int the_*_no;
  19.     int the_(_no;
  20.     int the_+_no;
  21.     int the_<_no;
  22.     int number;
  23.     int string;
  24.     double _this_is_ok;
  25.     double correct?;   
  26. }
复制代码

特殊符号中这个美元符号$是可以作为变量的,标准库中的string也可以,但不发起利用
其中我写的一个转换函数,因为它功能上没有任何题目,但是没有返回值,结果报了一个错误,让我修改,果然C++很严谨,记录以下这个错误。
  1. int swap(int &a, int &b){
  2.     int temp = a;
  3.     a = b;
  4.     b = temp;
  5. }
复制代码

标题:

编写一个程序,提示用户输入三个整数值,然后按照数值巨细次序用到逗号隔开的方式输出这些值。因此,用户输入10 4 6 时输出值为4,6,10.假如有两个值相同,那么他们按序一起输出。因此,输入4 4 5将会输出4,4,5。
  1. #include "std_lib_facilities.h"
  2. void swap(int &a, int &b){
  3.     int temp = a;
  4.     a = b;
  5.     b = temp;
  6. }
  7. void compare(int first, int second, int third){
  8.     if (first < second){
  9.         if (first < third){
  10.             if (second < third) {
  11.                 cout << first << ","<< second << "," << third << "\n";
  12.             }else{
  13.                 swap(second,third);
  14.                 cout << first << ","<< second << "," << third << "\n";
  15.             }
  16.         }else{
  17.             swap(first,third);
  18.             if (second < third) {
  19.                 cout << first << ","<< second << "," << third << "\n";
  20.             }else{
  21.                 swap(second,third);
  22.                 cout << first << ","<< second << "," << third << "\n";
  23.             }
  24.         }
  25.     }else{
  26.         swap(first,second);
  27.         if (first < third){
  28.             if (second < third) {
  29.                 cout << first << ","<< second << "," << third << "\n";
  30.             }else{
  31.                 swap(second,third);
  32.                 cout << first << ","<< second << "," << third << "\n";
  33.             }
  34.             
  35.         }
  36.     }
  37. }
  38. int main()
  39. {
  40.     cout << "Please enter three integer number:\n";
  41.     int first_num, second_num, third_num;
  42.     cin >> first_num >> second_num >> third_num;
  43.     compare(first_num,second_num,third_num);
  44. }
复制代码
两个值交换

  1. //方法1:
  2. void swap(int &a, int &b) {
  3.     int temp = a;
  4.     a = b;
  5.     b = temp;
  6. }
  7. swap(a,b);//函数调用
复制代码
  1. //方法2:
  2. void swap(int &a, int &b) {
  3.     a = a + b;
  4.     b = a - b;
  5.     a = a - b;
  6. }
  7. swap(a,b);//函数调用
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

风雨同行

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表