我正在开发一个包含多个相互补充的程序。第一个程序必须读取文件,然后将按空格分割的内容写入新文件。程序2应该以这个单词为基础,并根据是否以元音或辅音开头并在结尾处附加一些字符串(取决于其开头)的规则在其中添加猪拉丁语。我可以打开文件并从目录中读取内容,但是在查找和附加适当的规则以打印到新文件时遇到了麻烦。
int processingWord = 1; //0 is not in the middle of a word and 1 is in the middle
char c;
int bytes; //Should be holding the position of the pointer in the file
while((bytes = read(fileRead, &c, sizeof(c))) > 0) {
//printf("%c", c);
if(processingWord == 0) {
processingWord = 1;
}
if(processingWord == 1) {
//Figure out if a vowel or not
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
//Increment the first counter
shard1Count++;
}
//Get to the end of the string
if(c == '\n') {
c = 'r';
write(fileWrite, &c, sizeof(c));
c = 'a';
write(fileWrite, &c, sizeof(c));
c = 'y';
write(fileWrite, &c, sizeof(c));
c = '\n';
write(fileWrite, &c, sizeof(c));
processingWord = 0;
}
}
write(fileWrite, &c, sizeof(c));
}
这是我尝试在单词末尾以元音开头并在其末尾添加新“ ray”字符串的地方。文本文件看起来像这样
It
is
the
Zucca
Gigantopithecus,
or
Great
Pumpkin,
Charlie
Brown.
在新文件中输出应该看起来像这样
Itray
isray
hetay
uccaZay
igantopithecusGay,
orray
reatGay
umpkinPay,
harlieCay
rownBay.
编辑:processsingWord是一个想法,在我检查元音之前,我必须检查我是否在行尾。但是逻辑没有进行锻炼,输出结果全是乱码。 我当前的输出文件如下:
Itray
isray
theray
Zuccaray
Gigantopithecus,ray
orray
Greatray
Pumpkin,ray
Charlieray
Brown.ray
ray
答案 0 :(得分:1)
这是一个应该起作用的实现:
void doStuff(void);
int startsWithVowel(char c);
int isALetter(char c);
void doStuff(){
int processingWord = 0;
int already_latin = 0;
char c = 0;
char first_letter = 0;
while(read(fileRead, &c, sizeof(c)) > 0) {
if(processingWord == 0) {
processingWord = 1;
if(!startsWithVowel(c)){ //append constants to the end of the word in pig latin *EDIT*
first_letter = c;
continue;//Here we do not fall through and write
}
}
else{
if(isALetter(c)){ //This is the general case of just writing the read character
write(fileWrite, &c, sizeof(c));
}
else if(c != '\n'){ //Here is handling for , and . special characters
if(isALetter(first_letter)){ //we hit a . or , with a vower word, need to add first letter then "ray"
write(fileWrite, &first_letter, sizeof(first_letter));
}
write(fileWrite, "ray", sizeof("ray"));
write(fileWrite, &c, sizeof(c));
already_latin = 1;
}
else if(c == '\n') { //here is the end of the string
if(isALetter(first_letter)){
write(fileWrite, &first_letter, sizeof(first_letter));
}
if(!already_latin){
write(fileWrite, "ray", sizeof("ray"));
}
write(fileWrite, &c, sizeof(c));
processingWord = 0; //reset all the flags for the next word.
first_letter = 0;
already_latin = 0;
}//end of '\n'
}//end of if/else block
}//end of while loop
}//end of function
/* =========================================================
return true (1) if the character is a vowel and 0 otherwise
============================================================ */
int startsWithVowel(char c){
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
return 1;
}
return 0;
}
/* =========================================================
return true (1) if the character is a letter and 0 otherwise
============================================================ */
int isALetter(char c){
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))){
return 1;
}
return 0;
}
仍然有很多未使用的东西,例如bytes变量,情况肯定会更干净,但这应该可以满足您的需要。尝试运行它,让我知道它的运行情况,如果我仍然在这里今晚仍然无法更新任何错误
编辑 看起来我倒退了,只交换了元音(而不是常量)。我的猪拉丁语很生锈。
好吧,我创建了一个本地字符串来通过codechef.com/ide在线测试解析,您可以将其复制并粘贴到此处进行验证。将printfs更改为writes,它模仿了printfs,我认为您可以这样做:
#include <stdio.h>
#include <string.h>
void doStuff(void);
int startsWithVowel(char c);
int isALetter(char c);
char * str = "It\nis\nthe\nZucca\nGigantopithecus,\nor\nGreat\nPumpkin,\nCharlie\nBrown.";
int main(void) {
doStuff();
return 0;
}
void doStuff(){
int processingWord = 0;
char c = 0;
char first_letter = 0;
int already_latin = 0;
//while(read(fileRead, &c, sizeof(c)) > 0) {
while(strlen(str) > 0){ //Made local for testing, no file io here
c = str[0];
str++; //end of local nonsense you wont have to use
if(processingWord == 0) {
processingWord = 1;
if(!startsWithVowel(c)){
first_letter = c;
continue;//Here we don not fall through and write
}
}
if(processingWord == 1) {
if(isALetter(c)){ //This is the general case of just writing the read character
//write(fileWrite, &c, sizeof(c));
printf("%c",c);
//printf(" SHOULD PRINT FIRST LETTER VOWEL HERE ");
}
else if(c != '\n'){ //Here is handling for , and . special characters
if(isALetter(first_letter)){ //we hit a . or , with a vower word, need to add first letter then "ray"
//write(fileWrite, &first_letter, sizeof(first_letter));
printf("%cay%c",first_letter,c);
}
else{
//write(fileWrite, "ray", sizeof("ray"));
//write(fileWrite, &c, sizeof(c));
printf("ray%c", c);
}
already_latin = 1;
}
else if(c == '\n') { //here is the end of the string
if(!already_latin){
if(isALetter(first_letter)){
//write(fileWrite, &first_letter, sizeof(first_letter));
printf("%cay",first_letter);
//printf(" SHOULD PRINT FIRST LETTER CONSTANT HERE ");
}
else{
//write(fileWrite, "ray", sizeof("ray"));
printf("ray");
}
}
//write(fileWrite, &c, sizeof(c));
printf("%c", c);
processingWord = 0;
first_letter = 0;
already_latin = 0;
}//end of '\n'
}//end of if/else block
}//end of while loop
}//end of function
/* =========================================================
return true (1) if the character is a vowel and 0 otherwise
============================================================ */
int startsWithVowel(char c){
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
return 1;
}
return 0;
}
/* =========================================================
return true (1) if the character is a letter and 0 otherwise
============================================================ */
int isALetter(char c){
if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))){
return 1;
}
return 0;
}
输出: 伊特雷 伊斯雷 赫泰 uccaZay igantopithecusGay, 奥雷 同性恋 umpkinPay, 哈里·凯 rownBay。