需要帮忙。而不是 ascii 值,400 万+
need help. instead of ascii value,4 million+
大多数这些字符看起来像一些随机数而不是它们的 ascii 值,这是为什么?为什么它仍然有效?
#include <stdio.h>
#include <conio.h>
#define SIZE 10
void arrayFiller(int v[]);
void charTimes(int v[]);
void arrayPrinter(int v[]);
void CharlessOn(int v[]);
void stringToDigit(char string_num[]);
int main() {
int vec[SIZE];
char string_num[50];
arrayFiller(vec);
arrayPrinter(vec);
charTimes(vec);
CharlessOn(vec);
gets(string_num);
stringToDigit(string_num);
}
void arrayFiller(int v[]) {
printf("Please write down first a character and then number and repeat it 5 times, \nThe
character represents the character you want to print,\nAnd the number is the number of times the
character will be printed.\n");
for (int i = 0; i < SIZE; i++) {
int checker = 0;
if (i % 2 == 0 || i == 0) {
scanf("%c", &v[i]);
}
else {
do {
if (checker>0)
printf("The number must be between 1 and 10,Try again please.\n");
scanf("%d", &v[i]);
checker++;
} while (v[i] < 1 || v[i]>10);
getchar();
}
}
}
void charTimes(int v[]) {
int k = 1;
printf("\nHere is the Char few times\n");
for (int i = 0; i < SIZE; i += 2) {
for (int j = 0; j < v[k]; j++) {
printf("%c,", v[i]);
}
k += 2;
}
}
void arrayPrinter(int v[]) {
printf("\n here is your array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d,", v[i]);
}
}
void CharlessOn (int v[]) {
int charless,k = 1;
printf("\nPlease write down the character you want to get rid of:\n");
scanf("%c", &charless);
printf("%d", charless);
printf("\nHere is the string without the char you chose:\n");
for (int i = 0; i < SIZE; i += 2) {
for (int j = 0; j < v[k]&& v[i]!=charless; j++) {
printf("%c,", v[i]);
}
k += 2;
}
}
void stringToDigit(char string_num[]){
int holder=0,number=0;
for(int i = 0; string_num[i] != NULL;i++){
number*=10;
holder=string_num[i];
holder-=48;
number+=holder;
}
printf("the number is %d",number);
}
我的作业已经完成了可以说但是我错过了一个问题所以我正在重做,我发现了一个问题,我写的第一个e有strage值而第二个e也有starge值没多少第一个e
在scanf("%c", &v[i])
中,你应该为%c
传递一个char
的地址,但是&v[i]
是一个int
的地址。 scanf
已填充 v[i]
的一个字节,其余未初始化。
修改您的代码以将 char
的地址传递给 scanf
for %c
.
除了第一个答案。
查看函数 arrayFiller,if (i % 2 == 0 || i == 0)
是多余的。它与 if (i % 2 == 0)
相同
在检查一个字符的值时,它应该被视为一个字符,你必须与字符进行比较,'1'
与 1
不同,而且数字从 0 到 9 而不是 1 到 10。因此,您应该比较 while (v[i] < '0' || v[i]>'9');
.
而不是 while (v[i] < 1 || v[i]>10);
我不明白你所有功能的全部含义。您必须提供更多详细信息,说明您的代码的预期正确行为是什么。
大多数这些字符看起来像一些随机数而不是它们的 ascii 值,这是为什么?为什么它仍然有效?
#include <stdio.h>
#include <conio.h>
#define SIZE 10
void arrayFiller(int v[]);
void charTimes(int v[]);
void arrayPrinter(int v[]);
void CharlessOn(int v[]);
void stringToDigit(char string_num[]);
int main() {
int vec[SIZE];
char string_num[50];
arrayFiller(vec);
arrayPrinter(vec);
charTimes(vec);
CharlessOn(vec);
gets(string_num);
stringToDigit(string_num);
}
void arrayFiller(int v[]) {
printf("Please write down first a character and then number and repeat it 5 times, \nThe
character represents the character you want to print,\nAnd the number is the number of times the
character will be printed.\n");
for (int i = 0; i < SIZE; i++) {
int checker = 0;
if (i % 2 == 0 || i == 0) {
scanf("%c", &v[i]);
}
else {
do {
if (checker>0)
printf("The number must be between 1 and 10,Try again please.\n");
scanf("%d", &v[i]);
checker++;
} while (v[i] < 1 || v[i]>10);
getchar();
}
}
}
void charTimes(int v[]) {
int k = 1;
printf("\nHere is the Char few times\n");
for (int i = 0; i < SIZE; i += 2) {
for (int j = 0; j < v[k]; j++) {
printf("%c,", v[i]);
}
k += 2;
}
}
void arrayPrinter(int v[]) {
printf("\n here is your array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d,", v[i]);
}
}
void CharlessOn (int v[]) {
int charless,k = 1;
printf("\nPlease write down the character you want to get rid of:\n");
scanf("%c", &charless);
printf("%d", charless);
printf("\nHere is the string without the char you chose:\n");
for (int i = 0; i < SIZE; i += 2) {
for (int j = 0; j < v[k]&& v[i]!=charless; j++) {
printf("%c,", v[i]);
}
k += 2;
}
}
void stringToDigit(char string_num[]){
int holder=0,number=0;
for(int i = 0; string_num[i] != NULL;i++){
number*=10;
holder=string_num[i];
holder-=48;
number+=holder;
}
printf("the number is %d",number);
}
我的作业已经完成了可以说但是我错过了一个问题所以我正在重做,我发现了一个问题,我写的第一个e有strage值而第二个e也有starge值没多少第一个e
在scanf("%c", &v[i])
中,你应该为%c
传递一个char
的地址,但是&v[i]
是一个int
的地址。 scanf
已填充 v[i]
的一个字节,其余未初始化。
修改您的代码以将 char
的地址传递给 scanf
for %c
.
除了第一个答案。
查看函数 arrayFiller,if (i % 2 == 0 || i == 0)
是多余的。它与 if (i % 2 == 0)
相同
在检查一个字符的值时,它应该被视为一个字符,你必须与字符进行比较,'1'
与 1
不同,而且数字从 0 到 9 而不是 1 到 10。因此,您应该比较 while (v[i] < '0' || v[i]>'9');
.
而不是 while (v[i] < 1 || v[i]>10);
我不明白你所有功能的全部含义。您必须提供更多详细信息,说明您的代码的预期正确行为是什么。