Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c practices level1 -- 孔淳漳 #28

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
34 changes: 34 additions & 0 deletions practices/c/level1/p01_runningLetter/p01_Runningletter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前是C语言的作业,不能使用cpp做后缀,也不能使用c++的语法

#include <string.h>
#include <windows.h>

int main()
{
char c;
scanf ("%c", &c);
int i,j;
system("mode con cols=16 lines=9");
int width = GetSystemMetrics ( SM_CXSCREEN );
for (i = 0; i < 16; i++)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

循环控制变量,尽可能放进循环体内定义

{
system ("cls");
for (j = 0; j < i; j++)
{
printf (" ");
}
printf ("%c", c);
Sleep (100);
}
for(i = 15; i >= 0; i--)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重复的味道!这两段循环有些相似,请消除

{
system ("cls");
for (j = 0; j < i; j++)
{
printf (" ");
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进没对齐

printf ("%c", c);
Sleep (100);
printf ("\n");
}
return 0;
}
17 changes: 17 additions & 0 deletions practices/c/level1/p02_isPrime/p02_isPrime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<stdio.h>

int main(void)
{
int i, n;
scanf ("%d", &n);
for (i = 2; i < n; i++)
{
if(n % i == 0)
break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有缩进;
因为是短路语句,此处不换行也很好,like this:if(n % i == 0) break;
如果换行的话,最好用大括号包起来;

}
if(i == n)
printf ("%d is a prime\n", n);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进+大括号

else
printf ("%d isn't a prime\n", n);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

return 0;
}
15 changes: 15 additions & 0 deletions practices/c/level1/p03_Diophantus/p03_Diophantus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<stdio.h>

int main(void)
{
float i;
for(i = 0; i < 150; i++)
{
if((i / 6 + i / 12 + i / 7 + 5 + i / 2 + 4) == i)
{
printf ("When Diophantus's son dead, Diophantus's age is %g", i - 4);
break;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进对齐

}
return 0;
}
15 changes: 15 additions & 0 deletions practices/c/level1/p04_ narcissus/p04_Narcissus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<stdio.h>
#include<math.h>

int main(void)
{
int i;
for (i=100; i < 1000; i++)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

循环变量
哦,对了,需要修改编译选项才行,-std=c99,你的IDE里怎么设置,可以在群里咨询

{
if (pow (i / 100, 3) + pow((i % 100) / 10, 3) + pow(i % 10, 3) == i)
{
printf("%d\n", i);
}
}
return 0;
}
34 changes: 34 additions & 0 deletions practices/c/level1/p05_allPrimes/p05_allprimes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
#include<string.h>
#include<time.h>
#define N 1000001

int main()
{
float start, finish;
start = clock();
bool prime[N];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool是c++的语法,在C语言里是不能用的

memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for(int i = 2; i < N; i++)
{
if(prime[i])
for(int j = i * 2; j < N; j += i)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

貌似这个算法不对吧,这样只把各种2的倍数搞掉了啊

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进+大括号

{
prime[j] = false;
}
}
FILE * fp;
//fp = fopen("allprime.txt", "w");
freopen("allprime.txt", "w", stdout);
for(int k = 0; k <N; k++)
{
if(prime[k])
printf("%d ", k);
//fprintf(fp, "%d ", k);
}
finish = clock();
printf("\n%g\n", (finish - start) / CLOCKS_PER_SEC);
//fclose(fp);
return 0;
}
32 changes: 32 additions & 0 deletions practices/c/level1/p06_Goldbach/p06_Goldbach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>

int isprime(int i);
int main()
{
int x, y;
for(x = 4; x < 101; x += 2)
{
for(y = 2; y <= x / 2; y++)
{
if(isprime(y) != 0)
if(isprime(x - y) != 0)
printf("%d = %d + %d\n", x, y, x - y);
}
}
printf("Proof done.\n");
return 0;
}

int isprime(int i)
{
int j;
for(j = 2; j < i; j++)
{
if(i % j == 0)
break;
}
if(i == j)
return 1;
else
return 0;
}
37 changes: 37 additions & 0 deletions practices/c/level1/p07_encrypt_decrypt/p07_encrypt_decrypt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<stdio.h>
#include<string.h>

int main()
{
int i, n, command;
char text[256] = {'\0'}, encryption[256] = {'\0'};
printf("Plz enter your command.\n");
printf("1.Encrypt\n");
printf("2.Decrypt\n");
printf("3.Exit\n");
scanf("%d", &command);
if(command == 1)
{
printf("Plz enter the words you want to encrypt.\n");
scanf("%s", &text);
n = strlen(text);
for(i = 0; i < n; i++)
encryption[i] = text[i] + i + 6;
encryption[i] = '\0';
printf("Encrypt words are %s.\n", encryption);
}
else if(command == 2)
{
printf("Plz enter the words you want to decrypt.\n");
scanf("%s", &encryption);
n = strlen(encryption);
for(i = 0; i < n; i++)
text[i] = encryption[i] - i - 6;
text[i] = '\0';
printf("Decrypt words are %s.\n", text);
}
else if(command == 3)
return 0;
else
printf("Wrong command,plz try again.\n");
}