#include<stdio.h> #include<string.h> char s1[10001],s2[10001]; int a[10001],b[10001],c[10001]; int main(){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); scanf("%s\n%s",s1,s2); int i,j; a[0]=strlen(s1); b[0]=strlen(s2); for(i=1;i<=a[0];i++){ a[i]=s1[a[0]-1]-'0'; } for(i=1;i<=b[0];i++){ b[i]=s2[b[0]-1]-'0'; } for(i=1;i<=a[0];i++){ for(j=1;j<=b[0];j++){ c[i+j-1]+=a[i]*b[j]; } } for(i=1;i<a[0]+b[0];i++){ if(c[i]>=10){ c[i+1]=c[i]/10; c[i]%=10; } } int len=a[0]+b[0]; while(c[len]==0&&len>1){ len--; } for(i=len;i>=1;i--){ printf("%d",c[i]); } return 0; }哪里有问题
时间: 2024-03-31 13:34:49 浏览: 13
这段代码实现了两个字符串的高精度乘法,但是存在以下问题:
1. 在输入字符串 s1 和 s2 时,使用了“%s\n%s”格式化字符串。这样会导致程序在读取第一个字符串时,会将输入缓冲区中的换行符也读取进来,从而导致第二个字符串的读取出现问题。应该使用“%s %s”格式化字符串,去掉换行符。
2. 在将字符串转换为数字时,使用了“s1[a[0]-1]-'0'”这样的写法,但是这样会将字符 '0' - '9' 转换为 0-9 的数字。因此,这里应该使用“s1[a[0]-i]-'0'”这样的写法,从高位到低位将字符串转换为数字。
3. 在乘法运算中,没有考虑到进位的情况。当 c[i] >= 10 时,应该将进位部分加到下一位上。
4. 在输出结果时,没有考虑到结果为 0 的情况。应该特判一下,如果结果为 0,则直接输出 0。
修改后的代码如下:
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <malloc.h>改成c++
在C++中,`<stdio.h>`、`<stdlib.h>`、`<string.h>`和`<windows.h>`这些头文件在C++标准库中对应的是`iostream`、`cstdlib`、`string`以及`Windows.h`(注意,C++不支持`windows.h`,因为它主要用于Windows API)。为了将这段C代码转换为C++,我们需要做以下调整:
1. 使用`#include <iostream>`替换`#include <stdio.h>`,因为`iostream`包含了输入输出流和基本I/O操作。
2. 使用`#include <cstdlib>`替换`#include <stdlib.h>`,因为`cstdlib`包含了内存管理函数。
3. 使用`#include <string>`替换`#include <string.h>`,因为`string`库提供了字符串处理功能。
4. 删除`#include <windows.h>`,因为C++不支持Windows API,如果需要Windows特定的头文件,需要使用C++的命名空间或头文件,但这里假设你不需要Windows API。
下面是转换后的C++代码:
```cpp
#include <iostream>
#include <cstdlib>
#include <string>
// 使用std::string代替char数组
std::string strchr(const std::string& s, char c) {
auto it = std::find(s.begin(), s.end(), c);
if (it != s.end()) {
return std::string{s.begin(), it};
}
return "";
}
// 使用std::string和strcpy的等价函数std::copy
std::string mystr(std::string& dst, const std::string& str) {
std::copy(str.begin(), str.end(), dst.begin());
dst.resize(dst.size()); // 添加终止符'\0'
return dst;
}
int main() {
std::string s1 = "abc";
std::string s2 = mystr(s1);
std::cout << s2 << std::endl;
return 0;
}
```
编写串的堆式存储,并且不写#include <string.h>这句
以下是一个使用堆式存储的串的示例代码,实现了串的赋值、拼接、比较和输出等基本操作,不使用<string.h>库函数:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 字符串最大长度
typedef struct {
char *str; // 字符串指针
int len; // 字符串长度
} String;
// 初始化字符串
void initString(String *s) {
s->str = (char *)malloc(MAX_SIZE * sizeof(char)); // 分配初始空间
s->len = 0;
}
// 释放字符串空间
void freeString(String *s) {
free(s->str);
s->len = 0;
}
// 赋值操作
void assignString(String *s, char *c) {
int i, len = 0;
while (c[len] != '\0') { // 计算字符串长度
len++;
}
if (len > MAX_SIZE) {
printf("String too long!\n");
exit(1); // 报错退出
}
for (i = 0; i < len; i++) {
s->str[i] = c[i]; // 逐个字符复制
}
s->str[len] = '\0'; // 添加结束符
s->len = len;
}
// 拼接操作
void concatString(String *s, char *c) {
int i, j, len = 0;
while (c[len] != '\0') { // 计算字符串长度
len++;
}
if (s->len + len > MAX_SIZE) {
printf("String too long!\n");
exit(1); // 报错退出
}
for (i = s->len, j = 0; j < len; i++, j++) {
s->str[i] = c[j]; // 逐个字符复制
}
s->str[i] = '\0'; // 添加结束符
s->len += len;
}
// 比较操作
int cmpString(String *s1, String *s2) {
int i;
for (i = 0; i < s1->len && i < s2->len; i++) {
if (s1->str[i] < s2->str[i]) {
return -1;
} else if (s1->str[i] > s2->str[i]) {
return 1;
}
}
if (s1->len < s2->len) {
return -1;
} else if (s1->len > s2->len) {
return 1;
}
return 0;
}
// 输出操作
void printString(String *s) {
printf("%s\n", s->str);
}
int main() {
String s;
initString(&s);
assignString(&s, "hello");
concatString(&s, " world");
printString(&s);
freeString(&s);
return 0;
}
```
注意在本例中,赋值和拼接操作使用了逐个字符复制的方式,而比较操作则使用了逐个字符比较的方式,效率相对较低。在实际应用中,可以根据具体情况选择使用库函数或自己实现。