如何在C中生成期望值
how to generate expected value in C
我是 C 语言的新手,我正在尝试编写一个简单的自助银行控制台程序。我想做一个生成帐号的函数,所以我写了下面的代码:
int first = 7856123490;
int accountNumber;
int generateAccNo ()
{
printf("%d", first);
first = first + 1;
accountNumber = first;
return (accountNumber);
}
这里的整个想法是,当我调用这个函数时,它将获取名为 first
的变量中的值并将其加 1 并替换 first
和 [=24= 的值] 该值为 accountNumber
.
正如我所期望的,这个函数第一次运行它应该 return 7856123491 这个值,第二次它应该 return 7856123492。那我该怎么做呢?
long int first = 7856123490;
long int accountNumber;
int generateAccNo ()
{
printf("%ld \n", first);
first = first + 1;
accountNumber = first;
return (accountNumber);
}
int
的尺寸很小,无法容纳如此大的数字,因此我们将在声明 int
时添加关键字 long
- 为了在将
long int
与 %d
一起使用时抑制警告,我们将对 long int
使用 %ld
更便携的解决方案:在中使用定长类型。
#include <inttypes.h>
#include <stdio.h>
int64_t first = 7856123490;
int64_t accountNumber;
int64_t generateAccNo() {
first = first + 1;
accountNumber = first;
return (accountNumber);
}
#define TEST_CNT 10
int main(int argc, char **argv) {
for (int64_t i = 0; i < TEST_CNT; ++i) {
int64_t res = generateAccNo();
printf("%" PRId64 " %016" PRIX64 "\n", res, (uint64_t) res);
}
return 0;
}
使用大数字的简单方法,即使您有 N
位数的银行帐户也可以使用
void big_number_inc(char * const big_number, const int size) {
int i = size - 1;
big_number[i]++;
while ((i >= 0) && (big_number[i] > ASCII_9)) {
big_number[i--] = ASCII_0;
if (i >= 0) big_number[i]++;
}
}
完整参考。代码:
#include <stdio.h>
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define ASCII_0 48 // '0' assci value
#define ASCII_9 57 // '9' assci value
// internal use (debug only)
static void inline big_number_print(const char *const big_number, int size) {
for (int i = 0; i < size; i++)
printf("%c ", big_number[i]);
printf("\n");
}
void big_number_inc(char * const big_number, const int size) {
int i = size - 1;
big_number[i]++;
while ((i >= 0) && (big_number[i] > ASCII_9)) {
big_number[i--] = ASCII_0;
if (i >= 0) big_number[i]++;
}
}
int main(int argc, char *argv[]) {
// note: if max length of number doesn't match initial string length - 1,
// fill remaining with '0' or revert and start working from offset 0
char number[] = { '0', '0', '7', '8', '5', '6', '1', '2', '3', '4', '9', '0' };
for (int i = 0; i < 20; i++) {
big_number_inc(number, ARRAY_SIZE(number));
// wanna see the result
big_number_print(number, ARRAY_SIZE(number));
}
return 0;
}
我是 C 语言的新手,我正在尝试编写一个简单的自助银行控制台程序。我想做一个生成帐号的函数,所以我写了下面的代码:
int first = 7856123490;
int accountNumber;
int generateAccNo ()
{
printf("%d", first);
first = first + 1;
accountNumber = first;
return (accountNumber);
}
这里的整个想法是,当我调用这个函数时,它将获取名为 first
的变量中的值并将其加 1 并替换 first
和 [=24= 的值] 该值为 accountNumber
.
正如我所期望的,这个函数第一次运行它应该 return 7856123491 这个值,第二次它应该 return 7856123492。那我该怎么做呢?
long int first = 7856123490;
long int accountNumber;
int generateAccNo ()
{
printf("%ld \n", first);
first = first + 1;
accountNumber = first;
return (accountNumber);
}
int
的尺寸很小,无法容纳如此大的数字,因此我们将在声明int
时添加关键字 - 为了在将
long int
与%d
一起使用时抑制警告,我们将对long int
使用
long
%ld
更便携的解决方案:在
#include <inttypes.h>
#include <stdio.h>
int64_t first = 7856123490;
int64_t accountNumber;
int64_t generateAccNo() {
first = first + 1;
accountNumber = first;
return (accountNumber);
}
#define TEST_CNT 10
int main(int argc, char **argv) {
for (int64_t i = 0; i < TEST_CNT; ++i) {
int64_t res = generateAccNo();
printf("%" PRId64 " %016" PRIX64 "\n", res, (uint64_t) res);
}
return 0;
}
使用大数字的简单方法,即使您有 N
位数的银行帐户也可以使用
void big_number_inc(char * const big_number, const int size) {
int i = size - 1;
big_number[i]++;
while ((i >= 0) && (big_number[i] > ASCII_9)) {
big_number[i--] = ASCII_0;
if (i >= 0) big_number[i]++;
}
}
完整参考。代码:
#include <stdio.h>
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define ASCII_0 48 // '0' assci value
#define ASCII_9 57 // '9' assci value
// internal use (debug only)
static void inline big_number_print(const char *const big_number, int size) {
for (int i = 0; i < size; i++)
printf("%c ", big_number[i]);
printf("\n");
}
void big_number_inc(char * const big_number, const int size) {
int i = size - 1;
big_number[i]++;
while ((i >= 0) && (big_number[i] > ASCII_9)) {
big_number[i--] = ASCII_0;
if (i >= 0) big_number[i]++;
}
}
int main(int argc, char *argv[]) {
// note: if max length of number doesn't match initial string length - 1,
// fill remaining with '0' or revert and start working from offset 0
char number[] = { '0', '0', '7', '8', '5', '6', '1', '2', '3', '4', '9', '0' };
for (int i = 0; i < 20; i++) {
big_number_inc(number, ARRAY_SIZE(number));
// wanna see the result
big_number_print(number, ARRAY_SIZE(number));
}
return 0;
}