在c+中生成调度程序+; 我的C++作业是做一个调度器,但它不起作用,我想它如何< < /p> #include <stdio.h> #include <stdlib.h> struct schedule{ char date[12]; char time[6]; int importance; char name[128]; char content[128]; }; void showSchedule(struct schedule *sch, unsigned long long num){ int i; for(i = 0; i < num; i++){ int j; for(j = 0; j < 12; j++){ printf("%c",(sch+i)->date[j]); if(j==3||j==5||j==7){ printf("."); } } for(j = 0; j < 6; j++){ printf("%c",(sch+i)->time[j]); if(j==1) printf(":"); } printf("%d ",(sch)->importance); printf("%s ",(sch)->name); printf("%s\n",(sch)->content); } } int main(){ unsigned long long num; scanf("%ull",&num); struct schedule *sch; sch = (struct schedule *)malloc(num * sizeof(struct schedule)); int i; for(i = 0; i < num; i++){ scanf("%s",(sch+i)->date); scanf("%s",(sch+i)->time); scanf("%d",&(sch+i)->importance); scanf("%s",(sch+i)->name); scanf("%s",(sch+i)->content); } showSchedule(sch, num); free(sch); return 0; }
在代码完成运行后,输出应该是在c+中生成调度程序+; 我的C++作业是做一个调度器,但它不起作用,我想它如何< < /p> #include <stdio.h> #include <stdlib.h> struct schedule{ char date[12]; char time[6]; int importance; char name[128]; char content[128]; }; void showSchedule(struct schedule *sch, unsigned long long num){ int i; for(i = 0; i < num; i++){ int j; for(j = 0; j < 12; j++){ printf("%c",(sch+i)->date[j]); if(j==3||j==5||j==7){ printf("."); } } for(j = 0; j < 6; j++){ printf("%c",(sch+i)->time[j]); if(j==1) printf(":"); } printf("%d ",(sch)->importance); printf("%s ",(sch)->name); printf("%s\n",(sch)->content); } } int main(){ unsigned long long num; scanf("%ull",&num); struct schedule *sch; sch = (struct schedule *)malloc(num * sizeof(struct schedule)); int i; for(i = 0; i < num; i++){ scanf("%s",(sch+i)->date); scanf("%s",(sch+i)->time); scanf("%d",&(sch+i)->importance); scanf("%s",(sch+i)->name); scanf("%s",(sch+i)->content); } showSchedule(sch, num); free(sch); return 0; },c++,C++,在代码完成运行后,输出应该是 2017.03.02. 09:00 8 classes listentoclasses 2017.03.03. 13:30 7 tasks Codingtask 但是当我运行脚本时 2017.03.02. 09:00 S8 classes listentoclasses 2017.03.03. gra13:30 e8 classes listentoclasses 我得到这个输出 我能得到任何帮助吗? 通过在scanf(“%ull”和&num)行传递类型错误
2017.03.02. 09:00 8 classes listentoclasses
2017.03.03. 13:30 7 tasks Codingtask
但是当我运行脚本时
2017.03.02.
09:00 S8 classes listentoclasses
2017.03.03. gra13:30 e8 classes listentoclasses
我得到这个输出
我能得到任何帮助吗?- 通过在
scanf(“%ull”和&num)行传递类型错误的
数据来调用未定义的行为代码>:scanf()
需要%u
,但无符号int*
是&num
。您应该使用无符号long
而不是%llu
%ull
- 您运行了太多的迭代,无法打印日期和时间,并且使用通过
分配的值和未初始化的值调用了未定义的行为,这些值是不确定的malloc()
- 您忘记打印日期和时间、时间和重要性之间的空格
- 您正在重复打印第一个元素的
、重要性
和名称
内容
- 函数
中的某些行具有适当的缩进showSchedule
showSchedule
功能应该是:
void showSchedule(struct schedule *sch, unsigned long long num){
int i;
for(i = 0; i < num; i++){
int j;
for(j = 0; j < 8; j++){
printf("%c",(sch+i)->date[j]);
if(j==3||j==5||j==7){
printf(".");
}
}
printf(" ");
for(j = 0; j < 4; j++){
printf("%c",(sch+i)->time[j]);
if(j==1) printf(":");
}
printf(" %d ",(sch+i)->importance);
printf("%s ",(sch+i)->name);
printf("%s\n",(sch+i)->content);
}
}
void showSchedule(结构计划*sch,无符号长数值){
int i;
对于(i=0;i日期[j];
如果(j==3 | | j==5 | | j==7){
printf(“.”);
}
}
printf(“”);
对于(j=0;j<4;j++){
printf(“%c”,(sch+i)->时间[j]);
如果(j==1)printf(“:”);
}
printf(“%d”,(sch+i)->重要性);
printf(“%s”,(sch+i)->名称);
printf(“%s\n”,(sch+i)->内容);
}
}
看起来您在这两个方面缺少了+i
:printf(“%d”((sch)->重要性)代码>如果你要在C++中编写程序,为什么要编写C代码?不要忘记检查代码< > SCANF >以确保你得到了所有你要的令牌…@ 1201Stalk警报这个代码是有效的(我是指编译)C++代码。这将是修复错误后的正确答案。我不知道分数是否会因为没有使用C++的特定特性而被降低。我删除了我的坏答案LOL。但是简而言之,当你面对日期时,最好使用比标准更高的工具(比如12),比如Boo::日期。它会帮助你确定日期实际上是有效的(35.77.2020不是有效日期)。,并且在格式化方面也有很大帮助。例如,看看日期IO是如何完成的。当i
是num-1
时,num
是long-long int
但i
只是int
时,
还可以吗?而且,num
可以是long-long-long-int
但只是一个int,我们是否有溢出和导致UB的危险?@FantasticMrFox(sch+i)->当i
为num-1
时,
是正常的,因为在这种情况下sch
是指向num
元素数组的指针。我同意无符号长长num
和int i;
的组合是奇怪的。对这两个数组使用size\u t
应该更好。%zuadsize\u t
value。
void showSchedule(struct schedule *sch, unsigned long long num){
int i;
for(i = 0; i < num; i++){
int j;
for(j = 0; j < 8; j++){
printf("%c",(sch+i)->date[j]);
if(j==3||j==5||j==7){
printf(".");
}
}
printf(" ");
for(j = 0; j < 4; j++){
printf("%c",(sch+i)->time[j]);
if(j==1) printf(":");
}
printf(" %d ",(sch+i)->importance);
printf("%s ",(sch+i)->name);
printf("%s\n",(sch+i)->content);
}
}