PDA

View Full Version : سوال: تبدیل سی پلاس پلاس به سی



mohamad_sky
چهارشنبه 29 خرداد 1387, 16:05 عصر
سلام خسته نباشید من یه سری برنامه ++C دارم که می خوام انارو به C تبدیل کنم اما ارور می ده کمک کنید.
تا فردا شب حتما می خوام

1.فاکتوریل

#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
using namespace std;
const unsigned long long _DIV_NUM = 1000000000000000;
unsigned long long Arr[2000000];
int main()
{
ofstream cout ("BigFact.out");
int n ;
while(cin >> n) //while there's number 2 read :
{
clock_t startTime= clock(); //for calculating time
long long carry =0 , tmp=0;
unsigned int up =1;
cout << n << "!\n";


//------begin of calculations :
Arr[0]=1; //0! && 1! =1
for(int i=2; i<=n; i++) //calcuating n! :
{
for(unsigned int current=0; current<up; current++) //while we have digits in Arr :
{
tmp =(i* Arr[current]+carry ); //
Arr[current]= tmp % _DIV_NUM ; //numbers in Arr must be less than 1000 (only 3-digits in all the Arr cells)/
carry = tmp / _DIV_NUM ; //calcuating carry/
}
if(carry) //if we have carry then :
{
Arr[up++] = carry; //put carry into Arr and increase Arr size/
carry =0; //reset carry/
}
}
//-------end of calculations/
//-------begin of printing Factorial :
cout << Arr[up-1]; //print first digit[s]/
//calcuating the count of factorial digits :
int factLen=0;
for(;Arr[up-1]; factLen++ , Arr[up-1]/=10); //first digits count
factLen += (up-1) * 15 ; //digits count
//print the n! :
for(int i=up-2; i>=0; i--)
{
cout << setw(15) << setfill('0') << Arr[i];
Arr[i]=0; //reset Arr cells 4 next n!/
}


clock_t endTime= clock();
cout << "\nCaculating time : " << setprecision(3) << fixed << (double)(endTime - startTime) / CLOCKS_PER_SEC << endl ;
cout << "Digits count : " << factLen << endl << endl;
}
//-------end of printing Factorial/
return 0;
}


2.مثلث

#include <iostream>
#include <iomanip>
using namespace std;
int fact(int num) {
//variable to hold the value of
int num_factorial = 1;
// while num is greater than one multiply it by the value that is referenced
// by num_factorial, then reassign it to num_factorial. finally decrement the
// value of num and start again
while(num > 1){
num_factorial *= num;
num--;
}//end of while
return num_factorial;
}// end of fact function
//comb finds the value of a combination of 2 numbers
int comb(int n , int k) {
int combination = (fact(n)/(fact(k) * fact((n-k))));
return combination;
}//end of comb function
//printTab prints out a specified number of rows determined by the max rows
//minus the number of the current row
void printTab(int n, int max){
//set the counter equal to max minus the current row
//then print out 4 spaces during ever iteration
for(int i =(max-n); i > 0; i--) {
cout << "";
}//end of for
}// end of printTab function
// main function
int main() {
//vars to hold the variables in each loop
int max,row,column;
//print a blank line on execution to prevent the output from being right up against the CLI id
cout << endl;
//notes where the beginning of execution is for the goto loop
// and prompts the user for input
beginning: cout << "Enter the maximum number of rows: ";
cin >> max;
cout << endl;
// if the maximum rows is above 14 go back so we prevent a buffer overflow of sourts
if (max > 13 || max < 1){
cout << "Number out of range...Try again!\n";
goto beginning;
}//end of goto if
//increment row as long as it is less than the max rows;
for(row = 0; row < max ; row++){
//printTab aides in making output look better by tabbing over a n times depending on current
//value of the row compared to the maximum number of rows
cout << setw(2);
printTab(row,(max - 1));
//increment column as long as column is less then row +1.
//this only works since the maximum values of numbers in a row is 1+the row number
//ie in row 6 there are seven values in Pascal’s triangle,.
for(column = 0; column < row + 1; column++){
// if the column number equals 0 output "1" since it would pass
// 0 and 0 to the combination function which would cause execution fail
// from a divide by zero exception
if(row == 0){
cout << setw(4);
cout << "1";
break;


} else {
//use the iomanip function setw to make the spacing between values to
//ensure proper output and finally output the value and the spacing
cout << setw(4);
cout << comb(row,column) << "";
}//end of row if else
}//end of column for
//output and end line to ensure the next row starts on a new line
cout << endl;
}//end of row for
return 0;
}

arashkey
پنج شنبه 30 خرداد 1387, 21:03 عصر
به جای iostream‌باید stdio.h رو انکلود کنی
به cin‌باید از تابع scanf استفاده کنی
یعنی اگه عدد می خوایی بگیری بنویسی


scanf("%d",&i);

یادت نره & رو بزاری وگرنه کار نمی کنه
و %d برای اینه که بگی یه عدد می خوایی بگیری
f% برای float
برای اینکه بازه هم بدی از یه عدد اعشار استفاده می کنی (به جای setw)
یعنی مثلا می خوایی یه عدد اعشار رو با دو رقم اعشار تو خروجی چاپ کنی می نویسی


printf("%2.2f",f);

یه رشته ساده رو هم که با printf‌خروجی میگیری


printf("your text");

اینم یه نمونه برنامه فاکتریل با c
فقط یادت باشه که باید پسوند فایل هات c.‌ باشه


#include<stdio.h>
#include<conio.h>
float fact(int);
main(){
int x=1;
float y=1;
do{
clrscr();
printf("\n Enter a number(x) : ");
scanf("%d",&x);
}while((x>34)|(x<0));
y=fact(x);
printf("\n \n\n \t (%d)! = %0.0f \n",x,y);
getch();
return 0;
}
float fact(int x)
{
if(x>1)return fact(x-1)*x*1.0;
return 1;
}