我必须为我的 C 编程编写一个程序,将字符串读入 typedef 结构,利用动态数组进行存储,然后计算名称中元音和辅音的数量,元音是 'a'、'e'、'i '、'o'、'u' 和 'y'。字符 – ('-') 既不是辅音也不是元音。然后我需要更新数据,将每个数据添加 3 岁并打印出数据。一切都必须在单独的功能中。
- 计算所有 7 个名字中元音和辅音的数量
- 必须使用 typedef 结构和动态分配的数组创建
- 稍后应通过向每个配置文件添加 3 年来更新功能并打印最终结果
我需要分析的数据是这样的:
Fred Flintstone38男
Barney Rubble36男
威尔玛·弗林特斯通37女
Betty Rubble36女
鹅卵石 Flintstone4Female
Bam-Bam Rubble3Male
迪诺燧石2男
这是我到目前为止所得到的,但程序一直告诉我我有一个不兼容的指针类型,我无法弄清楚错误在哪里。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct
{
char name[20];
int age[2];
char gender[6];
}CARTOON;
// Function Prototype Declarations
int printit(CARTOON *, FILE *);
int loaddata(CARTOON *, int, FILE *, FILE *);
void countVowel (char **pNames, int *vowel, int *consonants);
void printResults (int vowel, int consonants);
// Main
int main (void)
{
char input[40];
int namesIndex;
int vowel;
int consonants;
CARTOON* pNames;
FILE *tptr;
FILE *bptr;
FILE *fptr; //reading in the file "data.txt"
if ((fptr=fopen("data.txt", "r"))==NULL)
{
printf("Error opening data.txt\n");
return -1;
}
if((bptr=fopen("lab5dat.bin","wb"))==NULL)
{
printf("Error creating lab4.bin\n");
return 2;
}
pNames =(char**)calloc(8,sizeof(CARTOON));
namesIndex = 0;
while (namesIndex < 8 && fgets(input,40,fptr))
{
*(pNames + namesIndex)=(char*) calloc(strlen(input)+1, sizeof(char));//pointer to first string in array namesIndex
strcpy(*(pNames + namesIndex), input);
namesIndex++;
}
*(pNames + namesIndex)=NULL;
namesIndex = 0;
pNames=(CARTOON*) calloc(12,sizeof(CARTOON));
loaddata(pNames, namesIndex, tptr, bptr);
fclose(tptr);
fclose(bptr);
if((bptr=fopen("lab5dat.bin","rb"))==NULL)
{
printf("Error opening lab5dat.bin\n");
return 3;
}
//printit(pNames, bptr);
// Prints the items in the "lab3.dat" file.
namesIndex=0;
while (*(pNames + namesIndex))
{
printf("%s\n", *(pNames + namesIndex));
namesIndex++;
}
// Calls function countVowel
countVowel (pNames, &vowel, &consonants);
//Calls function printResults
printResults(vowel, consonants);
system("pause");
return 0;
} //end
int printit(CARTOON *pNames, FILE *bptr)
{
int num;
printf("Data from print \n\n");
//num=fread(pNames, sizeof(CARTOON), 1, bptr);
while (!(feof(bptr)))
{
num=fread(pNames, sizeof(CARTOON), 1, bptr);
printf("%s %d %s \n",pNames->name,pNames->age,pNames->gender);
}
return 0;
}
int loaddata( CARTOON *pNames, int namesIndex, FILE *tptr, FILE *bptr)
{
printf("Data from loaddata\n\n");
while (!(feof(tptr)))
{
fgets((pNames + namesIndex)->name,20,tptr);
fscanf(tptr,"%d",&pNames[namesIndex].age);
fgets((pNames + namesIndex)->gender,18,tptr);
fwrite((pNames + namesIndex),sizeof(CARTOON),1,bptr);
printf("%s\n", (pNames + namesIndex)->name);
namesIndex++;
}
return 0;
}
//function countVowel will count the number of vowels and consonants
void countVowel (char **pNames, int *vowel, int *consonants)
{
int namesIndex;
int stringIndex;
namesIndex=0;
stringIndex=0;
*vowel=0;
*consonants=0;
while(*(pNames + namesIndex))
{
stringIndex=0;
while(stringIndex<strlen(*(pNames + namesIndex)))
{
if (isalpha (pNames[namesIndex][stringIndex] )) //Reads only alphabets
switch (toupper(pNames[namesIndex][stringIndex])) //makes everything capitalized.
{ case 'A': (*vowel)++ ;break; //*vowel count gets incremented by 1 whenever a vowel is found.
case 'E': (*vowel)++ ;break;
case 'I': (*vowel)++ ;break;
case 'O': (*vowel)++ ;break;
case 'U': (*vowel)++ ;break;
case 'Y': (*vowel)++ ;break;
default: (*consonants)++;break; // Everything that is not a vowel increments consonants by 1.
}
stringIndex++; // goes to the next index in the string
}
namesIndex++; //goes to the next array index, when end of string is reached
}
}
//Prints the result of the number of vowels and consonants
void printResults (int vowel, int consonants)
{
printf ("\n\nThere are %d vowels and %d consonants\n\n", vowel, consonants);
}
现在错误在这两行代码之间
*(pNames + namesIndex)=(char*) calloc(strlen(input)+1, sizeof(char));//pointer to first string in array namesIndex
strcpy(*(pNames + namesIndex), input);