如何在c中返回带变量的字符串
我的程序试图模拟自动售货机。除了返还硬币或零钱的部分外,一切都正常。我试图将插入机器的美分数输入到main之外的另一个代码中,然后返回一个字符串。这在C中是可能的,因为我一直从整数中获取一个初始化make指针,而没有强制转换错误。如果这是不可能的,有什么想法我可以这样做吗 谢谢如何在c中返回带变量的字符串,c,string,debugging,return,character,C,String,Debugging,Return,Character,我的程序试图模拟自动售货机。除了返还硬币或零钱的部分外,一切都正常。我试图将插入机器的美分数输入到main之外的另一个代码中,然后返回一个字符串。这在C中是可能的,因为我一直从整数中获取一个初始化make指针,而没有强制转换错误。如果这是不可能的,有什么想法我可以这样做吗 谢谢 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define NI 5 #define DI 10 #defi
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define NI 5
#define DI 10
#define QU 25
bool isValid(int num) {
return (num >= 10 && num <= 100 );
}
bool isMultiple(int num) {
return (num % 5 == 0);
}
char *coinReturn(int insert) {
int dimes = insert/DI;
int remainder = insert % DI;
int nickels = remainder/NI;
int remainder2= nickels % NI;
char *m = ("Change returned. %d nickels and %d dimes\n",nickels, dimes);
return m;
}
int
main (int argc, char *argv[])
{ for (int i = 1; i != argc; ++i) {
int price = atoi(argv[i]);
if (!isValid(price)) {
printf("Price muse be from 10 cents to 100 cents.\n");
break;
} else if (!isMultiple(price)) {
printf("Price must be a multiple of 5.\n");
break;
} else {
printf(" Welcome to my Vending Machine!\n");
printf("Pop is %d cents. Please enter nickels, dimes, or quarters\n", price);
int cents = 0;
char coin = '\0';
while (cents <= price && coin != 'E') {
printf(" PLease enter a coin [NDQR]\n");
scanf (" %c", &coin);
if (coin == 'N'|| coin == 'n') {
cents = cents + NI;
printf(" You have inserted 5 cents\n");
}
else if (coin == 'd' || coin == 'D') {
cents = cents + DI;
printf("You have inserted 10 cents\n");
}
else if (coin == 'Q' || coin == 'q') {
cents = cents + QU;
printf("You have entered 25 cents\n");
}
else if ( coin == 'R' || coin == 'r'){
printf("Change requested\n");
char *rtn2= coinReturn(cents);
printf("%s\n",rtn2 );
} else {
printf("Unknown coin. Rejected.\n");
}
int balance = price - cents;
printf("You have entered a total of %d cents\n", cents);
if (balance > 0) {
printf("You must enter %d more cents\n", balance);
} else {
char *rtn = coinReturn(cents);
printf("%s\n",rtn );
cents = 0;
}
}
printf("DONE!\n");
return 0;
}
}
#包括
#包括
#包括
#定义镍5
#定义DI 10
#定义曲25
bool是有效的(int num){
return(num>=10&&num这不起作用:
char*m=(“返回的更改为%d个镍币和%d个十美分硬币\n”,镍币,十美分)
也许可以去掉char*,只需调用printf?实际上可以按照您所说的做。以下是两种您可能喜欢的解决方案:
解决方案1:使用printf而不是返回。
首先,您实际上并不是通过写入来格式化字符串
char*m=(“返回的更改为%d个镍币和%d个十美分硬币\n”,镍币,十美分);
我们需要将这些值传递给函数来进行实际的格式化
其次,我不明白为什么我们需要从coinReturn
…返回,而我们可以简单地编写
printf(“返回的更改为%d个镍币和%d个十美分硬币\n”,镍币,十美分);
我们完了
但也许这不是我们想要的。也许我们想要为这个函数返回一个字符串指针,我们也可以做到:)
解决方案2:使用snprintf并返回指向静态缓冲区的指针
char *coinReturn(int insert) {
// set aside a buffer for our string.
static char buf[512];
int dimes = insert/DI;
int remainder = insert % DI;
int nickels = remainder/NI;
int remainder2= nickels % NI;
// output our string into the buffer.
snprintf(buf, sizeof(buf),
"Change returned. %d nickels and %d dimes\n", nickels, dimes);
return buf;
}
首先,我们留出一些内存来存储格式化的字符串
静态字符buf[512];
注意,有必要将buf
声明为static
,这样它就可以在coinReturn
函数的范围内生存
512将是我们的最大字符串长度,这应该足够了
让我们看一下下一部分:
snprintf(buf, sizeof(buf),
"Change returned. %d nickels and %d dimes\n", nickels, dimes);
(见:)
第一个参数是将写入字符串的位置,buf
第二个参数是snprintf
将写入的最大字节数(包括空终止符)
最后一个参数就像您已经熟悉的printf
return buf;
最后,我们可以返回buf
,知道结果将是指向格式化字符串的指针
在这两种解决方案中,第一种解决方案显然更有意义——我认为#2存在很多问题(例如:它不是线程安全的),但如果这是您想要的,它就可以完成任务。但是我需要以某种方式返回语句。我可以用printf来完成这项工作?不。您可以使用malloc
缓冲区格式化它,然后返回它。但是,您对返回所做的只是printf it。为什么不在函数中执行printf呢?