- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
长话短说:
printf()
按值传递时在 printLotInfo()
中打印垃圾。
代码
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
#include <string.h>
typedef struct Time
{
int hour; //Hour of day
int minute; //Minute of hour
} Time;
typedef struct Car
{
char * plateNumber; //String to hold plate
char hasPermit; //True/False
Time * enteringTime; //Time Struct
int lotParkedIn; //Where is the car located
} Car;
typedef struct ParkingLot
{
int lotNumber; //Lot identifier
double hourlyRate; //$$/h
double maxCharge; //Maximum daily charge
int capacity; //How many cars can be parked in the lot?
int currentCarCount; //Tracks # of cars in lot
double revenue; //How much money has this lot made?
} ParkingLot;
// Sets the hours and minutes amount for the given time t based
// on the specified hours h. (e.g., 1.25 hours would be 1 hour
// and 15 minutes)
void setHours(Time *t, double h)
{
if(h != -1)
{
t->hour = (int) h; //Bad practice but no overflow expected here
t->minute = (h - t->hour) * 60.0; //Cast truncates h. h - t.hour is h-truncated h
}
else
{
t->hour = -1;
t->minute = -1;
}
}
// Takes two Time objects (not pointers) and computes the difference
// in time from t1 to t2 and then stores that difference in the diff
// Time (which must be a pointer)
void difference(Time t1, Time t2, Time *diff)
{
diff->hour = t2.hour - t1.hour;
diff->minute = t2.minute - t1.minute;
if(diff->minute < 0)
{
diff->hour--; //If minutes are negative, decrement hour
diff->minute += 60; //and set minutes to complement of 60.
}
}
// Initialize the car pointed to by c to have the given plate and
// hasPermit status. The car should have it’s lotParkedIn set to
// 0 and enteringTime to be -1 hours and -1 minutes.
void initializeCar(Car *c, char *plate, char hasPermit)
{
Time * t = malloc(sizeof(Time)); //Allocate memory for time object
setHours(t, -1); //Set time with call to setHours()
c->plateNumber = plate;
c->hasPermit = hasPermit; //Set variables
c->lotParkedIn = 0;
c->enteringTime = t;
}
/********************** PROBLEM SECTION ************************************/
// Initialize the lot pointed to by p to have the given number,
// capacity, hourly rate and max charge values. The currentCarCount
// and revenue should be at 0.
void initializeLot(ParkingLot * p, int num, int cap, double rate, double max)
{
p->lotNumber = num;
p->hourlyRate = rate;
p->maxCharge = max;
p->capacity = cap;
p->currentCarCount = 0;
p->revenue = 0;
printf("PRINTING IN initializeLot():\nLot is: %d Capacity is: %d Rate is: %.2lf Maximum is: %.2lf\n",
p->lotNumber, p->capacity, p->hourlyRate, p->maxCharge);
}
// Print out the parking lot parameters so that is displays as
// follows: Parking Lot #2 - rate = $3.00, capacity 6, current cars 5
void printLotInfo(ParkingLot p)
{
printf("PRINTING IN printLotInfo():\n");
printf("Parking Lot #%d - rate = $%.2lf, capacity %d, current cars %d\n",
p.lotNumber, p.capacity, p.currentCarCount);
}
/************************* END PROBLEM SECTION *****************************/
// Simulate a car entering the parking lot
// ...
void carEnters(ParkingLot * p, Car * c, int hour, int minute)
{
if((p->capacity - p->currentCarCount) <= 0)
{
printf("Car %s arrives at Lot %d at %d:%1.2d, but the lot is full\n",
c->plateNumber, p->lotNumber, hour, minute);
return;
}
double timeToSet = (hour + (minute / 60.0));
setHours(c->enteringTime, timeToSet);
c->lotParkedIn = p->lotNumber;
p->currentCarCount += 1;
printf("Car %s arrives at Lot %d at %d:%1.2d.\n", c->plateNumber,
p->lotNumber, hour, minute);
}
// Simulate a car leaving the parking lot
// ...
void carLeaves(ParkingLot * p, Car * c, int hour, int minute)
{
Time * leaveTime = malloc(sizeof(Time));
Time * timeDifference = malloc(sizeof(Time)); //Allocate memory for time object
double timeToSet = (hour + (minute / 60.0));
setHours(leaveTime, timeToSet); //Set time with call to setHours()
difference(*(c->enteringTime), *leaveTime, timeDifference);
if(c->hasPermit == 0)
{
double carRevenue = p->hourlyRate * timeDifference->hour;
if(timeDifference->minute != 0)
carRevenue += p->hourlyRate;
if(carRevenue > p->maxCharge)
carRevenue = p->maxCharge;
p->revenue += carRevenue;
printf("Car %s leaves Lot %d at %d:%1.2d paid $%.2lf.\n",
c->plateNumber, p->lotNumber, hour, minute, carRevenue);
}
else
printf("Car %s leaves Lot %d at %d:%1.2d.\n",
c->plateNumber, p->lotNumber, hour, minute);
p->currentCarCount--;
free(c->enteringTime);
free(c);
free(leaveTime);
free(timeDifference);
}
/*BEGIN PROFESSOR'S CODE*/
int main() {
Car car1, car2, car3, car4, car5, car6, car7, car8, car9;
ParkingLot p1, p2;
// Set up 9 cars
initializeCar(&car1, "ABC 123", 0);
initializeCar(&car2, "ABC 124", 0);
initializeCar(&car3, "ABD 314", 0);
initializeCar(&car4, "ADE 901", 0);
initializeCar(&car5, "AFR 304", 0);
initializeCar(&car6, "AGD 888", 0);
initializeCar(&car7, "AAA 111", 0);
initializeCar(&car8, "ABB 001", 0);
initializeCar(&car9, "XYZ 678", 1);
// Set up two parking lots
initializeLot(&p1, 1, 4, 5.5, 20.0);
initializeLot(&p2, 2, 6, 3.0, 12.0);
printLotInfo(p1);
printLotInfo(p2);
printf("\n");
// Simulate cars entering the lots
carEnters(&p1, &car1, 7, 15);
carEnters(&p1, &car2, 7, 25);
carEnters(&p2, &car3, 8, 0);
carEnters(&p2, &car4, 8, 10);
carEnters(&p1, &car5, 8, 15);
carEnters(&p1, &car6, 8, 20);
carEnters(&p1, &car7, 8, 30);
carEnters(&p2, &car7, 8, 32);
carEnters(&p2, &car8, 8, 50);
carEnters(&p2, &car9, 8, 55);
printf("\n");
printLotInfo(p1);
printLotInfo(p2);
printf("\n");
// Simulate cars leaving the lots
carLeaves(&p2, &car4, 9, 0);
carLeaves(&p1, &car2, 9, 5);
carLeaves(&p1, &car6, 10, 0);
carLeaves(&p1, &car1, 10, 30);
carLeaves(&p2, &car8, 13, 0);
carLeaves(&p2, &car9, 15, 15);
carEnters(&p1, &car8, 17, 10);
carLeaves(&p1, &car5, 17, 50);
carLeaves(&p2, &car7, 18, 0);
carLeaves(&p2, &car3, 18, 15);
carLeaves(&p1, &car8, 20, 55);
printf("\n");
printLotInfo(p1);
printLotInfo(p2);
printf("\n");
// Display the total revenue
printf("Total revenue of Lot 1 is $%4.2f\n", p1.revenue);
printf("Total revenue of Lot 2 is $%4.2f\n", p2.revenue);
}
以上代码用于赋值。我们获得了功能原型(prototype),并负责让它们执行与 parking 场结构相关的某些 Action 。数据操作实际上按预期工作,在程序结束时为两个批处理提供预期的输出。
这让我认为 printf()
造成了麻烦,而不是潜在的值(value)。 initializeLot()
中专用的 printLotInfo()
函数和临时的 printf
语句之间的唯一区别是,一个从 struct 打印*
而另一个从结构打印。
我花了几个小时研究这个,我遇到了很多线程显示如何从函数中的 struct *
或 main 中的结构打印,但我还没有找到在单独的函数调用中从结构打印的一个很好的例子。我想我遗漏了一些明显的东西,比如不正确的取消引用运算符或类似的东西。
最佳答案
你缺少一个参数:
printf("Parking Lot #%d - rate = $%.2lf, capacity %d, current cars %d\n",
p.lotNumber, p.capacity, p.currentCarCount);
您的格式字符串有 4 个格式说明符,但您只提供了 3 个参数。因此,第二个格式说明符 %f
正在寻找传递了 int
的 double
,而第四个正在寻找一个int
其中没有 参数被传递。使用错误的格式说明符或没有提供足够的参数调用 undefined behavior .
使用时需要在参数列表的hourlyRate
字段中加入:
printf("Parking Lot #%d - rate = $%.2lf, capacity %d, current cars %d\n",
p.lotNumber, p.hourlyRate, p.capacity, p.currentCarCount);
关于c - 从 C 中按值传递的结构打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54834052/
我目前正在尝试基于哈希表构建字典。逻辑是:有一个名为 HashTable 的结构,其中包含以下内容: HashFunc HashFunc; PrintFunc PrintEntry; CompareF
如果我有一个指向结构/对象的指针,并且该结构/对象包含另外两个指向其他对象的指针,并且我想删除“包含这两个指针的对象而不破坏它所持有的指针”——我该怎么做这样做吗? 指向对象 A 的指针(包含指向对象
像这样的代码 package main import "fmt" type Hello struct { ID int Raw string } type World []*Hell
我有一个采用以下格式的 CSV: Module, Topic, Sub-topic 它需要能够导入到具有以下格式的 MySQL 数据库中: CREATE TABLE `modules` ( `id
通常我使用类似的东西 copy((uint8_t*)&POD, (uint8_t*)(&POD + 1 ), back_inserter(rawData)); copy((uint8_t*)&PODV
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我有一个指向结构的指针数组,我正在尝试使用它们进行 while 循环。我对如何准确初始化它并不完全有信心,但我一直这样做: Entry *newEntry = malloc(sizeof(Entry)
我正在学习 C,我的问题可能很愚蠢,但我很困惑。在这样的函数中: int afunction(somevariables) { if (someconditions)
我现在正在做一项编程作业,我并没有真正完全掌握链接,因为我们还没有涉及它。但是我觉得我需要它来做我想做的事情,因为数组还不够 我创建了一个结构,如下 struct node { float coef;
给定以下代码片段: #include #include #define MAX_SIZE 15 typedef struct{ int touchdowns; int intercepti
struct contact list[3]; int checknullarray() { for(int x=0;x<10;x++) { if(strlen(con
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Empty “for” loop in Facebook ajax what does AJAX call
我刚刚在反射器中浏览了一个文件,并在结构构造函数中看到了这个: this = new Binder.SyntaxNodeOrToken(); 我以前从未见过该术语。有人能解释一下这个赋值在 C# 中的
我经常使用字符串常量,例如: DICT_KEY1 = 'DICT_KEY1' DICT_KEY2 = 'DICT_KEY2' ... 很多时候我不介意实际的文字是什么,只要它们是独一无二的并且对人类读
我是 C 的新手,我不明白为什么下面的代码不起作用: typedef struct{ uint8_t a; uint8_t* b; } test_struct; test_struct
您能否制作一个行为类似于内置类之一的结构,您可以在其中直接分配值而无需调用属性? 前任: RoundedDouble count; count = 5; 而不是使用 RoundedDouble cou
这是我的代码: #include typedef struct { const char *description; float value; int age; } swag
在创建嵌套列表时,我认为 R 具有对列表元素有用的命名结构。我有一个列表列表,并希望应用包含在任何列表中的每个向量的函数。 lapply这样做但随后剥离了列表的命名结构。我该怎么办 lapply嵌套列
我正在做一个用于学习目的的个人组织者,我从来没有使用过 XML,所以我不确定我的解决方案是否是最好的。这是我附带的 XML 文件的基本结构:
我是新来的 nosql概念,所以当我开始学习时 PouchDB ,我找到了这个转换表。我的困惑是,如何PouchDB如果可以说我有多个表,是否意味着我需要创建多个数据库?因为根据我在 pouchdb
我是一名优秀的程序员,十分优秀!