printf("%d",printf("-123"))
时间: 2023-09-26 10:06:27 浏览: 30
As an AI language model, I am programmed to provide responses to user inputs. When there is no input or the input is unclear, I cannot provide a relevant response, and therefore, my output might be "null".
相关问题
优化代码void yinliao() { int l; char d[6][40]={"海南老盐水","鸭屎香柠檬茶","斑斓香茅冰饮","豆奶","可乐/雪碧","LED气泡水"}; { float price[6]={16,18,12,9,3,9}; printf("\n"); printf("饮料推荐\n"); printf(" %-6s%-6s%\n", "\t名称 ", "\t单价\n "); printf(" %-7s%-7s%\n", "\t海南老盐水", "\t16\n"); printf(" %-7s%-7s%\n", "\t鸭屎香柠檬茶", "\t18\n"); printf(" %-7s%-7s%\n", "\t斑斓香茅冰饮", "\t12\n"); printf(" %-7s%-7s%\n", "\t豆奶", "\t9\n"); printf(" %-7s%-7s%\n", "\t可乐/雪碧", "\t3\n"); printf(" %-7s%-7s%\n", "\tLED气泡水", "\t9\n"); } }
这段代码可以进行如下优化:
1. 使用常量来保存饮料名称和价格,避免魔法数字的使用。例如可以使用以下代码:
```c
const char* names[] = {"海南老盐水", "鸭屎香柠檬茶", "斑斓香茅冰饮", "豆奶", "可乐/雪碧", "LED气泡水"};
const float prices[] = {16.0f, 18.0f, 12.0f, 9.0f, 3.0f, 9.0f};
const int numDrinks = 6;
```
2. 使用循环来输出饮料列表。例如可以使用以下代码:
```c
printf("\n饮料推荐\n");
printf("%-10s%-10s\n", "名称", "单价");
for (int i = 0; i < numDrinks; ++i) {
printf("%-10s%-10.1f\n", names[i], prices[i]);
}
```
3. 去除不必要的大括号。例如可以使用以下代码:
```c
void yinliao() {
const char* names[] = {"海南老盐水", "鸭屎香柠檬茶", "斑斓香茅冰饮", "豆奶", "可乐/雪碧", "LED气泡水"};
const float prices[] = {16.0f, 18.0f, 12.0f, 9.0f, 3.0f, 9.0f};
const int numDrinks = 6;
printf("\n饮料推荐\n");
printf("%-10s%-10s\n", "名称", "单价");
for (int i = 0; i < numDrinks; ++i) {
printf("%-10s%-10.1f\n", names[i], prices[i]);
}
}
```
//Project - Rect #include <iostream> #include <cmath> using namespace std; //定义Point类 //定义Rect类 int main() { auto rt = Rect(Point(1,6),Point(7,8)); printf("Vertices of rectangle rt:\n"); printf("(%d,%d)-----------------------(%d,%d)\n", rt.tl.x,rt.tl.y,rt.topRight().x,rt.topRight().y); printf("(%d,%d)-----------------------(%d,%d)\n", rt.bottomLeft().x,rt.bottomLeft().y,rt.br.x,rt.br.y); printf("Size information of rectangle rt:\n"); printf("width - %d height - %d\n",rt.width(),rt.height()); printf("area - %d diagonal legnth - %.2f",rt.area(),rt.diagonalLength()); return 0; }请你补全这个代码
下面是补全后的代码:
```
#include <iostream>
#include <cmath>
using namespace std;
//定义Point类
class Point {
public:
int x, y;
Point(int x, int y) : x(x), y(y) {}
};
//定义Rect类
class Rect {
public:
Point tl, br;
Rect(Point tl, Point br) : tl(tl), br(br) {}
// 返回矩形的左下角坐标
Point bottomLeft() const {
return Point(tl.x, br.y);
}
// 返回矩形的右上角坐标
Point topRight() const {
return Point(br.x, tl.y);
}
// 返回矩形的宽度
int width() const {
return abs(br.x - tl.x);
}
// 返回矩形的高度
int height() const {
return abs(br.y - tl.y);
}
// 返回矩形的面积
int area() const {
return width() * height();
}
// 返回矩形对角线的长度
double diagonalLength() const {
return sqrt(pow(width(), 2) + pow(height(), 2));
}
};
int main() {
auto rt = Rect(Point(1,6), Point(7,8));
printf("Vertices of rectangle rt:\n");
printf("(%d,%d)-----------------------(%d,%d)\n", rt.tl.x, rt.tl.y, rt.topRight().x, rt.topRight().y);
printf("(%d,%d)-----------------------(%d,%d)\n", rt.bottomLeft().x, rt.bottomLeft().y, rt.br.x, rt.br.y);
printf("Size information of rectangle rt:\n");
printf("width - %d height - %d\n", rt.width(), rt.height());
printf("area - %d diagonal legnth - %.2f", rt.area(), rt.diagonalLength());
return 0;
}
```