1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include<iostream> #include <string> #include<stdlib.h> #include<stdio.h> using namespace std;
float triangle(float &x, float &y) { return x * y * 0.5; } float rectangle(float &x, float &y) { return x * y; } void swap(float &x, float &y) { float n; n = x; x = y; y = n;
} void print(float &x, float &y) { cout << "长为:" << x << "宽为:" << y << endl; } bool check(string str) { for (int i = 0; i < str.length(); i++) { if ((str[i] > '9' || str[i] < '0') && (str[i] != '.')) { return false; } } return true; } void get(float &x, float &y) { cout << "请输入x的值:"; string str1; cin >> str1; while (!check(str1)) { cout << "输入的不是数字,请重新输入!!!" << endl; cin >> str1; } x = atof(str1.c_str()); cout << "请输入y的值:"; string str2; cin >> str2; while (!check(str2)) { cout << "输入的不是数字,请重新输入!!!" << endl; cin >> str2; } y = atof(str2.c_str());
} int main() {
bool quit = false; float a = 2, b = 3; int choice; while (quit == false) { cout << "(0)退出(1)设置长宽(2)三角形(3)退长方形(4)交换长宽:"; cin >> choice; switch (choice) { case 1: cout << "设置前长和宽的值:"; print(a, b); get(a, b); cout << "设置后长和宽的值:"; print(a, b); break; case 2: cout << "三角形的面积为:" << triangle(a, b) << endl; break; case 3: cout << "长方形的面积为:" << rectangle(a, b) << endl; break; case 4: cout << "交换前长和宽的值:"; print(a, b); swap(a, b); cout << "交换后长和宽的值:"; print(a, b); break; default: quit=true; break; } } return 0; }
|