【发布时间】:2016-09-26 08:34:00
【问题描述】:
我的下一步是将我的程序分解为主代码之外的几个函数,在 spate 文件中并使用头文件。
我移动的第一个函数是更改为大写函数,效果很好。
我现在尝试创建一个新函数来执行从摩尔斯电码到字母数字的转换。似乎有很多错误。
我已经附加了我的主程序,然后是函数程序,然后是头文件。
主程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include "morse.h"
int main(void)
{
char *morse[] = {"/",".-","-...","-.-.","-..",".","..-.","-- .","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".- .","...","-","..-","...-",".--","-..-","-.--","--..","-----",".---- ","..---","...--","....-",".....","-....","--...","---..","----."};
char *alpha[] = {" ", "A", "B",
"C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T","U", "V", "W", "X", "Y", "Z" , "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
char *print_array[50];
int print_array_index = 0;
char hold[50];
int hold_index = 0;
char input[200];
int i = 0;
//Requesting input from the user
printf("Welcome to the Morse translator.\n");
printf("Enter input: ");
fgets(input, sizeof(input), stdin);
// Call to a function that converts user input to UPPERCASE
turnUpCase(input);
if (input[0]=='-' || input[0]=='.')
{
//Calls the function to convert Morse code to Alpha
morseToAlpha(input, *alpha, *morse);
}
// this section is now in a seperate function
/* while (input[i] !='\0') {
if (input[i] ==' ' || input[i] == '\n')
{
hold[hold_index] = '\0';
bool found = false;
for (int x = 0; x < sizeof(morse) / sizeof(char *); x++)
{
if (strcmp(morse[x], hold) == 0)
{
print_array[print_array_index++] = alpha[x];
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "Invalid Morse code!");
return 0;
}
hold_index = 0;
}
else
{
hold[hold_index++] = input[i];
}
i++;
}
for (int x = 0; print_array_index > x; x++)
//for (int x = 0; x < print_array_index; x++)
{
printf("%s", print_array[x]);
}
printf("\n");
}
*/
else if (isalnum(input[0]))
{
while (input[i]!='\0' && input[i] !='\n')
{
bool found = false;
for (int x=0; x < sizeof(alpha)/sizeof(char*);x++)
{
if (*alpha[x]==input[i])
{
print_array[print_array_index++] =
morse [x];
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "Invalid input!\n");
return 0;
}
i++;
}
printf("%s",print_array[0]);
for (int x=1; x < print_array_index; x++)
{
printf(" %s",print_array[x]);
}
printf("\n");
}
return 0;
}
我的功能导致错误
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include"morse.h"
void morseToAlpha(char *in, char *letters, char *code)
{
int i=0;
int x=0;
char *print_array[50];
int print_array_index =0;
char hold[50];
int hold_index = 0;
while (in[i] !='\0')
{
if (in[i] ==' ' || in[i] == '\n')
{
hold[hold_index] = '\0';
bool found = false;
for (int x = 0; x < sizeof(code) / sizeof(char *); x++)
{
if (strcmp(*code[x], hold) == 0)
{
print_array[print_array_index++] = letters[x];
found = true;
break;
}
}
if (!found)
{
fprintf(stderr, "Invalid Morse code!");
return 0;
}
hold_index = 0;
}
else
{
hold[hold_index++] = in[i];
}
i++;
}
for (int x = 0; print_array_index > x; x++)
//for (int x = 0; x < print_array_index; x++)
{
printf("%s", print_array[x]);
}
printf("+++++\n");
}
当前编译错误
morseToAlpha.c:在函数“morseToAlpha”中: morseToAlpha.c:32:29:错误:一元'*'的无效类型参数(有'int')
if (strcmp(*code[x], hold) == 0) ^ morseToAlpha.c:34:55:警告:赋值使指针从整数而不进行强制转换 [-Wint-conversion] print_array[print_array_index++] = 字母[x]; ^ morseToAlpha.c:45:13:警告:带有值的“返回”,在函数中返回 void 返回0; ^
【问题讨论】:
-
添加您遇到的错误。
-
您定义了
morseToAlpha()以将char*s 作为第二和第三个参数,但您传递了char**。此错误完全与您分发源代码的文件数量无关。 -
建议两个数组是错误的数据结构(很难检查),数组sstruct { char letter;字符 * 莫尔斯; } 会更好
标签: c