Подскажите, в чём ошибка!! - C (СИ)
Формулировка задачи:
Нужно написать программу, переводящую русский текст на транслит, вот мой код, но он не фурычит!!
#include "stdafx.h" #include <stdio.h> #include <Windows.h> void tr(char *a,char *b) { int i=0; int j=0; if (a[i]='я') { b[j]='y'; b[j+1]='a'; j=j+2; } else if (a[i]='ю') { b[j]='y'; b[j+1]='u'; j=j+2; } else if (a[i]='э') { b[j]='e'; j++; } else if( a[i]='ы') { b[j]='y'; j++; } if (a[i]='а') { b[j]='a'; j++; } else if (a[i]='б') { b[j]='b'; j++; } else if (a[i]='в') { b[j]='v'; j++; } else if (a[i]='г') { b[j]='g'; j++; } else if (a[i]='д') { b[j]='d'; j++; } else if (a[i]='е') { b[j]='e'; j++; } else if (a[i]='ё') { b[j]='y'; b[j+1]='o'; j=j+2; } else if (a[i]='ж') { b[j]='z'; b[j+1]='h'; j=j+2; } else if (a[i]='з') { b[j]='z'; j++; } else if (a[i]='и') { b[j]='i'; j++; } else if (a[i]='к') { b[j]='k'; j++; } else if (a[i]='л') { b[j]='l'; j++; } else if (a[i]='м') { b[j]='m'; j++; } else if (a[i]='н') { b[j]='n'; j++; } else if (a[i]='о') { b[j]='o'; j++; } else if (a[i]='п') { b[j]='p'; j++; } else if (a[i]='р') { b[j]='r'; j++; } else if (a[i]='с') { b[j]='c'; j++; } else if (a[i]='т') { b[j]='t'; j++; } else if (a[i]='у') { b[j]='y'; j++; } else if (a[i]='ф') { b[j]='f'; j++; } else if (a[i]='х') { b[j]='h'; j++; } else if (a[i]='ц') { b[j]='z'; j++; } else if (a[i]='ч') { b[j]='c'; b[j+1]='h'; j=j+2; } else if (a[i]='ш') { b[j]='s'; b[j+1]='h'; j=j+2; } else if (a[i]='щ') { b[j]='s'; b[j+1]='c'; b[j+2]='h'; j=j+3; } else {a[i]=b[j]; j++;} } int _tmain(int argc, _TCHAR* argv[]) { char a[]="privet ребята"; char b[300]; for(int i=0; i<strlen(a);i++) tr(a, b); printf(b); return 0; }
Решение задачи: «Подскажите, в чём ошибка!!»
textual
Листинг программы
#include <windows.h> #include <iostream> #include <conio.h> using namespace std; void ToTranslate(char *srcStr, char *resultStr); void Clear(char *str); int main(int argc, char* argv[]) { setlocale(LC_ALL, "Russian"); FILE *f = NULL;//указатель на открываемый файл int choice=0;//переменная для хранения выбора char srcStr[256]={0};//исходная строка, считаная из файла или с клавиатуры /*буфер result, для вмещения переведенной строки, сделаем больще, потому что есть символы которые заменяиться двойными пример "ж" - "zh", "ю" "yu" и т.д.*/ char resultStr[512]={0}; do { system("cls"); cout<<"\t\tПрограмма перевода русского текста в транслит\n"; cout.width(103); cout.fill('*'); cout<<"1. Перевод из файла \n"; cout<<"2. Перевод с клавиатуры \n"; cout<<"Esc. Выход\n"; choice=getch(); if(choice==27) { cout<<"\n\t\t\t\tДо свидания !\n\n"; system("pause"); exit(1); } else if(choice==49) { system("cls"); cout<<"\n\t\t\tВыбран режим перевода из файла\n\n"; if(!(f = fopen("C:\\file.txt","r")))//попытка открыть { cout<<"Файл не найден \n"; break; } cout<<"\nРезультат перевода: \n\n"; while(!feof(f))//пока не достигнут конец файла читаем данные { fgets(srcStr,256,f);//прочитали строку ToTranslate(srcStr,resultStr);//перевели и записали в result cout<<resultStr;//вывели перевод Clear(resultStr);//очистили строку, дабы текст при следующем шаге цикла не дублировался } Clear(resultStr); Clear(srcStr); fclose(f); cout<<"\n\n\t\tДля возврата в меню нажмите любую кнопку\n\n"; getch(); } else if(choice==50) { system("cls"); cout<<"\n\t\t\tВыбран режим перевода с клавиатуры\n\n"; cout<<"Введите строку для преобразования: \n"; SetConsoleCP(1251);//временно меняем кодировку ввода cin.getline(srcStr,255);//забираем строку в правильной кодировке SetConsoleCP(866);//меняем кодировку ввода назад ToTranslate(srcStr,resultStr);//переводим забранную строку и записываем в resultStr cout<<"\nРезультат перевода: \n"<<resultStr<<endl; cout<<"\n\n\t\tДля возврата в меню нажмите любую кнопку\n\n"; getch(); Clear(resultStr); Clear(srcStr); } }while(true); } void ToTranslate(char *srcStr, char *resultStr) { char temp[2]={0}; for(int i=0;i<strlen(srcStr);i++) { switch(srcStr[i]) { case 'а': strcat(resultStr,"a"); break; case 'б': strcat(resultStr,"b"); break; case 'в': strcat(resultStr,"v"); break; case 'г': strcat(resultStr,"g"); break; case 'д': strcat(resultStr,"d"); break; case 'е': strcat(resultStr,"e"); break; case 'ё': strcat(resultStr,"ye"); break; case 'ж': strcat(resultStr,"zh"); break; case 'з': strcat(resultStr,"z"); break; case 'и': strcat(resultStr,"i"); break; case 'й': strcat(resultStr,"y"); break; case 'к': strcat(resultStr,"k"); break; case 'л': strcat(resultStr,"l"); break; case 'м': strcat(resultStr,"m"); break; case 'н': strcat(resultStr,"n"); break; case 'о': strcat(resultStr,"o"); break; case 'п': strcat(resultStr,"p"); break; case 'р': strcat(resultStr,"r"); break; case 'с': strcat(resultStr,"s"); break; case 'т': strcat(resultStr,"t"); break; case 'у': strcat(resultStr,"u"); break; case 'ф': strcat(resultStr,"f"); break; case 'х': strcat(resultStr,"h"); break; case 'ц': strcat(resultStr,"ts"); break; case 'ч': strcat(resultStr,"ch"); break; case 'ш': strcat(resultStr,"sh"); break; case 'щ': strcat(resultStr,"shch"); break; case 'ъ': strcat(resultStr,"''"); break; case 'ы': strcat(resultStr,"i"); break; case 'ь': strcat(resultStr,"''"); break; case 'э': strcat(resultStr,"e"); break; case 'ю': strcat(resultStr,"yu"); break; case 'я': strcat(resultStr,"ya"); break; default: temp[0] = srcStr[i]; temp[1] = 0; strcat(resultStr,temp); break; } } } void Clear(char *str) { for(int i=0;i<strlen(str);i++) { str[i]=0; } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д