לא מצליח לקמפל - שפת C - עמוד 4 - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

לא מצליח לקמפל - שפת C


itai2k

Recommended Posts

  • תגובות 47
  • נוצר
  • תגובה אחרונה

טוב סוף סוף סיימתי את העבודה, תודה לכולם, לשניצל ו Gil28 על התמיכה! :xyxthumbs::yelclap:

הנה הקוד סתם למי שמועניין או שבעתיד נתקל בזה:



#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>


int numberToLettersFile(int num,FILE *out);
int numberToLetters(int num);

/* Program that change the numbers to word in range of 0-99. */
/* The user has 3 optios: */
/* Option one: enter numbers manually as a standard input and get the words as a standard output by running the program: ./numbers */
/* Option two: get numbers from file and get the word as a standard output by running the program: ./numbers in.txt */
/* Option tree: get numbers from input file and get the output on other file by running the program: ./numbers in.txt out.txt */


int main(int argc, char* argv[])
{
FILE *inputf, *outputf;
int i=0,num=0;
char line[100]={0};
char *temp;
int numbers[100]={0};
int counter;




if(argc > 3) /* more then 2 names */
{
fprintf(stderr,"Eror! You cant enter more then 2 names.\n Goodbye!\n");
exit(0);
}




if(argc==1) /* no names - regular input and output */


{
printf("please enter numbers between 0-99:\n");
gets(line); /* put the number in a string named 'line' */
temp = strtok(line," "); /* sparte the numbers and put them in the arry named 'numbers' */
while(temp!=NULL)
{
num=atoi(temp);
numbers[i]=num;
i++;
counter++;
temp = strtok(NULL," ");
}
for(i=0;i<counter-1;i++) /* send the numbers to the func numberToLetters the get them as a word */
numberToLetters(numbers[i]);


printf("\nCommand successfull!\nGood Bye!\n\n");
}


if(argc==2) /* if one name - input from file and a regular output */
{
if(!(inputf=fopen(argv[1],"r")))
{
perror("Cannot open input file\n");
exit(0);
}


while(!feof(inputf)) /* put all the numbers from the input file to the arry */
{
fscanf(inputf,"%d",&numbers[i]);
i++;
counter++;
}


for(i=0;i<counter-2;i++) /* send the numbers to the func numberToLetters the get them as a word */
numberToLetters(numbers[i]);

printf("\nCommand successfull!\nGood Bye!\n\n");
fclose(inputf);
}


if(argc==3) /* if two names - get the numbers from file and put the word in another file */
{
if(!(inputf=fopen(argv[1],"r")))
{
perror("Cannot open input file file\n");
exit(0);
}


if(!(outputf=fopen(argv[2],"wt")))
{
perror("Cannot open output file\n");
exit(0);
}


while(!feof(inputf)) /* put all the numbers from the input file to the arry */
{
fscanf(inputf,"%d",&numbers[i]);
i++;
counter++;
}


for(i=0;i<counter-1;i++) /* send the numbers to the func numberToLettersFile the get them as a words inside the file*/
numberToLettersFile(numbers[i],outputf);

printf("\nCommand successfull!\nGood Bye!\n\n");
fclose(inputf);
fclose(outputf);

}


return 0;
}


int numberToLettersFile(int num,FILE *out) /* get the numbers and put them inside an output file as a words */
{


int ahadot,asarot;
char firstWord[11][6]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
char tenPlus[10][10]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char secondWord[10][8]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};






if(num>=0 && num<=10) /* if number between 0-10 */
{
fprintf(out,"%s \n",firstWord[num]);
}


if(num>10 && num<20) /* if number between 11-19 */
{
fprintf(out,"%s \n",tenPlus[(num%10)-1]);
}


if(num>19 && num<100) /* if number between 20-99 */
{
ahadot=num%10;
asarot=num/10;


if(ahadot==0)
fprintf(out,"%s \n",secondWord[asarot-2]);


else
fprintf(out,"%s %s\n",secondWord[asarot-2],firstWord[ahadot]);


}
return 0;
}


int numberToLetters(int num)
{
int ahadot,asarot;
char firstWord[11][6]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
char tenPlus[10][10]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char secondWord[10][8]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};




if(num>=0 && num<=10) /* if number between 0-10 */
{
printf("%s \n",firstWord[num]);
}


if(num>10 && num<20) /* if number between 11-19 */
{
printf("%s \n",tenPlus[(num%10)-1]);
}


if(num>19 && num<100) /* if number between 20-99 */
{
ahadot=num%10;
asarot=num/10;


if(ahadot==0)
printf("%s \n",secondWord[asarot-2]);


else
printf("%s %s\n",secondWord[asarot-2],firstWord[ahadot]);
}
return 0;
}

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.


×
  • צור חדש...