私の次のステップは、プログラムをメインコードの外のいくつかの関数に分割し、spateファイルとヘッダファイルを使用することです。別ファイルで新しい関数を作成し、ヘッダファイルを使用する
私が移動した最初の機能は、大文字の関数への変更であり、これは素晴らしい動作です。
私は現在、モールス符号から英数字への変換を行うための新しい関数を作成しようとしました。たくさんのエラーが出ているようだ。
メインプログラムの後ろに、関数プログラムのあとにヘッダーファイルを続けて付けました。
MAIN PROGRAM
#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コンパイルの原因となっているんMy機能:機能 'においてmorseToAlpha': morseToAlpha.c:32 :29:エラー:unary '*'の無効な型引数( 'int'を持つ)
IF(のstrcmp(*コード[x]は、保持)== 0) ^ morseToAlpha.c:34:55:警告:割り当てキャストなし整数からポインタを行う[-Wint変換] print_array [print_array_index ++] =文字[x]; ^ morseToAlpha.c:45:13:警告:返り値を返す関数でvoidを返す return 0; ^
はあなたが取得しているエラーを追加:としてこのように、あなたの関数を宣言する必要があります。 –
'morseToAlpha()'を定義して、 'char *'を第2引数と第3引数としていましたが、 'char **'を渡しました。このミスは、ソースコードをいくつファイルに配布するかとは完全に独立しています。 – alk
2つの配列が不正なデータ構造(チェックするのは難しい)、sctruct {char letter; char * morse; } 良くなります –